Class: Manual

Inherits:
Shoes show all
Includes:
HH::Markup, Hpricot
Defined in:
lib/shoes/help.rb

Constant Summary collapse

IMAGE_RE =
/\!(\{([^}\n]+)\})?([^!\n]+\.\w+)\!/
CODE_RE =
/\{{3}(?:\s*\#![^\n]+)?(.+?)\}{3}/m
NL =
"\n"
LANG =
DOCS =
load_docs($lang =~ /\.txt$/ ? $lang : File.join(DIR, "../static/manual-#{LANG}.txt"))
PNUMS =
mk_page_numbers DOCS
PEND =
PNUMS.length
COLORS =
Shoes::COLORS
VERSION =
SHOES_VERSION

Constants included from HH::Markup

HH::Markup::TOKENIZER

Constants inherited from Shoes

Shoes::APPS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HH::Markup

#highlight

Methods inherited from Shoes

APPS, app, display, url

Class Method Details

.load_docs(path) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/shoes/help.rb', line 211

def self.load_docs path
  str = IO.read(path).force_encoding("UTF-8")
  (str.split(/^= (.+?) =/)[1..-1]/2).map do |k, v|
    sparts = v.split(/^== (.+?) ==/)
    sections = (sparts[1..-1]/2).map do |k2, v2|
      meth = v2.split(/^=== (.+?) ===/)
      k2t = k2[/^(?:The )?([\-\w]+)/, 1]
      meth_plain = meth[0].gsub(IMAGE_RE, '')
      h = {title: k2, section: k, description: meth[0], methods: (meth[1..-1]/2)}
      [k2t, h]
    end
    h = {description: sparts[0], sections: sections, class: "toc" + k.downcase.gsub(/\W+/, '')}
    [k, h]
  end
end

.mk_page_numbers(docs) ⇒ Object



227
228
229
230
231
232
233
234
235
236
# File 'lib/shoes/help.rb', line 227

def self.mk_page_numbers docs
  pnum = []
  docs.length.times do |i|
    pnum << [i, nil]
    docs[i][1][:sections].length.times do |j|
      pnum << [i, j]
    end
  end
  pnum
end

Instance Method Details

#color_pageObject



168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/shoes/help.rb', line 168

def color_page
  COLORS.each do |color, v|
    r, g, b = v
    c = v.dark? ? white : black
    flow width: 0.33, height: 55, margin_top: 5 do
      background send(color)
      para fg(strong(color), c), align: 'center'
      para fg("rgb(#{r}, #{g}, #{b})", c), align: 'center'
    end
  end
  para NL
end

#decoration(datas, re, &blk) ⇒ Object



465
466
467
468
469
470
471
472
473
474
475
476
477
478
# File 'lib/shoes/help.rb', line 465

def decoration datas, re, &blk
  datas.map do |data|
    if data.is_a? String
      txts = [data]
      data.match re do |md|
        n = data.index md[0]
        txts = [data[0...n], blk[md[1]], decoration([data[n+md[0].length..-1]], re, &blk)]
      end
      txts
    else
      data
    end
  end.flatten  
end

#find_pnum(page) ⇒ Object



203
204
205
206
207
208
209
# File 'lib/shoes/help.rb', line 203

def find_pnum page
  return 999 if page == 'Search'
  TOC_LIST.each_with_index do |e, i|
    title, section = e
    return i if title == page
  end
end

#get_title_and_desc(pnum) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/shoes/help.rb', line 21

def get_title_and_desc pnum
  chapter, section = PNUMS[pnum]
  return nil unless chapter
  if section
    [pnum, DOCS[chapter][1][:sections][section][1][:title], 
    DOCS[chapter][1][:sections][section][1][:description], 
    DOCS[chapter][1][:sections][section][1][:methods]]
  else
    [pnum, DOCS[chapter][0], DOCS[chapter][1][:description], []]
  end
end

#html_manualObject



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/shoes/help.rb', line 238

def html_manual
  dir = ask_save_folder
  return unless dir
  FileUtils.mkdir_p File.join(dir, 'static')
  FileUtils.mkdir_p File.join(dir, 'snapshots')
  %w[rshoes-icon.png purple_shoes-icon.png shoes-manual-apps.png manual.css code_highlighter.js code_highlighter_ruby.js].
    each{|x| FileUtils.cp "#{DIR}/../static/#{x}", "#{dir}/static"}
  Dir[File.join DIR, '../static/man-*.png'].each{|x| FileUtils.cp x, "#{dir}/static"}
  Dir[File.join DIR, '../snapshots/sample*.png'].each{|x| FileUtils.cp x, "#{dir}/snapshots"}

  TOC_LIST.length.times do |n|
    num, title, desc, methods = get_title_and_desc n
    open File.join(dir, "#{TOC_LIST[n][0]}.html"), 'w' do |f|
      f.puts mk_html(title, desc, methods, TOC_LIST[n+1], get_title_and_desc(n+1), mk_sidebar_list(num))
    end
  end
end

#index(pnum = 0) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/shoes/help.rb', line 11

def index pnum = 0
  font LANG == 'ja' ? 'MS UI Gothic' : 'Arial'
  #style Link, underline: false, weight: 'bold'
  #style LinkHover, stroke: '#06E'
  self.scroll_top = 0
  TOC.clear; TOC_LIST.clear
  table_of_contents.each{|toc| TOC << toc}
  pnum == '999' ? mk_search_page : manual( *get_title_and_desc(pnum.to_i) )
end

#manual(pnum, docs_title, docs_description, docs_methods) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/shoes/help.rb', line 43

def manual pnum, docs_title, docs_description, docs_methods
  flow do
    show_header docs_title
    show_toc
    paras = mk_paras docs_description
    flow width: 0.8, margin: [10, 0, 20, 0] do
      show_page paras, true
      show_methods docs_methods
      para link('top'){visit "/manual/0"}, "  ",
        link('prev'){visit "/manual/#{(pnum-1)%PEND}"}, "  ", 
        link('next'){visit "/manual/#{(pnum+1)%PEND}"}, "  ", 
        link('end'){visit "/manual/#{PEND-1}"}
    end
  end
end

#manual_p(str) ⇒ Object



364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# File 'lib/shoes/help.rb', line 364

def manual_p str
  str.gsub(/\n+\s*/, " ").
    gsub(/&/, '&amp;').gsub(/>/, '&gt;').gsub(/>/, '&lt;').gsub(/"/, '&quot;').
    gsub(/`(.+?)`/m, '<code>\1</code>').gsub(/\[\[BR\]\]/i, "<br />\n").
    gsub(/\^(.+?)\^/m, '\1').
    gsub(/'''(.+?)'''/m, '<strong>\1</strong>').gsub(/''(.+?)''/m, '<em>\1</em>').
    gsub(/\[\[((http|https):\/\/\S+?)\]\]/m, '<a href="\1" target="_new">\1</a>').
    gsub(/\[\[((http|https):\/\/\S+?) (.+?)\]\]/m, '<a href="\1" target="_new">\3</a>').
    gsub(/\[\[(\S+?)\]\]/m) do
      ms, mn = $1.split(".", 2)
      if mn
        '<a href="' + ms + '.html#' + mn + '">' + mn + '</a>'
      else
        '<a href="' + ms + '.html">' + ms + '</a>'
      end
    end.
    gsub(/\[\[(\S+?) (.+?)\]\]/m, '<a href="\1.html">\2</a>').
    gsub(/\!(\{[^}\n]+\})?([^!\n]+\.\w+)\!/) do
      '<img src="' + "static/#$2" + '" />'
    end
end

#marker(txt, term) ⇒ Object



450
451
452
453
454
455
456
457
# File 'lib/shoes/help.rb', line 450

def marker txt, term
  if term && txt
    tmp = txt.split(term).map{|s| [s, bg(term, yellow)]}.flatten
    txt =~ /#{term}$/ ? tmp : tmp[0...-1]
  else
    [txt]
  end
end

#mk_deco(datas) ⇒ Object



459
460
461
462
463
# File 'lib/shoes/help.rb', line 459

def mk_deco datas
  datas = decoration(datas, /`(.+?)`/m){|s| fg code(s), rgb(255, 30, 0)}
  datas = decoration(datas, /'''(.+?)'''/m){|s| strong s}
  decoration(datas, /''(.+?)''/m){|s| em s}
end

#mk_executable(code) ⇒ Object



160
161
162
163
164
165
166
# File 'lib/shoes/help.rb', line 160

def mk_executable code
  if code =~ /\# Not yet available/
    "Shoes.app{para 'Sorry, not yet available...'}"
  else
    code
  end
end

#mk_html(title, desc, methods, next_file, next_title, menu) ⇒ Object



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
# File 'lib/shoes/help.rb', line 256

def mk_html title, desc, methods, next_file, next_title, menu
  man = self
  Hpricot do
    xhtml_transitional do
      head do
        meta :"http-equiv" => "Content-Type", "content" => "text/html; charset=utf-8"
        title "The Purple Shoes Manual // #{title}"
        script type: "text/javascript", src: "static/code_highlighter.js"
        script type: "text/javascript", src: "static/code_highlighter_ruby.js"
        style type: "text/css" do
          text "@import 'static/manual.css';"
        end
      end
      body do
        div.main! do
          div.manual! do
            h2 "The Purple Shoes Manual #{VERSION}"
            h1 title

            paras = man.mk_paras desc
            div.intro{text man.manual_p(paras.shift)}

            html_paragraph = proc do 
              paras.each do |str|
                if str =~ CODE_RE
                  pre{code.rb $1.gsub(/^\s*?\n/, '')}
                else
                  cmd, str = case str
                  when /\A==== (.+) ====/; [:h4, $1]
                  when /\A=== (.+) ===/; [:h3, $1]
                  when /\A== (.+) ==/; [:h2, $1]
                  when /\A= (.+) =/; [:h1, $1]
                  when /\A\{COLORS\}/
                    COLORS.each do |color, v|
                      f = v.dark? ? "white" : "black"
                      div.color(style: "background: #{color}; color: #{f}"){h3 color.to_s; p("rgb(%d, %d, %d)" % v)}
                    end
                  when /\A\{SAMPLES\}/
                    man.mk_sample_names.each do |name|
                      name = name[0...-3]
                      div.sample do
                        h3 name
                        text '<a href="snapshots/%s.png"><img src="snapshots/%s.png" alt="%s" border=0 width=50 height=50></a>' % [name, name, name]
                      end
                    end
                  when /\A \* (.+)/m
                    ul{$1.split(/^ \* /).each{|x| li{self << man.manual_p(x)}}}
                  else
                    [:p, str]
                  end
                  send(cmd){self << man.manual_p(str)} if cmd.is_a?(Symbol)
                end
              end
            end

            html_paragraph.call

            methods.each do |m, d|
              n = m.index("\u00BB")
              n ? (sig, val = m[0...n-1], m[n-1..-1]) : (sig, val = m, nil)
              aname = sig[/^[^(=]+=?/].gsub(/\s/, '').downcase
              a name: aname
              div.divmethod do
                a sig, href: "##{aname}"
                text val if val
              end
              div.desc do
                paras = man.mk_paras d
                html_paragraph.call
              end
            end

            p.next{text "Next: "; a next_title[1], href: "#{next_file[0]}.html"} if next_title
          end
          div.sidebar do
            img src: "static/purple_shoes-icon.png"
            ul do
              li{a.prime "HELP", href: "./"}
              menu.each do |m|
                li do
                  unless m.is_a?(Array)
                    a m, href: "#{m}.html"
                  else
                    ul.sub do
                      m.each do |sm|
                        li{a sm, href: "#{sm}.html"}
                      end
                    end
                  end
                end
              end
            end
          end
        end
      end
    end
  end.to_html
end


149
150
151
152
153
154
# File 'lib/shoes/help.rb', line 149

def mk_links txts, term = nil
  txts.map{|txt| txt.gsub(IMAGE_RE, '')}.
    map{|txt| txt =~ /\[\[(\S+?)\]\]/m ? (t = $1.split('.'); link(ins *marker(t.last, term)){visit "/manual/#{find_pnum t.first}"}) : txt}.
    map{|txt| txt =~ /\[\[(\S+?) (.+?)\]\]/m ? (url = $1; link(ins *marker($2, term)){visit url =~ /^http/ ? url : "/manual/#{find_pnum url}"}) : 
    (txt.is_a?(String) ? marker(txt, term) : txt)}
end

#mk_paras(str) ⇒ Object



156
157
158
# File 'lib/shoes/help.rb', line 156

def mk_paras str
  str.split("\n\n") - ['']
end

#mk_sample_namesObject



192
193
194
195
196
197
198
199
200
201
# File 'lib/shoes/help.rb', line 192

def mk_sample_names
  Dir[File.join(DIR, '../samples/sample*.rb')].map do |file|
    orig_name = File.basename file
    dummy_name = orig_name.sub(/sample(.*)\.rb/){
      first, second = $1.split('-')
      "%02d%s%s" % [first.to_i, ('-' if second), second]
    }
    [dummy_name, orig_name]
  end.sort.map &:last
end

#mk_search_pageObject



386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# File 'lib/shoes/help.rb', line 386

def mk_search_page
  flow do
    show_header 'Search'
    show_toc
    pnum, docs_title, docs_description, docs_methods = get_title_and_desc(25)
    paras = mk_paras docs_description

    flow width: 0.8, margin: [10, 0, 20, 0] do
      el = edit_line width: 300
      button 'search' do
        term = el.text.strip
        unless term.empty?
          descs, methods = search term
          @f.clear{show_search_result term, descs, methods}
          aflush
        end
      end
      stack(height: 20){}
      @f = flow{}
    end
  end
end

#mk_sidebar_list(num) ⇒ Object



355
356
357
358
359
360
361
362
# File 'lib/shoes/help.rb', line 355

def mk_sidebar_list num
  toc = []
  [0..3, 4..9, 10..16, 17..32, 33..37].each do |r|
    toc.push TOC_LIST[r.first][0]
    toc.push(TOC_LIST[r.first+1..r.last].to_a.map &:first) if r.include?(num)
  end
  toc
end

#sample_pageObject



181
182
183
184
185
186
187
188
189
190
# File 'lib/shoes/help.rb', line 181

def sample_page
  mk_sample_names.each do |file|
    stack width: 80 do
      inscription file[0...-3]
      img = image File.join(DIR, "../snapshots/#{file[0..-3]}png"), width: 50, height: 50
      img.click{Dir.chdir(File.join DIR, '../samples'){instance_eval(IO.read(file),file,0)}}
      para NL
    end
  end
end

#search(term) ⇒ Object



409
410
411
412
413
414
415
416
417
418
419
420
421
# File 'lib/shoes/help.rb', line 409

def search term
  descs, methods = [], []
  PNUMS.each_with_index do |(chapter, section), pnum|
    pnum, docs_title, docs_description, docs_methods = get_title_and_desc(pnum)
    paras = mk_paras(docs_description)
    descs << [chapter, section, docs_title, paras] if paras.map{|txt| txt.gsub(CODE_RE, '').gsub(IMAGE_RE, '')}.join(' ').index(term)
    docs_methods.each do |docs_method|
      m, d = docs_method
      methods << [chapter, section, docs_title, docs_method] if m.index(term) or d.gsub(CODE_RE, '').gsub(IMAGE_RE, '').index(term)
    end
  end
  return descs, methods
end

#show_header(docs_title) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/shoes/help.rb', line 59

def show_header docs_title
  background tr_color("#ddd")..white, angle: 90
  background black..magenta, height: 90
  para fg("The Purple Shoes Manual #{VERSION}", gray), left: 120, top: 10
  title fg(docs_title, white), left: 120, top: 30, font: 'Coolvetica'
  image File.join(DIR, '../static/purple_shoes-icon.png'), left: 5, top: -12, width: 110, height: 110, nocontrol: true
end

#show_methods(docs_methods, term = nil) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/shoes/help.rb', line 80

def show_methods docs_methods, term = nil
  docs_methods.each do |m, d|
    flow do
      background rgb(60, 60, 60), curve: 5
      n = m.index("\u00BB")
      if n
        para '  ', fg(strong(m[0...n]), white), fg(strong(m[n..-1]), rgb(160, 160, 160))
      else
        para '  ', fg(strong(m), white)
      end
    end
    para NL
    show_page mk_paras(d), false, term
  end
end

#show_page(paras, intro = false, term = nil) ⇒ Object



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
# File 'lib/shoes/help.rb', line 96

def show_page paras, intro = false, term = nil
  paras.each_with_index do |text, i|
    if text =~ CODE_RE
      text.gsub CODE_RE do |lines|
        lines = lines.split NL
        n = lines[1] =~ /\#\!ruby/ ? 2 : 1
        _code = lines[n...-1].join(NL+'  ')
        flow do
          background rgb(190, 190, 190), curve: 5
          inscription link(fg('Run this', magenta)){eval mk_executable(_code), TOPLEVEL_BINDING}, '  ', align: 'right'
          if _code.include? 'te-su-to'
            para fg(code('  ' + _code), maroon), NL, margin: [-10, 10, 0, 20]
          else
            para *highlight('  ' + _code, nil).map{|e| code e}, NL, margin: [-10, 10, 0, 20]
          end
        end
        para NL
      end
      next
    end
    
    if text =~ /\A \* (.+)/m
      $1.split(/^ \* /).each do |txt|
        image File.join(DIR, '../static/purple_shoes-icon.png'), width: 20, height: 20
        flow(width: 510){show_paragraph txt, intro, i, term}
        para NL
      end
    else
      show_paragraph text, intro, i, term
      para NL
    end
  end
end

#show_paragraph(txt, intro, i, term = nil) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/shoes/help.rb', line 130

def show_paragraph txt, intro, i, term = nil
  txt = txt.gsub("\n", ' ').gsub(/\^(.+?)\^/m, '\1').gsub(/\[\[BR\]\]/i, "\n")
  txts = txt.split(/(\[\[\S+?\]\])/m).map{|s| s.split(/(\[\[\S+? .+?\]\])/m)}.flatten
  case txts[0]
  when /\A==== (.+) ====/; caption *marker($1, term), size: 24
  when /\A=== (.+) ===/; tagline *marker($1, term), size: 12, weight: 'bold'
  when /\A== (.+) ==/; subtitle *marker($1, term)
  when /\A= (.+) =/; title *marker($1, term)
  when /\A\{COLORS\}/; flow{color_page}
  when /\A\{SAMPLES\}/; flow{sample_page}
  else
    para *mk_deco(mk_links(txts, term).flatten), NL, (intro and i.zero?) ? {size: 16} : ''
    txt.gsub IMAGE_RE do
      para NL
      image File.join(DIR, "../static/#{$3}"), eval("{#{$2 or "margin_left: 50"}}")
    end
  end
end

#show_search_result(term, descs, methods) ⇒ Object



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
# File 'lib/shoes/help.rb', line 423

def show_search_result term, descs, methods
  return subtitle 'Not Found' if descs.empty? and methods.empty?
  methods.each do |(chapter, section, docs_title, docs_method)|
    flow margin: [10, 10, 0, 5] do
      background rgb(200, 200, 200), curve: 5
      para "#{DOCS[chapter][0]}: #{docs_title.sub('The', '').split(' ').first}: ", 
        link(docs_method[0]){@f.clear{title docs_title; show_methods [docs_method], term}; aflush}, NL
    end
    stack(height: 2){}
  end
  descs.each do |(chapter, section, docs_title, paras)|
    flow margin_left: 10 do
      if section
        background gray, curve: 5
        tagline link(fg(docs_title, white)){@f.clear{title docs_title; show_page paras, true, term}; aflush}, width: 320
        inscription "Sub-Section under #{DOCS[chapter][0]}", stroke: lightgrey, width: 180
      else
        background black.push(0.8), curve: 5
        subtitle link(fg(docs_title, white)){@f.clear{title docs_title; show_page paras, true, term}; aflush}, width: 320
        inscription 'Section Header', stroke: lightgrey, width: 100
      end
    end
    stack(height: 2){}
  end
  para NL
end

#show_tocObject



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/shoes/help.rb', line 67

def show_toc
  stack(height: 120){}
  flow width: 0.2, margin_left: 10 do
    flow(margin_right: 20) do
      background black.push(0.7), curve: 5
      inscription "Not findng it?\n", 'Try ', link(fg 'Search', white){visit '/manual/999'}, '!', align: 'center', stroke: lightgrey
    end
    stack(height: 10){}
    para *TOC
    para link(fg 'to_html', green){html_manual}
  end
end

#table_of_contentsObject



33
34
35
36
37
38
39
40
41
# File 'lib/shoes/help.rb', line 33

def table_of_contents
  PNUMS.map.with_index do |e, pnum|
    chapter, section = e
    title = section ? DOCS[chapter][1][:sections][section][1][:title] : DOCS[chapter][0]
    title = title.sub('The', '').split(' ').first
    TOC_LIST << [title, section]
    section ? ['   ', link(title){visit "/manual/#{pnum}"}, "\n"] : [link(fg(title, magenta)){visit "/manual/#{pnum}"}, "\n"]
  end.flatten
end