Skip to content

Commit 4b50423

Browse files
committed
Added RDoc::Servlet which serves HTML from ri data
Use `rdoc --server` to launch the HTML server. By default it runs on port 8214, but this can be overridden by providing a port. Support for pages and live search are currently missing. Added missing data to marshaled RDoc objects required for HTML rendering. Replaced RDoc::CodeObject#parent attribute with method for lookup from loaded code objects. RDoc::Context::Section now has multiple comments to allow merging and updating with an ri data store. RDoc::Markup::Document is now Enumerable. This allows duck-typing when merging comments in sections. RDoc::Generator::Darkfish can now generate output directly to a String. RDoc::Generator::Darkfish has been refactored to allow on-the-fly HTML construction for classes. Added RDoc::ERBPartial which allows ERB context reuse.
1 parent 6e3679d commit 4b50423

27 files changed

+1284
-176
lines changed

Diff for: lib/rdoc.rb

+2
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,12 @@ def self.load_yaml
135135

136136
autoload :CrossReference, 'rdoc/cross_reference'
137137
autoload :ERBIO, 'rdoc/erbio'
138+
autoload :ERBPartial, 'rdoc/erb_partial'
138139
autoload :Encoding, 'rdoc/encoding'
139140
autoload :Generator, 'rdoc/generator'
140141
autoload :Options, 'rdoc/options'
141142
autoload :Parser, 'rdoc/parser'
143+
autoload :Servlet, 'rdoc/servlet'
142144
autoload :RI, 'rdoc/ri'
143145
autoload :Stats, 'rdoc/stats'
144146
autoload :Store, 'rdoc/store'

Diff for: lib/rdoc/any_method.rb

+40-21
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ class RDoc::AnyMethod < RDoc::MethodAttr
77
# 2::
88
# RDoc 4
99
# Added calls_super
10+
# Added parent name and class
11+
# Added section title
1012

1113
MARSHAL_VERSION = 2 # :nodoc:
1214

@@ -107,6 +109,9 @@ def marshal_dump
107109
@params,
108110
@file.absolute_name,
109111
@calls_super,
112+
@parent.name,
113+
@parent.class,
114+
@section.title,
110115
]
111116
end
112117

@@ -117,36 +122,44 @@ def marshal_dump
117122
# * #full_name
118123
# * #parent_name
119124

120-
def marshal_load(array)
125+
def marshal_load array
121126
@dont_rename_initialize = nil
122127
@is_alias_for = nil
123128
@token_stream = nil
124129
@aliases = []
125-
126-
version = array[0]
127-
@name = array[1]
128-
@full_name = array[2]
129-
@singleton = array[3]
130-
@visibility = array[4]
131-
@comment = array[5]
132-
@call_seq = array[6]
133-
@block_params = array[7]
134-
# 8 handled below
135-
@params = array[9]
136-
# 10 handled below
137-
@calls_super = array[11]
130+
@parent = nil
131+
@parent_name = nil
132+
@parent_class = nil
133+
@section = nil
134+
@file = nil
135+
136+
version = array[0]
137+
@name = array[1]
138+
@full_name = array[2]
139+
@singleton = array[3]
140+
@visibility = array[4]
141+
@comment = array[5]
142+
@call_seq = array[6]
143+
@block_params = array[7]
144+
# 8 handled below
145+
@params = array[9]
146+
# 10 handled below
147+
@calls_super = array[11]
148+
@parent_name = array[12]
149+
@parent_title = array[13]
150+
@section_title = array[14]
138151

139152
array[8].each do |new_name, comment|
140153
add_alias RDoc::Alias.new(nil, @name, new_name, comment, @singleton)
141154
end
142155

143-
@parent_name = if @full_name =~ /#/ then
144-
$`
145-
else
146-
name = @full_name.split('::')
147-
name.pop
148-
name.join '::'
149-
end
156+
@parent_name ||= if @full_name =~ /#/ then
157+
$`
158+
else
159+
name = @full_name.split('::')
160+
name.pop
161+
name.join '::'
162+
end
150163

151164
@file = RDoc::TopLevel.new array[10] if version > 0
152165
end
@@ -219,6 +232,12 @@ def param_seq
219232
params
220233
end
221234

