Class: Coradoc::AsciiDoc::Transformer

Inherits:
Parslet::Transform
  • Object
show all
Defined in:
lib/coradoc/asciidoc/transformer.rb,
lib/coradoc/asciidoc/transformer/list_rules.rb,
lib/coradoc/asciidoc/transformer/misc_rules.rb,
lib/coradoc/asciidoc/transformer/text_rules.rb,
lib/coradoc/asciidoc/transformer/block_rules.rb,
lib/coradoc/asciidoc/transformer/header_rules.rb,
lib/coradoc/asciidoc/transformer/inline_rules.rb,
lib/coradoc/asciidoc/transformer/table_layout.rb,
lib/coradoc/asciidoc/transformer/structural_rules.rb,
lib/coradoc/asciidoc/transformer/table_cell_builder.rb,
lib/coradoc/asciidoc/transformer/block_type_classifier.rb,
lib/coradoc/asciidoc/transformer/source_line_extractor.rb,
lib/coradoc/asciidoc/transformer/attribute_list_normalizer.rb

Overview

Parslet::Transform subclass that converts AST to AsciiDoc model objects.

This transformer uses a modular rule system where each group of rules is defined in a separate file for maintainability.

Rule modules (each autoloaded):

  • HeaderRules: Document header, author, revision
  • InlineRules: Inline formatting (bold, italic, etc.)
  • TextRules: Text elements and paragraphs
  • BlockRules: Block elements (example, admonition, etc.)
  • ListRules: List items and list types
  • StructuralRules: Sections, tables, documents
  • MiscRules: Comments, attributes, media elements

Defined Under Namespace

Modules: AttributeListNormalizer, BlockRules, BlockTypeClassifier, HeaderRules, InlineRules, ListRules, MiscRules, SourceLineExtractor, StructuralRules, TableCellBuilder, TableLayout, TextRules

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build_table_cell(format, content) ⇒ Object



95
96
97
# File 'lib/coradoc/asciidoc/transformer.rb', line 95

def self.build_table_cell(format, content)
  TableCellBuilder.build(format, content)
end

.extract_inline_content(data) ⇒ Object

Helper method for extracting inline content (used by InlineRules)



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/coradoc/asciidoc/transformer.rb', line 48

def self.extract_inline_content(data)
  if data.is_a?(Hash) && data.key?(:content)
    data[:content]
  elsif data.is_a?(Array)
    data.map do |item|
      if item.is_a?(Hash) && item.key?(:text)
        text = item[:text]
        if text.is_a?(Model::Base) && text.class.attributes.key?(:content)
          text.content
        elsif text.is_a?(Model::Base)
          text
        else
          text.to_s
        end
      else
        item
      end
    end
  else
    data
  end
end

.extract_simple_inline_content(data) ⇒ Object

Helper method for extracting simple inline content



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/coradoc/asciidoc/transformer.rb', line 72

def self.extract_simple_inline_content(data)
  if data.is_a?(Hash) && data.key?(:content)
    data[:content]
  elsif data.is_a?(Array)
    data.map do |item|
      item.is_a?(Hash) && item.key?(:text) ? item[:text].to_s : item
    end.join
  else
    data
  end
end

.group_cells_into_rows(cells, explicit_col_count = nil) ⇒ Object



103
104
105
# File 'lib/coradoc/asciidoc/transformer.rb', line 103

def self.group_cells_into_rows(cells, explicit_col_count = nil)
  TableLayout.group_cells_into_rows(cells, explicit_col_count)
end

.infer_column_count(cells) ⇒ Object



107
108
109
# File 'lib/coradoc/asciidoc/transformer.rb', line 107

def self.infer_column_count(cells)
  TableLayout.infer_column_count(cells)
end

.legacy_transform(syntax_tree) ⇒ Object

Deprecated.

Use transform instead

Legacy transform method (deprecated)



152
153
154
# File 'lib/coradoc/asciidoc/transformer.rb', line 152

def self.legacy_transform(syntax_tree)
  new.apply(syntax_tree)
end

.lines_to_text_elements(lines) ⇒ Object

Convert parser-output "lines" into an array of TextElement model objects. Each line is one of:

- { text: <Array or scalar>, line_break: <str> }
- any other shape (passed through unchanged)

Used by the paragraph and reviewer_note rules to share the same line-shape handling (DRY).



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/coradoc/asciidoc/transformer.rb', line 130

def self.lines_to_text_elements(lines)
  Array(lines).map do |line|
    next line unless line.is_a?(Hash) && line.key?(:text)

    text_content = line[:text]
    transformed = if text_content.is_a?(Array)
                    text_content.map do |item|
                      item.is_a?(Hash) ? new.apply(item) : item
                    end
                  else
                    text_content
                  end

    Model::TextElement.new(
      content: transformed,
      line_break: line[:line_break]
    )
  end
end

.parse_block_content(text) ⇒ Object



91
92
93
# File 'lib/coradoc/asciidoc/transformer.rb', line 91

def self.parse_block_content(text)
  TableCellBuilder.parse_block_content(text)
end

.parse_cols_attribute(attrs) ⇒ Object



99
100
101
# File 'lib/coradoc/asciidoc/transformer.rb', line 99

def self.parse_cols_attribute(attrs)
  TableLayout.parse_cols_attribute(attrs)
end

.parse_inline_content(text, style = nil) ⇒ Object

Helper method for parsing inline content from raw text. Kept as a thin delegator for backwards compatibility; implementation lives in TableCellBuilder (table cells are the primary consumer).



87
88
89
# File 'lib/coradoc/asciidoc/transformer.rb', line 87

def self.parse_inline_content(text, style = nil)
  TableCellBuilder.parse_inline_content(text, style)
end

.regroup_table_rows(rows, attrs = nil) ⇒ Object



111
112
113
# File 'lib/coradoc/asciidoc/transformer.rb', line 111

def self.regroup_table_rows(rows, attrs = nil)
  TableLayout.regroup_table_rows(rows, attrs)
end

.transform(syntax_tree) ⇒ Object

Transform a syntax tree using this transformer's rules

Parameters:

  • syntax_tree (Hash, Array)

    The AST from the parser

Returns:

  • (Object)

    The transformed model object(s)



119
120
121
# File 'lib/coradoc/asciidoc/transformer.rb', line 119

def self.transform(syntax_tree)
  new.apply(syntax_tree)
end

Instance Method Details

#call_on_match(bindings, block) ⇒ Object

Single deepening seam for source_line propagation. Parslet's transform pipeline funnels every rule block through call_on_match(bindings, block); overriding it lets us post- process the block's result and inject source_line from the matched bindings, so individual rules no longer need to call SourceLineExtractor.extract themselves (DRY — was 47 call sites across 7 rule files).

Safety:

* Only Model::Base results get an injection — Strings, Arrays,
and intermediate hashes pass through unchanged.
* Existing explicit source_line values are preserved — the
injection is fill-in-the-blank, never overwrite.
* No Slice in the bindings → SourceLineExtractor returns nil,
no injection (synthetic transformations stay clean).


171
172
173
174
175
176
177
178
179
# File 'lib/coradoc/asciidoc/transformer.rb', line 171

def call_on_match(bindings, block)
  result = super
  return result unless result.is_a?(Model::Base)
  return result if result.source_line

  line = self.class::SourceLineExtractor.extract(bindings)
  result.source_line = line if line
  result
end