Class: RDoc::Generator::Bootstrap

Inherits:
Object
  • Object
show all
Defined in:
lib/hanna-bootstrap/version.rb,
lib/hanna-bootstrap.rb

Constant Summary collapse

VERSION =
'0.0.5'
LAYOUT =
'layout.haml'
CLASS_PAGE =
'page.haml'
METHOD_LIST_PAGE =
'method_list.haml'
FILE_PAGE =
CLASS_PAGE
SECTIONS_PAGE =
'sections.haml'
FILE_INDEX =
'file_index.haml'
CLASS_INDEX =
'class_index.haml'
METHOD_INDEX =
'method_index.haml'
CLASS_DIR =
'classes'
FILE_DIR =
'files'
INDEX_OUT =
'index.html'
FILE_INDEX_OUT =
File.join 'files',   'index.html'
CLASS_INDEX_OUT =
File.join 'classes', 'index.html'
METHOD_INDEX_OUT =
File.join 'method',  'index.html'
DESCRIPTION =
'Twitter Bootstrap theme for RDoc'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store, options) ⇒ Bootstrap

Returns a new instance of Bootstrap.



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/hanna-bootstrap.rb', line 54

def initialize( store, options )
  @options     = options
  @store       = store
  @templatedir = Pathname.new(options.template_dir || File.expand_path('../hanna-bootstrap/template_files', __FILE__))

  @files      = nil
  @classes    = nil
  @methods    = nil
  @attributes = nil

  @basedir = Pathname.pwd.expand_path
end

Class Method Details

.for(options) ⇒ Object



50
51
52
# File 'lib/hanna-bootstrap.rb', line 50

def self::for( options )
  new( options )
end

Instance Method Details

#build_javascript_search_index(entries) ⇒ Object



319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/hanna-bootstrap.rb', line 319

def build_javascript_search_index(entries)
  'var searchIndex = ' + (entries.map { |entry|
    method_name = entry.name
    module_name = entry.parent_name
    link = [classfile(entry.parent), (entry.aref rescue "method-#{entry.html_name}")].join('#')
    {
      'method' => method_name,
      'module' => module_name,
      'link'   => link
    }
  }.to_json)
end

#class_dirObject



280
281
282
# File 'lib/hanna-bootstrap.rb', line 280

def class_dir
  CLASS_DIR
end

#classfile(klass) ⇒ Object



351
352
353
354
# File 'lib/hanna-bootstrap.rb', line 351

def classfile(klass)
  # FIXME sloooooooow
  Pathname.new(File.join(CLASS_DIR, klass.full_name.split('::')) + '.html')
end

#default_values(path) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/hanna-bootstrap.rb', line 67

def default_values( path )
  {
    stylesheets: %w{
      bootstrap.min
      application
      coderay
    }.map {|f|outpath(File.join('css', "#{f}.css"), path)},
    javascripts: %w{
      jquery
      bootstrap.min
      index
      application
    }.map {|f|outpath(File.join('js', "#{f}.js"), path)},
    mainpage:   outpath('', path),
    files:      @files,
    classes:    @classes,
    methods:    @methods,
    attributes: @attributes,
    options:    @options,
    path:       path
  }

end

#file_dirObject



284
285
286
# File 'lib/hanna-bootstrap.rb', line 284

def file_dir
  FILE_DIR
end

#generateObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/hanna-bootstrap.rb', line 91

def generate
  @outputdir = Pathname.new( @options.op_dir ).expand_path( @basedir )

  @files      = @store.all_files.sort
  @classes    = @store.all_classes_and_modules.sort
  @methods    = @classes.map {|m| m.method_list }.flatten.sort
  @attributes = @classes.map(&:attributes).flatten.sort

  # Now actually write the output
  write_static_files

  generate_class_files
  generate_file_files
  generate_indexes

rescue StandardError => err
  p [ err.class.name, err.message, err.backtrace.join("\n  ") ]
  raise
end

#generate_class_filesObject



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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/hanna-bootstrap.rb', line 214

