Class: Erlapi::Generator

Inherits:
Object
  • Object
show all
Includes:
ERB::Util, Helpers, Templatable
Defined in:
lib/erlapi/generator.rb

Defined Under Namespace

Classes: SHtml

Constant Summary collapse

GENERATOR_DIRS =
[File.join('sdoc', 'generator'), File.join('rdoc', 'generator')]
TYPE_CLASS =

Used in js to reduce index sizes

1
TYPE_METHOD =
2
TYPE_FILE =
3
TEMPLATE_DIR =
Pathname.new(File.dirname(__FILE__)) + 'templates' + 'html' + 'shtml'
FILE_DIR =
'files'
CLASS_DIR =
'classes'
RESOURCES_DIR =
'resources'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#each_letter_group

Methods included from Templatable

#eval_template, #include_template, #render_template

Constructor Details

#initialize(options = {}) ⇒ Generator

Returns a new instance of Generator.



26
27
28
29
30
31
32
33
# File 'lib/erlapi/generator.rb', line 26

def initialize(options = {})    
  self.output_dir = Pathname.new(options[:output_dir])
  
  self.output_dir.mkdir if !self.output_dir.directory?      
    
  self.data = options[:data]
  define_paths
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



3
4
5
# File 'lib/erlapi/generator.rb', line 3

def data
  @data
end

#output_dirObject

Returns the value of attribute output_dir.



3
4
5
# File 'lib/erlapi/generator.rb', line 3

def output_dir
  @output_dir
end

#source_dirObject

Returns the value of attribute source_dir.



3
4
5
# File 'lib/erlapi/generator.rb', line 3

def source_dir
  @source_dir
end

Instance Method Details

#add_class_search_index(index) ⇒ Object

Add classes to search index array



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/erlapi/generator.rb', line 115

def add_class_search_index(index)
  debug_msg "  generating class search index"
  
  self.data.each do |mod_name, mod|      
    index[:searchIndex].push( search_string(mod.title) )
    index[:longSearchIndex].push( search_string(mod.title) )
    index[:info].push([
      mod.title,
      '',
      mod.title + '.html',
      '',       
      snippet(mod.summary),
      TYPE_CLASS
    ])
  end
end

#add_method_search_index(index) ⇒ Object

Add methods to search index array



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/erlapi/generator.rb', line 133

def add_method_search_index(index)
  debug_msg "  generating method search index"
  
  list = self.data.map { |mod_name, mod| 
    mod.funcs
  }.flatten
  
  list.each do |func|
    func.short_names.each do |func_name|
      index[:searchIndex].push( search_string(func.p_module.title + ':' + func_name) + '()' )
      index[:longSearchIndex].push(search_string(func_name))
      index[:info].push([
        func_name, 
        func.p_module.title + ':' + func_name,
        func.p_module.title + '.html#' + func.ref,
        '',        
        snippet(func.summary),
        TYPE_METHOD
      ])
    end
  end
end

#copy_resourcesObject

Copy all the resource files to output dir



50
51
52
53
54
# File 'lib/erlapi/generator.rb', line 50

def copy_resources
  resources_path = File.join(TEMPLATE_DIR, RESOURCES_DIR) + '/.'
debug_msg "Copying #{resources_path}/** to #{output_dir}/**"
  FileUtils.cp_r resources_path.to_s, output_dir.to_s, :preserve => true
end

#debug_msg(*msg) ⇒ Object

Output progress information if debugging is enabled



188
189
190
191
# File 'lib/erlapi/generator.rb', line 188

def debug_msg( *msg )
	return unless $DEBUG_DOC
	$stderr.puts( *msg )
end

#define_pathsObject



35
36
37
38
# File 'lib/erlapi/generator.rb', line 35

def define_paths
  @tree_file = output_dir + 'panel' + 'tree.js'
  @search_index_file = output_dir + 'panel' + 'search_index.js'
end

#generate(options = {}) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/erlapi/generator.rb', line 40

def generate(options = {})
  @options = options
  copy_resources
  generate_search_index
  generate_class_tree
  generate_class_files
  generate_index_file
end

#generate_class_filesObject

Generate a documentation file for each class



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/erlapi/generator.rb', line 57

def generate_class_files
	debug_msg "Generating class documentation in #{output_dir}"
   templatefile = TEMPLATE_DIR + 'class.rhtml'

	self.data.each do |mod_name, mod|
	  if mod_name
 			debug_msg "  working on %s" % [ mod.title]
 			outfile     = output_dir + (mod.title + '.html')
       # rel_prefix  = OUTPUT_DIR.relative_path_from( outfile.dirname )
     
 			debug_msg "  rendering #{outfile}"
 			self.render_template( templatefile, binding(), outfile )
 		end
	end
end

#generate_class_treeObject

Create class tree structure and write it as json



83
84
85
86
87
88
89
90
# File 'lib/erlapi/generator.rb', line 83

def generate_class_tree
  debug_msg "Generating class tree"    
  tree = self.data.map { |mod_name, mod| [mod_name, mod_name + '.html', '', []] }.sort{ |a, b| a[0] <=> b[0] }
  debug_msg "  writing class tree to %s" % @tree_file
  File.open(@tree_file, "w", 0644) do |f|
    f.write('var tree = '); f.write(tree.to_json)
  end
end

#generate_index_fileObject

Create index.html with frameset



74
75
76
77
78
79
80
# File 'lib/erlapi/generator.rb', line 74

def generate_index_file
	debug_msg "Generating index file in #{output_dir}"
   templatefile = TEMPLATE_DIR + 'index.rhtml'
   outfile      = output_dir + 'index.html'
  index_path = self.data.to_a.first[1].title + '.html'
  self.render_template( templatefile, binding(), outfile )
end

#generate_search_indexObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/erlapi/generator.rb', line 92

def generate_search_index
  debug_msg "Generating search index"
  
  index = {
    :searchIndex => [],
    :longSearchIndex => [],
    :info => []
  }
  
  add_class_search_index(index)
  add_method_search_index(index)
  
  debug_msg "  writing search index to %s" % @search_index_file
  data = {
    :index => index
  }
  File.open(@search_index_file, "w", 0644) do |f|
    f.write('var search_data = '); f.write(data.to_json)
  end
end

#search_string(string) ⇒ Object

Build search index key



159
160
161
162
# File 'lib/erlapi/generator.rb', line 159

def search_string(string)
  string ||= ''
  string.downcase.gsub(/\s/,'')
end

#snippet(str) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/erlapi/generator.rb', line 164

def snippet(str)
  str ||= ''
  if str =~ /^(?>\s*)[^\#]/
    content = str
  else
    content = str.gsub(/^\s*(#+)\s*/, '')
  end
  
  content = content.sub(/^(.{100,}?)\s.*/m, "\\1").gsub(/\r?\n/m, ' ')
  
  begin
    content.to_json
  rescue # might fail on non-unicode string
    begin
      content = Iconv.conv('latin1//ignore', "UTF8", content) # remove all non-unicode chars
      content.to_json
    rescue
      content = '' # something hugely wrong happend
    end
  end
  content
end