235+
def section # :nodoc:
236+
return @section if @section
237+
238+
@section = parent.add_section @section_title if parent
239+
end
240+
222241
##
223242
# Sets the store for this method and its referenced code objects.
224243

Diff for: lib/rdoc/class_module.rb

+94-15
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,13 @@ class RDoc::ClassModule < RDoc::Context
1414
# 2::
1515
# RDoc 3.13
1616
# * Added extends
17+
# 3::
18+
# RDoc 4.0
19+
# * Added sections
20+
# * Added in_files
21+
# * Added parent name
1722

18-
MARSHAL_VERSION = 2 # :nodoc:
23+
MARSHAL_VERSION = 3 # :nodoc:
1924

2025
##
2126
# Constants that are aliases for this class or module
@@ -133,6 +138,17 @@ def add_comment comment, location
133138
self.comment = original
134139
end
135140

141+
def add_things my_things, other_things # :nodoc:
142+
other_things.each do |group, things|
143+
my_things[group].each { |thing| yield false, thing } if
144+
my_things.include? group
145+
146+
things.each do |thing|
147+
yield true, thing
148+
end
149+
end
150+
end
151+
136152
##
137153
# Ancestors list for this ClassModule: the list of included modules
138154
# (classes will add their superclass if any).
@@ -241,8 +257,8 @@ def find_class_named name
241257
# Return the fully qualified name of this class or module
242258

243259
def full_name
244-
@full_name ||= if RDoc::ClassModule === @parent then
245-
"#{@parent.full_name}::#{@name}"
260+
@full_name ||= if RDoc::ClassModule === parent then
261+
"#{parent.full_name}::#{@name}"
246262
else
247263
@name
248264
end
@@ -285,7 +301,13 @@ def marshal_dump # :nodoc:
285301
method_types,
286302
extends.map do |ext|
287303
[ext.name, parse(ext.comment), ext.file_name]
288-
end
304+
end,
305+
@sections.values,
306+
@in_files.map do |tl|
307+
tl.absolute_name
308+
end,
309+
parent.full_name,
310+
parent.class,
289311
]
290312
end
291313

@@ -297,6 +319,8 @@ def marshal_load array # :nodoc:
297319
@parent = nil
298320
@temporary_section = nil
299321
@visibility = nil
322+
@classes = {}
323+
@modules = {}
300324

301325
@name = array[1]
302326
@full_name = array[2]
@@ -347,6 +371,23 @@ def marshal_load array # :nodoc:
347371
ext = add_extend RDoc::Extend.new(name, comment)
348372
ext.record_location RDoc::TopLevel.new file
349373
end if array[9] # Support Marshal version 1
374+
375+
sections = (array[10] || []).map do |section|
376+
[section.title, section]
377+
end
378+
379+
@sections = Hash[*sections.flatten]
380+
381+
add_section nil
382+
383+
@in_files = []
384+
385+
(array[11] || []).each do |filename|
386+
record_location RDoc::TopLevel.new filename
387+
end
388+
389+
@parent_name = array[12]
390+
@parent_class = array[13]
350391
end
351392

352393
##
@@ -415,6 +456,8 @@ def merge class_module
415456
end
416457
end
417458

459+
merge_sections cm
460+
418461
self
419462
end
420463

@@ -437,22 +480,46 @@ def merge_collections mine, other, other_files, &block # :nodoc:
437480
my_things = mine. group_by { |thing| thing.file }
438481
other_things = other.group_by { |thing| thing.file }
439482

440-
my_things.delete_if do |file, things|
441-
next false unless other_files.include? file
483+
remove_things my_things, other_files, &block
484+
add_things my_things, other_things, &block
485+
end
442486

443-
things.each do |thing|
444-
yield false, thing
445-
end
487+
##
488+
# Merges the comments in this ClassModule with the comments in the other
489+
# ClassModule +cm+.
446490

447-
true
491+
def merge_sections cm # :nodoc:
492+
my_sections = sections.group_by { |section| section.title }
493+
other_sections = cm.sections.group_by { |section| section.title }
494+
495+
other_files = cm.in_files
496+
497+
remove_things my_sections, other_files do |_, section|
498+
@sections.delete section.title
448499
end
449500

