-
Notifications
You must be signed in to change notification settings - Fork 0
/
book.rb
118 lines (86 loc) · 3.14 KB
/
book.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
# coding: utf-8
# Add local extensions to Prophecy here.
# This file will be loaded by the CLI.
#require 'pry'
#require 'pry-debugger'
require 'csv'
module Prophecy
class CLI < Thor
desc "markdown_verseindex", "Add page IDs to versepages.md and generate verse index for TOC"
def markdown_verseindex
versepath = File.join('manuscript', 'markdown', 'versepages.md')
text = IO.read(versepath)
# Add page IDs
if text.include?('{:.dhp-verse}')
text.gsub!('{:.dhp-verse}', '{:.dhp-verse #pageN}')
n = 1
while text =~ /#pageN/
text.sub!('#pageN', "#page#{n}")
n += 1
end
# Backup versepages.md
FileUtils.cp(versepath, "#{versepath}.bak")
File.open(versepath, "w"){|f| f << text }
end
pages = text.scan(/[{]:.dhp-verse (#page[0-9]+)[}]/)
versenum = text.scan(/> \*(v\. [0-9-]+)\*$/)
firstlines = text.scan(/\n\n> ([\w…\.;\?\!‘’, –-]+)[\.;\?\!‘’, \\–-]*$/m)
#binding.pry
unless pages.size == versenum.size && versenum.size == firstlines.size
puts "pages: #{pages.size} versenum: #{versenum.size} firstlines: #{firstlines.size}"
puts "Something wrong with the matches, the numbers are not equal."
exit 2
end
# Write index as csv
CSV.open('verseindex.csv', 'w') do |csv|
pages.each_with_index do |page, index|
csv << [ page[0], "#{versenum[index][0]} - #{firstlines[index][0]}" ]
end
end
end
end
class Book
def render_navpoints
ret = ""
lastplay = nil
@navpoints.each_with_index do |nav, idx|
ret += "<navPoint id=\"nav#{nav['playOrder']}\" playOrder=\"#{nav['playOrder']}\">\n"
ret += "<navLabel><text>#{nav['text']}</text></navLabel>\n"
ret += "<content src=\"#{nav['src']}\"/>"
next_nav = @navpoints[idx+1]
if !next_nav.nil? && next_nav['level'] < nav['level']
d = nav['level'] - next_nav['level'] + 1
d.times{ ret += "</navPoint>\n" }
elsif next_nav.nil? || next_nav['level'] == nav['level']
ret += "</navPoint>\n"
end
lastplay = nav['playOrder']
end
# Add verse index navpoints
require 'csv'
playorder = lastplay + 1
ret += "<navPoint id=\"nav#{playorder}\" playOrder=\"#{playorder}\">\n"
ret += "<navLabel><text>Index of Verses</text></navLabel>\n"
ret += "<content src=\"Text/versepages.xhtml#dhammapada-reflections\" />"
CSV.foreach('verseindex.csv') do |row|
playorder += 1
ret += "<navPoint id=\"nav#{playorder}\" playOrder=\"#{playorder}\">\n"
ret += "<navLabel><text>#{row[1]}</text></navLabel>\n"
ret += "<content src=\"Text/versepages.xhtml#{row[0]}\"/>"
ret += "</navPoint>\n"
end
ret += "</navPoint>"
ret
end
def verseindex_as_ul
require 'csv'
ret = []
ret << "<ul class='verseindex'>"
CSV.foreach('verseindex.csv') do |row|
ret << "<li><a href='../Text/versepages.xhtml#{row[0]}'>#{row[1]}</a></li>"
end
ret << "</ul>"
ret.join("\n")
end
end
end