-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathRakefile
234 lines (190 loc) · 8.73 KB
/
Rakefile
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
require 'yaml'
require 'hm'
require_relative 'lib/common'
require_relative 'lib/core_ext'
require_relative 'lib/structure'
require_relative 'lib/ruby_repo_index'
require_relative 'lib/rdoc_extractor'
require_relative 'lib/kernel_methods'
require_relative 'lib/sanitizer'
require_relative 'lib/renderers/chapter'
require_relative 'lib/renderers/toc'
structure = Structure.load
repo_index = RubyRepoIndex.new('ruby')
rdoc_cache = Hash.new { |h, path| h[path] = YAML.load_file(path) }
RDOC_TO_MARKDOWN = RDoc::Markup::ToMarkdown.new
CORE_REFERENCE = "https://ruby-doc.org/core-#{BOOK_RUBY_VERSION}.0"
LIB_REFERENCE = "https://ruby-doc.org/stdlib-#{BOOK_RUBY_VERSION}.0/libdoc/%s/rdoc"
desc 'Render everything'
task default: %i[toc chapters]
desc 'Render final book structure file'
task toc: '../_data/book.yml'
desc 'Render final book chapters from source Markdown'
task chapters: structure.each_chapter.map(&:out_path)
namespace :dev do
desc 'Just parse RDoc sources into YAML temporary representation'
task parse_rdoc: %w[
intermediate/rdoc/core.yml
intermediate/rdoc/ext.yml
intermediate/rdoc/lib.yml
]
desc 'Convert parsed RDoc to Markdown'
task rdoc2md: structure.each_part.select(&:from_repo?).reject(&:ignore?).map(&:raw_path).sort.uniq
desc 'Sanitize Markdown'
task sanitize_md: structure.each_part.filter_map(&:ready_path).sort.uniq
desc 'Validate that everything available in Ruby sources is listed in structure.yml, and vice versa'
task validate_listing: [Structure::PATH] +
%w[intermediate/rdoc/core.yml intermediate/rdoc/ext.yml intermediate/rdoc/lib.yml] do
from_structure = structure.each_part.select(&:from_repo?).map(&:path)
from_source =
repo_index.doc_files.map { _1.to_s.sub('ruby/', '').sub('.rdoc', '.md') }.sort +
rdoc_cache['intermediate/rdoc/core.yml'].keys.map { "core/#{_1.tr(':', '-')}.md" } +
rdoc_cache['intermediate/rdoc/ext.yml'].flat_map { |lib, mods|
mods.keys.map { "ext/#{lib}/#{_1.tr(':', '-')}.md" }
} +
rdoc_cache['intermediate/rdoc/lib.yml'].flat_map { |lib, mods|
mods.keys.map { "lib/#{lib}/#{_1.tr(':', '-')}.md" }
}
from_source.reject! { |path|
path.split('--').count >= 3 ||
path.split('--').count == 2 && !path.match?(/(Net|RubyVM|Enumerator|File)--/)
}
puts "Sources mentioned in structure.yml, but not found in current Ruby version:"
(from_structure - from_source).each { puts " #{_1}" }.then { puts " — None —" if _1.empty? }
puts "Modules found in current Ruby version, but not mentioned in structure.yml:"
(from_source - from_structure).each { puts " #{_1}" }.then { puts " — None —" if _1.empty? }
end
desc 'Validate that all content/ files are used in structure.yml'
task validate_content: [Structure::PATH] + Dir['content/**/*.md'] do |t|
from_structure = structure.each_part.select(&:content?).map(&:path) +
structure.each_part.filter_map(&:insert).flatten.filter_map { _1[:source] }.select(&File.method(:exist?))
from_source = t.prerequisites[1..]
puts "Content from content/ which is not mentioned in structure.yml:"
(from_source - from_structure).each { puts " #{_1}" }.then { puts " — None —" if _1.empty? }
end
desc 'Validate that no files in ../ is missing in structure.yml (artifacts of old versions)'
task validate_rendered: [Structure::PATH] do
from_structure = structure.each_chapter.map(&:out_path)
from_site = Dir['../**/*.md'].grep_v(/^\.\.\/_/)
puts "Rendered content which is not mentioned in structure.yml:"
(from_site - from_structure).each { puts " #{_1}" }.then { puts " — None —" if _1.empty? }
end
desc 'Validate internal cross-links'
task validate_links: structure.each_chapter.map(&:out_path) do |t|
puts "Validating internal links..."
existing = t.prerequisites.map { _1.sub('../', '') }
root = __dir__ + '/'
checker = DidYouMean::SpellChecker.new(dictionary: existing)
# Regexp is just too hard :( And probably broken.
t.prerequisites.grep_v(/regexp\.md/).each do |path|
# FIXME: Probably better way...
# Kramdown::Document.new(path)...and find real links...
dir = File.dirname(path.sub('../', ''))
# TODO: RELATIVE to current
File.read(path).scan(/\[(?:\w|\s)+?\]\((.+?)(?:\#.+)?\)/m).flatten.grep_v(/^https?:/)
.map { File.expand_path(_1, dir).sub(root, '') }
.-(existing)
.each do
candidates = checker.correct(_1)
puts "#{path}: #{_1} link incorrect." + (candidates ? " Options: #{candidates.join(', ')}" : "")
end
end
end
desc 'Run all validations on structure, intermediate and rendered files'
task validate: %i[validate_listing validate_content validate_rendered validate_links]
end
# Particular file rendering rules
# ===============================
directory 'intermediate/rdoc'
# Book structure conversion
# -------------------------
file '../_data/book.yml' => Structure::PATH do |t|
File.write t.name, Renderers::TOC.call(structure).to_yaml
end
# RDoc comment text extraction from *.c/*.rb files
# ----------------------------
file 'intermediate/rdoc/core.yml' => ['intermediate/rdoc', *repo_index.core_files] do |t|
RDocExtractor.call(*t.prerequisites, with_methods: %w[Kernel])
.then { |modules| File.write(t.name, modules.to_yaml) }
end
file 'intermediate/rdoc/ext.yml' => ['intermediate/rdoc', *repo_index.ext_files.values.flatten] do |t|
repo_index.ext_files
.to_h { |lib, pathes| [lib, RDocExtractor.call(*pathes)] }
.then { File.write(t.name, _1.to_yaml) }
end
file "intermediate/rdoc/lib.yml" => ['intermediate/rdoc', *repo_index.lib_files.values.flatten] do |t|
repo_index.lib_files
.to_h { |lib, pathes| [lib, RDocExtractor.call(*pathes)] }
.then { File.write(t.name, _1.to_yaml) }
end
file 'intermediate/sanitized/_special/kernel.md' => %w[intermediate/rdoc/core.yml config/kernel.yml] do |t|
FileUtils.mkdir_p File.dirname(t.name)
rdoc_cache[t.prerequisites.first].dig('Kernel', 'methods')
.then { KernelMethods.call(_1) }
.then { File.write t.name, _1 }
end
def rdoc2md(path, module_name, rdoc, link_prefix:)
FileUtils.mkdir_p(File.dirname(path))
File.write path,
"# #{module_name}\n\n" +
RDOC_TO_MARKDOWN.convert(rdoc) +
"\n[#{module_name} Reference](#{link_prefix}/#{module_name.gsub('::', '/')}.html)\n"
end
structure.each_chapter do |chap|
# Automatic conversion of RDoc to (dirty) Markdown
# ------------------------------------------------
chap.parts.select(&:doc?).each do |part|
file part.raw_path => part.src_path do |t|
FileUtils.mkdir_p File.dirname(t.name)
File.read(t.prerequisites.first)
.then(&RDOC_TO_MARKDOWN.method(:convert))
.then { File.write t.name, _1 }
end
end
chap.parts.select(&:core?).each do |part|
file part.raw_path => 'intermediate/rdoc/core.yml' do |t|
rdoc_cache[t.prerequisites.first]
.dig(part.module_name, 'main')
.tap { |mod| mod or warn "No module found: #{part.module_name}" }
&.then { |rdoc| rdoc2md(t.name, part.module_name, rdoc, link_prefix: CORE_REFERENCE) }
end
end
chap.parts.select(&:ext?).each do |part|
file part.raw_path => 'intermediate/rdoc/ext.yml' do |t|
rdoc_cache[t.prerequisites.first].dig(part.lib_name, part.module_name, 'main')
.tap { |mod| mod or warn "No module found: #{part.lib_name}/#{part.module_name}" }
&.then { |rdoc|
rdoc2md(t.name, part.module_name, rdoc, link_prefix: LIB_REFERENCE % part.lib_name)
}
end
end
chap.parts.select(&:lib?).each do |part|
file part.raw_path => "intermediate/rdoc/lib.yml" do |t|
rdoc_cache[t.prerequisites.first].dig(part.lib_name, part.module_name, 'main')
.tap { |mod| mod or warn "No module found: #{part.lib_name}/#{part.module_name}" }
&.then { |rdoc|
rdoc2md(t.name, part.module_name, rdoc, link_prefix: LIB_REFERENCE % part.lib_name)
}
end
end
# Sanitization of (RDoc-sourced) Markdown
# ---------------------------------------
chap.parts.select(&:raw_path).each do |part|
file part.ready_path => part.raw_path do |t|
unless File.exist?(t.prerequisites.first)
warn "File not exis #{t.prerequisites.first}"
next
end
FileUtils.mkdir_p File.dirname(t.name)
File.read(t.prerequisites.first)
.then { part.site? ? Sanitizer.from_site(part.path, _1) : Sanitizer.from_repo(part.path, _1) }
.then { File.write(t.name, _1) }
end
end
# Rendering to final Markdown in the root folder
# ----------------------------------------------
file chap.out_path => %w[config/structure.yml] + chap.parts.filter_map(&:ready_path) do |t|
FileUtils.mkdir_p File.dirname(t.name)
Renderers::Chapter.call(chap).then { File.write t.name, _1 }
end
end