Class: DocJs

Inherits:
Thor show all
Includes:
Thor::Actions
Defined in:
bin/docjs.rb

Overview

Note:

options declared in a docjs.yml will override command-line ones

Note:

for more information about the instance methods please type docjs help or docjs help TASK into your commandline or reference the guides.

Program Flow

Program flow

  1. Prepare Doc.js - Read configurations and setup application environment
  2. Process Markdown Documents - Read the optional markdown-documents and add them to the internal datastructure.
  3. Process JavaScript Files - Parse JavaScript-Files and add the found comments to the Dom. Afterwards process the tokens within the comments.
  4. Render Templates - Apply the Outputgenerators and save the rendered templates to disk
  5. Copy static resources - Finalize the output and copy the javascript, images and stylesheets to the output-directory

The Processor is the central module, which controls the workflow described above. It separates the tasks into different stages, which are performed one after another. (You can see this in the source-code of #docjs)

The Command Line Interface

The DocJs-class serves as command line interface (cli) and therefore extends Thor. It allows us to create tasks, which then in turn can be called from the command line. This way every instance method of DocJs will be turned into a commandline task.

docjs help configure

Will create an instance of DocJs and call the #help method on it. (Which results in the help screen being displayed). As help expects one argument 'configure' is passed to the call of help. All additional commandline arguments would be parsed by Thor and added to the instance variable configs.

The Internal Datastructure

All parsed CodeObjects (i.e. comments found in your JavaScript-soures) are internally represented as a Dom-Tree. They can be accessed via Dom.root

Also all Markdown-Documents are stored in the same Dom-tree, but under a different root-node called Dom.docs.

For more information about storing and retreiving Data in DocJs see Dom.

See Also:

Instance Method Summary (collapse)

Instance Method Details

- (Object) configure(output_file = "docjs.yml", preconfigured = {})



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'bin/docjs.rb', line 151

def configure(output_file = "docjs.yml", preconfigured = {})
  
  build = {
    'files'     => [],
    'docs'      => []
  }
  
  build.merge! preconfigured
  
  say "\nPlease enter the name of your App", :bold
  build['appname'] = ask ">"

  say "\nPlease enter the javascript-files you want to integrate into your documentation", :bold
  say "(You will be asked multiple times, unless your answer is empty) You also can specify a file-matching pattern, foo/**/*.js"
  
  while true do
    answer = ask ">"
    break if answer == ""
    build['files'] << answer
  end
  
  say "\nPlease enter the markdown-documentation-files you want to integrate into your documentation", :bold
  say "(You will be asked multiple times, unless your answer is empty) You also can specify a file-matching pattern, like docs/*.md"
  
  while true do
    answer = ask ">"
    break if answer == ""
    build['docs'] << answer
  end
  
  if not build['templates'] and yes? "Are you using your own templates?"
    say "\nPlease enter the path to your templates", :bold
    build['templates'] = ask ">"
  end
  
  if not build['output']
    say "\nWhere do you wan't your documentation generated to? Please enter a path", :bold
    build['output'] = ask ">"
  end
  
  if not build['loglevel']
    say "\nPlease specify the loglevel of your output: (error|warn|info|debug)", :bold
    
    while true do
      answer = ask ">"
      if %w(error warn info debug).include? answer
        build['loglevel'] = answer
        break
      end
      say "\nThe answer you've given is not one of (error|warn|info|debug). Please try again", :bold
    end          
  end
  
  if not build['logfile'] and yes? "Do you wan't to save your logs to a file?"
    say "\nPlease enter the path to your logfile", :bold
    build['logfile'] = ask ">"
  end
  
  # answers[:scss_build] = yes? "Do you wan't to integrate SCSS into your build-process? #{yes_no}"
  
  # maybe ask some more information to generate build.yml
  create_file output_file, build.to_yaml
end

- (Object) docjs(config_file = nil)



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
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'bin/docjs.rb', line 81

def docjs(config_file = nil)
  
  # Process the given command-line and yml-options
  # @see Thor#merge_options
  configs = config_file ? merge_options(options, config_file) : options
  
  begin
    
    # PREPARE DOCJS
  
    # Setup Logger and Configs
    setup_application configs
    
    Logger.info "Loading Application-Templates"
    load_templates
    
    # Config Thor path-settings
    DocJs.source_root(Configs.templates)
    self.destination_root = Configs.output
    
    
    # PROCESS MARKDOWN DOCUMENTS
    Processor.prepare_documents     # Stage #1
    
    # PROCESS JAVASCRIPT FILES
    Processor.process_files_to_dom  # Stage #2
    
    # RENDER TEMPLATES
    Processor.start_generators      # Stage #3
    
      
    # COPY STATIC RESOURCES
    Logger.info "Copying template resources to output"
    directory 'resources/img', './img'
    directory 'resources/css', './css'
    directory 'resources/js', './js'
  
  # Print error on console, if something bad happend  
  rescue Exception => error
    Logger.error error.message + "\n" + error.backtrace.map{|l| "  #{l}" }.join("\n")
  end    
end

- (Object) generators(template_path = nil)



274
275
276
277
278
279
280
281
282
283
# File 'bin/docjs.rb', line 274

def generators(template_path = nil)
  
  load_templates template_path
  
  say "Registered Generators:"
  
  gen_table = Generator::Generator.all.map{|gen| ["#{gen.name}","# #{gen.description}"] }.sort
  
  print_table gen_table, :ident => 2, :colwidth => 40    
end

- (Object) help(task = nil)



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'bin/docjs.rb', line 126

def help(task = nil)

  # Introduction
  unless task
    say "Welcome to Doc.js", :bold
    say "If you are using Doc.js for the first time in your project, you may want to create a config file (like docjs.yml)
A guided wizard can help you achieving this goal - Simply run the following command:
  docjs configure
\n
After creating a config-file (like docjs.yml) the documentation can be generated by running:
  docjs docjs.yml

For further information, please visit http://b-studios.github.com/doc.js\n\n"

    self.class.help(shell, false)
    
  # Help for a specific task
  else
    self.class.task_help(shell, task)    
  end
end

- (Object) scaffold(output_dir)



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
# File 'bin/docjs.rb', line 223

def scaffold(output_dir)
    
  setup_application options.merge({
    :output => output_dir,
    :templates => output_dir
  })
  
  # Setup Thor paths
  DocJs.source_root(Configs.root)
  self.destination_root = Configs.output

  yes_no = "(y|n)"
  
  Logger.info "We need some information from you, to customize the scaffolding process to your needs."    
  if yes? "Do you wan't to generate a docjs.yml? #{yes_no}"
    configure(Configs.wdir + '/docjs.yml', {
      'templates' => output_dir,
      'output'    => 'docs',
      'logfile'   => 'logfile.log',
      'loglevel'  => 'info'
    })
  end
      
  # Work with the answers    
  Logger.info "Copying the template files to #{Configs.templates}"
  directory 'templates', Configs.templates    # copy templates and resources
      
  Logger.info "Copying the included *.rb files to #{Configs.includes}"    
end

- (Object) tokens(template_path = nil)



258
259
260
261
262
263
264
265
266
267
268
269
# File 'bin/docjs.rb', line 258

def tokens(template_path = nil)

  load_templates template_path

  say "Supported tokens:\n\n"
  if options.details?
    table = [%w(TOKEN AREA TEMPLATE DESCRIPTION)] + Token::Handler.handlers.map{|k,v| [":#{k}",v.area, v.template, v.description] }.sort
  else
    table = Token::Handler.handlers.map{|k,v| [":#{k}","# #{v.description}"] }.sort
  end
  print_table table, :ident => 2, :colwidth => 20
end