2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
|
|
|
# A Document containing lists, headings, paragraphs, etc.
|
|
|
|
|
|
|
|
class RDoc::Markup::Document
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
include Enumerable
|
|
|
|
|
2011-06-16 00:59:24 -04:00
|
|
|
##
|
|
|
|
# The file this document was created from. See also
|
|
|
|
# RDoc::ClassModule#add_comment
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
attr_reader :file
|
2011-06-16 00:59:24 -04:00
|
|
|
|
2012-12-05 17:20:15 -05:00
|
|
|
##
|
|
|
|
# If a heading is below the given level it will be omitted from the
|
|
|
|
# table_of_contents
|
|
|
|
|
|
|
|
attr_accessor :omit_headings_below
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
|
|
|
# The parts of the Document
|
|
|
|
|
|
|
|
attr_reader :parts
|
|
|
|
|
|
|
|
##
|
|
|
|
# Creates a new Document with +parts+
|
|
|
|
|
|
|
|
def initialize *parts
|
|
|
|
@parts = []
|
2012-11-26 23:28:14 -05:00
|
|
|
@parts.concat parts
|
2011-06-16 00:59:24 -04:00
|
|
|
|
|
|
|
@file = nil
|
2012-12-05 17:20:15 -05:00
|
|
|
@omit_headings_from_table_of_contents_below = nil
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Appends +part+ to the document
|
|
|
|
|
|
|
|
def << part
|
|
|
|
case part
|
|
|
|
when RDoc::Markup::Document then
|
|
|
|
unless part.empty? then
|
2012-11-26 23:28:14 -05:00
|
|
|
parts.concat part.parts
|
2010-04-01 03:45:16 -04:00
|
|
|
parts << RDoc::Markup::BlankLine.new
|
|
|
|
end
|
|
|
|
when String then
|
|
|
|
raise ArgumentError,
|
|
|
|
"expected RDoc::Markup::Document and friends, got String" unless
|
|
|
|
part.empty?
|
|
|
|
else
|
|
|
|
parts << part
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def == other # :nodoc:
|
2011-06-16 00:59:24 -04:00
|
|
|
self.class == other.class and
|
|
|
|
@file == other.file and
|
|
|
|
@parts == other.parts
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# Runs this document and all its #items through +visitor+
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
def accept visitor
|
|
|
|
visitor.start_accepting
|
|
|
|
|
2012-12-05 17:20:15 -05:00
|
|
|
visitor.accept_document self
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
visitor.end_accepting
|
|
|
|
end
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
##
|
|
|
|
# Concatenates the given +parts+ onto the document
|
|
|
|
|
|
|
|
def concat parts
|
|
|
|
self.parts.concat parts
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Enumerator for the parts of this document
|
|
|
|
|
|
|
|
def each &block
|
|
|
|
@parts.each(&block)
|
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# Does this document have no parts?
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
def empty?
|
2011-07-30 20:19:00 -04:00
|
|
|
@parts.empty? or (@parts.length == 1 and merged? and @parts.first.empty?)
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
##
|
|
|
|
# The file this Document was created from.
|
|
|
|
|
|
|
|
def file= location
|
|
|
|
@file = case location
|
|
|
|
when RDoc::TopLevel then
|
2012-11-27 03:54:03 -05:00
|
|
|
location.relative_name
|
2012-11-26 23:28:14 -05:00
|
|
|
else
|
|
|
|
location
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-06-16 00:59:24 -04:00
|
|
|
##
|
|
|
|
# When this is a collection of documents (#file is not set and this document
|
|
|
|
# contains only other documents as its direct children) #merge replaces
|
|
|
|
# documents in this class with documents from +other+ when the file matches
|
|
|
|
# and adds documents from +other+ when the files do not.
|
|
|
|
#
|
|
|
|
# The information in +other+ is preferred over the receiver
|
|
|
|
|
|
|
|
def merge other
|
2011-07-30 20:19:00 -04:00
|
|
|
if empty? then
|
|
|
|
@parts = other.parts
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2011-06-16 00:59:24 -04:00
|
|
|
other.parts.each do |other_part|
|
|
|
|
self.parts.delete_if do |self_part|
|
|
|
|
self_part.file and self_part.file == other_part.file
|
|
|
|
end
|
|
|
|
|
|
|
|
self.parts << other_part
|
|
|
|
end
|
|
|
|
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2011-07-30 20:19:00 -04:00
|
|
|
##
|
|
|
|
# Does this Document contain other Documents?
|
|
|
|
|
|
|
|
def merged?
|
|
|
|
RDoc::Markup::Document === @parts.first
|
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
def pretty_print q # :nodoc:
|
2011-06-16 00:59:24 -04:00
|
|
|
start = @file ? "[doc (#{@file}): " : '[doc: '
|
|
|
|
|
|
|
|
q.group 2, start, ']' do
|
2010-04-01 03:45:16 -04:00
|
|
|
q.seplist @parts do |part|
|
|
|
|
q.pp part
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Appends +parts+ to the document
|
|
|
|
|
|
|
|
def push *parts
|
2012-11-26 23:28:14 -05:00
|
|
|
self.parts.concat parts
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Returns an Array of headings in the document.
|
|
|
|
#
|
|
|
|
# Require 'rdoc/markup/formatter' before calling this method.
|
|
|
|
|
|
|
|
def table_of_contents
|
|
|
|
accept RDoc::Markup::ToTableOfContents.to_toc
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|