# 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 += "

#{title}

\n" end # Insert the body @content = _pandoc.to('html5 '+(_pandoc['pandoc_flags']||'')) end @content end def rights # TODO: simplify year spans @rights ||= "

The content of this page is Copyright © #{years.sort.join(', ')} #{author.html}.

\n" + "

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

" 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")