-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkd-deepr.rb
391 lines (352 loc) · 12.4 KB
/
kd-deepr.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
# https://github.com/Shoes3/shoes3/wiki
# http://www.w3schools.com/tags/tag_li.asp
# http://stackoverflow.com/questions/4900167/override-module-method-from-another-module
# cjc : This gets very deep have to parse just about everything in the file into
# new intro, sections, subsections. Very Shoes manual like. Nasty.
require("rouge")
require("kramdown")
require 'fileutils'
include FileUtils
def open_url(url)
if RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/
system("start #{url}")
elsif RbConfig::CONFIG['host_os'] =~ /darwin/
system("open #{url}")
elsif RbConfig::CONFIG['host_os'] =~ /linux|bsd/
system("xdg-open #{url}")
end
end
module Rouge
module Formatters
class ShoesFormatter < Formatter
tag 'shoes'
def initialize(options)
@inline_theme = options.fetch(:inline_theme, nil)
@inline_theme = Theme.find(@inline_theme).new if @inline_theme.is_a? String
#puts @inline_theme.render
end
def stream(tokens, &b)
tokens.each do |tok, val|
yield "\t#{@inline_theme.style_for(tok).rendered_rules.to_a.join(';')}\n"
yield "#{tok} #{val.inspect}\n"
end
end
end
end
end
module Kramdown
module Converter
class Deeplook < Base
##include ShoesRouge
def initialize(root, options)
@cfg = options[:cfg]
## options[:syntax_highlighter] = "rouge"
#puts options
@intro = []
@section = ''
@subsection = ''
@results = []
@level = 0
@muddled = '' # this is computed My-SubSection.md used for hash keys
# by shoes_render.
@rstack = []
puts "init deeplook"
super
end
DISPATCHER = Hash.new {|h,k| h[k] = "convert_#{k}"}
def convert(el)
send(DISPATCHER[el.type], el)
puts "AT THE END"
close_level
end
def convert_root(el)
results = []
el.children.each do |inner_el|
results << send(DISPATCHER[inner_el.type], inner_el)
end
results
end
def convert_blank(el)
end
def convert_text(el)
@results << %{para("#{el.value.gsub("\n", ' ')}", :margin_left => 0, :margin_right => 0)}
return nil
end
# @level is the current level to close to put @results into
# the cfg in the proper place to show_ebook can display them
def close_level
# debugging - create a dump file for level's content
if @level > 0
dfl = "dump/#{@level}-#{@muddled}.rbd"
mkdir_p('dump');
File.open(dfl,'w') do |fl|
fl.write @results
end
end
# TODO: beware hash key collisions
case @level
when 1
@cfg['have_nav'] = true
@cfg['nested'] = true
opening = @title #@cfg['book_title']
@cfg['toc']['root'] = opening
landing = {title: opening, code: @results}
@cfg['code_struct'] << landing
@cfg['link_hash'][opening] = landing
# gird thy loins - going to create a section
@cfg['sections'][opening] = { :title => opening,
:display_order => [], 'intro' => opening}
@cfg['toc']['section_order'] << opening
when 2
sect_nm = @muddled
@cfg['toc']['section_order'] << @section
landing = {title: sect_nm, code: @results}
@cfg['code_struct'] << landing
@cfg['link_hash'][sect_nm] = landing
@cfg['sections'][@section]['intro'] = sect_nm
#@cfg['sections'][@section][:display_order] = [sect_nm]
when 3
sect_nm = @section
ss_nm = @muddled
@cfg['sections'][sect_nm][:display_order] << ss_nm
landing = {title: ss_nm, code: @results}
@cfg['code_struct'] << landing
@cfg['link_hash'][ss_nm] = landing
when 0
puts "discarding content before first header"
end
@results = []
end
# builds the section, subsections in cgf['sections'] etc
# copies accumatlated code into cfg['code_landing'] and cfg['link_hash']
def convert_header(el)
hlevel = el.options[:level]
txt = el.options[:raw_text]
mdkey = txt.gsub(' ','-')
mdkey << '.md'
case hlevel
when 1 # intro - only one of these
#close_level txt
@muddled = mdkey
@level = 1
@title = txt
@cfg['toc']['section_order'] = [] # replace existing
@results = [%{para(strong("#{txt}"), :size => 22, :margin_left => 6, :margin_right => gutter)}]
when 2 # section
close_level
@level = 2
@muddled = mdkey
@section = txt
@cfg['sections'][@section] = { :title => txt, :display_order => [] }
#puts "level 2 #{txt} for #{@cfg['sections'][@section].inspect}"
@results = [%{para(strong("#{txt}"), :size => 18, :margin_left => 6, :margin_right => gutter)}]
when 3 # subsection - [methods if you're thinking like Shoes Manual]
close_level
@level = 3
@muddled = mdkey
@subsection = txt
#puts "level 3 #{@section} under #{@cfg['sections'][@section].inspect}"
#@cfg['sections'][@section][:display_order] << mdkey
@results = [%{para(strong("#{txt}"), :size => 14, :margin_left => 6, :margin_right => gutter)}]
when 4
@results << %{para(strong("#{txt}"), :size => 12, :margin_left => 6, :margin_right => gutter)}
when 5
@results << %{para(strong("#{txt}"), :size => 10, :margin_left => 6, :margin_right => gutter)}
when 6
@results << %{para(strong("#{txt}"), :size => 8, :margin_left => 6, :margin_right => gutter)}
else
puts "header default"
@results << %{para(strong("#{txt}"), :margin_left => 6, :margin_right => gutter)}
end
return nil
end
def save
@rstack.push @results
@results = []
end
def restore
prev = @rstack.pop
newc = @results
@results = prev + newc
end
def convert_p(el)
save
el.children.each do |inner_el|
@results << send(DISPATCHER[inner_el.type], inner_el)
end
@results << %[flow(:margin_left => 6, :margin_right => gutter) { #{@results.join(";")} }]
restore
return nil
end
def convert_ul(el)
save
# doesn't do much in Shoes. Maybe create a stack for the list items to follow?
el.children.each do |inner_el|
@results << send(DISPATCHER[inner_el.type], inner_el)
end
restore
return nil
end
# currently this simulates a ul list item only
def convert_li(el)
#puts "\nli begin: #{ @results}"
save
el.children.each do |inner_el|
@results << %[flow(:margin_left => 30) { fill black; oval -10, 10, 6; #{send(DISPATCHER[inner_el.type], inner_el)} }]
end
#puts "\nli @results: #{@results}"
restore
return nil
end
##alias :convert_ol :convert_ul
##alias :convert_dl :convert_ul
def convert_smart_quote(el)
save
t = case el.value
when :lsquo
"\u2018"
when :rsquo
"\u2019"
when :ldquo
"\u201C"
when :rdquo
"\u201D"
end
#puts "smartquote sub #{t}"
@results << %{para("#{t}", :margin_left => 0, :margin_right => 0)}
restore
end
def convert_a(el)
puts "convert a called #{el.inspect}"
save
el.children.each do |inner_el|
@results << inner_el.value if inner_el.type.eql?(:text)
##send(DISPATCHER[inner_el.type], inner_el)
end
@results << %[para(link("#{@results.join}") { open_url("#{el.attr['href']}") }, :margin_left => 0, :margin_right => 0)]
restore
end
# from are `back ticks` in markdown
def convert_codespan(el)
save
# need to escape some things in the string like "
str = el.value
str.gsub!(/\"/, '\"')
@results << %[para "#{str}", font: 'monospace', stroke: coral]
restore
return nil
end
def convert_codeblock (el)
# More crazy logic?
str = el.value
exe_str = nil
display_str = nil
if str[/Shoes\.app/]
#puts "code is excutable: #{el.value}"
exe_str = str
else
#puts "code can't be run: #{str}"
@results << %Q[render_copy(#{el.value.inspect})]
return nil
end
if @cfg['syntax_highlight']
# do the hightling of 'exe_str' save results in 'display_str'
#return highlight_codeblock el
end
#%[render_code(%{#{el.value}})]
@results << %Q[render_code(#{el.value.inspect}, #{display_str})]
return nil
end
# TODO: syntax highlight not working (no errors - just doesn't return anything)
def highlight_codeblock(el)
#puts highlight_code(el.value, el.attr['class'], :span)
#h = ::Kramdown::Converter.syntax_highlighter(@options[:syntax_highlighter])
#puts h.call(self, el.value, el.attr['class'], :span)
#puts syntax_highlighter(self, el.value, el.attr['class'], :span)
puts "SB #{el.inspect}"
nil # until it's ready for Shoes to eval it.
end
def convert_strong(el)
save
el.children.each do |inner_el|
@results << inner_el.value
end
t = results.size > 1 ? @results.join : @results[0]
restore
@results << %[para strong("#{t}")]
return nil
end
def convert_img(el)
save
url = el.attr['src']
ext = File.extname(url);
lcl = @cfg['images'][url]
if lcl
@results << %[image "#{@cfg['doc_home']}/.ebook/images/#{lcl}"]
else
puts "Not an image: #{url}"
end
restore
return nil
end
def convert_gfmlink(el)
save
str = el.attr['gfmlink']
#puts "gfmlink find: #{str}"
# defer to Run Time method to figure it where to go
@results << %[para(link("#{str}") { show_link("#{str}")})]
restore
return nil
end
def convert_typographic_sym(el)
sub = case el.value
when :hellip
"\u2026"
when :ndash # non breaking dash
"\u2013"
else
# The Dotted Cross! - gotta pick something for unknown
"\u2025"
end
#puts "typegrapic sub: #{sub}"
@results << %[para"#{sub}"]
return nil
end
# I think this means Shoes italic, not strong
def convert_em(el)
save
el.children.each do |inner_el|
@results << inner_el.value
end
t = results.size > 1 ? @results.join : @results[0]
@results << %[para "#{t}", :emphasis => "italic"]
restore
return nil
end
# begin TODO fix these in kd-render/kd-deepr
def convert_br(el)
puts "convert_br called"
end
def convert_blockquote(el)
puts "convert_blockquote called"
end
def convert_table(el)
puts "convert_table called"
end
def convert_ol(el)
puts "convert_ol called"
end
def convert_html_element(el)
end
def syntax_highlighter(converter, text, lang, type)
opts = converter.options[:syntax_highlighter_opts].dup
lexer = ::Rouge::Lexer.find_fancy(lang || opts[:default_lang], text)
return nil unless lexer
opts[:wrap] = false if type == :span
formatter = ::Rouge::Formatters::ShoesFormatter.new(opts)
formatter.format(lexer.lex(text))
end
#end TODO fix above
end
end
end