-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
499 lines (432 loc) · 14.2 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
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
require 'bundler/setup'
Bundler.require
def directory? path
File.directory? path
end
def ref(raw_ref: ENV['REF'])
raw_ref or raise "Please set a REF env variable, e.g. `env REF=v0.8.0 rake doc`\n\n"
end
def components
components = ENV['COMPONENTS'].to_s
components = 'lib corelib stdlib' if components.empty?
components.split(/\W+/)
end
# The path to a local checkout of Opal, will use `ref` just to generate
# documentation titles and target folders. E.g. `env LOCAL="../opal" rake …`
local = ENV['LOCAL']
opal_dir = local || 'opal'
task :setup do
unless local
directory? 'opal' or sh 'git clone https://github.com/opal/opal.git opal'
Dir.chdir 'opal' do
sh "git fetch --all"
sh "git reset --hard"
sh "git clean -fx"
sh "git checkout --detach #{ref}"
end
end
directory? 'gh-pages' or sh 'git clone [email protected]:opal/docs.git gh-pages --reference . -b gh-pages'
Dir.chdir 'gh-pages' do
sh "git reset --hard"
sh "git clean -fx"
sh "git checkout gh-pages"
end unless ENV['SKIP_GH_PAGES_RESET']
end
def base_title
"Opal #{pretty_ref(ref)}"
end
def pretty_ref(ref)
ref.tr('-', '.').sub('stable', 'x')
end
task :api => :setup do
base_dir = "gh-pages/api/#{ref}"
# Still need to decide how to format the runtime, for now let's just put it
# in a markdown file and render it as it is.
runtime_path = "#{opal_dir}/opal/corelib/runtime.js"
markdown_lines = runtime_markdown(runtime_docs(File.read runtime_path))
File.write "#{opal_dir}/opal/corelib/RUNTIME.md", markdown_lines.join("\n")
ruby_lines = runtime_ruby(runtime_docs(File.read runtime_path))
File.write "#{opal_dir}/opal/corelib/runtime.js.rb", ruby_lines.join("\n")
components.each do |component|
yard(component: component, base_dir: base_dir, base_title: base_title)
end
end
task :guides => :setup do
base_dir = "gh-pages/guides/#{ref}"
pygments_css = Pygments.css(style: 'colorful')
css = <<-CSS
body {font-family: sans-serif;}
#{pygments_css}
CSS
target_paths = []
files = Dir["#{opal_dir}/docs/*.md"]
title_for = -> file do
File.read(file).scan(/^#([^#].*?)$/).flatten.first.strip
rescue
warn "ERROR: missing a title for #{file}, looking for a subtitle"
File.read(file).scan(/^#+([^#].*?)$/).flatten.first.strip
end
target_for = -> file { File.basename(file).sub('.md', '.html') }
mkdir_p base_dir
is_index = -> { _1.end_with? 'index.md' }
files.each do |path|
html_contents = markdown(File.read(path))
target_path = target_for[path]
title = title_for[path]
puts "#{path.ljust 40} → #{title}"
html_title = "#{base_title} · #{title}"
html_nav = %{<nav><a href="./index.html">« Back to index</a></nav><hr>}
html_footer = %{<footer><hr/>
You're encouraged to help improve the quality of this guide.
Please contribute if you see any typos, factual errors, or missing information.<br/>
To get started, <a href="https://github.com/opal/opal/tree/master/docs">head to the docs folder in the main repo</a>.
</footer>}
html_body = <<-HTML
#{html_nav unless is_index[path]}
#{html_contents}
#{html_footer}
HTML
File.write "#{base_dir}/#{target_path}", html_template(html_body, title: html_title, css: css)
end
unless files.any?(is_index)
html_title = "#{base_title} · Guides"
html_body = %{
<h1>#{html_title}</h1>
<ul>
#{files.map {|t| "<li><a href='./#{target_for[t]}'>#{title_for[t]}</a></li>"}.join}
</ul>
}
File.write "#{base_dir}/index.html", html_template(html_body, title: html_title)
end
end
task :index do
html_title = 'Opal · Documentation Central'
version_sorting = -> v {
segments = v.split('.').map do |segment|
if segment =~ /^\d+$/
-(segment.to_i)
else
segment
end
end
segments [3] ||= '0' # if we have not beta/rc/etc let's use the string "0"
segments
}
sorting = -> v {
case v
when /^v/
['1_version', *version_sorting[v[1..-1]]]
else
['0_branch']
end
}
api_versions = Dir['gh-pages/api/*/*/index.html' ].map{|f| f.scan(%r{/api/([^/]+)/}) }.flatten.uniq.sort_by(&sorting)
guides_versions = Dir['gh-pages/guides/*/index.html'].map{|f| f.scan(%r{/guides/([^/]+)/})}.flatten.uniq.sort_by(&sorting)
api_versions.each do |version|
ENV['REF'] = version
components_index(base_title: base_title, components: components, base_dir: "gh-pages/api/#{version}")
end
api_path = -> v { "./api/#{v}/index.html" }
guides_path = -> v { "./guides/#{v}/index.html" }
stable_v = (api_versions - ['master']).sort_by { |v| Gem::Version.new(v.sub(/^v/, '')) }.last
stable_html = <<-HTML
<div class="jumbotron">
<h1>#{pretty_ref(stable_v)} <small>stable</small></h1>
<p><a href="https://github.com/opal/opal/blob/master/CHANGELOG.md">See the full Changelog to see <b>what's new</b></a></p>
<p>
<a class="btn btn-primary btn-lg" href="#{api_path[stable_v]}" role="button">API Docs</a>
<a class="btn btn-primary btn-lg" href="#{guides_path[stable_v]}" role="button">Guides</a>
</p>
</div>
HTML
api_html = <<-HTML
<div style="float: left; min-width: 50%;">
<h3>API Docs</h3>
<ul>
#{api_versions.map {|v| "<li><a href='#{api_path[v]}'>#{pretty_ref(v)}</a></li>"}.join}
</ul>
</div>
HTML
guides_html = <<-HTML
<div style="float: left; min-width: 50%;">
<h3>Guides</h3>
<ul>
#{guides_versions.map {|v| "<li><a href='#{guides_path[v]}'>#{pretty_ref(v)}</a></li>"}.join}
</ul>
</div>
HTML
other_versions_html = <<-HTML
<div class="well">
<h2>All versions</h2>
#{guides_html}
#{api_html}
<div style="clear:both"></div>
</div>
HTML
html_body = <<-HTML
<div class="page-header">
<h1>#{html_title}</h1>
</div>
#{stable_html}
#{other_versions_html}
HTML
File.write "gh-pages/index.html", html_template(html_body, title: html_title)
end
def sdoc(component:, base_dir:, base_title:)
target = case component
when 'corelib' then 'opal/opal'
when 'stdlib' then 'opal/stdlib'
when 'lib' then 'opal/lib'
end
sh %{
sdoc
--format sdoc
--markup tomdoc
--github
--output #{base_dir}/#{component}
--title "#{base_title} · #{component}"
--hyperlink-all
#{target}
}.gsub(/\n */, " ").strip
end
def yard(component:, base_dir:, base_title:)
target = case component
when 'corelib' then 'opal/opal'
when 'stdlib' then 'opal/stdlib'
when 'lib' then 'opal/lib'
end
target = File.expand_path(target)
without_root = -> path { File.expand_path(path)[(File.expand_path(__dir__).size+1)..-1] }
output = "#{__dir__}/#{base_dir}/#{component}"
rm_rf "#{output}/*"
sh %{
yardoc
--template-path="#{__dir__}/templates/yard/"
--output=#{output}
--title="#{component} (#{base_title})"
--db="#{__dir__}/.yardoc-#{base_dir.downcase.gsub(/[^a-z\d]/,'-')}-#{component}"
--exclude 'node_modules'
--markup="markdown"
--no-cache
--main #{without_root[target]}/README.md
'#{without_root[target]}/**/*.rb' '#{without_root[target]}/**/*.js.rb'
-
#{without_root[target]}/**/*.md
}.gsub(/\n */, " ").strip
end
def components_index(base_title:, components:, base_dir:)
html_title = "#{base_title} API Documentation Index"
html_body = <<-HTML
<h1>#{html_title}</h1>
<ul>
#{components.map {|c| "<li><a href='./#{c}/index.html'>#{c}</a></li>"}.join}
</ul>
HTML
File.write "#{base_dir}/index.html", html_template(html_body, title: html_title)
end
class HTMLwithPygments < Redcarpet::Render::HTML
def block_code(code, language)
language ||= 'text'
Pygments.highlight(code, lexer: language)
rescue
Pygments.highlight(code, lexer: 'text')
end
NOTES_REGEXP = '^(TIP|IMPORTANT|CAUTION|WARNING|NOTE|INFO|TODO)[.:](.*?)'
def paragraph(text)
if text =~ /#{NOTES_REGEXP}/
convert_notes(text)
else
"<p>#{text}</p>"
end
end
def convert_notes(body)
# The following regexp detects special labels followed by a
# paragraph, perhaps at the end of the document.
#
# It is important that we do not eat more than one newline
# because formatting may be wrong otherwise. For example,
# if a bulleted list follows the first item is not rendered
# as a list item, but as a paragraph starting with a plain
# asterisk.
body.gsub(/#{NOTES_REGEXP}(\n(?=\n)|\Z)/m) do
css_class = case $1
when 'CAUTION', 'IMPORTANT'
'warning'
when 'TIP'
'info'
else
$1.downcase
end
%(<div class="#{css_class}"><p>#{$2.strip}</p></div>)
end
end
end
def markdown(text)
renderer = HTMLwithPygments.new(:hard_wrap => true, :filter_html => true)
options = {
:autolink => true,
:space_after_headers => true,
:fenced_code_blocks => true,
:tables => true,
:strikethrough => true,
:smart => true,
:hard_wrap => true,
:safelink => true,
:no_intraemphasis => true,
}
Redcarpet::Markdown.new(renderer, options).render(text)
end
def html_template(html, title:, css: '')
require 'ostruct'
require 'erb'
require 'pathname'
templates = Pathname(__dir__+'/templates')
current_page = OpenStruct.new(data: OpenStruct.new(title: title))
page_classes = OpenStruct.new
css = templates.join('application.css').read + css.to_s
ERB.new(templates.join('layout.erb').read).result(binding)
end
# @returns some fake ruby code containing the docs and the code coming from runtime.js
def runtime_docs(runtime_js_code)
lines = runtime_js_code.split("\n")
functions = []
in_comment = false
scan_block_comment = -> lines {
next if lines.empty?
next unless lines.first.to_s.strip.start_with?('/*')
lines.shift # skip the start
comment = []
comment << lines.shift.strip.gsub(/^\*+ /, '') until lines.first.strip.end_with? '*/'
lines.shift # skip the end
comment
}
scan_line_comment = -> lines {
next if lines.empty?
next unless lines.first.strip.start_with? '//'
comment = []
comment << lines.shift.strip.gsub(%r{^// ?}, '') while lines.first.strip.start_with? '//'
comment
}
skip_blank_lines = -> lines {
next if lines.empty?
lines.shift if lines.first.strip.empty?
}
scan_function_body = -> lines {
next if lines.empty?
next unless lines.first =~ /\bfunction[^}]*$/
indentation = lines.first.scan(/^(\s*)\S/).flatten.first
body = [lines.shift]
until lines.first =~ /^#{indentation}\};?/
body << lines.shift
end
body << lines.shift # the closure
body
}
scan_line = -> lines {
line = lines.shift
[line]
}
until lines.empty?
current ||= {}
# we're done with this
if current[:body]
functions << current
current = nil
next
end
current[:comment] = scan_block_comment[lines] || scan_line_comment[lines]
if current[:comment]
current[:body] = scan_function_body[lines] || scan_line[lines]
else
lines.shift
end
end
functions
end
def runtime_markdown(data)
extract_function_name = -> first_line {
function_args = ' *\(([^\)]*)\)\{'
first_line = first_line.strip.chomp(';').chomp("{").sub(/^var /, '').strip
case first_line
when /\W((?:Opal\.)?\w+) *= *function#{function_args}/ then "function: `#{$1}(#{$2}})`"
when /function *(\w+)#{function_args}/ then "function: `#{$1}(#{$2})`"
else "`#{first_line}`"
end
}
markdown = [
'# runtime.js'
]
data.each do |hash|
comment:, body: = **hash
markdown << "## #{extract_function_name[body.first]}"
markdown << ""
markdown += comment
markdown << ""
markdown << "```js"
lead_space = body.first.scan(/^( *)/).flatten.first
markdown += body.map{|line| line.gsub(/^#{lead_space}/, '') }
markdown << "```"
markdown << ""
markdown << ""
end
markdown
end
def runtime_ruby(data)
extract_function_name = -> line {
next unless line =~ /function/
name = line.scan(/Opal\.\w+|function +(\w+)/).flatten.first
args = line.scan(/function[^(]*\(([^(]*)\)/).flatten.first
return name, args
}
ruby = []
data.each do |hash|
comment:, body: = **hash
method_name, method_args = extract_function_name[body.first]
next if method_name.nil? or method_name =~ /^[A-Z]/
ruby += comment.map {|l| "# #{l.gsub("@returns", "@return")}"}
# ruby << "define_method #{method_name.inspect} do |*args|"
ruby << "def self.#{method_name}(*)"
ruby << "<<-JAVASCRIPT"
lead_space = body.first.scan(/^( *)/).flatten.first
ruby += body.map{|line| " "+line.gsub(/^#{lead_space}/, '') }
ruby << "JAVASCRIPT"
ruby << "end"
ruby << ""
ruby << ""
end
[
'module __JS__',
%Q{ # This module is just a placeholder for showing the documentation of the},
%Q{ # internal JavaScript runtime. The methods you'll find defined below},
%Q{ # are actually JavaScript functions attached to the `Opal` global object},
%Q{ module Opal},
] + ruby.map {|l| ' '+l} + [
%Q{ end},
%Q{end},
]
end
# FOR FUTURE REF:
#
# DOCCO/GROC
# path = 'opal/opal/corelib/runtime.js'
# contents = File.read path
# normalized_contents = contents.gsub(%r{^(\s)*(?m:/\*.*?\*/ *$)}) do |match|
# lines = match.strip.split("\n")
# leading = lines.first.scan(/^ */).first + '// '
# normalized = lines.map do |line|
# line.sub(%r{^ *(?:/\*+|\* +|\*/|(?=[\S]|$))}, leading)
# end.compact.join("\n")
# "\n"+normalized
# end
# File.write path, normalized_contents
# puts normalized_contents
# # sh "groc --root opal/opal/corelib --output #{base_dir} runtime.js"
# sh "docco --output #{base_dir} opal/opal/corelib/runtime.js"
# File.write path, contents
#
# DOXX
# command = "doxx --template #{doc_repo.join('doxx-templates/opal.jade')} "\
# "--source opal/corelib --target #{doc_base}/#{git}/#{name} "\
# "--title \"Opal runtime.js Documentation\" --readme opal/README.md"
# puts command; system command or $stderr.puts "Please install doxx with: npm install"
task default: [:api, :guides]