summaryrefslogtreecommitdiff
path: root/lib/page_local.rb
blob: 7121c8e339336792c8b29af2aa9e75a0398f718e (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
115
116
117
118
119
120
121
122
# coding: utf-8
require 'date'
require 'set'

require 'config'
require 'license'
require 'page'
require 'pandoc'
require 'person'
require 'sitegen'

class LocalPage < Page
	def initialize(infile)
		@infile = infile
		Sitegen::add(self)
	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 += "<h1 class=title>#{atom_title}</h1>\n"
			end
			
			# Insert the body
			@content = _pandoc.to('html5 '+(_pandoc['pandoc_flags']||''))
		end
		@content
    end

	def atom_rights
		# TODO: simplify year spans
		@rights ||= "<p>The content of this page is Copyright © #{years.sort.join(', ')} #{atom_author.html}.</p>\n" +
					"<p>This page is licensed under the #{local_license.html} license.</p>"
	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(/\.[^\/.]*$/, '')
			@depends = {
				'' => Set[local_infile],
				"#{basename}.html" => Set[local_infile, "tmpl/page.html.erb"],
				#"#{basename}.atom" => Set[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