summaryrefslogtreecommitdiff
path: root/lib/page_local.rb
blob: 1ca14f002eb756e7fd2397e21215995991e316a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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")