diff options
Diffstat (limited to 'util.rb')
-rw-r--r-- | util.rb | 54 |
1 files changed, 33 insertions, 21 deletions
@@ -60,33 +60,45 @@ class License end class Page - attr_accessor :title, :author, :gitdate, :date, :license, :slug, :content, :rights, :breadcrumbs def initialize(infile) - input = File.read(infile) - doc = Pandoc::load('markdown', input) + @infile = infile + end - if doc['markdown_options'] - doc = Pandoc::load('markdown'+doc['markdown_options'], input) - end + def infile ; @infile ; end + def input ; @input ||= File.read(infile) ; end + def pandoc + if @pandoc.nil? + @pandoc = Pandoc::load('markdown', input) - gitdate = `git log -n1 --format='%cI' -- #{infile}` + if @pandoc['markdown_options'] + @pandoc = Pandoc::load('markdown'+@pandoc['markdown_options'], input) + end + end + @pandoc + end - @title = doc['title'] || input.split("\n",2).first - @author = Person.new(doc['author'] || "Luke Shumaker") - @gitdate = DateTime.iso8601(gitdate) unless gitdate.empty? - @date = Date.parse(doc['date']) unless doc['date'].nil? - @license = License.new(doc['license'] || "CC BY-SA-3.0") - @slug = infile.sub(/\..*$/,'').sub(/^.*\//,'') - @content = doc.to('html5') - @rights = "<p>The content of this page is Copyright © #{@date.year unless @date.nil?} #{@author.html}.</p>\n" + - "<p>This page is licensed under the #{@license.html} license.</p>" + def title ; @title ||= pandoc['title'] || input.split("\n",2).first ; end + def author ; @author ||= Person.new( pandoc['author'] || "Luke Shumaker") ; end + def license ; @license ||= License.new(pandoc['license'] || "CC BY-SA-3.0") ; end + def date ; @date ||= Date.parse(pandoc['date']) unless pandoc['date'].nil? ; end + def slug ; @slug ||= infile.sub(/\..*$/,'').sub(/^.*\//,'') ; end + def content ; @content ||= pandoc.to('html5') ; end - @breadcrumbs = '<a href="/">Luke Shumaker</a> » ' - if (@slug == 'index') - @breadcrumbs += "blog" - else - @breadcrumbs += '<a href=/blog>blog</a> » ' + @slug + def gitdate + if @gitdate.nil? + raw = `git log -n1 --format='%cI' -- #{infile}` + @gitdate = DateTime.iso8601(raw) unless raw.empty? end + @gitdate + end + + def rights + @rights ||= "<p>The content of this page is Copyright © #{date.year unless date.nil?} #{author.html}.</p>\n" + + "<p>This page is licensed under the #{license.html} license.</p>" + end + + def breadcrumbs + @breadcrumbs ||= '<a href="/">Luke Shumaker</a> » ' + ( (slug == 'index') ? "blog" : "<a href=/blog>blog</a> » #{slug}" ) end end |