summaryrefslogtreecommitdiff
path: root/lib/page_index.rb
blob: 31d4682828b6743c0ee517fcde816864e9f4c810 (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
123
124
125
126
127
128
129
130
131
132
133
134
# coding: utf-8
require 'erb'
require 'set'
require 'yaml'

require 'config'
require 'page_local'
require 'page_remote'
require 'person'

class IndexPage < LocalPage
	def initialize(dirname)
		super(dirname)
	end

	def _metadata
		if @metadata.nil?
			yamlfile = local_infile+"/index.yaml"
			if File::exist?(yamlfile)
				@metadata = YAML::load(File::read(yamlfile))
			else
				@metadata = {}
			end
		end
		@metadata
	end
	def _ls
		@ls ||= Dir::entries(local_infile)
					.select{|fname|not fname.start_with?(".")}
					.map{|fname|"#{local_infile}/#{fname}"}
	end
	def index_pages
		if @pages.nil?
			@pages = Set[]
			for path in _ls
				page = LocalPage::load(path)
				unless page.nil?
					@pages.add(page)
				end
			end
			for data in (_metadata['external'] || [])
				@pages.add(RemotePage::new(data))
			end
		end
		@pages
	end
	def index_pages_leaves
		ret = Set[]
		index_pages.each do |page|
			if page.is_a?(IndexPage)
				ret.merge(page.index_pages)
			else
				ret.add(page)
			end
		end
		return ret
	end

	def index_link(cururl, depth)
		ret = ''
		unless depth <= 1
			ret += "<section><h#{depth}>[#{atom_title}](#{cururl.route_to(url)})</h#{depth}>\n\n"
		end
		for page in index_pages.select{|page|not page.is_a?(IndexPage)}.sort_by{|page|page.atom_updated}.reverse
			ret += page.index_link(cururl, depth+1)
		end
		ret += "\n"
		for page in index_pages.select{|page|page.is_a?(IndexPage)}.sort_by{|page|page.atom_title}
			ret += page.index_link(cururl, depth+1)
		end
		ret += "\n"
		unless depth <= 1
			ret += "</section>\n\n"
		end
		return ret.gsub(/\n\n+/, "\n\n")
	end
	def index_title
		_metadata['title'] || File::basename(local_infile)
	end
	def index_author
		Person::new(_metadata['author'] || Config::get.default_author)
	end

	def local_intype
		return 'markdown'
	end
	def local_outfile
		local_infile.sub(/^src/, 'out')+"/index.html"
	end
	def index_depends
		if @index_depends.nil?
			basename = local_infile.sub(/^src/, 'out')
			deps = Set[local_infile]
			yamlfile = local_infile+"/index.yaml"
			if File::exist?(yamlfile)
				deps.add(yamlfile)
			end
			deps.add('config.yaml')
			index_pages.select{|p|p.is_a?(LocalPage)}.each{|p|deps.merge(p.local_depends[''])}
			@index_depends = deps
		end
		@index_depends
	end
	def local_depends
		if @depends.nil?
			basename = local_infile.sub(/^src/, 'out')
			@depends = {
				'' => index_depends,
				"#{basename}/index.html" => index_depends.clone.merge(["tmpl/index.md.erb", "tmpl/page.html.erb"]),
				"#{basename}/index.atom" => index_depends.clone.merge(["tmpl/index.atom.erb", "tmpl/page.atom.erb"]),
			}
		end
		@depends
	end
	def url
		@outurl ||= Config::get.url + local_outfile.sub(/^out/, '').sub(/\/index\.html$/, '/')
	end
	def local_srcurl
		return nil
	end

	def page_published
		return nil
	end
	def page_updated
		return nil
	end
	def page_years
		return Set[]
	end
end

ERB::new(File::read("tmpl/index.atom.erb")).def_method(IndexPage, 'atom()', "tmpl/index.atom.erb")
ERB::new(File::read("tmpl/index.md.erb")).def_method(IndexPage, 'local_input()', "tmpl/index.md.erb")