Class: WR::WRBDoc

Inherits:
Object
  • Object
show all
Includes:
ModAutoloading
Defined in:
lib/wrb/documents/make_doc.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ModAutoloading

included

Constructor Details

#initialize(fname) ⇒ WRBDoc

Returns a new instance of WRBDoc.



251
252
253
254
# File 'lib/wrb/documents/make_doc.rb', line 251

def initialize(fname)
  @fname = fname
  @hsh = {}
end

Class Method Details

.wrb_show_doc(htm) ⇒ Object



497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
# File 'lib/wrb/documents/make_doc.rb', line 497

def self.wrb_show_doc(htm)
  pid = Registry.open(RC::HKCU,
    'Software\Microsoft\Windows\Shell\Associations\UrlAssociations\HTTP\UserChoice'){|r|
    r.read('Progid')
  }
  path = Registry.open(RC::HKCR, pid+'\shell\open\command'){|r| r.read}.match(/^\".+?\"/)[0]
  doc = WR_INSTALLED_DIR + "/wrb/documents/#{htm}"
  if RbConfig::CONFIG['arch'] =~ /cygwin/
    s = `cygpath -a #{path}`
    s.gsub!(/[ ()]/){'\\' + $&}
    d = `cygpath -aw #{doc}`
    exec s.chomp + ' "' + d.chomp + '"' + " &"
  else
    exec "start " + path + ' "' + doc.tr('/', '\\') + '"'
  end
end

Instance Method Details

#_get_methods(hsh, h, m) ⇒ Object



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
# File 'lib/wrb/documents/make_doc.rb', line 274

def _get_methods(hsh, h, m)
  r = ""
  h.keys.sort.each{|s|
    i = s.intern
    j = s.dup
    next unless m.index(i)
    d, c = h[j]
    jr = j.gsub(/([\[\]\=\?])/, '\\\\\1') # add escape charactor for regexp.
    if d
      if c.size==1 && c[0] =~ /\s*\#$/ # Don't output.
        next
      elsif c[0] =~ /#\s*#{jr}(.+)/
        r << "    <dt><code>#{j}#{d=$1}</code></dt>\n"
        c.shift
      else
        r << "    <dt><code>#{i}#{d}</code></dt>\n"
      end
      if a=hsh[:aliases]
        if ar=a[j]
          ar.each{|i|
            r << "    <dt><code>#{i}#{d}</code></dt>\n"
          }
        end
      end
      if c
        s = ""
        t = []
        c.each{|i|
          if i=~/#\s*\w+:/
            t << i.sub(/#/, '').strip
          else
            s << i.sub(/#/, '').strip
          end
        }
        if s.empty?
        elsif t.empty?
          r << "    <dd>#{s}</dd>\n"
        else
          r << "    <dd>#{s}\n    <table>\n"
          t.each{|i|
            nam, typ, dsc = i.split(":").map{|j| j.split(';')}.flatten
            r << "      <tr><td class=name1> #{nam} </td> <td class=type> #{typ} </td>" +
            "<td class=desc> #{dsc} </td></tr>\n"
          }
          r << "    </table>\n    </dd>\n"
        end
      end
    else
#          r << "    <dt>#{j}</dt>\n"
    end
  }
  r
end

#get_class_description(klass, hsh) ⇒ Object



262
263
264
265
266
267
268
269
270
271
272
# File 'lib/wrb/documents/make_doc.rb', line 262

def get_class_description(klass, hsh)
  si = get_sampleimage()
  s = hsh[:fullname]
  if (a=s.split('<'))[1]
    a[1].strip!
    s = "#{a[0]} &lt; <a href=\"#{a[1].downcase}.html\">#{a[1]}</a>"
  end
  c = hsh[:comments].map{|i| i.sub /^\s*# ?/, ''}.join
  "  <h3> #{s} <span class=gohome> <a href=\"index.html\">Home </a>#{si}</span></h3>\n" +
  (c.empty? ? "" : "  <p#{si.empty? ? '' : ' class=fixedh'}>#{c}</p>\n")
end

#get_defaultevents(klass, hsh) ⇒ Object



404
405
406
407
408
409
410
411
412
413
414
415
# File 'lib/wrb/documents/make_doc.rb', line 404

def get_defaultevents(klass, hsh)
  return if !(h=hsh[:defaultevents]) || h.empty?
  r =  "  <h4> Default events <span class=spl>( can be defined without using `events=' method )</h4>\n"
  if c=h.delete(:__COMMENTS__)
    r << "  <p>" + c.map{|i| i.sub(/#\s*/, '').strip}.join + "</p>\n"
  end
  r << "  <dl>\n"
  h.keys.sort.each{|k|
    r << "    <dt><code>#{h[k].sub(/#\s*/, '')}</code></dt>\n"
  }
  r << "  </dl>\n"
end

#get_headerObject



473
474
475
476
477
478
479
480
481
482
483
484
# File 'lib/wrb/documents/make_doc.rb', line 473

def get_header()
  h= @parsedata.modules["WR"][@cname]
  if @klass < WinControl
    cls = "Controls"
  elsif (@klass < CommonDialog rescue nil)
    cls = "Common dialogs"
  elsif @klass == Dialog || @klass==Form
  else
    cls = "Other resources"
  end
  [cls, h[:fullname].split('<')[0].strip, h[:comments][0].to_s.sub(/\s*#/, '').strip]
end

#get_instance_methods(klass, hsh) ⇒ Object

; dpp hsh



345
346
347
348
349
350
351
352
# File 'lib/wrb/documents/make_doc.rb', line 345

def get_instance_methods(klass, hsh) #; dpp hsh[:methods]
  return if !(h=hsh[:methods]) || h.empty?
  ancestor = hsh[:fullname].split('<')[1]
  ancestor = ancestor ? eval(ancestor) : Object
  m = klass.public_instance_methods
  r = _get_methods(hsh, h, m)
  r.empty? ? "" : "  <h4> Instance methods </h4>\n  <dl>\n#{r}  </dl>\n" 
end

#get_nested_class(klass, hsh) ⇒ Object



424
425
426
427
428
429
430
431
432
# File 'lib/wrb/documents/make_doc.rb', line 424

def get_nested_class(klass, hsh)
  h = hsh.select{|k,v| k.is_a?(String) && v[:type]=="class"}
  s = ''
  h.keys.each{|k| #; dpp k #hsh[k]
    s << get_nested_class_description(klass.module_eval(k), hsh[k])#; dpp hsh[k][:methods]
    s << get_instance_methods(klass.module_eval(k), hsh[k])
  }
  s
end

#get_nested_class_description(klass, hsh) ⇒ Object



417
418
419
420
421
422
# File 'lib/wrb/documents/make_doc.rb', line 417

def get_nested_class_description(klass, hsh)
  s = klass.to_s.sub('WR::', '')
  c = hsh[:comments].map{|i| i.strip.sub /^#\s*/, ''}.join
  "  <h3> #{s} </h3>\n" +
  (c.empty? ? "" : "  <p>#{c}</p>\n")
end

#get_predefinedevents(klass, hsh) ⇒ Object



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/wrb/documents/make_doc.rb', line 375

def get_predefinedevents(klass, hsh)
  return if !(h=hsh[:predefinedevents]) || h.empty?
  r =  "  <h4> Pre-defined events <span class=spl> ( can be added by `events=` method )</span></h4>\n"
  if c=h.delete(:__COMMENTS__)
    r << "  <p>" + c.map{|i| i.sub(/#\s*/, '').strip}.join + "</p>\n"
  end
  r << "  <table>\n"
  r << "<tr><th width=20%>Symbol</th><th width=20%>Notification code</th><th>Definition <span class=spl>( `???' is name of the control )</span></th></tr>\n"
  h.keys.sort.each{|k| #dpp k, h[k]
    if h[k].match(/\[(.+)\].*#(.+)/) #; dpp k, h[k]
      d = $1.split(',')
      f = $2.strip
    else
      d = h[k].sub(/\[(.+)\]/, '\1').split(',') #; dpp d
      if d[0]=~/WM_COMMAND/ && d[-1]=~/hi/
        f = "???_#{k.tr(':', '')}(idcmd, code, hwnd)"
      elsif d[0]=~/WM_NOTIFY/ && d[-2]=~/xs/
        c = "<span class=ralign>arg is a #{d[-1].strip}</span>"
        f = "???_#{k.tr(':', '')}(arg)#{c}"
      else
        f = "???_#{k.tr(':', '')}(*args)"
      end
    end
    d = d.size>=3 ? d[1].strip : d[0].strip
    r << "    <tr><td class=name1><code>#{k}</code></td><td class=type><code>#{d}</code></td><td class=desc><code>#{f}</code></td></code></tr>\n"
  }
  r << "  </table>\n"
end

#get_sampleimageObject



256
257
258
259
260
# File 'lib/wrb/documents/make_doc.rb', line 256

def get_sampleimage()
  simg = 'images/' + @bname + '.png'
  path = File.dirname(__FILE__) + '/' + simg
  File.exists?(path) ? "  <div class=imgright><img src=\"#{simg}\"></div>\n" : ''
end

#get_samplescript(klass, hsh) ⇒ Object



434
435
436
437
438
439
440
441
442
443
# File 'lib/wrb/documents/make_doc.rb', line 434

def get_samplescript(klass, hsh)
  dir = File.dirname(__FILE__)
  fname = dir + '/samples/sample_' + hsh[:fullname].split('<')[0].strip.downcase + '.rb'
  s = (open(fname){|f| f.read} rescue nil)
  if s
    s = Parse4Color.new(s).parse('')
    r =  "  <h4> Sample script </h4>\n"
    r << "  <pre class=cd>\n" + s + "  </pre>\n"
  end
end

#get_singleton_methods(klass, hsh) ⇒ Object



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/wrb/documents/make_doc.rb', line 328

def get_singleton_methods(klass, hsh)
  return if !(h=hsh[:singletons]) || h.empty?
  ancestor = hsh[:fullname].split('<')[1]
  ancestor = ancestor ? eval(ancestor) : Object
  m = klass.singleton_methods.map{|i| "self.#{i}".intern}
#      m << "self.new".intern
#      dpp h.keys
  if v=h.delete("self.new")#; dpp v
    s = v[1].map{|i| i.sub(/^\s*#/, '').strip}.join
    r = "    <dt><code>self.new#{v[0].strip}</code></dt>\n    <dd>#{s}</dd>\n"
  else
    r = ''
  end
  r << _get_methods(hsh, h, m)
  r.empty? ? "" : "  <h4> Singleton methods </h4>\n  <dl>\n#{r}  </dl>\n" 
end

#get_styles(klass, hsh) ⇒ Object



354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/wrb/documents/make_doc.rb', line 354

def get_styles(klass, hsh)
  return if (!h=hsh[:styles]) || h.empty?
  k = hsh[:fullname].split('<')[0].strip
  r =  "  <h4> #{k} styles <span class=spl>( both the symbols and the integers can be used )</span></h4>\n"
  if c=h.delete(:__COMMENTS__)
    r << "  <p>" + c.map{|i| i.sub(/#\s*/, '').strip}.join + "</p>\n"
  end
  r << "  <table>\n"
  r << "<tr><th>Symbol</th><th>Integer value</th><th>Symbol</th><th>Integer value</th></tr>\n"
  h.keys.sort.each_with_index{|k, i|
    if i%2==0
      r << "    <tr><td class=name1><code>#{k}</code></td><td class=type><code>#{h[k]}</code></td>"
    else
      r << "<td class=name1><code>#{k}</code></td><td class=type><code>#{h[k]}</code></td></tr>\n"
    end
  }
  r << "</tr>\n" if h.keys.size%2==1
  r << "  </table>\n"
end

#output(*r) ⇒ Object



445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
# File 'lib/wrb/documents/make_doc.rb', line 445

def output(*r)
  title = r.shift
  res = <<EEOOSS
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="en">
<head>
<meta content="text/html; charset=utf-8" http-equiv="content-type">
<title>#{title}</title>
<link href="wrb.css" type="text/css" rel="stylesheet">
<link rel="shortcut icon" href="favicon.png">
</head>
<body>
#{r.join}
</body>
</html>
EEOOSS
end

#parseObject



463
464
465
466
467
468
469
470
471
# File 'lib/wrb/documents/make_doc.rb', line 463

def parse()
  s = open(@fname){|f| f.read}
  @parsedata = ParseScript.new(s)
  @parsedata.parse
  @bname = File.basename(@fname, ".rb")
  @cname = @parsedata.modules['WR'].select{|k, v| k.is_a?(String) && k=~/#{@bname}/i}.keys[0]
  @klass = WR.module_eval(@cname)
  self
end

#to_htmlObject



486
487
488
489
490
491
492
493
494
495
# File 'lib/wrb/documents/make_doc.rb', line 486

def to_html()
  klass = @klass
  title = @cname
  hsh = @parsedata.modules["WR"][@cname]
  class_description = get_class_description(klass, hsh)
  output title, class_description, get_singleton_methods(klass, hsh),
         get_instance_methods(klass, hsh), get_styles(klass, hsh),
         get_predefinedevents(klass, hsh), get_defaultevents(klass, hsh), 
         get_nested_class(klass, hsh), get_samplescript(klass, hsh)
end