Class: Coradoc::AsciiDoc::Serializer::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/coradoc/asciidoc/serializer/formatter.rb

Overview

Handles AsciiDoc-specific formatting for attributes and special structures

Class Method Summary collapse

Class Method Details

.admonition(type, content) ⇒ String

Format an admonition

Parameters:

  • type (String)

    Admonition type (NOTE, TIP, etc.)

  • content (String)

    Admonition content

Returns:

  • (String)

    Formatted admonition



92
93
94
# File 'lib/coradoc/asciidoc/serializer/formatter.rb', line 92

def admonition(type, content)
  "#{type.upcase}: #{content}"
end

.attribute_list(attrs) ⇒ String

Format an attribute list for AsciiDoc output

Parameters:

Returns:

  • (String)

    Formatted attribute list



12
13
14
15
16
# File 'lib/coradoc/asciidoc/serializer/formatter.rb', line 12

def attribute_list(attrs)
  return '' if attrs.nil?

  attrs.to_s
end

.block_attributes(attrs) ⇒ String

Format block attributes (anchor, role, options, etc.)

Parameters:

Returns:

  • (String)

    Formatted block attributes



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/coradoc/asciidoc/serializer/formatter.rb', line 21

def block_attributes(attrs)
  return '' if attrs.nil?
  return '' if attrs.is_a?(String) && attrs.empty?
  return '' if attrs.is_a?(Hash) && attrs.empty?

  # If it's an AttributeList model, use its serialization
  return attribute_list(attrs) if attrs.is_a?(Coradoc::AsciiDoc::Model::AttributeList)

  # Otherwise format as hash
  lines = []

  # Handle anchor if present
  if attrs[:id] || attrs['id']
    id = attrs[:id] || attrs['id']
    lines << "[[#{id}]]"
  end

  # Handle other attributes
  attr_parts = []
  attrs.each do |key, value|
    next if [:id, 'id'].include?(key)

    attr_parts << case key
                  when :role, 'role'
                    ".#{value}"
                  when :options, 'options'
                    "[#{value}]"
                  else
                    "#{key}=#{value}"
                  end
  end

  lines << "[#{attr_parts.join(',')}]" unless attr_parts.empty?
  lines.join("\n")
end

.block_delimiter(type) ⇒ String

Format a block delimiter

Parameters:

  • type (Symbol)

    Block type (:example, :sidebar, :quote, etc.)

Returns:

  • (String)

    Block delimiter



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/coradoc/asciidoc/serializer/formatter.rb', line 71

def block_delimiter(type)
  case type
  when :example
    '===='
  when :sidebar
    '****'
  when :quote
    '____'
  when :listing, :literal
    '----'
  when :passthrough
    '++++'
  else
    '----'
  end
end

.escape_text(text, escape_chars: [], pass_through: []) ⇒ String

Escape special AsciiDoc characters in text Works by prepending a backslash to all delimiter characters in the string that are adjacent to a whitespace.

Parameters:

  • text (String)

    Text to escape

  • escape_chars (Array<String>) (defaults to: [])

    Characters to escape by prepending backslash

  • pass_through (Array<String>) (defaults to: [])

    Characters to pass through using pass:[] macro

Returns:

  • (String)

    Escaped text



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/coradoc/asciidoc/serializer/formatter.rb', line 121

def escape_text(text, escape_chars: [], pass_through: [])
  return '' if text.nil?

  result = text.to_s.dup

  regex_chars = Regexp.escape(escape_chars.join)
  unless regex_chars.empty?
    result.gsub!(
      /((?<=\s)[#{regex_chars}]+)|([#{regex_chars}]+(?=\s))/
    ) do |match|
      match.chars.map { |c| "\\#{c}" }.join
    end
  end

  regex_pass = Regexp.escape(pass_through.join)
  result.gsub!(/([#{regex_pass}]+)/, '{pass:[\\1]}') unless regex_pass.empty?

  result
end

.list_marker(type, level = 1, _index = nil) ⇒ String

Format a list marker

Parameters:

  • type (Symbol)

    List type (:ordered, :unordered, :definition)

  • level (Integer) (defaults to: 1)

    Nesting level

  • index (Integer, nil)

    Item index for ordered lists

Returns:

  • (String)

    List marker



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/coradoc/asciidoc/serializer/formatter.rb', line 101

def list_marker(type, level = 1, _index = nil)
  case type
  when :ordered
    "#{'.' * level} "
  when :unordered
    "#{'*' * level} "
  when :definition
    ''
  else
    '* '
  end
end

.section_heading(level, title, id = nil) ⇒ String

Format a section heading

Parameters:

  • level (Integer)

    Section level (1-6)

  • title (String)

    Section title

  • id (String, nil) (defaults to: nil)

    Optional section ID

Returns:

  • (String)

    Formatted section heading



62
63
64
65
66
# File 'lib/coradoc/asciidoc/serializer/formatter.rb', line 62

def section_heading(level, title, id = nil)
  prefix = '=' * level
  anchor = id ? "[[#{id}]]\n" : ''
  "#{anchor}#{prefix} #{title}"
end