# coding: utf-8 require 'date' require 'set' require 'config' require 'license' require 'page' require 'pandoc' require 'person' class LocalPage < Page def initialize(infile) @infile = infile super() end # Some of this code looks a little weird because it is # super-aggressively lazy-evaluated and cached. def local_infile ; @infile ; end def local_input ; @input ||= File::read(local_infile); end def local_intype types = { 'md' => 'markdown' } ext = File::extname(local_infile).gsub(/^[.]/, '') return types[ext] || ext end def _pandoc if @pandoc.nil? @pandoc = Pandoc::load(local_intype, local_input) if @pandoc['pandoc_format'] @pandoc = Pandoc::load(@pandoc['pandoc_format'], local_input) end end @pandoc end # Query simple document metadata def atom_author ; @author ||= Person::new( _pandoc['author'] || Config::get.default_author) ; end def atom_title ; @title ||= _pandoc['title'] || local_input.split("\n",2).first ; end def html_class ; @class ||= _pandoc['class'] ; end def html_head_extra ; @head ||= _pandoc['html_head_extra'] ; end def local_license ; @license ||= License::new(_pandoc['license'] || Config::get.default_license); end def page_categories ; @cats ||= _pandoc['categories'] || [] ; end def atom_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 += "
The content of this page is Copyright © #{years.sort.join(', ')} #{atom_author.html}.
\n" + "This page is licensed under the #{local_license.html} license.
" end def _gitdates @gitdates ||= `git log --format='%cI' -- #{local_infile}`.split("\n").select{|s|!s.empty?}.map{|s|DateTime::iso8601(s).to_time} end def page_published if @_published.nil? raw = _pandoc['published'] @_published = DateTime::parse(raw).to_time unless raw.nil? end if @_published.nil? @_published = _gitdates.sort.first end @_published end def page_updated if @_updated.nil? raw = _pandoc['updated'] @_updated = DateTime::parse(raw).to_time unless raw.nil? end if @_updated.nil? @updated = _gitdates.sort.last end @_updated end def page_years @years ||= Set[*_gitdates.map{|dt|dt.year}] end def local_outfile local_infile.sub(/^src/, 'out').sub(/\.[^\/.]*$/, '.html') end def local_depends if @depends.nil? basename = local_infile.sub(/^src/, 'out').sub(/\.[^\/.]*$/, '') deps = Set['config.yaml', local_infile] @depends = { '' => deps, "#{basename}.html" => deps.clone.merge(["tmpl/page.html.erb"]), #"#{basename}.atom" => deps.clone.merge([local_infile, "tmpl/page.atom.erb"]), } end @depends end def local_srcurl @srcurl ||= Config::get.url + local_infile.sub(/^src/, '') end def url @outurl ||= Config::get.url + local_outfile.sub(/^out/, '') end end