Class: Asciidoctor::PDF::Rhrev::Converter

Inherits:
Object
  • Object
show all
Includes:
Exporter, Renderer, Rhrev::Helpers
Defined in:
lib/asciidoctor/rhrev/converter.rb

Instance Method Summary collapse

Methods included from Exporter

#build_export_description_xrefs, #convert_anchor_to_xref_for_export, #export_change_text, #export_change_with_bullets, #export_to_adoc_file, #should_export_to_file?

Methods included from Renderer

#add_custom_first_row_to_markup, #allocate_revision_history_extent, #build_cell, #build_consolidated_list, #build_description_xrefs, #build_list_row, #build_location_text, #build_table_via_parsing, #build_text_cell, #build_text_row, #build_xref_text, #check_if_numbered, #create_revision_table_properly, #format_as_list, #format_location_for_display, #format_with_role, #get_column_widths, #ink_prose, #ink_revision_history, #ink_revision_history_content, #is_initial_release?, #render_adoc_include, #render_initial_release_table, #resolve_pagerhrefs_in_table, #stamp_foreground_image

Methods included from Rhrev::Helpers

#antora_build?, #convert_anchor_to_xref, #debug_log, #format_prev_rev, #needs_asciidoc_cell?, #preprocess_attribute_content, #with_attribute_missing_suppressed

Constructor Details

#initialize(*args) ⇒ Converter

Returns a new instance of Converter.



23
24
25
26
27
28
29
30
31
32
# File 'lib/asciidoctor/rhrev/converter.rb', line 23

def initialize *args
  super
  @revision_history_extent = nil
  @revision_prefix = nil
  @rhrev_deferred_pages = nil
  @pagerhref_tables = []
  @export_completed = false
  @catalog = nil
  @manual_mode = false
end

Instance Method Details

#allocate_pagerhref_table_extent(node) ⇒ Object



345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/asciidoctor/rhrev/converter.rb', line 345

def allocate_pagerhref_table_extent node
  @rendering_deferred_table = true
  
  extent = dry_run onto: self do
    super_convert_table node
  end
  
  @rendering_deferred_table = false
  
  # Move cursor to after the allocated space
  extent.each_page { |first_page| start_new_page unless first_page }
  move_cursor_to extent.to.cursor
  
  # Store for later re-rendering with actual page numbers
  @pagerhref_tables << {
    node: node,
    extent: extent
  }
end

#catalog_block_anchor(node) ⇒ Object



288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/asciidoctor/rhrev/converter.rb', line 288

def catalog_block_anchor node
  @anchor_catalog ||= {}
  if node.id
    @anchor_catalog[node.id] ||= {
      title: node.title,
      context: node.context
    }
    page_num = page_number
    @anchor_catalog[node.id][:dest] = { page: page_num, y: cursor }
    
    revision_history.link_dest_to_page node.id, page_num, 1, y: cursor
  end
end

#collect_document_level_entries(doc) ⇒ Object



390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# File 'lib/asciidoctor/rhrev/converter.rb', line 390

def collect_document_level_entries doc
  return if @document_entries_collected
  @document_entries_collected = true
  
  (0..9).each do |major|
    (0..9).each do |minor|
      revision = "#{major}-#{minor}"
      prevrev_attr = "#{revision}-prevrev"
      
      if doc.attr(prevrev_attr)
        all_attr_name = "#{@revision_prefix}#{revision}-all"
        if (all_value = doc.attr(all_attr_name))
          revision_history.add_all_entry revision, all_value
        end
        
        cover_attr_name = "#{@revision_prefix}#{revision}-cover"
        if (cover_value = doc.attr(cover_attr_name))
          revision_history.add_cover_entry revision, cover_value
        end
      end
    end
  end
end

#collect_revision_entries(node) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/asciidoctor/rhrev/converter.rb', line 203

