Module: Asciidoctor::PDF::Rhrev::Exporter

Included in:
Converter
Defined in:
lib/asciidoctor/rhrev/exporter.rb

Instance Method Summary collapse

Instance Method Details

#build_export_description_xrefs(xref_target, doc, entry = nil) ⇒ Object

Build description cell xrefs based on rhrev-description-xrefstyle



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/asciidoctor/rhrev/exporter.rb', line 154

def build_export_description_xrefs xref_target, doc, entry = nil
  xrefstyle_config = doc.attr 'rhrev-description-xrefstyle', ''
  styles = xrefstyle_config.split(/\s+/).reject(&:empty?)
  
  # Check if entry has reftext defined
  has_reftext = entry && entry[:reftext] && !entry[:reftext].to_s.empty?
  
  # Check if entry is numbered (sections with sectnum, or blocks with caption_number)
  is_numbered = entry && (entry[:sectnum] || entry[:caption_number])
  
  if styles.empty?
    # No xrefstyle specified - use default (empty brackets)
    "a|xref:#{xref_target}[]"
  elsif styles.length == 1
    # Single xrefstyle
    "a|xref:#{xref_target}[xrefstyle=#{styles[0]}]"
  elsif styles.join(' ') == 'short basic'
    # Special case: "short basic"
    # - Numbered items (sections with sectnum, blocks with caption) → ALWAYS output both
    #   e.g., "Section 1.1" + "Features", "Table 5" + "Pin Descriptions"
    # - Unnumbered items with reftext (floating titles) → output only basic
    #   to avoid duplication like "Register X Register X"
    if is_numbered
      # Numbered item - always use both styles
      xrefs = styles.map { |style| "xref:#{xref_target}[xrefstyle=#{style}]" }
      "a|#{xrefs.join(" \n")}"
    elsif has_reftext
      # Unnumbered with reftext - use basic only to avoid duplication
      "a|xref:#{xref_target}[xrefstyle=basic]"
    else
      # Unnumbered without reftext - use both styles
      xrefs = styles.map { |style| "xref:#{xref_target}[xrefstyle=#{style}]" }
      "a|#{xrefs.join(" \n")}"
    end
  else
    # Multiple xrefstyles - output each
    xrefs = styles.map { |style| "xref:#{xref_target}[xrefstyle=#{style}]" }
    "a|#{xrefs.join(" \n")}"
  end
end

#convert_anchor_to_xref_for_export(anchor, doc = nil) ⇒ Object

Convert Antora-style anchor to xref format for export e.g., “chapter-1:::section-1-1” -> “chapter-1.adoc#section-1-1”



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/asciidoctor/rhrev/exporter.rb', line 127

def convert_anchor_to_xref_for_export anchor, doc = nil
  return anchor unless anchor
  
  # Skip transformation for standalone asciidoctor-pdf builds
  return anchor if doc && !antora_build?(doc)
  
  # Auto-detect separator (---- for xml_ids, ::: for default)
  separator = if anchor.include?('----')
                '----'
              elsif anchor.include?(':::')
                ':::'
              else
                nil
              end
  
  if separator
    parts = anchor.split(separator, 2)
    page_id = parts[0]
    fragment = parts[1]
    "#{page_id}.adoc##{fragment}"
  else
    # No separator - just a page reference
    "#{anchor}.adoc"
  end
end

#export_change_text(output, change_text, is_all_or_cover: false) ⇒ Object

Export change text, matching embedded behavior for all/cover entries



196
197
198
199
200
201
202
203
204
205
# File 'lib/asciidoctor/rhrev/exporter.rb', line 196

def export_change_text output, change_text, is_all_or_cover: false
  lines = change_text.to_s.split(/\r?\n/).map(&:strip).reject(&:empty?)
  
  # Single line without asterisks = plain text
  if is_all_or_cover && lines.length == 1 && !lines[0].include?('*')
    output << lines[0]
  else
    export_change_with_bullets output, change_text
  end
end

#export_change_with_bullets(output, change_text) ⇒ Object

Export change text with proper bullet formatting



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
# File 'lib/asciidoctor/rhrev/exporter.rb', line 208

def export_change_with_bullets output, change_text
  return if change_text.to_s.empty?
  
  lines = change_text.to_s.split(/\r?\n/).map(&:strip).reject(&:empty?)
  
  lines.each do |line|
    # Split on inline asterisks (e.g., "Text. * Bullet one ** Sub-bullet")
    tokens = line.split(/\s(?=\*+\s)/)
    first = tokens.shift
    
    # First token becomes a bullet
    if first && !first.empty?
      first = first.strip
      if first.start_with?('*')
        output << first
      else
        output << "* #{first}"
      end
    end
    
    # Remaining tokens are already asterisk-prefixed
    tokens.each do |token|
      token = token.strip
      if token =~ /^(\*+)\s+(.*)/
        stars = $1
        content = $2.strip
        output << "#{stars} #{content}"
      end
    end
  end
