diff options
author | Luke Shumaker <lukeshu@sbcglobal.net> | 2017-01-06 19:50:45 -0500 |
---|---|---|
committer | Luke Shumaker <lukeshu@sbcglobal.net> | 2017-01-06 19:50:45 -0500 |
commit | 7edb003cd1c9b53ffdff11ef85532e39f08db16d (patch) | |
tree | 180db82a4af7720508ae2732393401bf4d27cadf /lib/page_local.rb | |
parent | 7d875df65221d4da91953cf129a03e76fe8e5d29 (diff) |
wip
Diffstat (limited to 'lib/page_local.rb')
-rw-r--r-- | lib/page_local.rb | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/lib/page_local.rb b/lib/page_local.rb new file mode 100644 index 0000000..1ca14f0 --- /dev/null +++ b/lib/page_local.rb @@ -0,0 +1,114 @@ +# coding: utf-8 +require 'date' +require 'erb' +require 'set' + +require 'config' +require 'license' +require 'page' +require 'pandoc' +require 'person' + +class LocalPage < Page + def initialize(infile) + @infile = infile + end + + # Some of this code looks a little weird because it is + # super-aggressively lazy-evaluated and cached. + + def _infile ; @infile ; end + def _input ; @input ||= File::read(_infile) ; end + def _pandoc + if @pandoc.nil? + types = { + 'md' => 'markdown' + } + + ext = File::extname(_infile).gsub(/^[.]/, '') + type = types[ext] || ext + @pandoc = Pandoc::load(type, _input) + + if @pandoc['pandoc_format'] + @pandoc = Pandoc::load(@pandoc['pandoc_format'], _input) + end + end + @pandoc + end + + # Query simple document metadata + def title ; @title ||= _pandoc['title'] || _input.split("\n",2).first ; end + def author ; @author ||= Person::new( _pandoc['author'] || Config::get.default_author) ; end + def license ; @license ||= License::new(_pandoc['license'] || Config::get.default_license); end + def head ; @head ||= _pandoc['html_head_extra'] ; end + def class ; @class ||= _pandoc['class'] ; end + def _tags ; @_tags ||= _pandoc['tags'] || [] ; end + + def content + if @content.nil? + @content = '' + # Only insert the title if it came from Pandoc metadata; + # if the title was inferred from the the body content, + # then it is already in the page. + unless _pandoc['title'].nil? + @content += "<h1 class=title>#{title}</h1>\n" + end + + # Insert the body + @content = _pandoc.to('html5 '+(_pandoc['pandoc_flags']||'')) + end + @content + end + + def rights + # TODO: simplify year spans + @rights ||= "<p>The content of this page is Copyright © #{years.sort.join(', ')} #{author.html}.</p>\n" + + "<p>This page is licensed under the #{license.html} license.</p>" + end + + def _gitdates + @gitdates ||= `git log --format='%cI' -- #{_infile}`.split('\n').select{|s|!s.empty?}.map{|s|DateTime::iso8601(s)} + end + + def _published + if @_published.nil? + raw = _pandoc['published'] + @_published = Datetime::parse(raw) unless raw.nil? + end + if @_published.nil? + @_published = _gitdates.sort.first + end + @_published + end + + def _updated + if @_updated.nil? + raw = _pandoc['updated'] + @_updated = DateTime::parse(raw) unless raw.nil? + end + if @_updated.nil? + @updated = _gitdates.sort.last + end + @_updated + end + + def _years + @years ||= Set[*_gitdates.map{|dt|dt.year}] + end + + def abssrcpath + @srcpath ||= _infile.sub(/^(src|out)\//, '/') + end + def absoutpath + @outpath ||= abssrcpath.sub(/\.[^\/.]*$/, '.html').sub(/\/index[.]html$/, '') + end + + def url + @url ||= Config::get.url + absoutpath + end + def srcurl + @srcurl ||= Config::get.url + abssrcpath + end +end + +ERB::new(File::read("tmpl/page.html.erb")).def_method(LocalPage, 'html()', "tmpl/page.html.erb") |