def collect_revision_entries node
  return if @prescan_complete
  return unless node.respond_to?(:attributes)
  
  is_document = node.class.to_s.include?('Document')
  return if is_document
  
  # Access raw attributes safely
  attr_entries = node.instance_variable_get(:@attributes) rescue {}
  return if attr_entries.nil? || attr_entries.empty?
  
  # Check for rhrev attributes before checking ID to debug missing IDs
  has_rhrev = attr_entries.keys.any? { |k| k.to_s.start_with?(@revision_prefix) }
  return unless has_rhrev
  
  has_id = node.id && !node.id.empty? rescue false
  unless has_id
    debug_log "Node #{node.context} has rhrev attributes but NO ID. Skipping. Attributes: #{attr_entries.keys.select{|k| k.to_s.start_with?(@revision_prefix)}}", @document
    return
  end
  
  debug_log "Found entry candidate on #{node.context} id=#{node.id}", @document
  
  attr_entries.each do |key, value|
    key_str = key.to_s
    next unless key_str.start_with?(@revision_prefix)
    next if key_str.include?('-all') || key_str.end_with?('-cover')
    
    revision = key_str.sub("#{@revision_prefix}", '')
    
    debug_log "Adding entry: Rev=#{revision}, Anchor=#{node.id}, Change=#{value}", @document
    
    revision_history.add_entry revision, node.id, value.to_s,
      reftext: nil,
      title: nil,
      sectnum: nil,
      context: node.context,
      is_chapter: false,
      sectname: nil,
      caption_number: nil,
      source_line: node.lineno
  end
end

#convert_document(node) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/asciidoctor/rhrev/converter.rb', line 38

def convert_document node
  @revision_prefix = node.attr 'revhistoryprefix', 'rhrev'
  
  # Check if we're in manual mode (pagerhref support needed)
  @manual_mode = node.attr('rhrev') == 'manual'
  
  # Initialize catalog
  @catalog = revision_history
  
  # Prescan document to populate catalog before rendering
  # This is necessary for accurate space allocation
  prescan_document node
  
  # Render document (this will visit all nodes and update entry metadata)
  result = super
  
  # Ink revision history if allocated
  if @revision_history_extent && @catalog
    ink_revision_history node, @revision_history_extent
  end
  
  # Re-render tables with pagerhrefs (only in manual mode)
  ink_pagerhref_tables if @manual_mode && @pagerhref_tables && !@pagerhref_tables.empty?
  
  # Export to file AFTER rendering (metadata like sectnum, caption_number are now populated)
  if node.attr?('rhrev-export-to-file') && @catalog
    export_to_adoc_file node
  end
  
  result
end

#convert_example(node) ⇒ Object



130
131
132
133
134
135
136
137
# File 'lib/asciidoctor/rhrev/converter.rb', line 130

def convert_example node
  # Skip collect during render if prescan already did it
  collect_revision_entries node unless @prescan_complete
   node
  result = super
  catalog_block_anchor node
  result
end

#convert_floating_title(node) ⇒ Object



148
149
150
151
152
153
154
155
# File 'lib/asciidoctor/rhrev/converter.rb', line 148

def convert_floating_title node
  # Skip collect during render if prescan already did it
  collect_revision_entries node unless @prescan_complete
   node
  result = super
  catalog_block_anchor node
  result
end

#convert_image(node) ⇒ Object



121
122
123
124
125
126
127
128
# File 'lib/asciidoctor/rhrev/converter.rb', line 121

def convert_image node
  # Skip collect during render if prescan already did it
  collect_revision_entries node unless @prescan_complete
   node
  result = super
  catalog_block_anchor node
  result
end

#convert_inline_quoted(node) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/asciidoctor/rhrev/converter.rb', line 166