end

#export_to_adoc_file(doc) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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
120
121
122
123
# File 'lib/asciidoctor/rhrev/exporter.rb', line 9

def export_to_adoc_file doc
  # Prevent double export
  return if @export_completed
  @export_completed = true
  
  export_attr = doc.attr('rhrev-export-to-file')
  adoc_output = doc.attr('rhrev-adoc-output')
  
  # Determine output filename
  # rhrev-export-to-file can be: true (use rhrev-adoc-output or default), or a filename
  if export_attr == true || export_attr == 'true' || export_attr == ''
    output_file = adoc_output || 'revhistory.adoc'
  else
    output_file = export_attr || adoc_output || 'revhistory.adoc'
  end
  
  return unless output_file
  
  # Make path relative to document directory if not absolute
  unless output_file.start_with?('/')
    doc_dir = doc.attr('docdir') || Dir.pwd
    output_file = File.join(doc_dir, output_file)
  end
  
  # Create directory if it doesn't exist
  output_dir = File.dirname(output_file)
  FileUtils.mkdir_p(output_dir) unless output_dir == '.' || File.directory?(output_dir)
  
  debug_log "Exporting revision history to: #{output_file}", doc
  
  output = []
  
  # Get column configuration
  column_widths = doc.attr 'rhrev-table-column-width', '30,70'
  
  output << "[cols='#{column_widths}']"
  output << "|==="
  
  # Get localization strings
  page_label = doc.attr 'rhrev-localization-page', 'Page'
  major_changes_text = doc.attr 'rhrev-localization-major-changes', 'Major changes since'
  all_text = doc.attr 'rhrev-localization-all', 'All'
  cover_text = doc.attr 'rhrev-localization-cover', 'Cover'
  
  # Get export format (pagerhref or xref)
  export_format = doc.attr 'rhrev-export-format', 'pagerhref'
  
  # Process each revision
  revision_history.sorted_revisions.each do |revision|
    prevrev_attr = "#{revision}-prevrev"
    prevrevdate_attr = "#{revision}-prevrevdate"
    revision_label = doc.attr('version-label', 'Revision')
    prev_title = doc.attr(prevrev_attr) || revision.tr('-', '.')
    prev_date = doc.attr(prevrevdate_attr) || ""
    prev_rev_text = format_prev_rev(prev_title, prev_date, doc)
    
    # Build header text, avoiding "Revision Revision" repetition
    if prev_title.strip.downcase.start_with?(revision_label.downcase)
      header_text = "*#{major_changes_text} #{prev_rev_text}*"
    else
      header_text = "*#{major_changes_text} #{revision_label} #{prev_rev_text}*"
    end
    
    output << ""
    output << "a|*#{page_label}*"
    output << "a|#{header_text}"
    
    # All entry
    if (all_change = revision_history.instance_variable_get(:@all_entries)[revision])
      output << ""
      output << "a|#{all_text}"
      output << "a|"
      export_change_text output, all_change, is_all_or_cover: true
    end
    
    # Cover entry
    if (cover_change = revision_history.instance_variable_get(:@cover_entries)[revision])
      next if doc.backend.to_s == 'html5'
      output << ""
      output << "a|#{cover_text}"
      output << "a|"
      export_change_text output, cover_change, is_all_or_cover: true
    end
    
    # Block entries - sort by sequence (source order in document)
    entries = revision_history.entries[revision] || []
    entries = entries.sort_by { |e| e[:sequence] || 0 }
    
    entries.each do |entry|
      page_num = entry[:dest] ? entry[:dest][:page] : '???'
      
      # Convert anchor to portable xref format for Antora export
      xref_anchor = convert_anchor_to_xref_for_export(entry[:anchor], doc)
      
      output << ""
      if export_format == 'pagerhref'
        # Use pagerhref macro for manual mode imports
        output << "a|pagerhref:#{xref_anchor}[]"
      else
        # Use xref syntax with page number
        output << "a|xref:#{xref_anchor}[#{page_num}]"
      end
      
      # Build description cell with xrefs based on rhrev-description-xrefstyle
      output << build_export_description_xrefs(xref_anchor, doc, entry)
      output << ""  # Blank line before bullet list for AsciiDoc parsing
      export_change_with_bullets output, entry[:change]
    end
  end
  
  output << "|==="
  
  File.write(output_file, output.join("\n"))
  debug_log("Exported revision history to #{output_file}", doc)
end

#should_export_to_file?(doc) ⇒ Boolean

Returns:

  • (Boolean)


5
6
7
# File 'lib/asciidoctor/rhrev/exporter.rb', line 5

def should_export_to_file? doc
  doc.attr?('rhrev-export-to-file') || doc.attr?('rhrev-adoc-output')
end