Class: Coradoc::AsciiDoc::Parser::Base

Inherits:
Parslet::Parser
  • Object
show all
Includes:
Model::Block, Model::Inline, Model::List, Citation, Content, Stem, Text, Model::Admonition, Model::AttributeList, Model::Bibliography, Model::DocumentAttributes, Model::Header, Model::Paragraph, Model::Section, Model::Table, Model::Term
Defined in:
lib/coradoc/asciidoc/parser/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Stem

#stem, #stem_type

Methods included from Text

#attr_name, #block_image, #comment_block, #comment_line, #date, #digit, #digits, #email, #empty_line, #endline, #eof?, #file_path, #include_directive, #keyword, #line_end, #line_ending, #line_start?, #newline, #newline_single, #rich_text, #rich_texts, #space, #space?, #special_character, #tag, #text, #word, #words

Methods included from Content

#asciidoc_char, #asciidoc_char_with_id, #element_id, #element_id_inline, #glossaries, #glossary, #list_prefix, #literal_space, #literal_space?, #page_break, #text_line

Methods included from Citation

#cross_reference, #xref_anchor, #xref_arg, #xref_str

Class Method Details

.config(key) ⇒ Object

Raises:

  • (ArgumentError)


102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/coradoc/asciidoc/parser/base.rb', line 102

def self.config(key)
  # NOTE: These are internal dispatch configuration options for the parser:
  # - add_dispatch: Enables automatic method dispatching
  # - with_params: Supports parameterized rule invocation
  c = {
    add_dispatch: true,
    with_params: true
  }

  raise ArgumentError, "Unknown config key: #{key}. Available keys: #{c.keys.join(', ')}" unless c.key?(key)

  c[key]
end

.parse(string) ⇒ Hash

Parse an AsciiDoc string

Parameters:

  • string (String)

    The Asciidoc string to parse

Returns:

  • (Hash)

    The parsed AST object



79
80
81
82
83
# File 'lib/coradoc/asciidoc/parser/base.rb', line 79

def self.parse(string)
  new.parse(string)
rescue Parslet::ParseFailed => e
  warn e.parse_failure_cause.ascii_tree
end

.parse_file(filename) ⇒ Hash

Parse an AsciiDoc file

Parameters:

  • filename (String)

    The filename of the Asciidoc file to parse

Returns:

  • (Hash)

    The parsed AST object



72
73
74
# File 'lib/coradoc/asciidoc/parser/base.rb', line 72

def self.parse_file(filename)
  parse(File.read(filename))
end

Instance Method Details

#rule_dispatch(rule_name, *args, **kwargs) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/coradoc/asciidoc/parser/base.rb', line 85

def rule_dispatch(rule_name, *args, **kwargs)
  @dispatch_data ||= {}
  dispatch_key = [rule_name, args, kwargs.to_a.sort]
  dispatch_hash = dispatch_key.hash.abs
  unless @dispatch_data.key?(dispatch_hash)
    alias_name = :"#{rule_name}_#{dispatch_hash}"
    Coradoc::AsciiDoc::Parser::Base.class_exec do
      rule(alias_name) do
        send(rule_name, *args, **kwargs)
      end
    end
    @dispatch_data[dispatch_hash] = alias_name
  end
  dispatch_method = @dispatch_data[dispatch_hash]
  send(dispatch_method)
end