-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlovedoctor.rb
executable file
·170 lines (142 loc) · 4.63 KB
/
lovedoctor.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
#!/usr/bin/env ruby
require 'bundler/setup'
require 'rubygems' if RUBY_VERSION < '1.9'
require 'pathname'
require 'asciidoctor'
require 'asciidoctor/extensions'
def byteify files
count = 0
res = ""
files.each do |(path, file)|
res += "var fileData#{count+=1} = [];\n"
file.unpack('C*').each_slice 10240 do |bytes|
res += "fileData#{count}.push.apply(fileData#{count}, #{bytes});\n"
end
res += "Module['FS_createDataFile']('#{path.dirname}', '#{path.basename}', fileData#{count}, true, true);"
end
res
end
def package name, files
<<EOF
(function () {
var canvas = document.getElementById('#{name}-canvas');
var Module = {
arguments: ['./'],
printErr: console.error.bind(console),
setStatus: function (e) {
if (!e && Module.didSyncFS && Module.remainingDependencies === 0)
Module.callMain(Module.arguments);
},
canvas: canvas,
didSyncFS: false,
totalDependencies: 0,
remainingDependencies: 0,
expectedDataFileDownloads: 1,
finishedDataFileDownloads: 0,
monitorRunDependencies: function(left) {
this.remainingDependencies = left;
this.totalDependencies = Math.max(this.totalDependencies, left);
},
preRun: [function(){ Module.ENV.SDL_EMSCRIPTEN_KEYBOARD_ELEMENT = "##{name}-canvas"; }]
};
canvas.module = Module;
function runWithFS () {
#{byteify(files)}
}
if (Module['calledRun']) {
runWithFS();
} else {
if (!Module['preRun']) Module['preRun'] = [];
Module["preRun"].push(runWithFS); // FS is not initialized yet, wait for it
}
window.mod = Module;
})();
EOF
end
class LoveWikiMacro < Asciidoctor::Extensions::InlineMacroProcessor
use_dsl
named :wiki
name_positional_attributes 'alt'
def process parent, target, attrs
text = Asciidoctor::Inline.new parent, :quoted, (attrs['alt'] or target), {type: :monospaced}
target = "https://love2d.org/wiki/#{target}"
(create_anchor parent, text.render, type: :link, target: target).render
end
end
Asciidoctor::Extensions.register do
block do
named :livecode
on_context :pass
parse_content_as :raw
name_positional_attributes 'name'
process do |parent, reader, attrs|
name = attrs.delete('name')
files = []
if attrs.has_key? 'multi' then
reader.read.split("###").each do |t|
spl = t.split("\n")
next if spl == []
files << [Pathname.new(spl[0]), spl.drop(1).join("\n")]
end
else
files << [Pathname.new("main.lua"), reader.read]
end
create_pass_block parent, %(<div class="livecode">
<canvas id="#{name}-canvas" data-module="#{name}"></canvas>
<script>#{package name, files}</script>
</div>), attrs
end
end
block_macro do
named :livecode
parse_content_as :raw
process do |parent, target, attrs|
name = attrs.delete('name') || target
target = Pathname.new File.join "book", "code", target
files = Pathname.glob(File.join(target, "**", "*")).collect { |file| [file.relative_path_from(target), file.binread] }
create_pass_block parent, %(<div class="livecode">
<canvas id="#{name}-canvas" data-module="#{name}"></canvas>
<script>#{package name, files}</script>
</div>), attrs
end
end
block_macro do
named :code_example
parse_content_as :raw
process do |parent, target, attrs|
code_dir = File.join(parent.document.base_dir, 'code', target)
exclude = (attrs.delete('exclude') || 'lib/*').split(',')
include = (attrs.delete('include') || '**/*.lua').split(',')
include.map! {|pat| Dir.glob(File.join(code_dir, pat)) }
exclude.map! {|pat| Dir.glob(File.join(code_dir, pat)) }
res = create_open_block parent, "", attrs, content_model: :compound
attrs['language'] ||= 'lua'
(include.inject(&:+) - exclude.inject(&:+)).each do |file|
block = create_listing_block res, File.read(file), attrs, subs: [:highlight, :callouts]
block.style = "source"
block.title = File.basename(file)
res << block
end
res
end
end
include_processor do
process do |doc, reader, target, attributes|
reader.push_include File.read(File.join(doc.base_dir, 'code', target)), target, target, 1, attributes
reader
end
def handles? target
target =~ %r(world[1-3]/.*/)
end
end
inline_macro LoveWikiMacro
end
if __FILE__ == $0
require 'asciidoctor/cli'
ARGV << "-T"
ARGV << "templates"
invoker = Asciidoctor::Cli::Invoker.new ARGV
GC.start
invoker.invoke!
exit invoker.code
end