lib/asciidoctor/block.rb in asciidoctor-0.0.5 vs lib/asciidoctor/block.rb in asciidoctor-0.0.6

- old
+ new

@@ -7,18 +7,25 @@ # => ["<em>This</em> is a &lt;test&gt;"] class Asciidoctor::Block # Public: Get the Symbol context for this section block. attr_reader :context + # Public: Create alias for context to be consistent w/ AsciiDoc + alias :blockname :context + + # Public: Get the Hash of attributes for this block + attr_reader :attributes + # Public: Get the Array of sub-blocks for this section block. attr_reader :blocks # Public: Get/Set the original Array content for this section block. attr_accessor :buffer # Public: Get/Set the String section anchor name. attr_accessor :anchor + alias :id :anchor # Public: Get/Set the Integer block level (for nested elements, like # list elements). attr_accessor :level @@ -39,23 +46,37 @@ # elements). Would probably be better to pass in just the document. def initialize(parent, context, buffer=nil) @parent = parent @context = context @buffer = buffer - + @attributes = {} @blocks = [] end # Public: Get the Asciidoctor::Document instance to which this Block belongs def document return @document if @document @document = (@parent.is_a?(Asciidoctor::Document) ? @parent : @parent.document) end + def attr(name, default = nil) + default.nil? ? @attributes.fetch(name.to_s, self.document.attr(name)) : + @attributes.fetch(name.to_s, self.document.attr(name, default)) + end + + def attr?(name) + @attributes.has_key?(name.to_s) || self.document.attr?(name) + end + + def update_attributes(attributes) + @attributes.update(attributes) + end + # Public: Get the Asciidoctor::Renderer instance being used for the ancestor # Asciidoctor::Document instance. def renderer + # wouldn't @parent.renderer work here? I believe so document.renderer end # Public: Get the rendered String content for this Block. If the block # has child blocks, the content method should cause them to be @@ -126,52 +147,34 @@ # * bold, mono # * double/single quotes # * super/sub script def content - Asciidoctor.debug "For the record, buffer is:" - Asciidoctor.debug @buffer.inspect + #Asciidoctor.debug "For the record, buffer is:" + #Asciidoctor.debug @buffer.inspect case @context - when :dlist - @buffer.map do |dt, dd| - if !dt.anchor.nil? && !dt.anchor.empty? - html_dt = "<a id=#{dt.anchor}></a>" + htmlify(dt.content) - else - html_dt = htmlify(dt.content) - end - if dd.content.empty? - html_dd = '' - else - html_dd = "<p>#{htmlify(dd.content)}</p>" - end - html_dd += dd.blocks.map{|block| block.render}.join - - [html_dt, html_dd] - end - when :oblock, :quote + when :preamble, :oblock, :example, :sidebar blocks.map{|block| block.render}.join - when :olist, :colist + when :colist @buffer.map do |li| - htmlify(li.content) + li.blocks.map{|block| block.render}.join + htmlify(li.text) + li.blocks.map{|block| block.render}.join end - when :ulist - @buffer.map do |element| - if element.is_a? Asciidoctor::ListItem - element.content = sub_attributes(element.content) - end - # TODO - not sure why tests work the same whether or not this is commented out. - # I think that I am likely not yet testing unordered list items with no block - # content. Still and all, it seems like this should be all done by list_item.render . - element.render # + element.blocks.map{|block| block.render}.join - end + # lists get iterated in template + # list items recurse into this block when their text and content methods are called + when :ulist, :olist, :dlist + @buffer when :listing - @buffer.map{|l| CGI.escapeHTML(l).gsub(/(<\d+>)/,'<b>\1</b>')}.join + sub_special_chars(@buffer.join).gsub(/&lt;(\d+)&gt;/, '<b>\1</b>') when :literal - htmlify( @buffer.join.gsub( '*', '{asterisk}' ).gsub( '\'', '{apostrophe}' )) - when :verse - htmlify( sub_attributes(@buffer).map{ |l| l.strip }.join( "\n" ) ) + sub_special_chars(@buffer.join) + when :quote, :verse, :admonition + if !@buffer.nil? + htmlify(sub_attributes(@buffer).map{ |l| l.strip }.join( "\n" )) + else + blocks.map{|block| block.render}.join + end else lines = sub_attributes(@buffer).map do |line| line.strip line.gsub(Asciidoctor::REGEXP[:line_break], '\1{br-asciidoctor}') end @@ -193,13 +196,13 @@ result = lines.map do |line| Asciidoctor.debug "#{__method__} -> Processing line: #{line}" f = sub_special_chars(line) # gsub! doesn't have lookbehind, so we have to capture and re-insert f = f.gsub(/ (^|[^\\]) \{ (\w([\w\-_]+)?\w) \} /x) do - if self.document.defines.has_key?($2) - # Substitute from user defines first - $1 + self.document.defines[$2] + if self.document.attributes.has_key?($2) + # Substitute from user attributes first + $1 + self.document.attributes[$2] elsif Asciidoctor::INTRINSICS.has_key?($2) # Then do intrinsics $1 + Asciidoctor::INTRINSICS[$2] elsif Asciidoctor::HTML_ELEMENTS.has_key?($2) $1 + Asciidoctor::HTML_ELEMENTS[$2] @@ -213,11 +216,11 @@ end end Asciidoctor.debug "#{__method__} -> Processed line: #{f}" f end - Asciidoctor.debug "#{__method__} -> result looks like #{result.inspect}" + #Asciidoctor.debug "#{__method__} -> result looks like #{result.inspect}" result.reject! {|l| l =~ /\{ZZZZZ\}/} if return_string result = result.join end @@ -240,19 +243,33 @@ else $1 + "{#{$2}}" end end end - Asciidoctor.debug "#{__method__} -> result looks like #{result.inspect}" + #Asciidoctor.debug "#{__method__} -> result looks like #{result.inspect}" result.reject! {|l| l =~ /\{ZZZZZ\}/} if return_string result = result.join end result end + # Public: Append a sub-block to this section block + # + # block - The new sub-block. + # + # block = Block.new(parent, :preamble) + # + # block << Block.new(block, :paragraph, 'p1') + # block << Block.new(block, :paragraph, 'p2') + # block.blocks + # => ["p1", "p2"] + def <<(block) + @blocks << block + end + private # Private: Return a String HTML version of the source string, with # Asciidoc characters converted and HTML entities escaped. # @@ -282,10 +299,10 @@ "#{pre}link:#{url}[#{link}]" end html.gsub!(Asciidoctor::REGEXP[:biblio], '<a name="\1">[\1]</a>') - html.gsub!(Asciidoctor::REGEXP[:ruler], '<hr>\n') + html.gsub!(Asciidoctor::REGEXP[:ruler], "<hr>\n") html.gsub!(/``([^`']*)''/m, '&ldquo;\1&rdquo;') html.gsub!(/(?:\s|^)`([^`']*)'/m, '&lsquo;\1&rsquo;') # TODO: This text thus quoted is supposed to be rendered as an # "inline literal passthrough", meaning that it is rendered