Module: Przn::Parser
- Defined in:
- lib/przn/parser.rb
Constant Summary collapse
- SIZE_SCALES =
Size names → Kitty text sizing scale (1-7)
{ 'xx-small' => 1, 'x-small' => 1, 'small' => 2, 'large' => 3, 'x-large' => 4, 'xx-large' => 5, 'xxx-large' => 6, 'xxxx-large' => 7, '1' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5, '6' => 6, '7' => 7, }.freeze
- NAMED_COLORS =
{ 'red' => 31, 'green' => 32, 'yellow' => 33, 'blue' => 34, 'magenta' => 35, 'cyan' => 36, 'white' => 37, 'bright_red' => 91, 'bright_green' => 92, 'bright_yellow' => 93, 'bright_blue' => 94, 'bright_magenta' => 95, 'bright_cyan' => 96, 'bright_white' => 97, }.freeze
Class Method Summary collapse
- .parse(markdown) ⇒ Object
-
.parse_font_attrs(str) ⇒ Object
HTML4-style <font face=“…” size=“…” color=“…”> attributes.
- .parse_image_attrs(str, attrs) ⇒ Object
-
.parse_inline(text) ⇒ Object
Parse inline text with Rabbit-compatible markup.
- .parse_slide(raw) ⇒ Object
- .parse_table(lines) ⇒ Object
-
.parse_xml_attrs(str) ⇒ Object
Generic attribute scanner — ‘key=“value”` pairs, returned as a hash with symbolized keys.
-
.split_slides(markdown) ⇒ Object
Split on h1 headings (Rabbit-compatible).
Class Method Details
.parse(markdown) ⇒ Object
30 31 32 33 |
# File 'lib/przn/parser.rb', line 30 def parse(markdown) = (markdown) Presentation.new(.map { |raw| (raw) }) end |
.parse_font_attrs(str) ⇒ Object
HTML4-style <font face=“…” size=“…” color=“…”> attributes. Kramdown’s name=“…” legacy spelling for the family is also accepted and folded into :face so the renderer has one shape to handle.
234 235 236 237 238 |
# File 'lib/przn/parser.rb', line 234 def parse_font_attrs(str) attrs = parse_xml_attrs(str) attrs[:face] = attrs.delete(:name) if attrs.key?(:name) && !attrs.key?(:face) attrs.slice(:face, :size, :color) end |
.parse_image_attrs(str, attrs) ⇒ Object
250 251 252 253 254 255 |
# File 'lib/przn/parser.rb', line 250 def parse_image_attrs(str, attrs) str = str.sub(/\A:?\s*/, '') str.scan(/([\w-]+)=['"]([^'"]*)['"]/) do |key, value| attrs[key.tr('-', '_')] = value end end |
.parse_inline(text) ⇒ Object
Parse inline text with Rabbit-compatible markup
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'lib/przn/parser.rb', line 269 def parse_inline(text) segments = [] scanner = StringScanner.new(text) until scanner.eos? if scanner.scan(/\{::tag\s+name="([^"]+)"\}(.*?)\{:\/tag\}/) segments << [:tag, scanner[2], scanner[1]] elsif scanner.scan(/<size=([^>\s]+)>(.*?)<\/size>/) segments << [:tag, scanner[2], scanner[1]] elsif scanner.scan(/<font((?:\s+\w+="[^"]+")+)\s*>(.*?)<\/font>/) segments << [:font, scanner[2], parse_font_attrs(scanner[1])] elsif scanner.scan(/\{::font((?:\s+\w+="[^"]+")+)\}(.*?)\{:\/font\}/) segments << [:font, scanner[2], parse_font_attrs(scanner[1])] elsif scanner.scan(/\{::note\}(.*?)\{:\/note\}/) segments << [:note, scanner[1]] elsif scanner.scan(/<note>(.*?)<\/note>/) segments << [:note, scanner[1]] elsif scanner.scan(/\{::wait\/\}/) || scanner.scan(/<wait\s*\/>/) # skip wait markers in inline text elsif scanner.scan(/</) segments << [:text, "<"] elsif scanner.scan(/>/) segments << [:text, ">"] elsif scanner.scan(/&/) segments << [:text, "&"] elsif scanner.scan(/`([^`]+)`/) segments << [:code, scanner[1]] elsif scanner.scan(/\*\*(.+?)\*\*/) segments << [:bold, scanner[1]] elsif scanner.scan(/\*(.+?)\*/) segments << [:italic, scanner[1]] elsif scanner.scan(/~~(.+?)~~/) segments << [:strikethrough, scanner[1]] else segments << [:text, scanner.scan(/[^`*~{<&]+|./)] end end segments end |
.parse_slide(raw) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/przn/parser.rb', line 56 def (raw) blocks = [] lines = raw.lines i = 0 while i < lines.size line = lines[i] case line # {::comment} ... {:/comment} block — skip entirely when /\A\s*\{::comment\}/ i += 1 i += 1 while i < lines.size && !lines[i].match?(/\{:\/comment\}/) # Block alignment: {:.center} or {:.right} when /\A\s*\{:\.(\w+)\}\s*\z/ blocks << {type: :align, align: Regexp.last_match(1).to_sym} # Block alignment, XML form: <center>content</center> or <right>content</right> when /\A\s*<(center|right)>(.*)<\/\1>\s*\z/ blocks << {type: :align, align: Regexp.last_match(1).to_sym} blocks << {type: :paragraph, content: Regexp.last_match(2)} # Slide background (Echoes OSC 7772): # <bg color="#..."/> — solid (bg-color) # <bg from="#..." to="#..." angle="N"/> — linear gradient (bg-gradient) when /\A\s*<bg((?:\s+\w+="[^"]+")*)\s*\/>\s*\z/ blocks << {type: :bg, attrs: parse_xml_attrs(Regexp.last_match(1))} # Fenced code block when /\A\s*```(\w*)\s*\z/ lang = Regexp.last_match(1) lang = nil if lang.empty? code_lines = [] i += 1 while i < lines.size && !lines[i].match?(/\A\s*```\s*\z/) code_lines << lines[i] i += 1 end # Check for kramdown IAL on next line: {: lang="ruby"} if (i + 1) < lines.size && lines[i + 1]&.match?(/\A\s*\{:/) i += 1 if lines[i].match(/lang="(\w+)"/) lang = Regexp.last_match(1) end end blocks << {type: :code_block, content: code_lines.join, language: lang} # Indented code block (4 spaces) when /\A {4}(.*)$/ code_lines = [Regexp.last_match(1)] while (i + 1) < lines.size && lines[i + 1].match?(/\A {4}/) i += 1 code_lines << lines[i].sub(/\A {4}/, '') end # Check for kramdown IAL: {: lang="ruby"} lang = nil if (i + 1) < lines.size && lines[i + 1]&.match?(/\A\s*\{:/) i += 1 if lines[i].match(/lang="(\w+)"/) lang = Regexp.last_match(1) end end blocks << {type: :code_block, content: code_lines.join("\n") + "\n", language: lang} # h1 (slide title) when /\A#\s+(.*)/ blocks << {type: :heading, level: 1, content: Regexp.last_match(1).strip} # h2-h6 (sub-headings within slide) when /\A(\#{2,6})\s+(.*)/ level = Regexp.last_match(1).size text = Regexp.last_match(2).strip blocks << {type: :heading, level: level, content: text} # Block quote when /\A>\s?(.*)/ quote_lines = [Regexp.last_match(1)] while (i + 1) < lines.size && (m = lines[i + 1].match(/\A>\s?(.*)/)) i += 1 quote_lines << m[1] end blocks << {type: :blockquote, content: quote_lines.join("\n")} # Table when /\A\|/ table_lines = [line.strip] while (i + 1) < lines.size && lines[i + 1].match?(/\A\|/) i += 1 table_lines << lines[i].strip end blocks << parse_table(table_lines) # Unordered list (* or - item) when /\A[*\-]\s+(.*)/ items = [] while i < lines.size && (lines[i].match?(/\A[*\-]\s+/) || lines[i].match?(/\A {2,}[*\-]\s+/) || lines[i].match?(/\A {2,}\S/)) if lines[i].match(/\A(\s*)[*\-]\s+(.*)/) depth = Regexp.last_match(1).size / 2 items << {text: Regexp.last_match(2), depth: depth} elsif lines[i].match(/\A {2,}(\S.*)/) # Continuation line items.last[:text] << " " << Regexp.last_match(1) if items.last else break end i += 1 end i -= 1 blocks << {type: :unordered_list, items: items} # Ordered list when /\A(\s*)\d+\.\s+(.*)/ items = [] while i < lines.size && lines[i].match?(/\A\s*\d+\.\s+/) lines[i].match(/\A(\s*)\d+\.\s+(.*)/) depth = Regexp.last_match(1).size / 3 items << {text: Regexp.last_match(2), depth: depth} i += 1 end i -= 1 blocks << {type: :ordered_list, items: items} # Image: {:attrs} when /\A!\[([^\]]*)\]\((\S+?)(?:\s+"([^"]*)")?\)(.*)/ alt = Regexp.last_match(1) path = Regexp.last_match(2) title = Regexp.last_match(3) rest = Regexp.last_match(4).strip attrs = {} if rest.match(/\{([^}]+)\}/) parse_image_attrs(Regexp.last_match(1), attrs) elsif rest.match(/\{(.+)/) || ((i + 1) < lines.size && lines[i + 1]&.match?(/\A\s*\{/)) attr_str = rest.sub(/\A\{:?\s*/, '') while !attr_str.include?('}') && (i + 1) < lines.size i += 1 attr_str << " " << lines[i].strip end attr_str = attr_str.sub(/\}\s*\z/, '') parse_image_attrs(attr_str, attrs) end blocks << {type: :image, path: path, alt: alt, title: title, attrs: attrs} # Definition list: term on one line, : definition on next when /\A(\S.*)\s*\z/ if (i + 1) < lines.size && lines[i + 1].match?(/\A:\s{3}/) term = Regexp.last_match(1).strip i += 1 definition_lines = [] while i < lines.size && lines[i].match?(/\A:\s{3}(.*)|\A {4}(.*)/) if lines[i].match(/\A:\s{3}(.*)/) definition_lines << Regexp.last_match(1) elsif lines[i].match(/\A {4}(.*)/) definition_lines << Regexp.last_match(1) end i += 1 end i -= 1 blocks << {type: :definition_list, term: term, definition: definition_lines.join("\n")} else blocks << {type: :paragraph, content: Regexp.last_match(1).strip} end when /\A\s*\z/ blocks << {type: :blank} else blocks << {type: :paragraph, content: line.strip} end i += 1 end Slide.new(blocks) end |
.parse_table(lines) ⇒ Object
257 258 259 260 261 262 263 264 265 266 |
# File 'lib/przn/parser.rb', line 257 def parse_table(lines) rows = [] lines.each do |line| next if line.match?(/\A\|[-|:\s]+\|\s*\z/) # separator row cells = line.split('|').map(&:strip).reject(&:empty?) rows << cells end {type: :table, header: rows.first, rows: rows.drop(1)} end |
.parse_xml_attrs(str) ⇒ Object
Generic attribute scanner — ‘key=“value”` pairs, returned as a hash with symbolized keys. Doesn’t validate which keys are allowed; callers slice.
242 243 244 245 246 247 248 |
# File 'lib/przn/parser.rb', line 242 def parse_xml_attrs(str) attrs = {} str.scan(/(\w+)="([^"]+)"/) do |key, value| attrs[key.to_sym] = value end attrs end |
.split_slides(markdown) ⇒ Object
Split on h1 headings (Rabbit-compatible)
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/przn/parser.rb', line 36 def (markdown) chunks = [] current = +"" in_fence = false markdown.each_line do |line| if line.match?(/\A\s*```/) in_fence = !in_fence current << line elsif !in_fence && line.match?(/\A#\s/) chunks << current unless current.strip.empty? current = +line else current << line end end chunks << current unless current.strip.empty? chunks end |