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. --- .gitignore | 2 +- post-commit | 2 +- util.rb | 54 +++++++++++++++++++++++++++++++++--------------------- 3 files changed, 35 insertions(+), 23 deletions(-) diff --git a/.gitignore b/.gitignore index 6e5dabf..2d65ef7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ /public/*.html /public/index.* /.var* -/.tmp* +.tmp* diff --git a/post-commit b/post-commit index 574f468..09d2f7f 100755 --- a/post-commit +++ b/post-commit @@ -4,7 +4,7 @@ branch=$(git name-rev --name-only HEAD) if [[ $branch == master ]]; then git checkout pre-generated git merge master -m 'bogus' - make + make --always-make -j12 git add . git commit --amend -m "make: $(git log -n1 master --pretty=format:%B)" git checkout master 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