Module: Shoes::Manual

Defined in:
lib/shoes/help.rb

Constant Summary collapse

PARA_RE =
/\s*?(\{{3}(?:.+?)\}{3})|\n\n/m
CODE_RE =
/\{{3}(?:\s*\#![^\n]+)?(.+?)\}{3}/m
IMAGE_RE =
/\!(\{([^}\n]+)\})?([^!\n]+\.\w+)\!/
CODE_STYLE =
{:size => 9, :margin => 12}
INTRO_STYLE =
{:size => 16, :margin_bottom => 20, :stroke => "#000"}
SUB_STYLE =
{:stroke => "#CCC", :margin_top => 10}
IMAGE_STYLE =
{:margin => 8, :margin_left => 100}
COLON =
": "

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.pathObject



16
17
18
19
20
21
22
# File 'lib/shoes/help.rb', line 16

def self.path
  path = "#{DIR}/static/manual-#{Shoes.language}.txt"
  unless File.exists? path
    path = "#{DIR}/static/manual-en.txt"
  end
  path
end

Instance Method Details



330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/shoes/help.rb', line 330

def add_next_link(docn, optn)
  opt1, optn = @docs[docn][1], optn + 1
  if opt1['sections'][optn]
    @doc.para "Next: ",
      link(opt1['sections'][optn][1]['title']) { open_methods(opt1['sections'][optn][0]) },
      :align => "right"
  elsif @docs[docn + 1]
    @doc.para "Next: ",
      link(@docs[docn + 1][0]) { open_section(@docs[docn + 1][0].gsub(/\W/, '')) },
      :align => "right"
  end
end

#class_treeObject



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/shoes/help.rb', line 173

def class_tree
  tree = {}
  Shoes.constants.each do |c|
    k = Shoes.const_get(c)
    next unless k.respond_to? :superclass
    
    c = "Shoes::#{c}"
    if k.superclass == Object
      tree[c] ||= []
    else
      k.ancestors[1..-1].each do |sk|
        break if [Object, Kernel].include? sk
        next unless sk.is_a? Class #don't show mixins
        (tree[sk.name] ||= []) << c
        c = sk.name
      end
    end
  end
  tree
end

#color_pageObject



159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/shoes/help.rb', line 159

def color_page
  color_names = (Shoes::COLORS.keys*"\n").split("\n").sort
  flow do
    color_names.each do |color|
      flow :width => 0.33 do
        c = send(color)
        background c
        para strong(color), "\n", c, :stroke => (c.dark? ? white : black),
          :margin => 4, :align => 'center'
      end
    end
  end
end

#dewikify(str, intro = false) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/shoes/help.rb', line 111

def dewikify(str, intro = false)
  proc do
    wiki_tokens(str, intro) do |sym, text|
      case sym when :intro
        dewikify_p :para, text, INTRO_STYLE
      when :code
        dewikify_code(text)
      when :colors
        color_page
      when :index
        index_page
      when :samples
        sample_page
      when :list
        text.each { |t| stack(:margin_left => 30) { 
          fill black; oval -10, 7, 6; dewikify_p :para, t } }
      else
        dewikify_p sym, text
      end
    end
  end
end

#dewikify_code(str) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/shoes/help.rb', line 57

def dewikify_code(str)
  str = str.gsub(/\A\n+/, '').chomp
  stack :margin_bottom => 12 do 
    background rgb(210, 210, 210), :curve => 4
    para code(str), CODE_STYLE 
    stack :top => 0, :right => 2, :width => 70 do
      rnts = stack do
        background "#8A7", :margin => [0, 2, 0, 2], :curve => 4 
        para link("Run this", :stroke => "#eee", :underline => "none") { run_code(str) },
          :margin => 4, :align => 'center', :weight => 'bold', :size => 9
      end
      rnts.hide if str.match(/Shoes\.app|alert|confirm|ask|info|warn|debug/).nil?
      stack do
        background "#8A7", :margin => [0, 2, 0, 2], :curve => 4 
        para link("Copy this", :stroke => "#eee", :underline => "none") { self.clipboard = str },
          :margin => 4, :align => 'center', :weight => 'bold', :size => 9
      end
    end
  end
end

#dewikify_hi(str, terms, intro = false) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/shoes/help.rb', line 24

def dewikify_hi(str, terms, intro = false)
  if terms
    code = []
    str = str.
      gsub(CODE_RE) { |x| code << x; "CODE#[#{code.length-1}]" }.
      gsub(/#{Regexp::quote(terms)}/i, '@\0@').
      gsub(/CODE#\[(\d+)\]/) { code[$1.to_i] }
  end
  dewikify(str, intro)
end

#dewikify_p(ele, str, *args) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/shoes/help.rb', line 35

def dewikify_p(ele, str, *args)
  str = str.gsub(/\n+\s*/, " ").dump.
    gsub(/`(.+?)`/m, '", code("\1"), "').gsub(/\[\[BR\]\]/i, "\n").
    gsub(/\^(.+?)\^/m, '\1').
    gsub(/\[\[(.+\.)?@(.+?)@\]\]/m, '", link(strong("\2".empty? ? "\1" : "\2" , :fill => yellow)) { open_link("\1\2") }, "').
    gsub(/@(.+?)@/m, '", strong("\1", :fill => yellow), "').
    gsub(/'''(.+?)'''/m, '", strong("\1"), "').gsub(/''(.+?)''/m, '", em("\1"), "').
    gsub(/\[\[(\S+?)\]\]/m, '", link("\1".split(".", 2).last) { open_link("\1") }, "').
    gsub(/\[\[(\S+?) (.+?)\]\]/m, '", link("\2") { open_link("\1") }, "').
    gsub(IMAGE_RE, '", *args); stack(IMAGE_STYLE.merge({\2})) { image("#{DIR}/static/\3") }; #{ele}("')
  #debug str if str =~ /The list of special keys/
  a = str.split(', ", ", ')
  if a.size == 1
    eval("#{ele}(#{str}, *args)")
  else
    flow do
      a[0...-1].each{|s| eval("#{ele}(#{s}, ',', *args)")}
      eval("#{ele}(#{a[-1]}, *args)")
    end
  end
end

#index_pageObject



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/shoes/help.rb', line 194

def index_page
  tree = class_tree
  shown = []
  index_p = proc do |k, subs|
    unless shown.include? k
      stack :margin_left => 20 do
        flow do 
          para "", :font => case RUBY_PLATFORM
            when /mingw/; "MS UI Gothic"
            when /darwin/; "AppleGothic, Arial"
            else "Arial"
            end
          para k
        end
        subs.uniq.sort.each do |s|
          index_p[s, tree[s]]
        end if subs
      end
      shown << k
    end
  end
  tree.sort.each &index_p
end

#load_docs(path) ⇒ Object



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

def load_docs path
  return @docs if @docs
  #str = IO.read(path).force_encoding("ASCII-8BIT") # or UTF-8
  str = IO.read(path).force_encoding("UTF-8") # or UTF-8
  #$DEBUG = true
  #$DEBUGF = File.open("ft#{RUBY_VERSION[/\d/]}.log", 'w')
  @search = Search.new
  @sections, @methods, @mindex = {}, {}, {}
  @docs =
    (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, '')
        @search.add_document :uri => "T #{k2t}", :body => "#{k2}\n#{meth_plain}".downcase

        hsh = {'title' => k2, 'section' => k,
          'description' => meth[0],
          'methods' => (meth[1..-1]/2).map { |_k,_v|
            @search.add_document :uri => "M #{k}#{COLON}#{k2t}#{COLON}#{_k}", :body => "#{_k}\n#{_v}".downcase
            @mindex["#{k2t}.#{_k[/[\w\.]+/]}"] = [k2t, _k]
            [_k, _v]
        }}
        @methods[k2t] = hsh
        [k2t, hsh]
      end

      @search.add_document :uri => "S #{k}", :body => "#{k}\n#{sparts[0]}".downcase
      hsh = {'description' => sparts[0], 'sections' => sections, 
         'class' => "toc" + k.downcase.gsub(/\W+/, '')}
      @sections[k] = hsh
      [k, hsh]
    end
  $DEBUGF.flush if $DEBUG
  @search.finish!
  @docs
end

#make_html(path, title, menu, &blk) ⇒ Object



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

def make_html(path, title, menu, &blk)
=begin
  require 'hpricot'
  File.open(path, 'w') do |f|
    f << Hpricot do
      xhtml_transitional do
        head do
          meta :"http-equiv" => "Content-Type", "content" => "text/html; charset=utf-8"
          title "The 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! &blk
            div.sidebar do
              img :src => "static/shoes-icon.png"
              ul do
                li { a.prime "HELP", :href => "./" }
                menu.each do |m, sm|
                  li do
                    a m, :href => "#{m[/^\w+/]}.html"
                    if sm
                      ul.sub do
                        sm.each { |smm| li { a smm, :href => "#{smm}.html" } }
                      end
                    end
                  end
                end

              end
            end
          end
        end
      end
    end.to_html
  end
=end
end

#manual_search(terms) ⇒ Object



400
401
402
403
404
405
# File 'lib/shoes/help.rb', line 400

def manual_search(terms)
  terms += " " if terms.length == 1
  @search.find_all(terms).map do |title, count|
    title.split(" ", 2)
  end
end


314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/shoes/help.rb', line 314

def open_link(head)
  if head == "Search"
    show_search
  elsif @sections.has_key? head
    open_section(head)
  elsif @methods.has_key? head
    open_methods(head)
  elsif @mindex.has_key? head
    head, sub = @mindex[head]
    open_methods(head, nil, sub)
  elsif head =~ /^(http|https):\/\//
    debug head
    open_url head
  end
end

#open_methods(meth_s, terms = nil, meth_a = nil) ⇒ Object



376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/shoes/help.rb', line 376

def open_methods(meth_s, terms = nil, meth_a = nil)
  visited(meth_s)
  meth_h = @methods[meth_s]
  @title.replace meth_h['title']
  @doc.clear do
    unless meth_a
      instance_eval &dewikify_hi(meth_h['description'], terms, true)
    end
    meth_h['methods'].each do |mname, expl|
      if meth_a.nil? or meth_a == mname
        sig, val = mname.split("»", 2)
        stack(:margin_top => 8, :margin_bottom => 8) { 
          background "#333".."#666", :curve => 3, :angle => 90
          tagline sig, (span("»", val, :stroke => "#BBB") if val), :margin => 4 }
        instance_eval &dewikify_hi(expl, terms)
      end
    end
  end
  optn = nil
  docn = @docs.index { |_,h| optn = h['sections'].index { |x,| x == meth_s } } rescue nil
  add_next_link(docn, optn) if docn
  app.slot.scroll_top = 0
end

#open_section(sect_s, terms = nil) ⇒ Object



365
366
367
368
369
370
371
372
373
374
# File 'lib/shoes/help.rb', line 365

def open_section(sect_s, terms = nil)
  visited(sect_s)
  sect_h = @sections[sect_s]
  sect_cls = sect_h['class']
  @toc.each { |k,v| v.send(k == sect_cls ? :show : :hide) }
  @title.replace sect_s
  @doc.clear(&dewikify_hi(sect_h['description'], terms, true)) 
  add_next_link(@docs.index { |x,| x == sect_s }, -1) rescue nil
  app.slot.scroll_top = 0
end

#open_url(url) ⇒ Object



304
305
306
307
308
309
310
311
312
# File 'lib/shoes/help.rb', line 304

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

#run_code(str) ⇒ Object



218
219
220
221
# File 'lib/shoes/help.rb', line 218

def run_code str
#    eval(str, TOPLEVEL_BINDING)
  eval(str, binding) # for alert, confirm, ask in Built_in section examples
end

#sample_pageObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/shoes/help.rb', line 134

def sample_page
  folder = File.join DIR, 'samples'
  h = {}
  ['simple', 'good', 'expert'].each do |d|
    Dir.glob(File.join folder, d, "*").each do |file|
      if File.extname(file) == '.rb'
        key = File.basename(file)
        h[d] ? h[d].push(file) : h[d] = [file]
      end
    end
  end
  stack do
    h.each do |k, v|
      subtitle k
      flow do
        v.sort.each do |file|
          para link(File.basename(file, '.rb')) {
            Dir.chdir(folder){eval IO.read(file).force_encoding("UTF-8"), TOPLEVEL_BINDING}
          }
        end
      end
    end
  end
end

#show_searchObject



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

def show_search
  visited("Search")
  @toc.each { |k,v| v.hide }
  @title.replace "Search"
  @doc.clear do
    dewikify_p :para, "Try method names (like `button` or `arrow`) or topics (like `slots`)", :align => 'center'
    flow :margin_left => 60 do
      @edit_search = edit_line :width => -60 do |terms|
        @results.clear do
          termd = terms.text.downcase
          #found = termd.empty? ? [] : manual_search(termd)
          found = (termd.empty? or termd[0] == 'z' or termd[0] == 'y') ? [] : manual_search(termd)
          para "#{found.length} matches", :align => "center", :margin_bottom => 0
          found.each do |typ, head|
            flow :margin => 4 do
              case typ
              when "S"
                background "#333", :curve => 4
                caption strong(link(head, :stroke => white) { open_section(head, terms.text) })
                para "Section header", Shoes::Manual::SUB_STYLE
              when "T"
                background "#777", :curve => 4
                caption strong(link(head, :stroke => "#EEE") { open_methods(head, terms.text) })
                hsh = @methods[head]
                para "Sub-section under #{hsh['section']} (#{hsh['methods'].length} methods)", Shoes::Manual::SUB_STYLE
              when "M"
                background "#CCC", :curve => 4
                sect, subhead, head = head.split(Shoes::Manual::COLON, 3)
                para strong(sect, Shoes::Manual::COLON, subhead, Shoes::Manual::COLON, link(head) { open_methods(subhead, terms.text, head) })
              end
            end
          end
        end
      end
      timer(0) { @edit_search.focus }
    end
    @results = stack
  end
  app.slot.scroll_top = 0
end

#visit_backObject



348
349
350
351
352
353
354
355
# File 'lib/shoes/help.rb', line 348

def visit_back
  if 1 < @visited[:back].size
     @visited[:clicked] = true
     @visited[:forward] << @visited[:back].pop
     open_link(@visited[:back].last)
  end
  @visited[:clicked] = false
end

#visit_forwardObject



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

def visit_forward
   unless @visited[:forward].empty?
      @visited[:clicked] = true
      open_link(@visited[:forward].pop)
   end
   @visited[:clicked] = false
end

#visited(page) ⇒ Object



343
344
345
346
# File 'lib/shoes/help.rb', line 343

def visited(page)
  @visited[:forward] = [] unless @visited[:clicked]
  @visited[:back] << page unless @visited[:back].last.eql?(page)
end

#wiki_tokens(str, intro = false) ⇒ Object



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

def wiki_tokens(str, intro = false)
  paras = str.split(PARA_RE).reject { |x| x.empty? }
  if intro
    yield :intro, paras.shift
  end
  paras.map do |ps|
    if ps =~ CODE_RE
      yield :code, $1
    else
      case ps
      when /\A\{COLORS\}/
        yield :colors, nil
      when /\A\{INDEX\}/
        yield :index, nil
      when /\A\{SAMPLES\}/
        yield :samples, nil
      when /\A \* (.+)/m
        yield :list, $1.split(/^ \* /)
      when /\A==== (.+) ====/
        yield :caption, $1
      when /\A=== (.+) ===/
        yield :tagline, $1
      when /\A== (.+) ==/
        yield :subtitle, $1
      when /\A= (.+) =/
        yield :title, $1
      else
        yield :para, ps
      end
    end
  end
end