Class: RDoc::Generator::Hanna

Inherits:
Object
  • Object
show all
Defined in:
lib/hanna_gudao.rb

Constant Summary collapse

STYLE =
'styles.sass'
LAYOUT =
'layout.haml'
INDEX_PAGE =
'index.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 =
'fr_file_index.html'
CLASS_INDEX_OUT =
'fr_class_index.html'
METHOD_INDEX_OUT =
'fr_method_index.html'
STYLE_OUT =
File.join('css', 'style.css')
DESCRIPTION =
'a HAML-based HTML generator that scales'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Hanna

Returns a new instance of Hanna.



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/hanna_gudao.rb', line 55

def initialize( options )
  @options = options

  @templatedir = Pathname.new File.expand_path('../hanna_gudao/template_files', __FILE__)

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

  @basedir = Pathname.pwd.expand_path
end

Class Method Details

.for(options) ⇒ Object



51
52
53
# File 'lib/hanna_gudao.rb', line 51

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

Instance Method Details

#build_javascript_search_index(entries) ⇒ Object



275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/hanna_gudao.rb', line 275

def build_javascript_search_index(entries)
  result = "var search_index = [\n"
  entries.each do |entry|
    method_name = entry.name
    module_name = entry.parent_name
    # FIXME link
    html = link_to_method(entry, [classfile(entry.parent), (entry.aref rescue "method-#{entry.html_name}")].join('#'))
    result << "  { method: '#{method_name.downcase}', " +
                    "module: '#{module_name.downcase}', " +
                    "html: '#{html}' },\n"
  end
  result << "]"
  result
end

#class_dirObject



233
234
235
# File 'lib/hanna_gudao.rb', line 233

def class_dir
  CLASS_DIR
end

#classfile(klass) ⇒ Object



309
310
311
312
# File 'lib/hanna_gudao.rb', line 309

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

#file_dirObject



237
238
239
# File 'lib/hanna_gudao.rb', line 237

def file_dir
  FILE_DIR
end

probably should bring in nokogiri/libxml2 to do this right.. not sure if it’s worth it.



225
226
227
228
229
230
231
# File 'lib/hanna_gudao.rb', line 225

def frame_link(content)
  content.gsub(%r!<a href="http://[^>]*>!).each do |tag|
    a_tag, rest = tag.split(' ', 2)
    rest.gsub!(/target="[^"]*"/, '')
    a_tag + ' target="_top" ' + rest
  end
end

#generate(top_levels) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/hanna_gudao.rb', line 68

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

  @files      = top_levels.sort
  @classes    = RDoc::TopLevel.all_classes_and_modules.sort
  @methods    = @classes.map(&:method_list).flatten.sort
  @attributes = @classes.map(&:attributes).flatten.sort

  # Now actually write the output
  write_static_files
  generate_indexes
  generate_class_files
  generate_file_files

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

#generate_class_filesObject



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
# File 'lib/hanna_gudao.rb', line 157

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)
    stylesheet = Pathname.new(STYLE_OUT).relative_path_from(outfile.dirname)
    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

    values = { 
      :file => klass.path, 
      :entry => klass,
      :stylesheet => stylesheet,
      :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_filesObject



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
# File 'lib/hanna_gudao.rb', line 122

def generate_file_files
  file_page = haml_file(templjoin(FILE_PAGE))
  method_list_page = haml_file(templjoin(METHOD_LIST_PAGE))

  # FIXME non-Ruby files
  @files.each do |file|
    path = Pathname.new(file.path)
    stylesheet = Pathname.new(STYLE_OUT).relative_path_from(path.dirname)
    
    values = { 
      :file => file, 
      :entry => file,
      :stylesheet => stylesheet,
      :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(file.path), 'w') { |f| f << result }
  end
end

#generate_index(outfile, templfile, index_name, values) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/hanna_gudao.rb', line 107

def generate_index(outfile, templfile, index_name, values)
  values.merge!({
    :stylesheet => STYLE_OUT,
    :list_title => "#{index_name} Index"
  })

  index = haml_file(templjoin(templfile))

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

#generate_indexesObject

FIXME refactor



98
99
100
101
102
103
104
105
# File 'lib/hanna_gudao.rb', line 98

def generate_indexes
  @main_page_uri = @files.find { |f| f.name == @options.main_page }.path rescue ''
  File.open(outjoin(INDEX_OUT), 'w') { |f| f << haml_file(templjoin(INDEX_PAGE)).to_html(binding) }

  generate_index(FILE_INDEX_OUT,   FILE_INDEX,   'File',   { :files => @files})
  generate_index(CLASS_INDEX_OUT,  CLASS_INDEX,  'Class',  { :classes => @classes })
  generate_index(METHOD_INDEX_OUT, METHOD_INDEX, 'Method', { :methods => @methods, :attributes => @attributes })
end

#h(html) ⇒ Object



241
242
243
# File 'lib/hanna_gudao.rb', line 241

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

#haml_file(file) ⇒ Object



322
323
324
# File 'lib/hanna_gudao.rb', line 322

def haml_file(file)
  Haml::Engine.new(File.read(file), :format => :html4)
end


290
291
292
293
294
295
296
297
298
299
300
# File 'lib/hanna_gudao.rb', line 290

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

  if url
      %[<a target="docwin" href="#{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)”.



303
304
305
306
307
# File 'lib/hanna_gudao.rb', line 303

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(<span class="method_name">#{h method_name}</span> <span class="module_name">(#{h module_name})</span>), url, classname
end

#outjoin(name) ⇒ Object



314
315
316
# File 'lib/hanna_gudao.rb', line 314

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

#render_class_tree(entries, parent = nil) ⇒ Object

XXX may my sins be not visited upon my sons.



246
247
248
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
# File 'lib/hanna_gudao.rb', line 246

def render_class_tree(entries, 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
        out << '<li>'
        out << link_to(text, classfile(klass))
      end

      subentries = @classes.select { |x| x.full_name[/^#{klass.full_name}::/] }
      subentries.each { |x| namespaces[x.full_name] = true }
      out << "\n<ol>" + render_class_tree(subentries, klass) + "\n</ol>"

      if klass.document_self
        out << '</li>'
      end
    end

    out
  end
end

#sanitize_code_blocks(text) ⇒ Object



213
214
215
216
217
218
219
220
221
# File 'lib/hanna_gudao.rb', line 213

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



318
319
320
# File 'lib/hanna_gudao.rb', line 318

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

#with_layout(values) ⇒ Object



208
209
210
211
# File 'lib/hanna_gudao.rb', line 208

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

#write_static_filesObject



87
88
89
90
91
92
93
94
95
# File 'lib/hanna_gudao.rb', line 87

def write_static_files
  css_dir = outjoin('css')

  unless File.directory?(css_dir)
    FileUtils.mkdir css_dir
  end

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