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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
|
# frozen_string_literal: true
require "nokogiri"
require "octokit"
require "pathname"
def expand(content, path, &get_content)
content.gsub(/include::(\S+)\[\]/) do |_line|
if File.dirname(path) == "."
new_fname = $1
else
new_fname = (Pathname.new(path).dirname + Pathname.new($1)).cleanpath.to_s
end
new_content = get_content.call(new_fname)
if new_content
expand(new_content.gsub("\xEF\xBB\xBF".force_encoding("UTF-8"), ""), new_fname) { |c| get_content.call(c) }
else
puts "#{new_fname} could not be resolved for expansion"
""
end
end
end
desc "Reset book html to trigger re-build"
task reset_book2: :environment do
Book.where(edition: 2).each do |book|
book.ebook_html = "0000000000000000000000000000000000000000"
book.save
end
end
def genbook(code, &get_content)
nav = '<div id="nav"><a href="[[nav-prev]]">prev</a> | <a href="[[nav-next]]">next</a></div>'
progit = get_content.call("progit.asc")
chapters = {}
appnumber = 0
chnumber = 0
secnumber = 0
# The chapter files are historically located in book/<chapter_name>/1-<chapter_name>.asc
# The new localisation of these files are at the root of the project
chapter_files = /(book\/[01A-C].*\/1-[^\/]*?\.asc|(?:ch[0-9]{2}|[ABC])-[^\/]*?\.asc)/
chaps = progit.scan(chapter_files).flatten
chaps.each_with_index do |filename, _index|
# select the chapter files
if /(book\/[01].*\/1-[^\/]*\.asc|ch[0-9]{2}-.*\.asc)/.match?(filename)
chnumber += 1
chapters["ch#{secnumber}"] = ["chapter", chnumber, filename]
secnumber += 1
end
# detect the appendices
next unless filename =~ /(book\/[ABC].*\.asc|[ABC].*\.asc)/
appnumber += 1
chapters["ch#{secnumber}"] = ["appendix", appnumber, filename]
secnumber += 1
end
# we strip the includes that don't match the chapters we want to include
initial_content = progit.gsub(/include::(.*\.asc)\[\]/) do |match|
if $1 =~ chapter_files
match
else
""
end
end
begin
l10n_file = URI.open("https://raw.githubusercontent.com/asciidoctor/asciidoctor/master/data/locale/attributes-#{code}.adoc").read
rescue StandardError
l10n_file = ""
end
initial_content.gsub!("include::ch01", l10n_file + "\ninclude::ch01")
content = expand(initial_content, "progit.asc") { |filename| get_content.call(filename) }
# revert internal links decorations for ebooks
content.gsub!(/<<.*?\#(.*?)>>/, "<<\\1>>")
asciidoc = Asciidoctor::Document.new(content, attributes: { "lang" => code })
html = asciidoc.render
alldoc = Nokogiri::HTML(html)
number = 1
Book.where(edition: 2, code: code).destroy_all
book = Book.create(edition: 2, code: code)
alldoc.xpath("//div[@class='sect1']").each_with_index do |entry, index|
chapter_title = entry.at("h2").content
if !chapters["ch#{index}"]
puts "not including #{chapter_title}\n"
break
end
chapter_type, chapter_number = chapters["ch#{index}"]
chapter = entry
next if !chapter_title
next if !chapter_number
number = chapter_number
if chapter_type == "appendix"
number = 100 + chapter_number
end
pretext = entry.search("div[@class=sectionbody]/div/p").to_html
id_xref = chapter.at("h2").attribute("id").to_s
schapter = book.chapters.where(number: number).first_or_create
schapter.title = chapter_title.to_s
schapter.chapter_type = chapter_type
schapter.chapter_number = chapter_number
schapter.sha = book.ebook_html
schapter.save
# create xref
csection = schapter.sections.where(number: 1).first_or_create
xref = Xref.where(book_id: book.id, name: id_xref).first_or_create
xref.section = csection
xref.save
section = 1
chapter.search("div[@class=sect2]").each do |sec|
id_xref = sec.at("h3").attribute("id").to_s
section_title = sec.at("h3").content
html = sec.inner_html.to_s + nav
html.gsub!("<h3", "<h2")
html.gsub!(/\/h3>/, "/h2>")
html.gsub!("<h4", "<h3")
html.gsub!(/\/h4>/, "/h3>")
html.gsub!("<h5", "<h4")
html.gsub!(/\/h5>/, "/h4>")
xlink = html.scan(/href="1-.*?\.html\#(.*?)"/)
xlink&.each do |link|
xref = link.first
begin
html.gsub!(/href="1-.*?\.html\##{xref}"/, "href=\"ch00/#{xref}\"")
rescue StandardError
nil
end
end
xlink = html.scan(/href="\#(.*?)"/)
xlink&.each do |link|
xref = link.first
begin
html.gsub!(/href="\##{xref}"/, "href=\"ch00/#{xref}\"")
rescue StandardError
nil
end
end
subsec = html.scan(/<img src="(.*?)"/)
subsec&.each do |sub|
sub = sub.first
begin
html.gsub!(/<img src="#{sub}"/, "<img src=\"/book/en/v2/#{sub}\"")
rescue StandardError
nil
end
end
puts "\t\t#{chapter_type} #{chapter_number}.#{section} : #{chapter_title} . #{section_title} - #{html.size}"
csection = schapter.sections.where(number: section).first_or_create
csection.title = section_title.to_s
csection.html = pretext + html
csection.save
xref = Xref.where(book_id: book.id, name: id_xref).first_or_create
xref.section = csection
xref.save
# record all the xrefs
sec.search(".//*[@id]").each do |id|
id_xref = id.attribute("id").to_s
xref = Xref.where(book_id: book.id, name: id_xref).first_or_create
xref.section = csection
xref.save
end
section += 1
pretext = ""
end
end
book.sections.each do |section|
section.set_slug
section.save
end
end
desc "Generate book html directly from git repo"
task remote_genbook2: :environment do
@octokit = Octokit::Client.new(access_token: ENV.fetch("GITHUB_API_TOKEN", nil))
if ENV["GENLANG"]
books = Book.all_books.select { |code, _repo| code == ENV["GENLANG"] }
else
books = Book.all_books.reject do |code, repo|
repo_head = @octokit.commit(repo, "HEAD").sha
book = Book.where(edition: 2, code: code).first_or_create
repo_head == book.ebook_html
end
end
books.each do |code, repo|
blob_content = Hash.new do |blobs, sha|
content = Base64.decode64(@octokit.blob(repo, sha, encoding: "base64").content)
blobs[sha] = content.force_encoding("UTF-8")
end
repo_head = @octokit.commit(repo, "HEAD")
repo_tree = @octokit.tree(repo, repo_head.commit.tree.sha, recursive: true)
Book.transaction do
genbook(code) do |filename|
file_handle = repo_tree.tree.detect { |tree| tree[:path] == filename }
if file_handle
blob_content[file_handle[:sha]]
end
end
book = Book.where(edition: 2, code: code).first_or_create
book.ebook_html = repo_head.sha
begin
rel = @octokit.latest_release(repo)
get_url = lambda do |name_re|
asset = rel.assets.find { |asset| name_re.match(asset.name) }
asset&.browser_download_url
end
book.ebook_pdf = get_url.call(/\.pdf$/)
book.ebook_epub = get_url.call(/\.epub$/)
book.ebook_mobi = get_url.call(/\.mobi$/)
rescue Octokit::NotFound
book.ebook_pdf = nil
book.ebook_epub = nil
book.ebook_mobi = nil
end
book.save
end
rescue StandardError => e
puts e.message
end
end
desc "Generate book html directly from git repo"
task local_genbook2: :environment do
if ENV["GENLANG"] && ENV["GENPATH"]
genbook(ENV["GENLANG"]) do |filename|
File.open(File.join(ENV["GENPATH"], filename), "r") { |infile| File.read(infile) }
end
end
end
|