1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
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
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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
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
| | # Copyright (C) 2015-2016 all contributors <olddoc-public@80x24.org>
# License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
# Loosely derived from Darkfish in the main rdoc distribution
require 'rdoc'
require 'erb'
require 'pathname'
require 'yaml'
require 'cgi'
# oldweb is an \RDoc template and not intended as a programming API.
# You may specify it as an \RDoc formatter:
#
# rdoc -f oldweb ...
class Oldweb
RDoc::RDoc.add_generator(self) # :nodoc:
include ERB::Util # :nodoc:
attr_reader :class_dir # :nodoc:
attr_reader :file_dir # :nodoc:
# description of the generator
DESCRIPTION = 'minimal HTML generator'
# version of this generator
VERSION = '1'
def initialize(store, options) # :nodoc:
# just because we're capable of generating UTF-8 to get human names
# right does not mean we should overuse it for quotation marks and such,
# our clients may not have the necessary fonts.
RDoc::Text::TO_HTML_CHARACTERS[Encoding::UTF_8] =
RDoc::Text::TO_HTML_CHARACTERS[Encoding::ASCII]
less_html!
@store = store
@options = options
@base_dir = Pathname.pwd.expand_path
@dry_run = options.dry_run
@file_output = true
@template_dir = Pathname.new(File.join(File.dirname(__FILE__), 'oldweb'))
@template_cache = {}
@classes = nil
@context = nil
@files = nil
@methods = nil
@modsort = nil
@class_dir = nil
@file_dir = nil
@outputdir = nil
@old_vcs_url = nil
@git_tag = nil
# olddoc-specific stuff
# infer title from README
if options.title == 'RDoc Documentation' && File.readable?('README')
line = File.open('README') { |fp| fp.gets.strip }
line.sub!(/\A=+\s*/, '')
options.title = line
end
# load olddoc config
cfg = '.olddoc.yml'
if File.readable?(cfg)
@old_cfg = YAML.load(File.read(cfg))
else
@old_cfg = {}
warn "#{cfg} not readable"
end
%w(toc_max).each { |k| v = @old_cfg[k] and @old_cfg[k] = v.to_i }
@old_cfg['toc_max'] ||= 6
ni = {}
noindex = @old_cfg['noindex'] and noindex.each { |k| ni[k] = true }
@old_cfg['noindex'] = ni
cgit_url = Array(@old_cfg['cgit_url'])
source = @old_cfg['source_code'] ||= []
if source.empty?
source << "git clone #{cgit_url[0]}" if cgit_url[0]
git_url = @old_cfg['git_url']
if git_url && git_url != cgit_url[0]
source << "git clone #{git_url}"
end
end
if cgit_url[0]
cgit_url.each do |u|
u += '/tree/%s' # path name
tag = @git_tag and u << "id=#{CGI.escape(tag)}"
u << '#n%d' # lineno
end
@old_vcs_url = cgit_url[0]
end
@oldweb_style = nil # used by dark216
end
def generate # :nodoc:
setup
generate_class_files
generate_file_files
generate_table_of_contents
src = Dir["#@outputdir/README*.html"].first
begin
dst = "#@outputdir/index.html"
File.link(src, dst)
rescue SystemCallError
IO.copy_stream(src, dst)
end if src
end
def rel_path(out_file) # :nodoc:
rel_prefix = @outputdir.relative_path_from(out_file.dirname)
rel_prefix.to_s == '.' ? '' : "#{rel_prefix}/"
end
# called standalone by servelet
def generate_class(klass, template_file = nil) # :nodoc:
setup
current = klass
template_file ||= @template_dir + 'class.rhtml'
out_file = @outputdir + klass.path
rel_prefix = rel_path(out_file)
@title = "#{klass.type} #{klass.full_name}"
@suppress_warning = [ rel_prefix, current ]
render_template(template_file, out_file) { |io| binding }
end
# Generate a documentation file for each class and module
def generate_class_files # :nodoc:
setup
template_file = @template_dir + 'class.rhtml'
current = nil
@classes.each do |klass|
current = klass
generate_class(klass, template_file)
end
rescue => e
e!(e, "error generating #{current.path}: #{e.message} (#{e.class})")
end
# Generate a documentation file for each file
def generate_file_files # :nodoc:
setup
@files.each do |file|
generate_page(file) if file.text?
end
end
# Generate a page file for +file+
def generate_page(file, out_file = @outputdir + file.path) # :nodoc:
setup
template_file = @template_dir + 'page.rhtml'
rel_prefix = rel_path(out_file)
current = file
@title = "#{file.page_name} - #{@options.title}"
# use the first header as title instead of page_name if there is one
File.open("#{@options.root}/#{file.absolute_name}") do |f|
line = f.gets.strip
line.sub!(/^=+\s*/, '') and @title = line
end
@suppress_warning = [ current, rel_prefix ]
render_template(template_file, out_file) { |io| binding }
rescue => e
e!(e, "error generating #{out_file}: #{e.message} (#{e.class})")
end
# Generates the 404 page for the RDoc servlet
def generate_servlet_not_found(message) # :nodoc:
setup
template_file = @template_dir + 'servlet_not_found.rhtml'
rel_prefix = ''
@suppress_warning = rel_prefix
@title = 'Not Found'
render_template(template_file) { |io| binding }
rescue => e
e!(e, "error generating servlet_not_found: #{e.message} (#{e.class})")
end
# Generates the servlet root page for the RDoc servlet
def generate_servlet_root(installed) # :nodoc:
setup
template_file = @template_dir + 'servlet_root.rhtml'
rel_prefix = ''
@suppress_warning = rel_prefix
@title = 'Local RDoc Documentation'
render_template(template_file) { |io| binding }
rescue => e
e!(e, "error generating servlet_root: #{e.message} (#{e.class})")
end
def generate_table_of_contents # :nodoc:
setup
template_file = @template_dir + 'table_of_contents.rhtml'
out_file = @outputdir + 'table_of_contents.html'
rel_prefix = rel_path(out_file)
@suppress_warning = rel_prefix
@title = "Table of Contents - #{@options.title}"
render_template(template_file, out_file) { |io| binding }
rescue => e
e!(e, "error generating table_of_contents.html: #{e.message} (#{e.class})")
end
def setup # :nodoc:
return if @outputdir
@outputdir = Pathname.new(@options.op_dir).expand_path(@base_dir)
return unless @store
@classes = @store.all_classes_and_modules.sort
@files = @store.all_files.sort
@methods = @classes.flat_map(&:method_list).sort
@modsort = @classes.select(&:display?).sort
end
# Creates a template from its components and the +body_file+.
def assemble_template(body_file) # :nodoc:
body = body_file.read
head = @template_dir + '_head.rhtml'
tail = @template_dir + '_tail.rhtml'
"<html><head>#{head.read.strip}</head><body\n" \
"id=\"top\">#{body.strip}#{tail.read.strip}</body></html>"
end
# Renders the ERb contained in +file_name+ relative to the template
# directory and returns the result based on the current context.
def render(file_name) # :nodoc:
template_file = @template_dir + file_name
template = template_for(template_file, false, RDoc::ERBPartial)
template.filename = template_file.to_s
template.result(@context)
end
# Load and render the erb template in the given +template_file+ and write
# it out to +out_file+.
# Both +template_file+ and +out_file+ should be Pathname-like objects.
# An io will be yielded which must be captured by binding in the caller.
def render_template(template_file, out_file = nil) # :nodoc:
io_output = out_file && !@dry_run && @file_output
erb_klass = io_output ? RDoc::ERBIO : ERB
template = template_for(template_file, true, erb_klass)
if io_output
out_file.dirname.mkpath
out_file.open('w', 0644) do |io|
io.set_encoding(@options.encoding)
@context = yield io
template_result(template, @context, template_file)
end
else
@context = yield nil
template_result(template, @context, template_file)
end
end
# Creates the result for +template+ with +context+. If an error is raised a
# Pathname +template_file+ will indicate the file where the error occurred.
def template_result(template, context, template_file) # :nodoc:
template.filename = template_file.to_s
template.result(context)
rescue NoMethodError => e
e!(e, "Error while evaluating #{template_file.expand_path}: #{e.message}")
end
def template_for(file, page = true, klass = ERB) # :nodoc:
template = @template_cache[file]
return template if template
if page
template = assemble_template(file)
erbout = 'io'
else
template = file.read
template = template.encode(@options.encoding)
file_var = File.basename(file).sub(/\..*/, '')
erbout = "_erbout_#{file_var}"
end
if ERB.instance_method(:initialize).parameters.assoc(:key)
template = klass.new(template, trim_mode: '<>', eoutvar: erbout)
else
template = klass.new(template, nil, '<>', erbout)
end
@template_cache[file] = template
end
def e!(e, msg) # :nodoc:
raise RDoc::Error, msg, e.backtrace
end
def method_srclink(m) # :nodoc:
url = @old_vcs_url or return ""
line = m.line or return ""
path = CGI.escape(m.file_name)
%Q(<a href="#{url % [ path, line ]}">source</a>)
end
# reach into RDoc internals to generate less HTML
module LessHtml # :nodoc:
def accept_verbatim(verbatim)
@res << "\n<pre>#{verbatim.text.rstrip.encode(xml: :text)}</pre>\n"
end
def accept_heading(heading)
level = [6, heading.level].min
label = heading.label(@code_object)
@res << "<h#{level}"
@res << (@options.output_decoration ? "\nid=\"#{label}\">" : ">")
@res << to_html(heading.text)
@res << "</h#{level}>"
end
end
def less_html! # :nodoc:
klass = RDoc::Markup::ToHtml
return if klass.include?(Oldweb::LessHtml)
klass.__send__(:remove_method, :accept_heading)
klass.__send__(:remove_method, :accept_verbatim)
klass.__send__(:include, Oldweb::LessHtml)
end
end
# :startdoc:
|