Class: AsciidoctorDiagramLayout::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/asciidoctor_diagram_layout/parser.rb

Overview

Parses rowcol layout DSL text into a node tree.

Format: one directive per line. Containers (cols, rows) declare a nested block via indentation. Leaf zones use cell.

An optional size in parentheses sets a fixed percentage:

cell(30): Name
cols(40):

Examples:

Two-column layout

Parser.new.parse("cols:\n  cell: Sidebar\n  cell: Content\n")

Constant Summary collapse

LINE_PATTERN =

:nodoc:

/\A(?<keyword>cols|rows|cell)(?:\((?<size>\d+)\))?:(?<rest>.*)\z/i
TAB_WIDTH =

:nodoc:

4

Instance Method Summary collapse

Instance Method Details

#parse(dsl, implicit_direction = :rows) ⇒ ContainerNode

Parses a DSL string and returns a ContainerNode tree root.

Parameters:

  • dsl (String)

    layout DSL

  • implicit_direction (Symbol) (defaults to: :rows)

    :rows or :cols — fallback direction when the top-level is a bare cell

Returns:

Raises:

  • (ParseError)

    on syntax errors or empty input



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/asciidoctor_diagram_layout/parser.rb', line 29

def parse(dsl, implicit_direction = :rows)
  lines = dsl.split("\n", -1)
  state = ParseState.new(lines)
  roots = parse_children(state, 0)
  raise ParseError, "Empty input: expected at least one cell, rows, or cols" if roots.empty?
  if roots.size == 1 && roots.first.is_a?(ContainerNode)
    roots.first
  else
    ContainerNode.new(implicit_direction, :auto, roots)
  end
end