Class: Coradoc::Markdown::Parser::AstProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/coradoc/markdown/parser/ast_processor.rb

Overview

Post-processes the AST produced by BlockParser.

This processor handles:

  • Escape sequence processing (# -> #, * -> *, etc.)

  • Hard line break detection (two+ spaces at end of line)

  • Inline element parsing (emphasis, code spans, etc.)

Constant Summary collapse

ESCAPABLE_CHARS =

Characters that can be escaped in Markdown

%w[
  ! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \\ ] ^ _ ` { | } ~
].freeze

Class Method Summary collapse

Class Method Details

.apply_typography(text) ⇒ Object

Apply typography substitutions (Kramdown extension)

  • – to en-dash (–)

  • — to em-dash (—)

  • … to ellipsis (…)



68
69
70
71
72
73
74
75
76
# File 'lib/coradoc/markdown/parser/ast_processor.rb', line 68

def apply_typography(text)
  return text if text.nil?

  result = text.to_s
  # Order matters: --- before --
  result = result.gsub('---', '')  # em-dash
  result = result.gsub('--', '')   # en-dash
  result.gsub('...', '') # ellipsis
end

.extract_inline_elements(text) ⇒ Object

Extract inline Kramdown elements from text Returns an array of elements: text, footnote references, etc.



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
# File 'lib/coradoc/markdown/parser/ast_processor.rb', line 37

def extract_inline_elements(text)
  return [text] if text.nil? || text.empty?

  elements = []
  remaining = text

  # Pattern for footnote reference: [^name]
  fn_pattern = /\[\^([^\]]+)\]/

  until remaining.empty?
    match = remaining.match(fn_pattern)
    if match
      # Add text before the match
      elements << match.pre_match unless match.pre_match.empty?
      # Add the footnote reference
      elements << { fn_ref: match[1] }
      remaining = match.post_match
    else
      # No more matches - add remaining text
      elements << remaining
      break
    end
  end

  elements.length == 1 ? elements.first : elements
end

.process(ast, parse_inlines: true) ⇒ Array

Process the AST, applying all post-processing rules

Parameters:

  • ast (Array)

    The parsed AST from BlockParser

  • parse_inlines (Boolean) (defaults to: true)

    Whether to parse inline elements

Returns:

  • (Array)

    The processed AST



27
28
29
30
31
32
33
# File 'lib/coradoc/markdown/parser/ast_processor.rb', line 27

def process(ast, parse_inlines: true)
  return ast if ast.nil?

  result = process_node(ast)
  result = process_inlines(result) if parse_inlines
  result
end