-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathbook.rb
392 lines (348 loc) · 10.6 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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# frozen_string_literal: true
require 'fileutils'
require 'yaml'
class Book
@@all_books = {
"az" => "progit2-aze/progit2",
"be" => "progit/progit2-be",
"bg" => "progit/progit2-bg",
"cs" => "progit-cs/progit2-cs",
"de" => "progit/progit2-de",
"en" => "progit/progit2",
"es" => "progit/progit2-es",
"fr" => "progit/progit2-fr",
"gr" => "progit2-gr/progit2",
"id" => "progit/progit2-id",
"it" => "progit/progit2-it",
"ja" => "progit/progit2-ja",
"ko" => "progit/progit2-ko",
"mk" => "progit2-mk/progit2",
"ms" => "progit2-ms/progit2",
"nl" => "progit/progit2-nl",
"pl" => "progit2-pl/progit2-pl",
"pt-br" => "progit/progit2-pt-br",
"pt-pt" => "progit2-pt-pt/progit2",
"ru" => "progit/progit2-ru",
"sl" => "progit/progit2-sl",
"sr" => "progit/progit2-sr",
"tl" => "progit2-tl/progit2",
"tr" => "progit/progit2-tr",
"sv" => "progit2-sv/progit2",
"uk" => "progit/progit2-uk",
"uz" => "progit/progit2-uz",
"zh" => "progit/progit2-zh",
"zh-tw" => "progit/progit2-zh-tw",
"fa" => "progit2-fa/progit2"
}
def self.all_books
@@all_books
end
attr_accessor :chapters
attr_accessor :xrefs
attr_accessor :ebook_pdf
attr_accessor :ebook_epub
attr_accessor :ebook_mobi
attr_accessor :sha
def initialize(edition, language_code)
@edition = edition
@language_code = language_code
@chapters = []
@xrefs = {}
end
def front_matter
front_matter = {
"category" => "book",
"section" => "documentation",
"subsection" => "book",
"sidebar" => "book",
"book" => {
"language_code" => @language_code
}
}
end
def content_note
"### DO NOT EDIT! Generated by script/update-book2.rb"
end
def wrap_front_matter(front_matter)
"#{front_matter.to_yaml.sub("---\n", "---\n#{self.content_note}\n")}---\n"
end
def absolute_path(path, top_level = "content")
File.absolute_path(File.join(File.dirname(__FILE__), "..", "external", "book", top_level, "book", @language_code, "v#{@edition}", path))
end
def relative_url(path)
return "book/#{@language_code}/v#{@edition}#{path.nil? || path.empty? || path == "." ? "" : "/#{path}"}"
end
def fixImagePath(path)
if path == "images/tagapangasiwa.png"
# In https://github.com/progit2-tl/progit2/pull/45 the
# reference was changed, but the file was not renamed
"images/collaborators.png"
else
path
end
end
def removeAllFiles
FileUtils.rm_rf(absolute_path("."))
end
def save
chapters = []
@chapters.each do |chapter|
next if chapter.nil?
sections = []
chapter.sections.each do |section|
next if section.nil?
sections.append({
"cs_number" => section.cs_number,
"title" => section.title,
"url" => section.relative_url(nil).gsub(/%3F/, '?')
})
end
chapters.append({
"cs_number" => chapter.cs_number,
"title" => chapter.title,
"sections" => sections
})
end
data = {
"language_code" => @language_code,
"repository_url" => "https://github.com/#{@@all_books[@language_code]}",
"chapters" => chapters
}
path = File.join(File.dirname(__FILE__), "..", "external", "book", "data", "book", "#{@language_code}.yml")
FileUtils.mkdir_p(File.dirname(path))
File.open(path, 'w') do |file|
file.write("#{self.content_note}\n")
file.write(data.to_yaml.strip)
end
front_matter = self.front_matter
front_matter["page_title"] = "Git - Book"
front_matter["url"] = "/book/#{@language_code}/v#{@edition}.html"
front_matter["aliases"] = [
"/book/#{@language_code}/v#{@edition}/index.html",
"/book/#{@language_code}/v1/index.html",
"/book/#{@language_code}/index.html"
]
if @language_code == "en"
front_matter["aliases"].push("/book/index.html")
front_matter["aliases"].push("/book/v1/index.html")
end
front_matter["book"]["front_page"] = true
front_matter["book"]["repository_url"] = "https://github.com/#{@@all_books[@language_code]}"
front_matter["book"]["sha"] = self.sha
if self.ebook_pdf
front_matter["book"]["ebook_pdf"] = self.ebook_pdf
end
if self.ebook_epub
front_matter["book"]["ebook_epub"] = self.ebook_epub
end
if self.ebook_mobi
front_matter["book"]["ebook_mobi"] = self.ebook_mobi
end
path = self.absolute_path("_index.html")
FileUtils.mkdir_p(File.dirname(path))
File.open(path, 'w') do |file|
file.write(self.wrap_front_matter(front_matter))
end
front_matter = { "redirect_to" => "book/#{@language_code}/v#{@edition}" }
File.write(self.absolute_path("../_index.html"), self.wrap_front_matter(front_matter))
FileUtils.mkdir_p(self.absolute_path("../v1"))
File.write(self.absolute_path("../v1/_index.html"), self.wrap_front_matter(front_matter))
if @language_code == "en"
File.write(self.absolute_path("../../_index.html"), self.wrap_front_matter(front_matter))
FileUtils.mkdir_p(self.absolute_path("../../v1"))
File.write(self.absolute_path("../../v1/_index.html"), self.wrap_front_matter(front_matter))
end
FileUtils.mkdir_p(self.absolute_path("ch00"))
@xrefs.each do |id_xref, section|
path = self.absolute_path("ch00/#{id_xref}.html")
if section == 'redirect-to-en'
if id_xref == 'ch01-introduction'
id_xref = 'ch01-getting-started'
elsif id_xref == 'ch02-git-basics' || id_xref == '_bab_dasar-dasar_git'
id_xref = 'ch02-git-basics-chapter'
elsif id_xref == '_percabangan_git'
id_xref = 'ch03-git-branching'
elsif id_xref == 'r_git_on_the_server'
id_xref = 'ch04-git-on-the-server'
elsif id_xref == '_getting_notes' || id_xref == '_sharing_notes'
# Was removed in e2af0d7b (Remove dead notes content, 2014-11-09),
# fall back to `git fetch`
id_xref = '_git_fetch'
elsif id_xref == 'r_undoing' || id_xref == '_mengembalikan_ke_sebelumnya'
id_xref = '_undoing'
end
if id_xref == '_git_notes'
url = "docs/git-notes"
else
url = "book/en/v2/ch00/#{id_xref}"
end
else
url = "#{section.relative_url(nil)}##{id_xref}"
end
front_matter = { "redirect_to" => url }
File.open(path, 'w') do |file|
file.write(self.wrap_front_matter(front_matter))
end
end
end
def book_v1_aliases(cs_number)
if @book_v1_aliases.nil?
path = File.absolute_path(File.join(File.dirname(__FILE__), "..", "data", "book_v1.yml"))
if File.exists?(path)
@book_v1_aliases = YAML.load_file(path)&.[](@language_code)
end
@book_v1_aliases = {} if @book_v1_aliases.nil?
end
return @book_v1_aliases[cs_number]&.flat_map { |title|
["/book/#{@language_code}/#{title}.html", "/book/#{@language_code}/v1/#{title}.html"]
}
end
end
class Chapter
def initialize(book)
@book = book
@sections = []
@previous_chapter = @book.chapters[-1]
if not @previous_chapter.nil?
@previous_chapter.next_chapter = self
end
end
def cs_number
"#{self.chapter_type == "appendix" ? "A" : ""}#{self.chapter_number}"
end
def front_matter
front_matter = @book.front_matter
front_matter["book"]["chapter"] = {
"title" => self.title,
"number" => self.chapter_number
}
return front_matter
end
def wrap_front_matter(front_matter)
@book.wrap_front_matter(front_matter)
end
attr_accessor :title
attr_accessor :chapter_type
attr_accessor :chapter_number
attr_accessor :sha
attr_accessor :sections
attr_accessor :book
def next_chapter=(chapter)
@next_chapter = chapter
end
def absolute_path(path)
return @book.absolute_path(path)
end
def relative_url(path)
return @book.relative_url(path)
end
def previous_chapter
return @previous_chapter
end
def next_chapter
return @next_chapter
end
def save
# TODO
end
def book_v1_aliases(cs_number)
return @book.book_v1_aliases(cs_number)
end
end
class Section
def initialize(chapter, section_number)
@chapter = chapter
@section_number = section_number
@previous_section = @chapter.sections[-1]
if @previous_section.nil?
previous_chapter = chapter.previous_chapter
if not previous_chapter.nil?
@previous_section = previous_chapter.sections[-1]
end
end
if not @previous_section.nil?
@previous_section.next_section = self
end
end
def cs_number
"#{@chapter.cs_number}.#{@section_number}"
end
def front_matter
front_matter = @chapter.front_matter
front_matter["title"] = "Git - #{self.title}"
front_matter["book"]["section"] = {
"title" => self.title,
"number" => @section_number,
"cs_number" => self.cs_number,
"previous" => self.previous_section_url,
"next" => self.next_section_url
}
if @slug =~ /:|[^-A-Za-z0-9_]/
relative_url = self.relative_url(@slug)
front_matter["url"] = "/#{relative_url.gsub(/%3F/, '?')}.html"
if relative_url =~ /%3F/
front_matter["aliases"] = [
"/#{relative_url.gsub(/%3F.*/, '')}.html"
]
end
end
v1_aliases = @chapter.book_v1_aliases(self.cs_number)
unless v1_aliases.nil?
front_matter["aliases"] = [] if front_matter["aliases"].nil?
front_matter["aliases"] += v1_aliases
end
return front_matter
end
attr_accessor :title
attr_accessor :html
attr_accessor :slug
def next_section=(section)
@next_section = section
end
def slug
return @slug if not @slug.nil?
if self.title.empty?
title = @chapter.title
else
title = (@chapter.title + "-" + self.title)
end
@slug = title.gsub(/\(|\)|\./, "").gsub(/\s+/, "-").gsub("'", "-")
end
def absolute_path(path)
return @chapter.absolute_path(path)
end
def relative_url(path)
if path.nil? || path.empty?
path = self.slug
end
return @chapter.relative_url(path.gsub(/\?/, '%3F'))
end
def previous_section_url
if @previous_section.nil?
return self.relative_url(nil)
end
return self.relative_url(@previous_section.slug)
end
def next_section_url
if @next_section.nil?
return self.relative_url(nil)
end
return self.relative_url(@next_section.slug)
end
def save
return if self.slug.nil?
path = self.absolute_path(self.slug)
FileUtils.mkdir_p(File.dirname(path))
File.open("#{path}.html", 'w') do |file|
file.write(@chapter.wrap_front_matter(self.front_matter))
file.write(self.html.strip)
end
end
def saveImage(path, content)
path = @chapter.book.absolute_path(path, "static")
FileUtils.mkdir_p(File.dirname(path))
File.open(path, 'w') do |file|
file.write(content)
end
end
end