11module Asciidoctor
22class AbstractBlock < AbstractNode
3+ # Public: The types of content that this block can accomodate
4+ attr_accessor :content_model
5+
6+ # Public: Substitutions to be applied to content in this block
7+ attr_reader :subs
8+
9+ # Public: Get/Set the String name of the render template
10+ attr_accessor :template_name
11+
312 # Public: Get the Array of Asciidoctor::AbstractBlock sub-blocks for this block
413 attr_reader :blocks
514
@@ -9,19 +18,57 @@ class AbstractBlock < AbstractNode
918 # Public: Set the String block title.
1019 attr_writer :title
1120
21+ # Public: Get/Set the String style (block type qualifier) for this block.
22+ attr_accessor :style
23+
24+ # Public: Get/Set the caption for this block
25+ attr_accessor :caption
26+
1227 def initialize ( parent , context )
1328 super ( parent , context )
29+ @content_model = :compound
30+ @subs = [ ]
31+ @template_name = "block_#{ context } "
1432 @blocks = [ ]
1533 @id = nil
1634 @title = nil
35+ @caption = nil
36+ @style = nil
1737 if context == :document
1838 @level = 0
1939 elsif !parent . nil? && !self . is_a? ( Section )
2040 @level = parent . level
2141 else
2242 @level = nil
2343 end
24- @next_section_index = 0
44+ @next_section_index = 0
45+ @next_section_number = 1
46+ end
47+
48+ # Public: Get the rendered String content for this Block. If the block
49+ # has child blocks, the content method should cause them to be
50+ # rendered and returned as content that can be included in the
51+ # parent block's template.
52+ def render
53+ @document . playback_attributes @attributes
54+ renderer . render ( @template_name , self )
55+ end
56+
57+ # Public: Get an rendered version of the block content, rendering the
58+ # children appropriate to content model that this block supports.
59+ def content
60+ @blocks . map { |b | b . render } * EOL
61+ end
62+
63+ # Public: A convenience method that checks whether the specified
64+ # substitution is enabled for this block.
65+ #
66+ # name - The Symbol substitution name
67+ #
68+ # Returns A Boolean indicating whether the specified substitution is
69+ # enabled for this block
70+ def sub? name
71+ @subs . include? name
2572 end
2673
2774 # Public: A convenience method that indicates whether the title instance
@@ -54,30 +101,25 @@ def title
54101 end
55102 end
56103
57- # Public: Determine whether this Block contains block content
58- #
59- # returns Whether this Block has block content
60- #
61- #--
62- # TODO we still need another method that answers
63- # whether this Block *can* have block content
64- # that should be the option 'sectionbody'
65- def blocks?
66- !blocks . empty?
104+ # Public: Convenience method that returns the interpreted title of the Block
105+ # with the caption prepended.
106+ #
107+ # Concatenates the value of this Block's caption instance variable and the
108+ # return value of this Block's title method. No space is added between the
109+ # two values. If the Block does not have a caption, the interpreted title is
110+ # returned.
111+ #
112+ # Returns the String title prefixed with the caption, or just the title if no
113+ # caption is set
114+ def captioned_title
115+ %(#{ @caption } #{ title } )
67116 end
68117
69- # Public: Get the element at i in the array of blocks.
70- #
71- # i - The Integer array index number.
72- #
73- # section = Section.new
118+ # Public: Determine whether this Block contains block content
74119 #
75- # section << 'foo'
76- # section << 'bar'
77- # section[1]
78- # => "bar"
79- def []( i )
80- @blocks [ i ]
120+ # Returns A Boolean indicating whether this Block has block content
121+ def blocks?
122+ !@blocks . empty?
81123 end
82124
83125 # Public: Append a content block to this block's list of blocks.
@@ -86,98 +128,36 @@ def [](i)
86128 #
87129 # Examples
88130 #
89- # block = Block.new(parent, :preamble)
131+ # block = Block.new(parent, :preamble, :content_model => :compound )
90132 #
91- # block << Block.new(block, :paragraph, 'p1')
92- # block << Block.new(block, :paragraph, 'p2')
93- # block.blocks
94- # # => ["p1", "p2"]
133+ # block << Block.new(block, :paragraph, :source => 'p1')
134+ # block << Block.new(block, :paragraph, :source => 'p2')
135+ # block.blocks?
136+ # # => true
137+ # block.blocks.size
138+ # # => 2
95139 #
96140 # Returns nothing.
97141 def <<( block )
98- if block . is_a? ( Section )
99- assign_index ( block )
100- end
142+ # parent assignment pending refactor
143+ #block.parent = self
101144 @blocks << block
102145 end
103146
104- # Public: Insert a content block at the specified index in this block's
105- # list of blocks.
106- #
107- # i - The Integer array index number.
108- # val = The content block to insert.
109- #
110- # section = Section.new
111- #
112- # section << 'foo'
113- # section << 'baz'
114- # section.insert(1, 'bar')
115- # section.blocks
116- # ["foo", "bar", "baz"]
117- def insert ( i , block )
118- @blocks . insert ( i , block )
119- end
120-
121- # Public: Delete the element at i in the array of section blocks,
122- # returning that element or nil if i is out of range.
123- #
124- # i - The Integer array index number.
125- #
126- # section = Section.new
127- #
128- # section << 'foo'
129- # section << 'bar'
130- # section.delete_at(1)
131- # => "bar"
132- #
133- # section.blocks
134- # => ["foo"]
135- def delete_at ( i )
136- @blocks . delete_at ( i )
137- end
138-
139- # Public: Clear this Block's list of blocks.
140- #
141- # section = Section.new
142- #
143- # section << 'foo'
144- # section << 'bar'
145- # section.blocks
146- # => ["foo", "bar"]
147- # section.clear_blocks
148- # section.blocks
149- # => []
150- def clear_blocks
151- @blocks = [ ]
152- end
153-
154- # Public: Get the Integer number of blocks in this block
155- #
156- # Examples
157- #
158- # section = Section.new
159- #
160- # section.size
161- # => 0
162- #
163- # section << 'foo'
164- # section << 'bar'
165- # section.size
166- # => 2
167- def size
168- @blocks . size
169- end
170-
171147 # Public: Get the Array of child Section objects
172148 #
173149 # Only applies to Document and Section instances
174150 #
175151 # Examples
176152 #
177153 # section = Section.new(parent)
178- # section << Block.new(section, :paragraph, 'paragraph 1')
154+ # section << Block.new(section, :paragraph, :source => 'paragraph 1')
179155 # section << Section.new(parent)
180- # section << Block.new(section, :paragraph, 'paragraph 2')
156+ # section << Block.new(section, :paragraph, :source => 'paragraph 2')
157+ # section.blocks?
158+ # # => true
159+ # section.blocks.size
160+ # # => 3
181161 # section.sections.size
182162 # # => 1
183163 #
@@ -189,6 +169,55 @@ def sections
189169 }
190170 end
191171
172+ # Internal: Lock-in the substitutions for this block
173+ #
174+ # Looks for an attribute named "subs". If present, resolves the
175+ # substitutions and assigns it to the subs property on this block.
176+ # Otherwise, assigns a set of default substitutions based on the
177+ # content model of the block.
178+ #
179+ # Returns nothing
180+ def lock_in_subs
181+ default_subs = [ ]
182+ case @content_model
183+ when :simple
184+ default_subs = SUBS [ :normal ]
185+ when :verbatim
186+ if @context == :listing || ( @context == :literal && !( option? 'listparagraph' ) )
187+ default_subs = SUBS [ :verbatim ]
188+ else
189+ default_subs = SUBS [ :basic ]
190+ end
191+ when :raw
192+ default_subs = SUBS [ :pass ]
193+ else
194+ return
195+ end
196+
197+ if ( custom_subs = @attributes [ 'subs' ] )
198+ @subs = resolve_block_subs custom_subs , @context
199+ else
200+ @subs = default_subs . dup
201+ end
202+
203+ # QUESION delegate this logic to method?
204+ if @context == :listing && @style == 'source' && ( @document . basebackend? 'html' ) &&
205+ ( ( highlighter = @document . attributes [ 'source-highlighter' ] ) == 'coderay' ||
206+ highlighter == 'pygments' ) && ( attr? 'language' )
207+ @subs = @subs . map { |sub | sub == :specialcharacters ? :highlight : sub }
208+ end
209+ end
210+
211+ # Public: Remove a substitution from this block
212+ #
213+ # sub - The Symbol substitution name
214+ #
215+ # Returns nothing
216+ def remove_sub sub
217+ @subs . delete sub
218+ nil
219+ end
220+
192221 # Public: Generate a caption and assign it to this block if one
193222 # is not already assigned.
194223 #
@@ -211,15 +240,15 @@ def assign_caption(caption = nil, key = nil)
211240 end
212241
213242 if caption . nil?
214- if @document . attr? ( 'caption' )
215- @caption = @document . attr ( 'caption' )
243+ if @document . attributes . has_key? 'caption'
244+ @caption = @document . attributes [ 'caption' ]
216245 elsif title?
217246 key ||= @context . to_s
218247 caption_key = "#{ key } -caption"
219- if @document . attributes . has_key? ( caption_key )
248+ if @document . attributes . has_key? caption_key
220249 caption_title = @document . attributes [ "#{ key } -caption" ]
221250 caption_num = @document . counter_increment ( "#{ key } -number" , self )
222- @caption = @attributes [ 'caption' ] = "#{ caption_title } #{ caption_num } . "
251+ @caption = "#{ caption_title } #{ caption_num } . "
223252 end
224253 else
225254 @caption = caption
@@ -239,6 +268,10 @@ def assign_caption(caption = nil, key = nil)
239268 def assign_index ( section )
240269 section . index = @next_section_index
241270 @next_section_index += 1
271+ if section . numbered
272+ section . number = @next_section_number
273+ @next_section_number += 1
274+ end
242275 end
243276
244277 # Internal: Reassign the section indexes
@@ -250,6 +283,7 @@ def assign_index(section)
250283 # returns nothing
251284 def reindex_sections
252285 @next_section_index = 0
286+ @next_section_number = 0
253287 @blocks . each { |block |
254288 if block . is_a? ( Section )
255289 assign_index ( block )
0 commit comments