def convert_inline_quoted node
  if node.text&.match?(/^\[pagerhref:/)
    if node.text =~ /^\[pagerhref:(.+)\]$/
      anchor = $1
      lookup_anchor = anchor.gsub('----', ':::')
      
      if @anchor_catalog && @anchor_catalog[lookup_anchor] && @anchor_catalog[lookup_anchor][:dest]
        page_num = @anchor_catalog[lookup_anchor][:dest][:page]
        %(<a anchor="#{lookup_anchor}"><span class="pagerhref">#{page_num}</span></a>)
      else
        if scratch?
          %(<a anchor="#{lookup_anchor}"><span class="pagerhref">99</span></a>)
        else
          %(<a anchor="#{lookup_anchor}"><span class="pagerhref">??</span></a>)
        end
      end
    else
      super
    end
  else
    super
  end
end

#convert_listing(node) ⇒ Object



139
140
141
142
143
144
145
146
# File 'lib/asciidoctor/rhrev/converter.rb', line 139

def convert_listing node
  # Skip collect during render if prescan already did it
  collect_revision_entries node unless @prescan_complete
   node
  result = super
  catalog_block_anchor node
  result
end

#convert_rhrev(node) ⇒ Object



157
158
159
160
161
162
163
164
# File 'lib/asciidoctor/rhrev/converter.rb', line 157

def convert_rhrev node
  return unless node.document.attr? 'rhrev'
  return if @revision_history_extent
  
  collect_document_level_entries node.document
  @revision_history_extent = allocate_revision_history_extent node.document
  nil
end

#convert_section(node, opts = {}) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/asciidoctor/rhrev/converter.rb', line 70

def convert_section node, opts = {}
  # Skip collect during render if prescan already did it
  collect_revision_entries node unless @prescan_complete
   node
  
  if node.id
    @anchor_catalog ||= {}
    @anchor_catalog[node.id] ||= {
      title: node.title,
      reftext: node.reftext,
      sectnum: node.sectnum,
      context: node.context,
      level: node.level
    }
    @anchor_catalog[node.id][:dest] = { page: page_number, y: cursor }
    
    # Link destination to page for revision history
    revision_history.link_dest_to_page node.id, page_number, 1, y: cursor
  end
  
  super
end

#convert_table(node) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/asciidoctor/rhrev/converter.rb', line 93

def convert_table node
  has_id = node.id && !node.id.empty? rescue false
  has_rhrev = false
  if node.respond_to?(:attributes) && node.attributes
    has_rhrev = node.attributes.keys.any? { |k| k.to_s.start_with?(@revision_prefix || 'rhrev') } rescue false
  end
  
  # Check for pagerhref macros in table (only in manual mode)
  # This check is expensive so we skip it unless in manual mode
  has_pagerhref = @manual_mode && !@rendering_deferred_table && table_contains_pagerhref?(node)
  
  if !has_id && !has_rhrev && !has_pagerhref
    return super
  end
  
  # Skip collect during render if prescan already did it
  collect_revision_entries node unless @prescan_complete
   node
  
  if has_pagerhref
    allocate_pagerhref_table_extent node
  else
    result = super
    catalog_block_anchor node
    result
  end
end

#ink_pagerhref_tablesObject



370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/asciidoctor/rhrev/converter.rb', line 370

def ink_pagerhref_tables
  return if scratch?
  
  @anchor_catalog ||= {}
  
  @pagerhref_tables.each do |table_info|
    extent = table_info[:extent]
    node = table_info[:node]
    
    # Go back to the allocated space
    go_to_page extent.from.page
    move_cursor_to extent.from.cursor
    
    # Re-render the table with flag set to prevent recursion
    @rendering_deferred_table = true
    super_convert_table node
    @rendering_deferred_table = false
  end
end

#prescan_document(doc) ⇒ Object



414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# File 'lib/asciidoctor/rhrev/converter.rb', line 414

def prescan_document doc
  debug_log "Starting prescan_document", doc
  
  # Suppress attribute missing warnings during prescan
  # This prevents counter attributes like {counter:tablecounter} from incrementing
  with_attribute_missing_suppressed doc do
    collect_document_level_entries doc
    
    # Use find_by with context filter - more efficient than single traversal
    # because find_by(context:) can skip non-matching subtrees
    [:section, :floating_title, :example, :listing, :table, :image].each do |ctx|
      doc.find_by(context: ctx).each do |node|
        collect_revision_entries node
      end
    end
    
    @prescan_complete = true
    debug_log "Prescan complete. Catalog has #{revision_history.entries.values.flatten.size} entries.", doc
  end
end

#revision_historyObject



34
35
36
# File 'lib/asciidoctor/rhrev/converter.rb', line 34

def revision_history
  @catalog ||= Catalog.new
end

#start_new_chapter(chapter) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/asciidoctor/rhrev/converter.rb', line 190

def start_new_chapter chapter
  if !@revision_history_extent && chapter.document && (chapter.document.attr? 'rhrev')
    rhrev_value = chapter.document.attr('rhrev')
    export_to_file = chapter.document.attr? 'rhrev-export-to-file'
    # Effectively only when rhrev is 'true', add the revision history section at the beginning of the document (after the title_page)
    if rhrev_value != 'macro' && rhrev_value != 'manual' && !export_to_file
      collect_document_level_entries chapter.document
      @revision_history_extent = allocate_revision_history_extent chapter.document
    end
  end
  super
end

#super_convert_table(node) ⇒ Object



365
366
367
368
# File 'lib/asciidoctor/rhrev/converter.rb', line 365

def super_convert_table node
  # Call the parent class method directly to avoid recursion
  self.class.superclass.instance_method(:convert_table).bind(self).call(node)
end

#table_contains_pagerhref?(node) ⇒ Boolean

Returns:

  • (Boolean)


302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/asciidoctor/rhrev/converter.rb', line 302

def table_contains_pagerhref? node
  return false unless node.context == :table
  
  # Check cell content WITHOUT triggering attribute substitution
  # This prevents counter attributes like {counter:tablecounter} from incrementing
  # We MUST NOT call cell.text or access cell.inner_document as those trigger parsing
  node.rows[:body].each do |row|
    row.each do |cell|
      # Check the raw @text instance variable if already evaluated
      if cell.instance_variable_defined?(:@text)
        raw_text = cell.instance_variable_get(:@text).to_s
        return true if raw_text.include?('pagerhref:')
      end
      # Check the inner document's source lines if available (before parsing)
      if cell.instance_variable_defined?(:@inner_document)
        inner_doc = cell.instance_variable_get(:@inner_document)
        if inner_doc && inner_doc.instance_variable_defined?(:@lines)
          lines = inner_doc.instance_variable_get(:@lines) || []
          return true if lines.any? { |l| l.to_s.include?('pagerhref:') }
        end
      end
      # Last resort: check cell's style attribute for AsciiDoc cells
      # AsciiDoc cells (a|) will have inner content that might contain pagerhref
      if cell.style == :asciidoc
        # For asciidoc cells, we need to check the source
        # Access the cell's source blocks if available
        if cell.instance_variable_defined?(:@inner_document)
          inner = cell.instance_variable_get(:@inner_document)
          if inner && inner.respond_to?(:blocks)
            inner.blocks.each do |block|
              if block.instance_variable_defined?(:@lines)
                lines = block.instance_variable_get(:@lines) || []
                return true if lines.any? { |l| l.to_s.include?('pagerhref:') }
              end
            end
          end
        end
      end
    end
  end
  false
end

#update_revision_entry_metadata(node) ⇒ Object



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/asciidoctor/rhrev/converter.rb', line 247

def  node
  return unless node.id
  return unless node.respond_to?(:attributes)
  
  with_attribute_missing_suppressed node.document do
    node.attributes.each do |key, value|
      key_str = key.to_s
      next unless key_str.start_with?(@revision_prefix)
      next if key_str.include?('-all') || key_str.end_with?('-cover')
      
      revision = key_str.sub("#{@revision_prefix}", '')
      
      entries = revision_history.entries[revision]
      next unless entries
      
      entry = entries.find { |e| e[:anchor] == node.id }
      next unless entry
      
      if entry[:change].to_s.empty?
        entry[:change] = value
      end
      
      entry[:reftext] = node.reftext
      entry[:title] = node.title
      entry[:sectnum] = node.respond_to?(:sectnum) ? node.sectnum : nil
      
      if node.context == :section && node.document.doctype == 'book'
        entry[:is_chapter] = (node.level == 0 || node.level == 1)
      end
      
      if node.respond_to?(:caption) && node.caption
        if node.caption =~ /(\d+)/
          entry[:caption_number] = $1
        end
      end
      
      entry[:sectname] = node.sectname if node.respond_to?(:sectname)
    end
  end
end