450-
other_things.each do |file, things|
451-
my_things[file].each { |thing| yield false, thing } if
452-
my_things.include?(file)
501+
other_sections.each do |group, sections|
502+
if my_sections.include? group
503+
my_sections[group].each do |my_section|
504+
other_section = cm.sections_hash[group]
453505

454-
things.each do |thing|
455-
yield true, thing
506+
my_comments = my_section.comments
507+
other_comments = other_section.comments
508+
509+
other_files = other_section.in_files
510+
511+
merge_collections my_comments, other_comments, other_files do |add, comment|
512+
if add then
513+
my_section.add_comment comment
514+
else
515+
my_section.remove_comment comment
516+
end
517+
end
518+
end
519+
else
520+
sections.each do |section|
521+
add_section group, section.comments
522+
end
456523
end
457524
end
458525
end
@@ -547,6 +614,18 @@ def remove_nodoc_children
547614
end
548615
end
549616

617+
def remove_things my_things, other_files # :nodoc:
618+
my_things.delete_if do |file, things|
619+
next false unless other_files.include? file
620+
621+
things.each do |thing|
622+
yield false, thing
623+
end
624+
625+
true
626+
end
627+
end
628+
550629
##
551630
# Search record used by RDoc::Generator::JsonIndex
552631

Diff for: lib/rdoc/code_object.rb

+34-9
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ class RDoc::CodeObject
7575
attr_accessor :offset
7676

7777
##
78-
# Our parent CodeObject
78+
# Sets the parent CodeObject
7979

80-
attr_accessor :parent
80+
attr_writer :parent
8181

8282
##
8383
# Did we ever receive a +:nodoc:+ directive?
@@ -105,12 +105,16 @@ class RDoc::CodeObject
105105
# Creates a new CodeObject that will document itself and its children
106106

107107
def initialize
108-
@metadata = {}
109-
@comment = ''
110-
@parent = nil
111-
@file = nil
112-
@full_name = nil
113-
@store = nil
108+
@metadata = {}
109+
@comment = ''
110+
@parent = nil
111+
@parent_name = nil # for loading
112+
@parent_class = nil # for loading
113+
@section = nil
114+
@section_title = nil # for loading
115+
@file = nil
116+
@full_name = nil
117+
@store = nil
114118

115119
@document_children = true
116120
@document_self = true
@@ -132,7 +136,6 @@ def comment=(comment)
132136
if comment and not comment.empty? then
133137
normalize_comment comment
134138
else
135-
# TODO is this sufficient?
136139
# HACK correct fix is to have #initialize create @comment
137140
# with the correct encoding
138141
if String === @comment and
@@ -265,6 +268,28 @@ def ignored?
265268
@ignored
266269
end
267270

271+
##
272+
# Our parent CodeObject
273+
274+
def parent
275+
return @parent if @parent
276+
return nil unless @parent_name
277+
278+
if @parent_class == RDoc::TopLevel then
279+
@parent = @store.add_file @parent_name
280+
else
281+
@parent = @store.find_class_or_module @parent_name
282+
283+
return @parent if @parent
284+
285+
begin
286+
@parent = @store.load_class @parent_name
287+
rescue RDoc::Store::MissingFileError
288+
nil
289+
end
290+
end
291+
end
292+
268293
##
269294
# File name of our parent
270295

Diff for: lib/rdoc/comment.rb

+8-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ class RDoc::Comment
2222

2323
attr_accessor :location
2424

25+
##
26+
# For duck-typing when merging classes at load time
27+
28+
alias file location # :nodoc:
29+
2530
##
2631
# The text for this comment
2732

@@ -143,7 +148,9 @@ def format= format
143148
end
144149

145150
def inspect # :nodoc:
146-
"#<%s:%x %s %p>" % [self.class, object_id, @location.absolute_name, @text]
151+
location = @location ? @location.absolute_name : '(unknown)'
152+
153+
"#<%s:%x %s %p>" % [self.class, object_id, location, @text]
147154
end
148155

149156
##

0 commit comments

Comments
 (0)