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



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

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



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/shoes/help.rb', line 164

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



150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/shoes/help.rb', line 150

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



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/shoes/help.rb', line 104

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



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/shoes/help.rb', line 56

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
      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
    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
# 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, '", 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



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/shoes/help.rb', line 185

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



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

def load_docs path
  return @docs if @docs
  str = Shoes.read_file(path)
  @search = Shoes::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
  @search.finish!
  @docs
end

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



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

def make_html(path, title, menu, &blk)
  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

#manual_search(terms) ⇒ Object



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

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


288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/shoes/help.rb', line 288

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:\/\//
    debug head
    visit head
  end
end

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



327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/shoes/help.rb', line 327

def open_methods(meth_s, terms = nil, meth_a = nil)
  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



317
318
319
320
321
322
323
324
325
# File 'lib/shoes/help.rb', line 317

def open_section(sect_s, terms = nil)
  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

#run_code(str) ⇒ Object



209
210
211
# File 'lib/shoes/help.rb', line 209

def run_code str
  eval(str, TOPLEVEL_BINDING)
end

#sample_pageObject



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

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

#show_searchObject



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

def show_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_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
    end
    @results = stack
  end
  app.slot.scroll_top = 0
end

#wiki_tokens(str, intro = false) ⇒ Object



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

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