Class: Coradoc::AsciiDoc::Serializer::Formatter
- Inherits:
-
Object
- Object
- Coradoc::AsciiDoc::Serializer::Formatter
- Defined in:
- lib/coradoc/asciidoc/serializer/formatter.rb
Overview
Handles AsciiDoc-specific formatting for attributes and special structures
Class Method Summary collapse
-
.admonition(type, content) ⇒ String
Format an admonition.
-
.attribute_list(attrs) ⇒ String
Format an attribute list for AsciiDoc output.
-
.block_attributes(attrs) ⇒ String
Format block attributes (anchor, role, options, etc.).
-
.block_delimiter(type) ⇒ String
Format a block delimiter.
-
.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.
-
.list_marker(type, level = 1, _index = nil) ⇒ String
Format a list marker.
-
.section_heading(level, title, id = nil) ⇒ String
Format a section heading.
Class Method Details
.admonition(type, content) ⇒ String
Format an 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
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.)
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
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.
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
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
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 |