2008-01-13 22:34:05 -05:00
|
|
|
require 'rdoc/markup'
|
|
|
|
require 'rdoc/markup/lines'
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2008-01-13 22:34:05 -05:00
|
|
|
class RDoc::Markup
|
2003-12-01 02:12:49 -05:00
|
|
|
|
|
|
|
##
|
|
|
|
# A Fragment is a chunk of text, subclassed as a paragraph, a list
|
2008-01-12 22:35:34 -05:00
|
|
|
# entry, or verbatim text.
|
2003-12-01 02:12:49 -05:00
|
|
|
|
|
|
|
class Fragment
|
|
|
|
attr_reader :level, :param, :txt
|
|
|
|
attr_accessor :type
|
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
##
|
|
|
|
# This is a simple factory system that lets us associate fragement
|
2008-01-18 19:06:19 -05:00
|
|
|
# types (a string) with a subclass of fragment
|
|
|
|
|
|
|
|
TYPE_MAP = {}
|
|
|
|
|
|
|
|
def self.type_name(name)
|
|
|
|
TYPE_MAP[name] = self
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.for(line)
|
|
|
|
klass = TYPE_MAP[line.type] ||
|
|
|
|
raise("Unknown line type: '#{line.type.inspect}:' '#{line.text}'")
|
|
|
|
return klass.new(line.level, line.param, line.flag, line.text)
|
|
|
|
end
|
|
|
|
|
2003-12-01 02:12:49 -05:00
|
|
|
def initialize(level, param, type, txt)
|
|
|
|
@level = level
|
|
|
|
@param = param
|
|
|
|
@type = type
|
|
|
|
@txt = ""
|
|
|
|
add_text(txt) if txt
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_text(txt)
|
|
|
|
@txt << " " if @txt.length > 0
|
|
|
|
@txt << txt.tr_s("\n ", " ").strip
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
"L#@level: #{self.class.name.split('::')[-1]}\n#@txt"
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# A paragraph is a fragment which gets wrapped to fit. We remove all
|
2008-01-12 22:35:34 -05:00
|
|
|
# newlines when we're created, and have them put back on output.
|
2003-12-01 02:12:49 -05:00
|
|
|
|
|
|
|
class Paragraph < Fragment
|
2008-01-18 19:06:19 -05:00
|
|
|
type_name :PARAGRAPH
|
2003-12-01 02:12:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
class BlankLine < Paragraph
|
2008-01-18 19:06:19 -05:00
|
|
|
type_name :BLANK
|
2003-12-01 02:12:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
class Heading < Paragraph
|
2008-01-18 19:06:19 -05:00
|
|
|
type_name :HEADING
|
2003-12-01 02:12:49 -05:00
|
|
|
|
|
|
|
def head_level
|
|
|
|
@param.to_i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# A List is a fragment with some kind of label
|
|
|
|
|
|
|
|
class ListBase < Paragraph
|
2008-01-18 19:06:19 -05:00
|
|
|
LIST_TYPES = [
|
|
|
|
:BULLET,
|
|
|
|
:NUMBER,
|
|
|
|
:UPPERALPHA,
|
|
|
|
:LOWERALPHA,
|
|
|
|
:LABELED,
|
|
|
|
:NOTE,
|
|
|
|
]
|
2003-12-01 02:12:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
class ListItem < ListBase
|
2008-01-18 19:06:19 -05:00
|
|
|
type_name :LIST
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2008-02-09 22:59:08 -05:00
|
|
|
def to_s
|
|
|
|
text = if [:NOTE, :LABELED].include? type then
|
|
|
|
"#{@param}: #{@txt}"
|
|
|
|
else
|
|
|
|
@txt
|
|
|
|
end
|
|
|
|
|
|
|
|
"L#@level: #{type} #{self.class.name.split('::')[-1]}\n#{text}"
|
|
|
|
end
|
|
|
|
|
2003-12-01 02:12:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
class ListStart < ListBase
|
|
|
|
def initialize(level, param, type)
|
|
|
|
super(level, param, type, nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class ListEnd < ListBase
|
|
|
|
def initialize(level, type)
|
|
|
|
super(level, "", type, nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Verbatim code contains lines that don't get wrapped.
|
|
|
|
|
|
|
|
class Verbatim < Fragment
|
2008-01-18 19:06:19 -05:00
|
|
|
type_name :VERBATIM
|
2003-12-01 02:12:49 -05:00
|
|
|
|
|
|
|
def add_text(txt)
|
|
|
|
@txt << txt.chomp << "\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# A horizontal rule
|
2008-01-12 22:35:34 -05:00
|
|
|
|
2003-12-01 02:12:49 -05:00
|
|
|
class Rule < Fragment
|
2008-01-18 19:06:19 -05:00
|
|
|
type_name :RULE
|
2003-12-01 02:12:49 -05:00
|
|
|
end
|
|
|
|
|
2008-01-12 22:35:34 -05:00
|
|
|
##
|
|
|
|
# Collect groups of lines together. Each group will end up containing a flow
|
2008-01-13 22:34:05 -05:00
|
|
|
# of text.
|
2003-12-01 02:12:49 -05:00
|
|
|
|
|
|
|
class LineCollection
|
2008-01-12 22:35:34 -05:00
|
|
|
|
2003-12-01 02:12:49 -05:00
|
|
|
def initialize
|
|
|
|
@fragments = []
|
|
|
|
end
|
|
|
|
|
|
|
|
def add(fragment)
|
|
|
|
@fragments << fragment
|
|
|
|
end
|
|
|
|
|
|
|
|
def each(&b)
|
|
|
|
@fragments.each(&b)
|
|
|
|
end
|
|
|
|
|
2008-01-12 22:35:34 -05:00
|
|
|
def to_a # :nodoc:
|
2003-12-01 02:12:49 -05:00
|
|
|
@fragments.map {|fragment| fragment.to_s}
|
|
|
|
end
|
|
|
|
|
2008-01-12 22:35:34 -05:00
|
|
|
##
|
2003-12-01 02:12:49 -05:00
|
|
|
# Factory for different fragment types
|
2008-01-12 22:35:34 -05:00
|
|
|
|
2003-12-01 02:12:49 -05:00
|
|
|
def fragment_for(*args)
|
|
|
|
Fragment.for(*args)
|
|
|
|
end
|
|
|
|
|
2008-01-12 22:35:34 -05:00
|
|
|
##
|
|
|
|
# Tidy up at the end
|
|
|
|
|
2003-12-01 02:12:49 -05:00
|
|
|
def normalize
|
|
|
|
change_verbatim_blank_lines
|
|
|
|
add_list_start_and_ends
|
|
|
|
add_list_breaks
|
|
|
|
tidy_blank_lines
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
@fragments.join("\n----\n")
|
|
|
|
end
|
|
|
|
|
|
|
|
def accept(am, visitor)
|
|
|
|
visitor.start_accepting
|
|
|
|
|
|
|
|
@fragments.each do |fragment|
|
|
|
|
case fragment
|
|
|
|
when Verbatim
|
|
|
|
visitor.accept_verbatim(am, fragment)
|
|
|
|
when Rule
|
|
|
|
visitor.accept_rule(am, fragment)
|
|
|
|
when ListStart
|
|
|
|
visitor.accept_list_start(am, fragment)
|
|
|
|
when ListEnd
|
|
|
|
visitor.accept_list_end(am, fragment)
|
|
|
|
when ListItem
|
|
|
|
visitor.accept_list_item(am, fragment)
|
|
|
|
when BlankLine
|
|
|
|
visitor.accept_blank_line(am, fragment)
|
|
|
|
when Heading
|
|
|
|
visitor.accept_heading(am, fragment)
|
|
|
|
when Paragraph
|
|
|
|
visitor.accept_paragraph(am, fragment)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
visitor.end_accepting
|
|
|
|
end
|
2008-01-12 22:35:34 -05:00
|
|
|
|
2003-12-01 02:12:49 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
# If you have:
|
|
|
|
#
|
|
|
|
# normal paragraph text.
|
|
|
|
#
|
|
|
|
# this is code
|
|
|
|
#
|
|
|
|
# and more code
|
|
|
|
#
|
2008-01-12 22:35:34 -05:00
|
|
|
# You'll end up with the fragments Paragraph, BlankLine, Verbatim,
|
|
|
|
# BlankLine, Verbatim, BlankLine, etc.
|
2003-12-01 02:12:49 -05:00
|
|
|
#
|
2008-01-12 22:35:34 -05:00
|
|
|
# The BlankLine in the middle of the verbatim chunk needs to be changed to
|
|
|
|
# a real verbatim newline, and the two verbatim blocks merged
|
|
|
|
|
2003-12-01 02:12:49 -05:00
|
|
|
def change_verbatim_blank_lines
|
|
|
|
frag_block = nil
|
|
|
|
blank_count = 0
|
|
|
|
@fragments.each_with_index do |frag, i|
|
|
|
|
if frag_block.nil?
|
|
|
|
frag_block = frag if Verbatim === frag
|
|
|
|
else
|
|
|
|
case frag
|
|
|
|
when Verbatim
|
|
|
|
blank_count.times { frag_block.add_text("\n") }
|
|
|
|
blank_count = 0
|
|
|
|
frag_block.add_text(frag.txt)
|
|
|
|
@fragments[i] = nil # remove out current fragment
|
|
|
|
when BlankLine
|
|
|
|
if frag_block
|
|
|
|
blank_count += 1
|
|
|
|
@fragments[i] = nil
|
|
|
|
end
|
|
|
|
else
|
|
|
|
frag_block = nil
|
|
|
|
blank_count = 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@fragments.compact!
|
|
|
|
end
|
|
|
|
|
2008-01-12 22:35:34 -05:00
|
|
|
##
|
|
|
|
# List nesting is implicit given the level of indentation. Make it
|
|
|
|
# explicit, just to make life a tad easier for the output processors
|
2003-12-01 02:12:49 -05:00
|
|
|
|
|
|
|
def add_list_start_and_ends
|
|
|
|
level = 0
|
|
|
|
res = []
|
|
|
|
type_stack = []
|
|
|
|
|
|
|
|
@fragments.each do |fragment|
|
|
|
|
# $stderr.puts "#{level} : #{fragment.class.name} : #{fragment.level}"
|
|
|
|
new_level = fragment.level
|
|
|
|
while (level < new_level)
|
|
|
|
level += 1
|
|
|
|
type = fragment.type
|
|
|
|
res << ListStart.new(level, fragment.param, type) if type
|
|
|
|
type_stack.push type
|
|
|
|
# $stderr.puts "Start: #{level}"
|
|
|
|
end
|
|
|
|
|
|
|
|
while level > new_level
|
|
|
|
type = type_stack.pop
|
|
|
|
res << ListEnd.new(level, type) if type
|
|
|
|
level -= 1
|
|
|
|
# $stderr.puts "End: #{level}, #{type}"
|
|
|
|
end
|
|
|
|
|
|
|
|
res << fragment
|
|
|
|
level = fragment.level
|
|
|
|
end
|
|
|
|
level.downto(1) do |i|
|
|
|
|
type = type_stack.pop
|
|
|
|
res << ListEnd.new(i, type) if type
|
|
|
|
end
|
|
|
|
|
|
|
|
@fragments = res
|
|
|
|
end
|
|
|
|
|
2008-01-12 22:35:34 -05:00
|
|
|
##
|
|
|
|
# Inserts start/ends between list entries at the same level that have
|
|
|
|
# different element types
|
2003-12-01 02:12:49 -05:00
|
|
|
|
|
|
|
def add_list_breaks
|
|
|
|
res = @fragments
|
|
|
|
|
|
|
|
@fragments = []
|
|
|
|
list_stack = []
|
|
|
|
|
|
|
|
res.each do |fragment|
|
|
|
|
case fragment
|
|
|
|
when ListStart
|
|
|
|
list_stack.push fragment
|
|
|
|
when ListEnd
|
|
|
|
start = list_stack.pop
|
|
|
|
fragment.type = start.type
|
|
|
|
when ListItem
|
|
|
|
l = list_stack.last
|
|
|
|
if fragment.type != l.type
|
|
|
|
@fragments << ListEnd.new(l.level, l.type)
|
|
|
|
start = ListStart.new(l.level, fragment.param, fragment.type)
|
|
|
|
@fragments << start
|
|
|
|
list_stack.pop
|
|
|
|
list_stack.push start
|
|
|
|
end
|
|
|
|
else
|
|
|
|
;
|
|
|
|
end
|
|
|
|
@fragments << fragment
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-01-12 22:35:34 -05:00
|
|
|
##
|
|
|
|
# Tidy up the blank lines:
|
2003-12-01 02:12:49 -05:00
|
|
|
# * change Blank/ListEnd into ListEnd/Blank
|
|
|
|
# * remove blank lines at the front
|
|
|
|
|
|
|
|
def tidy_blank_lines
|
|
|
|
(@fragments.size - 1).times do |i|
|
2008-02-09 22:59:08 -05:00
|
|
|
if BlankLine === @fragments[i] and ListEnd === @fragments[i+1] then
|
|
|
|
@fragments[i], @fragments[i+1] = @fragments[i+1], @fragments[i]
|
2003-12-01 02:12:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# remove leading blanks
|
|
|
|
@fragments.each_with_index do |f, i|
|
|
|
|
break unless f.kind_of? BlankLine
|
|
|
|
@fragments[i] = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
@fragments.compact!
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2008-01-12 22:35:34 -05:00
|
|
|
|
2003-12-01 02:12:49 -05:00
|
|
|
end
|
2008-01-12 22:35:34 -05:00
|
|
|
|