-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
129 lines (105 loc) · 2.48 KB
/
app.rb
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
require 'erb'
require 'fileutils'
module StaticSiteDsl
def page(name, &blk)
@pages ||= {}
@pages[name] = blk
end
def call
dir = 'site'
FileUtils.mkdir_p dir
@pages.each_pair do |name, proc|
File.open(File.join(dir, name + '.html'), 'w') do |fw|
fw.puts proc.call
end
end
end
def render(template, &blk)
ERB.new(File.read(template)).result(binding, &blk)
end
end
class SplendorVeritatisIndices
extend StaticSiteDsl
def self.layout
render 'views/_layout.erb' do
yield
end
end
page 'index' do
render 'views/frames.erb'
end
page 'intro' do
layout do
render 'views/intro.erb'
end
end
page 'antiphonale' do
layout do
IndexRenderer.new('antiphonale').render
end
end
end
class IndexRenderer
def initialize(index)
@index = index
@site = 'http://unpeudetout.info/splendorveritatis' # originally 'http://splendorveritatis.org'
@book_path = @index
end
def index_path(i)
File.join 'data', i+'.txt'
end
def page_url(page)
"#{@site}/#{@book_path}/large-#{page}.html"
end
def render
b = '' # output buffer
File.open(index_path(@index)) do |fr|
title = fr.gets
b << "<h1>#{title}</h1>"
i = 1
fr.each_line do |l|
i += 1
next if l.start_with? '#'
next if l =~ /^\s*$/
b << entry(l, i)
end
end
b
end
def entry(str, line)
m = str.match(/^(\d+)\s+(=*)(.*)$/)
if m.nil?
return error(str, line)
end
page = m[1]
heading_level = m[2].size rescue 0
label = m[3]
label = modify label, heading_level
"<p class=\"lvl#{heading_level}\"><a href=\"#{page_url(page)}\" target=\"content\"><span class=\"pageno\">#{page}</span> #{label}</a></p>"
end
def error(str, line)
"<p><b>Error on line #{line}: '#{str}'</b></p>"
end
def modify(str, heading_level)
shortcuts = {
'v' => 'ad vesperas',
'vig' => 'ad vigilias',
'n1' => 'in i. nocturno',
'n2' => 'in ii. nocturno',
'n3' => 'in iii.nocturno',
'L' => 'ad laudes', # uppercase, in order to avoid l vs. 1 confusion
'1' => 'ad primam',
'3' => 'ad tertiam',
'6' => 'ad sextam',
'9' => 'ad nonam',
'v2' => 'in ii. vesperis',
'c' => 'ad completorium',
}
str
.gsub(/in festo/i, '')
.gsub(/[a-zL0-9]+/) do |match|
((heading_level == 0) && shortcuts[match]) || match
end
end
end
SplendorVeritatisIndices.call