summaryrefslogtreecommitdiff
path: root/lib/page_index.rb
blob: 178525c246e7fde5f18b3833eddd66d2e5915fa6 (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
# coding: utf-8
require 'erb'
require 'set'
require 'yaml'

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

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}"}
					.select{|path|Dir::exist?(path) or Config::get.html_suffixes.include?(File::extname(path).gsub(/^[.]/, ''))}
	end
	def index_pages
		if @pages.nil?
			@pages = Set[]
			for path in _ls
				@pages.add( Dir::exist?(path) ? IndexPage::new(path) : LocalPage::new(path) )
			end
			for data in (_metadata['external'] || [])
				@pages.add(RemotePage::new(data))
			end
		end
		@pages
	end

	def index_link(cururl, depth)
		ret = ''
		unless depth <= 1
			ret += "<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_published}
			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"
		return ret.gsub(/\n\n+/, "\n\n")
	end
	def atom_title
		_metadata['title']
	end

	def local_outfile
		local_infile.sub(/^src/, 'out')+"/index.html"
	end
	def local_depends
		if @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
			index_pages.select{|p|!p.is_a?(RemotePage)}.each{|p|deps.merge(p.local_depends[''])}
			@depends = {
				'' => deps,
				"#{basename}/index.html" => deps.clone.merge(["tmpl/index.md.erb", "tmpl/page.html.erb"]),
				"#{basename}/index.atom" => deps.clone.merge(["tmpl/index.atom.erb", "tmpl/page.atom.erb"]),
			}
		end
		@depends
	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")