def generate_class_files
  class_page       = haml_file(templjoin(CLASS_PAGE))
  method_list_page = haml_file(templjoin(METHOD_LIST_PAGE))
  sections_page    = haml_file(templjoin(SECTIONS_PAGE))
  # FIXME refactor

  @classes.each do |klass|
    outfile = classfile(klass)
    sections = {}
    klass.each_section do |section, constants, attributes|
      method_types = []
      alias_types = []
      klass.methods_by_type(section).each do |type, visibilities|
        visibilities.each do |visibility, methods|
          aliases, methods = methods.partition{|x| x.is_alias_for}
          method_types << ["#{visibility.to_s.capitalize} #{type.to_s.capitalize}", methods.sort] unless methods.empty?
          alias_types << ["#{visibility.to_s.capitalize} #{type.to_s.capitalize}", aliases.sort] unless aliases.empty?
        end
      end
      sections[section] = {:constants=>constants, :attributes=>attributes, :method_types=>method_types, :alias_types=>alias_types}
    end

    path = Pathname.new(klass.path)

    values = default_values(path).merge({
      :file => klass.path,
      :entry => klass,
      :classmod => klass.type,
      :title => klass.full_name,
      :list_title => nil,
      :description => klass.description,
      :sections => sections
    })

    result = with_layout(values) do
      h = {:values => values}
      class_page.to_html(binding, h) do
        method_list_page.to_html(binding, h) + sections_page.to_html(binding, h)
      end
    end

    # FIXME XXX sanity check
    dir = outfile.dirname
    unless File.directory? dir
      FileUtils.mkdir_p dir
    end

    File.open(outfile, 'w') { |f| f << result }
  end
end

#generate_file_file(file, path = nil) ⇒ Object



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
# File 'lib/hanna-bootstrap.rb', line 184

def generate_file_file(file, path = nil)
  file_page = haml_file(templjoin(FILE_PAGE))
  method_list_page = haml_file(templjoin(METHOD_LIST_PAGE))

  path = Pathname.new(path || file.path)
  values = default_values(path).merge({
    :file => file,
    :entry => file,
    :classmod => nil,
    :title => file.base_name,
    :list_title => nil,
    :description => file.description
  })

  result = with_layout(values) do
    file_page.to_html(binding, :values => values) do
      method_list_page.to_html(binding, values)
    end
  end

  # FIXME XXX sanity check
  dir = path.dirname
  unless File.directory? dir
    FileUtils.mkdir_p dir
  end

  File.open(outjoin(path.to_path), 'w') { |f| f << result }

end

#generate_file_filesObject



175
176
177
178
179
180
181
182
# File 'lib/hanna-bootstrap.rb', line 175

def generate_file_files

  # FIXME non-Ruby files
  @files.each do |file|
    generate_file_file(file)
    generate_file_file(file, 'index.html') if @options.main_page == file.name
  end
end

#generate_index(outfile, templfile, index_name) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/hanna-bootstrap.rb', line 155

def generate_index(outfile, templfile, index_name)
  path = Pathname.new(outfile)
  dir = path.dirname
  unless File.directory? dir
    FileUtils.mkdir_p dir
  end

  values = default_values(path).merge({
    :list_title => "#{index_name} Index"
  })

  index = haml_file(templjoin(templfile))

  File.open(path, 'w') do |f|
    f << with_layout(values) do
      index.to_html(binding, values)
    end
  end
end

#generate_indexesObject



149
150
151
152
153
# File 'lib/hanna-bootstrap.rb', line 149

def generate_indexes
  generate_index(FILE_INDEX_OUT,   FILE_INDEX,   'File')
  generate_index(CLASS_INDEX_OUT,  CLASS_INDEX,  'Class')
  generate_index(METHOD_INDEX_OUT, METHOD_INDEX, 'Method')
end

#h(html) ⇒ Object



288
289
290
# File 'lib/hanna-bootstrap.rb', line 288

def h(html)
  CGI::escapeHTML(html)
end

#haml_file(file) ⇒ Object



368
369
370
# File 'lib/hanna-bootstrap.rb', line 368

def haml_file(file)
  Haml::Engine.new(File.read(file))
end

#highlighted_code_block(method) ⇒ Object



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
397
398
399
400
401
# File 'lib/hanna-bootstrap.rb', line 372

