summaryrefslogtreecommitdiff
path: root/lib/page_index.rb
blob: 3e25813027808b2c4d241d3ac22ccaa38c0f2f49 (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
# 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 = []
			for path in _ls
				if Dir::exist?(path)
					page = IndexPage::new(path)
					@pages.unshift(page)
					@pages += page.index_pages
				else
					@pages.unshift(LocalPage::new(path))
				end
			end
			for data in _metadata['external']
				@pages.unshift(RemotePage::new(data))
			end
		end
		@pages
	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.each{|p|deps.merge(p.local_outfile[''])}
			@depends = {
				"#{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")