From 6f7d74f8a0c5a77441b34e3b42950c18127da3d7 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sat, 3 Sep 2016 16:28:56 -0400 Subject: improve Lazy load everything in the Page class. --- util.rb | 54 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 33 insertions(+), 21 deletions(-) (limited to 'util.rb') diff --git a/util.rb b/util.rb index e50406a..160a74d 100644 --- a/util.rb +++ b/util.rb @@ -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 = "

The content of this page is Copyright © #{@date.year unless @date.nil?} #{@author.html}.

\n" + - "

This page is licensed under the #{@license.html} license.

" + 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 = 'Luke Shumaker » ' - if (@slug == 'index') - @breadcrumbs += "blog" - else - @breadcrumbs += 'blog » ' + @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 ||= "

The content of this page is Copyright © #{date.year unless date.nil?} #{author.html}.

\n" + + "

This page is licensed under the #{license.html} license.

" + end + + def breadcrumbs + @breadcrumbs ||= 'Luke Shumaker » ' + ( (slug == 'index') ? "blog" : "blog » #{slug}" ) end end -- cgit v1.2.3-54-g00ecf