def highlighted_code_block(method)
  lang = CodeRay::FileType.fetch(method.file.name, :text, true)
  src = method.token_stream.map(&:text).join

  # from RDoc::Generator::Markup#markup_code
  indent = src.length
  lines = src.lines.to_a
  start = 0
  line_numbers = false
  if src =~ /\A.*#\ *File/i # remove '# File' comment
    line = lines.shift
    line_numbers = @options.line_numbers if line.sub!(/\A(.*)(, line (\d+))/, '\1')
    start = $3.to_i
  end
  lines.each do |line|
    if line =~ /^ *(?=\S)/
      n = $&.length
      indent = n if n < indent
      break if n == 0
    end
  end
  src = lines.join
  src.gsub!(/^#{' ' * indent}/, '') if indent > 0

  CodeRay.highlight(src, lang,
    :line_numbers        => line_numbers ? :table : nil,
    :line_number_anchors => "#{method.aref}-source-",
    :line_number_start   => start
    )
end


332
333
334
335
336
337
338
339
340
341
342
# File 'lib/hanna-bootstrap.rb', line 332

def link_to(text, url = nil, classname = nil, base = nil)
  class_attr = classname ? ' class="%s"' % classname : ''

  if url
      %[<a href="#{base}#{url}"#{class_attr}>#{text}</a>]
  elsif classname
      %[<span#{class_attr}>#{text}</span>]
  else
    text
  end
end

method_text is in the form of “ago (ActiveSupport::TimeWithZone)”.



345
346
347
348
349
# File 'lib/hanna-bootstrap.rb', line 345

def link_to_method(entry, url = nil, classname = nil)
  method_name = entry.pretty_name rescue entry.name
  module_name = entry.parent_name rescue entry.name
  link_to %Q(<li><strong>#{h method_name}</strong><small>#{h module_name}</small></li>), url, classname
end

#outjoin(name) ⇒ Object



360
361
362
# File 'lib/hanna-bootstrap.rb', line 360

def outjoin(name)
  File.join(@outputdir, name)
end

#outpath(path, from_path) ⇒ Object



356
357
358
# File 'lib/hanna-bootstrap.rb', line 356

def outpath( path, from_path )
  Pathname.new(path).relative_path_from(from_path.dirname)
end

#render_class_tree(entries, from_path, parent = nil) ⇒ Object

XXX may my sins be not visited upon my sons.



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
# File 'lib/hanna-bootstrap.rb', line 293

def render_class_tree(entries, from_path, parent=nil)
  namespaces = { }

  entries.sort.inject('') do |out, klass|
    unless namespaces[klass.full_name]
      if parent
        text = '<span class="parent">%s::</span>%s' % [parent.full_name, klass.name]
      else
        text = klass.name
      end

      if klass.document_self
        link = Pathname.new(classfile(klass)).relative_path_from(from_path.dirname)
        out << "<li>#{ link_to(text, link) }</li>"
      end

      subentries = @classes.select { |x| x.full_name[/^#{klass.full_name}::/] }
      subentries.each { |x| namespaces[x.full_name] = true }
      out << render_class_tree(subentries, from_path, klass)

    end

    out
  end
end

#sanitize_code_blocks(text) ⇒ Object



270
271
272
273
274
275
276
277
278
# File 'lib/hanna-bootstrap.rb', line 270

def sanitize_code_blocks(text)
  text.gsub(/<pre>(.+?)<\/pre>/m) do
    code = $1.sub(/^\s*\n/, '')
    indent = code.gsub(/\n[ \t]*\n/, "\n").scan(/^ */).map{ |i| i.size }.min
    code.gsub!(/^#{' ' * indent}/, '') if indent > 0

      "<pre>#{code}</pre>"
  end
end

#templjoin(name) ⇒ Object



364
365
366
# File 'lib/hanna-bootstrap.rb', line 364

def templjoin(name)
  File.join(@templatedir, name)
end

#with_layout(values) ⇒ Object



265
266
267
268
# File 'lib/hanna-bootstrap.rb', line 265

def with_layout(values)
  layout = haml_file(templjoin(LAYOUT))
  layout.to_html(binding, :values => values) { yield }
end

#write_static_filesObject



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
# File 'lib/hanna-bootstrap.rb', line 111

def write_static_files
  css_dir = outjoin('css')
  js_dir  = outjoin('js')
  img_dir = outjoin('img')

  [css_dir, js_dir, img_dir].each { |dir|
    FileUtils.mkdir dir unless File.directory?(dir)
  }

  File.open(File.join(css_dir, 'application.css'), 'w') { |f|
    f << Sass::Engine.new(File.read(templjoin('application.sass'))).to_css
  }

  File.open(File.join(js_dir, 'application.js'), 'w') { |f|
    f << CoffeeScript.compile(File.read(templjoin('application.coffee')))
  }

  File.open(File.join(js_dir, 'index.js'), 'w') { |f|
    f << build_javascript_search_index(@methods + @attributes)
  }

  FileUtils.cp %w{
    bootstrap.min.js
    jquery.js
  }.map{|f| templjoin f }, js_dir

  FileUtils.cp %w{
    glyphicons-halflings-white.png
    glyphicons-halflings.png
  }.map{|f| templjoin f }, img_dir

  FileUtils.cp %w{
    bootstrap.min.css
    coderay.css
  }.map{|f| templjoin f }, css_dir

end