Class: Shomen::Yard::Generator

Inherits:
Generator
  • Object
show all
Defined in:
lib/shomen-yard/generator.rb

Overview

This adapter is used to convert YARD’s documentation extracted from a local store (‘.yardoc`) to Shomen’s pure-data format.

Constant Summary collapse

STORE =
'.yardoc'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Generator

Require YARD library.

Returns nothing.



20
21
22
23
24
25
26
# File 'lib/shomen-yard/generator.rb', line 20

def initialize(options)
  require 'yard'

  @store = STORE

  super(options)
end

Instance Attribute Details

#storeObject

The location YARD documentation cache. The default is ‘.yardoc`.

Returns String.



37
38
39
# File 'lib/shomen-yard/generator.rb', line 37

def store
  @store
end

#tableObject (readonly)

The hash object that is used to store the generated documentation.

Returns documentation table. [Hash]



32
33
34
# File 'lib/shomen-yard/generator.rb', line 32

def table
  @table
end

#use_cacheObject

Use pre-existant cache instead of regenerating documentation.



40
41
42
# File 'lib/shomen-yard/generator.rb', line 40

def use_cache
  @use_cache
end

Instance Method Details

#collect_filesObject (private)

Collect files given list of globs.

Returns Array of files.



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/shomen-yard/generator.rb', line 169

def collect_files
  globs = self.files
  globs = globs.map{ |glob| Dir[glob] }.flatten.uniq
  globs = globs.map do |glob|
    if File.directory?(glob)
      Dir[File.join(glob, '**/*')]
    else
      glob
    end
  end
  list = globs.flatten.uniq.compact
  list = list.reject{ |path| File.extname(path) == '.html' }
  list = list.select{ |path| File.file?(path) }
  list
end

#debug_msg(msg) ⇒ Object (private)

Output progress information if debugging is enabled



471
472
473
474
475
476
477
478
479
# File 'lib/shomen-yard/generator.rb', line 471

def debug_msg(msg)
  return unless $DEBUG
  case msg[-1,1]
    when '.' then tab = "= "
    when ':' then tab = "== "
    else          tab = "* "
  end
  $stderr.puts(tab + msg)
end

#filesObject

Files to be documented.

Returns Array of file paths.



52
53
54
55
56
57
58
59
60
# File 'lib/shomen-yard/generator.rb', line 52

def files
  @files ||= (
    list = []
    list.concat scripts
    list.concat documents
    list.concat(['lib', 'README*']) if list.empty?
    list
  )
end

#generateObject

Generate with YARD as backend processor.

Returns documentation table. [Hash]



65
66
67
68
# File 'lib/shomen-yard/generator.rb', line 65

def generate
  preconfigure unless use_cache?
  generate_table
end

#generate_class(yard_class) ⇒ Object (private)

Generate a class or module structure. Note that as to whether ‘methods` also contains the accessor methods listed in `accessors` is left to YARD to determine.

yard_class - YARD class documentation object.

Returns Hash of class data that has been placed in the table



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
# File 'lib/shomen-yard/generator.rb', line 200

def generate_class(yard_class)
  debug_msg(yard_class.path.to_s)

  meths = yard_class.meths(:included=>false, :inherited=>false)

  if yard_class.type == :class
    model = Model::Class.new
    model.superclass = yard_class.superclass ? yard_class.superclass.path : 'Object'
  else 
    model = Model::Module.new
  end

  model.path            = yard_class.path
  model.name            = yard_class.name.to_s
  model.namespace       = yard_class.namespace.path  #full_name.split('::')[0...-1].join('::'),
  model.comment         = yard_class.docstring.to_s
  model.format          = 'rdoc'  #TODO: how to determine? rdoc, markdown or plaintext ?
  model.constants       = yard_class.constants.map{ |x| x.path }  #TODO: complete_name(x.name, c.full_name) }
  model.includes        = yard_class.instance_mixins.map{ |x| x.path }
  model.extensions      = yard_class.class_mixins.map{ |x| x.path }
  model.modules         = yard_class.children.select{ |x| x.type == :module }.map{ |x| x.path }
                          #yard_class.modules.map{ |x| complete_name(x.name, c.full_name) }
  model.classes         = yard_class.children.select{ |x| x.type == :class }.map{ |x| x.path }
                          #yard_class.classes.map{ |x| complete_name(x.name, c.full_name) }

  model.methods         = meths.select.map{ |m| m.path }
  #model.methods         = meths.select{ |m| m.scope == :instance }.map{ |m| m.path }
  #model.class_methods   = meths.select{ |m| m.scope == :class }.map{ |m| m.path }

  model.accessors       = yard_class.attributes[:class].map{ |k, rw| yard_class.path + '.' + k.to_s } +
                          yard_class.attributes[:instance].map{ |k, rw| yard_class.path + '#' + k.to_s }
  #model.class_accessors = yard_class.attributes[:class].map{ |k, rw| yard_class.path + '.' + k.to_s }

  model.files           = yard_class.files.map{ |f, l| "/#{f}" } # :#{l}" }

  model.tags            = translate_tags(yard_class)

  #@files.concat(yard_class.files.map{ |f, l| f })

  @table[model.path] = model.to_h
end

#generate_constant(yard_constant) ⇒ Object (private)

Generate a constant.



371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'lib/shomen-yard/generator.rb', line 371

def generate_constant(yard_constant)
  debug_msg(yard_constant.path.to_s)

  model = Model::Constant.new

  model.path      = yard_constant.path
  model.name      = yard_constant.name.to_s
  model.namespace = yard_constant.namespace.path  
  model.comment   = yard_constant.docstring.to_s
  model.format    = 'rdoc'  #  TODO: how to determine? rdoc, markdown or plain 
  model.value     = yard_constant.value
  model.tags      = translate_tags(yard_constant)
  model.files     = yard_constant.files.map{|f,l| "/#{f}"}  # or "#{f}:#{l}" ?

  @table[model.path] = model.to_h
end

#generate_document(yard_document) ⇒ Object (private)

Generate a file.



390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# File 'lib/shomen-yard/generator.rb', line 390

def generate_document(yard_document)
  debug_msg(yard_document)

  model = Model::Document.new

  # FIXME: make absolute
  absolute_path = yard_document.to_s

  model.path   = yard_document.to_s
  model.name   = File.basename(absolute_path)
  model.mtime  = File.ctime(absolute_path)
  model.ctime  = File.mtime(absolute_path)
  model.text   = File.read(absolute_path)
  model.format = mime_type(absolute_path)

  webcvs = ['webcvs'] || webcvs
  if webcvs
    model.uri = File.join(webcvs, model.path)
  end

  @table['/'+model.path] = model.to_h
end

#generate_metadataObject (private)

Generate project metadata entry.

Returns Hash of metadata added to the documentation table.



188
189
190
191
# File 'lib/shomen-yard/generator.rb', line 188

def 
   = Metadata.new
  @table['(metadata)'] = .to_h
end

#generate_method(yard_method) ⇒ Object (private)

Generate a method structure.



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
# File 'lib/shomen-yard/generator.rb', line 279

def generate_method(yard_method)
  debug_msg(yard_method.to_s)

  # not sure what to do with methods with no signatures ?
  if !yard_method.signature
    debug_msg "no method signature -- #{yard_method.inspect}"
    return 
  end

  model = Model::Method.new
  #class_model = object.scope == :instance ? Shomen::Module::Method : Shomen::Model::Function

  model.path        = yard_method.path
  model.name        = yard_method.name.to_s
  model.namespace   = yard_method.parent.path  
  model.comment     = yard_method.docstring.to_s
  model.format      = 'rdoc'  # TODO: how to determine? rdoc, markdown or plain 
  model.aliases     = yard_method.aliases.map{ |a| a.path }  #method_name(a) }
  # TODO: how to get alias_for from YARD?
  #model.alias_for = method_name(yard_method.alias_for)
  model.singleton   = (yard_method.scope == :class)

  model.declarations << yard_method.scope.to_s
  model.declarations << yard_method.visibility.to_s
  # FIXME
  #model.declarations << yard_method.attr_info

  model.interfaces = []
  yard_method.tags.each do |tag|
    case tag
    when ::YARD::Tags::OverloadTag
      model.interfaces << parse_interface(tag)
    end
  end
  model.interfaces << parse_interface(yard_method)

  model.returns = (
    rtns = []
    yard_method.tags(:return).each do |tag|
      tag.types.to_a.each do |t|
        rtns << {'type'=>t, 'comment'=>tag.text}
      end
    end
    rtns
  )

  model.file     = '/'+yard_method.file
  model.line     = yard_method.line.to_i
  model.source   = yard_method.source.to_s.strip
  model.language = yard_method.source_type.to_s
  model.dynamic  = yard_method.dynamic

  model.tags     = translate_tags(yard_method)

  @table[model.path] = model.to_h
end

#generate_script(yard_script) ⇒ Object (private)

Generate a script entry.



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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
# File 'lib/shomen-yard/generator.rb', line 415

def generate_script(yard_script)
  debug_msg(yard_script)

  model = Model::Script.new

  # FIXME: make absolute
  absolute_path = yard_script.to_s

  model.path  = yard_script.to_s
  model.name  = File.basename(absolute_path)
  model.ctime = File.ctime(absolute_path)
  model.mtime = File.mtime(absolute_path)

  if source?
    model.source   = File.read(absolute_path) #file.comment
    model.language = mime_type(absolute_path)
  end

  webcvs = ['webcvs'] || webcvs
  if webcvs
    model.uri      = File.join(webcvs, model.path)
    model.language = mime_type(absolute_path)
  end

  #  model.header        = ""
  #  model.footer        = ""
  #  model.requires      =
  #  model.constants     =
  #  model.modules       =
  #  model.classes       =
  #  model.methods       =
  #  model.class_methods =

  @table['/'+model.path] = model.to_h

  #table[index] = Shomen::Model::Script.new(
  #  "name"        => File.basename(object),
  #  "path"        => object,
  #  #"loadpath"    => "lib",
  #  "mtime"       => File.mtime(object),
  #  "header"      => "",
  #  "footer"      => "",
  #  # "requires"    : ["fileutils"],
  #  # "constants"   : ["MusicStore::CONFIG_DIRECTORY"],
  #  # "modules"     : ["MusicStore", "MusicStore::MusicMixin"],
  #  # "classes"     : ["MusicStore::Song"],
  #  # "functions"   : ["MusicStore.config_directory"],
  #  # "methods"     : ["MusicStore::MusicMixin#play", "MusicStore::MusicMixin#artist"]
  #  "source"      => File.read(object)
  #).to_h

  @table['/'+model.path] = model.to_h
end

#generate_tableObject (private)

Generate the shomen data structure.

Returns Hash of documentation table.



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
# File 'lib/shomen-yard/generator.rb', line 105

def generate_table
  if not File.exist?(store)
    $stderr.puts "ERROR: YARD database not found -- '#{store}`."
    exit -1
  end

  @table = {}

  scripts = []

  

  @registry = YARD::Registry.load!(store)
  @registry.each do |object|
    case object.type
    when :constant
      scripts.push(object.file)
      generate_constant(object)
    when :class, :module
      scripts.push(object.file)
      generate_class(object)
      # TODO: is this needed?
      #object.constants.each do |c|
      #  generate_constant(c)
      #end
    when :method
      scripts.push(object.file)
      generate_method(object)
    else
      $stderr.puts "What is an #{object.type}? Ignored!"
    end
  end

  # TODO: Are c/c++ sourse files working okay?
  # TODO: Add a generator for non-ruby script (e.g. .js)?
  collect_files.each do |file|
    case File.extname(file)
    when '.rb', '.rbx', '.c', '.cpp'
      generate_script(file)
    when '.rdoc', '.md', '.markdown', '.txt'
      generate_document(file)
    else
      generate_document(file)
    end
  end

  # TODO: Also pass parent ?
  scripts.uniq.each do |file|
    generate_script(file)
  end

  return @table
end

#mime_type(path) ⇒ Object (private)

Given a file return offical mime-type basic on file extension.

FIXME: official mime types?



484
485
486
487
488
489
490
491
492
493
# File 'lib/shomen-yard/generator.rb', line 484

def mime_type(path)
  case File.extname(path)
  when '.rb', '.rbx'      then 'text/x-ruby'
  when '.c'               then 'text/c-source'  # x-c-code
  when '.js'              then 'text/ecmascript'
  when '.rdoc'            then 'text/rdoc'
  when '.md', '.markdown' then 'text/markdown'
  else 'text/plain'
  end
end

#parse_interface(yard_method) ⇒ Object (private)

Parse a yard method’s interface.



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
# File 'lib/shomen-yard/generator.rb', line 337

def parse_interface(yard_method)
  args, block = [], {}
  image, returns = yard_method.signature.split(/[=-]\>/)
  image = image.strip
  if i = image.index(/\)\s*\{/)
    block['image'] = image[i+1..-1].strip
    image          = image[0..i].strip
  end
  image = image.sub(/^def\s*/, '')
  image = image.sub(/^self\./, '')
  image = image.sub('( )','()')

  yard_method.parameters.each do |n,v|
    n = n.to_s
    case n
    when /^\&/
      block['name'] = n
    else
      args << (v ? {'name'=>n,'default'=>v} : {'name'=>n})
    end
  end

  result = {}
  result['signature']  = image
  result['arguments']  = args
  #result['parameters'] = params
  result['block']      = block unless block.empty?
  result['returns']    = returns.strip if returns
  result
end

#preconfigureObject (private)

Create YARD cache.

Returns nothing.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/shomen-yard/generator.rb', line 83

def preconfigure
  argv = []
  argv.concat ["-q"]
  argv.concat ["-n"]
  argv.concat ["-b", store]
  argv.concat ["--markup", markup] if markup
  argv.concat ["--debug"] if $DEBUG
  #argv.concat ["--no-save"] #unless save
  argv.concat scripts
  #argv.concat documents unless documents.empty?

  # clear the registry in memory to remove any previous runs
  YARD::Registry.clear

  yard = YARD::CLI::Yardoc.new
  $stderr.puts('yard ' + argv.join(' ')) if $DEBUG
  yard.run(*argv)
end

#translate_tags(yard_object) ⇒ Object (private)

Convert YARD Tags to simple Hash.

TODO: Remove param tags?



498
499
500
501
502
503
504
505
# File 'lib/shomen-yard/generator.rb', line 498

def translate_tags(yard_object)
  tags = {}
  yard_object.tags.each do |tag|
    next if tag.tag_name == 'return'
    tags[tag.tag_name] = tag.text
  end
  return tags
end

#use_cache?Boolean

Use pre-existant cache instead of regenerating documentation.

Returns true/false.

Returns:

  • (Boolean)


45
46
47
# File 'lib/shomen-yard/generator.rb', line 45

def use_cache?
  @use_cache
end