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/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, StructuralRules, TableCellBuilder, TableLayout, TextRules

Class Method Summary collapse

Class Method Details

.build_table_cell(format, content) ⇒ Object



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

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)



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

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



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

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



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

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



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

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)



148
149
150
# File 'lib/coradoc/asciidoc/transformer.rb', line 148

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).



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

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



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

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

.parse_cols_attribute(attrs) ⇒ Object



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

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).



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

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

.regroup_table_rows(rows, attrs = nil) ⇒ Object



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

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)



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

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