Class: Mdlint::Parser::BlockParser
- Inherits:
-
Object
- Object
- Mdlint::Parser::BlockParser
- Defined in:
- lib/mdlint/parser/block_parser.rb,
sig/internal.rbs
Constant Summary collapse
- ATX_HEADING_REGEXP =
/\A {0,3}(\#{1,6})(?:\s+(.*))?$/- SETEXT_HEADING_REGEXP =
/\A {0,3}(=+|-+)\s*\z/- FENCE_OPEN_REGEXP =
/\A {0,3}(`{3,}|~{3,})(.*)\z/- BLOCKQUOTE_REGEXP =
/\A {0,3}> ?/- HR_REGEXP =
/\A {0,3}([-*_])(?:\s*\1){2,}\s*\z/- BULLET_LIST_REGEXP =
/\A( {0,3})([-*+])\s+/- ORDERED_LIST_REGEXP =
/\A( {0,3})(\d{1,9})([.)])\s+/- CODE_BLOCK_INDENT =
/\A {4}/- HTML_BLOCK_START_1 =
/\A {0,3}<(script|pre|style|textarea)(?:[\s>]|\z)/i- HTML_BLOCK_START_2 =
/\A {0,3}<!--/- HTML_BLOCK_START_3 =
/\A {0,3}<\?/- HTML_BLOCK_START_4 =
/\A {0,3}<![A-Z]/- HTML_BLOCK_START_5 =
/\A {0,3}<!\[CDATA\[/- HTML_BLOCK_START_6 =
/\A {0,3}<\/?(?:address|article|aside|base|basefont|blockquote|body|caption|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption|figure|footer|form|frame|frameset|h1|h2|h3|h4|h5|h6|head|header|hr|html|iframe|legend|li|link|main|menu|menuitem|nav|noframes|ol|optgroup|option|p|param|search|section|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul)(?:\s|\/?>|$)/i- HTML_BLOCK_START_7 =
/\A {0,3}<\/?[A-Za-z][A-Za-z0-9]*(?:\.[A-Za-z0-9_-]+)?(?:\s[^>]*>|\/?>)\s*\z/- REFERENCE_DEF_REGEXP =
Reference definition: [label]: url "title"
/\A {0,3}\[((?:\\.|[^\]])+)\]:/- FOOTNOTE_DEF_REGEXP =
/\A {0,3}\[\^([^\]]+)\]:\s*(.*?)\s*$/- MATH_BLOCK_REGEXP =
/\A {0,3}\$\$\s*$/
Instance Method Summary collapse
- #alert_attributes(content_lines) ⇒ Object
- #append_loose_paragraph(state, content, start_line) ⇒ Object
- #append_nested_tokens(state, nested_lines, start_line) ⇒ Object
- #block_boundary?(line) ⇒ Boolean
- #collect_loose_item_content(state, pattern, base_indent, nested_lines = nil, content_indent = base_indent + 2) ⇒ Object
- #dedent_list_line(line, base_indent) ⇒ String
- #fence_match(line) ⇒ Object
- #find_directive_closing_line(state, name) ⇒ Object
- #front_matter_markers(opener) ⇒ Object
- #front_matter_payload?(format, lines) ⇒ Boolean
- #html_block_start?(line) ⇒ Boolean
-
#initialize(options = {}) ⇒ BlockParser
constructor
A new instance of BlockParser.
- #lazy_blockquote_continuation?(line) ⇒ Boolean
- #mark_current_list_loose(state) ⇒ Object
- #normalize_reference_label(label) ⇒ String
- #parse(src) ⇒ Object
- #parse_atx_heading(state) ⇒ Object
- #parse_blank_line(state) ⇒ Object
- #parse_block(state) ⇒ Object
- #parse_blockquote(state) ⇒ Object
- #parse_bullet_list(state) ⇒ Object
- #parse_code_block(state) ⇒ Object
- #parse_directive(state) ⇒ Object
- #parse_fence(state) ⇒ Object
- #parse_footnote_definition(state) ⇒ Object
- #parse_front_matter(state) ⇒ Object
- #parse_hr(state) ⇒ Object
- #parse_html_block(state) ⇒ Object
- #parse_list_items(state, pattern, _marker, base_indent = 0) ⇒ Object
- #parse_math_block(state) ⇒ Object
- #parse_ordered_list(state) ⇒ Object
- #parse_ordered_list_items(state, delimiter, base_indent = 0) ⇒ Object
- #parse_paragraph(state) ⇒ Object
- #parse_reference_definition(state) ⇒ Object
- #parse_reference_tail(tail) ⇒ Object
- #parse_reference_title(value) ⇒ Object
- #parse_setext_heading(state) ⇒ Object
- #parse_table(state) ⇒ Object
- #reference_definition_candidate?(state) ⇒ Boolean
- #split_table_row(line) ⇒ Object
- #strip_code_indent(raw_line, expanded_line) ⇒ String
- #strip_fence_indent(raw_line, indent) ⇒ String
- #strip_task_marker(content) ⇒ Object
- #table_alignment(cell) ⇒ Object
- #table_delimiter?(line) ⇒ Boolean
- #table_row?(line) ⇒ Boolean
- #task_attributes(content) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ BlockParser
Returns a new instance of BlockParser.
29 30 31 32 |
# File 'lib/mdlint/parser/block_parser.rb', line 29 def initialize( = {}) @options = @dialect = Dialect.resolve([:dialect]) end |
Instance Method Details
#alert_attributes(content_lines) ⇒ Object
1074 1075 1076 1077 |
# File 'lib/mdlint/parser/block_parser.rb', line 1074 def alert_attributes(content_lines) match = content_lines.first.to_s.match(/\A\[!(NOTE|TIP|IMPORTANT|WARNING|CAUTION)\]\s*/i) match ? { alert: match[1].downcase } : {} end |
#append_loose_paragraph(state, content, start_line) ⇒ Object
763 764 765 766 767 768 769 |
# File 'lib/mdlint/parser/block_parser.rb', line 763 def append_loose_paragraph(state, content, start_line) return if content.to_s.empty? state.tokens << Token.new(type: :paragraph_open, tag: "p", nesting: 1, level: state.level, map: [start_line, state.line]) state.tokens << Token.new(type: :inline, content: content, level: state.level + 1, map: [start_line, state.line]) state.tokens << Token.new(type: :paragraph_close, tag: "p", nesting: -1, level: state.level) end |
#append_nested_tokens(state, nested_lines, start_line) ⇒ Object
709 710 711 712 713 714 715 716 717 718 |
# File 'lib/mdlint/parser/block_parser.rb', line 709 def append_nested_tokens(state, nested_lines, start_line) return if nested_lines.empty? nested_tokens = BlockParser.new(@options).parse(nested_lines.join("\n")) nested_tokens.each do |token| token.level += state.level token.map = token.map.map { |line| line + start_line } if token.map state.tokens << token end end |
#block_boundary?(line) ⇒ Boolean
1079 1080 1081 1082 1083 1084 |
# File 'lib/mdlint/parser/block_parser.rb', line 1079 def block_boundary?(line) line.match?(ATX_HEADING_REGEXP) || fence_match(line) || line.match?(HR_REGEXP) || line.match?(BLOCKQUOTE_REGEXP) || line.match?(BULLET_LIST_REGEXP) || line.match?(ORDERED_LIST_REGEXP) || line.match?(REFERENCE_DEF_REGEXP) || line.match?(FOOTNOTE_DEF_REGEXP) end |
#collect_loose_item_content(state, pattern, base_indent, nested_lines = nil, content_indent = base_indent + 2) ⇒ Object
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 |
# File 'lib/mdlint/parser/block_parser.rb', line 720 def collect_loose_item_content(state, pattern, base_indent, nested_lines = nil, content_indent = base_indent + 2) return unless state.blank_line? state.skip_blank_lines return if state.eof? next_match = state.current_line.match(pattern) if next_match && next_match[1].length <= base_indent mark_current_list_loose(state) return end return unless state.current_line.match?(/\A {#{content_indent},}/) mark_current_list_loose(state) if state.current_line.match?(/\A {#{base_indent + 4},}/) && nested_lines while !state.eof? if state.blank_line? index = state.line index += 1 while index < state.lines.length && state.blank_line?(index) break if index >= state.lines.length || !state.lines[index].match?(/\A\s+/) nested_lines << "" state.next_line elsif state.current_line.match?(/\A\s+/) nested_lines << dedent_list_line(state.current_line, base_indent) state.next_line else break end end return nil end content_lines = [] while !state.eof? && !state.blank_line? && !state.current_line.match?(pattern) break unless state.current_line.match?(/\A {#{content_indent},}/) content_lines << state.raw_line.sub(/\A\s+/, "") state.next_line end content_lines.join("\n").strip end |
#dedent_list_line(line, base_indent) ⇒ String
705 706 707 |
# File 'lib/mdlint/parser/block_parser.rb', line 705 def dedent_list_line(line, base_indent) line[(base_indent + 2)..] || line.lstrip end |
#fence_match(line) ⇒ Object
1117 1118 1119 1120 1121 1122 1123 |
# File 'lib/mdlint/parser/block_parser.rb', line 1117 def fence_match(line) match = line.to_s.match(FENCE_OPEN_REGEXP) return unless match return if match[1].start_with?("`") && match[2].include?("`") match end |
#find_directive_closing_line(state, name) ⇒ Object
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 |
# File 'lib/mdlint/parser/block_parser.rb', line 1086 def find_directive_closing_line(state, name) depth = 1 index = state.line + 1 while index < state.lines.length candidate = state.lines[index] nested = candidate.match(/\A {0,3}:::([A-Za-z][\w-]*)(?:\s+.*)?\s*\z/) if nested depth += 1 elsif candidate.strip == ":::" depth -= 1 return index if depth.zero? end index += 1 end nil end |
#front_matter_markers(opener) ⇒ Object
206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/mdlint/parser/block_parser.rb', line 206 def front_matter_markers(opener) case opener.strip when "---" [:yaml, "---"] when "+++" [:toml, "+++"] when ";;;" [:json, ";;;"] when "{" [:json, "}"] end end |
#front_matter_payload?(format, lines) ⇒ Boolean
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 |
# File 'lib/mdlint/parser/block_parser.rb', line 1125 def front_matter_payload?(format, lines) return false if lines.empty? || lines.all? { |line| line.strip.empty? } case format when :yaml lines.any? { |line| line.match?(/\A\s*[^#\s][^:]*:/) } when :toml lines.any? { |line| line.match?(/\A\s*[^#\s=]+\s*=/) } when :json lines.join.match?(/[{}\[\]]|\A\s*\"[^\"]+\"\s*:/) else false end end |
#html_block_start?(line) ⇒ Boolean
838 839 840 841 |
# File 'lib/mdlint/parser/block_parser.rb', line 838 def html_block_start?(line) [HTML_BLOCK_START_1, HTML_BLOCK_START_2, HTML_BLOCK_START_3, HTML_BLOCK_START_4, HTML_BLOCK_START_5, HTML_BLOCK_START_6, HTML_BLOCK_START_7].any? { |pattern| line.match?(pattern) } end |
#lazy_blockquote_continuation?(line) ⇒ Boolean
843 844 845 846 847 848 |
# File 'lib/mdlint/parser/block_parser.rb', line 843 def lazy_blockquote_continuation?(line) !line.match?(ATX_HEADING_REGEXP) && !fence_match(line) && !line.match?(HR_REGEXP) && !line.match?(BLOCKQUOTE_REGEXP) && !line.match?(BULLET_LIST_REGEXP) && !line.match?(ORDERED_LIST_REGEXP) && !html_block_start?(line) && !line.match?(REFERENCE_DEF_REGEXP) && !line.match?(FOOTNOTE_DEF_REGEXP) end |
#mark_current_list_loose(state) ⇒ Object
771 772 773 774 775 776 777 778 779 780 |
# File 'lib/mdlint/parser/block_parser.rb', line 771 def mark_current_list_loose(state) list_index = state.tokens.rindex { |token| %i[bullet_list_open ordered_list_open].include?(token.type) } return unless list_index state.tokens[list_index].attrs[:tight] = false state.tokens[(list_index + 1)..].to_a.reverse_each do |token| break if %i[bullet_list_open ordered_list_open].include?(token.type) token.attrs[:tight] = false if token.type == :list_item_open end end |
#normalize_reference_label(label) ⇒ String
934 935 936 |
# File 'lib/mdlint/parser/block_parser.rb', line 934 def normalize_reference_label(label) label.to_s.gsub(/\\([!"#$%&'()*+,\-.\/:;<=>?@\[\\\]^_`{|}~])/, '\\1').gsub(/\s+/, " ").strip.downcase end |
#parse(src) ⇒ Object
34 35 36 37 38 39 40 41 42 |
# File 'lib/mdlint/parser/block_parser.rb', line 34 def parse(src) state = State.new(src) until state.eof? parse_block(state) end state.tokens end |
#parse_atx_heading(state) ⇒ Object
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/mdlint/parser/block_parser.rb', line 226 def parse_atx_heading(state) line = state.current_line match = line.match(ATX_HEADING_REGEXP) return false unless match level = match[1].length content = match[2].to_s.gsub(/\s+#+\s*\z/, "").strip content = "" if content.match?(/\A#+\s*\z/) start_line = state.line state.tokens << Token.new( type: :heading_open, tag: "h#{level}", nesting: 1, level: state.level, markup: "#" * level, map: [start_line, start_line + 1] ) inline_token = Token.new( type: :inline, content: content, level: state.level + 1, map: [start_line, start_line + 1] ) state.tokens << inline_token state.tokens << Token.new( type: :heading_close, tag: "h#{level}", nesting: -1, level: state.level, markup: "#" * level ) state.next_line true end |
#parse_blank_line(state) ⇒ Object
219 220 221 222 223 224 |
# File 'lib/mdlint/parser/block_parser.rb', line 219 def parse_blank_line(state) return false unless state.blank_line? state.next_line true end |
#parse_block(state) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/mdlint/parser/block_parser.rb', line 46 def parse_block(state) return if state.eof? parse_front_matter(state) || parse_blank_line(state) || parse_atx_heading(state) || parse_directive(state) || parse_math_block(state) || parse_fence(state) || parse_hr(state) || parse_blockquote(state) || parse_bullet_list(state) || parse_ordered_list(state) || parse_table(state) || parse_html_block(state) || parse_footnote_definition(state) || parse_reference_definition(state) || parse_code_block(state) || parse_setext_heading(state) || parse_paragraph(state) end |
#parse_blockquote(state) ⇒ Object
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 |
# File 'lib/mdlint/parser/block_parser.rb', line 369 def parse_blockquote(state) line = state.current_line return false unless line.match?(BLOCKQUOTE_REGEXP) start_line = state.line content_lines = [] while !state.eof? if state.current_line.match?(BLOCKQUOTE_REGEXP) content_lines << state.raw_line.sub(BLOCKQUOTE_REGEXP, "") state.next_line elsif !state.blank_line? && lazy_blockquote_continuation?(state.current_line) content_lines << state.raw_line.sub(/\A\s+/, "") state.next_line else break end end state.tokens << Token.new( type: :blockquote_open, tag: "blockquote", nesting: 1, level: state.level, markup: ">", attrs: alert_attributes(content_lines), map: [start_line, state.line] ) content_lines[0] = content_lines[0].sub(/\A\[!(?:NOTE|TIP|IMPORTANT|WARNING|CAUTION)\]\s*/i, "") if alert_attributes(content_lines)[:alert] state.level += 1 inner_content = content_lines.join("\n") inner_parser = BlockParser.new(@options) inner_tokens = inner_parser.parse(inner_content) inner_tokens.each do |token| token.level += state.level if token.map token.map = token.map.map { |l| l + start_line } end state.tokens << token end state.level -= 1 state.tokens << Token.new( type: :blockquote_close, tag: "blockquote", nesting: -1, level: state.level, markup: ">" ) true end |
#parse_bullet_list(state) ⇒ Object
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 |
# File 'lib/mdlint/parser/block_parser.rb', line 426 def parse_bullet_list(state) line = state.current_line match = line.match(BULLET_LIST_REGEXP) return false unless match marker = match[2] start_line = state.line state.tokens << Token.new( type: :bullet_list_open, tag: "ul", nesting: 1, level: state.level, markup: marker, attrs: { tight: true }, map: [start_line, nil] ) list_token_index = state.tokens.length - 1 state.level += 1 parse_list_items(state, BULLET_LIST_REGEXP, marker, match[1].length) state.level -= 1 state.tokens[list_token_index].map[1] = state.line state.tokens << Token.new( type: :bullet_list_close, tag: "ul", nesting: -1, level: state.level, markup: marker ) true end |
#parse_code_block(state) ⇒ Object
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 |
# File 'lib/mdlint/parser/block_parser.rb', line 850 def parse_code_block(state) return false unless state.current_line.match?(CODE_BLOCK_INDENT) start_line = state.line content_lines = [] while !state.eof? if state.current_line.match?(CODE_BLOCK_INDENT) content_lines << strip_code_indent(state.raw_line, state.current_line) state.next_line elsif state.blank_line? && state.line < state.lines.length - 1 content_lines << "" state.next_line else break end end content_lines.pop while content_lines.last == "" state.tokens << Token.new( type: :code_block, tag: "code", content: content_lines.join("\n") + "\n", map: [start_line, state.line] ) true end |
#parse_directive(state) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/mdlint/parser/block_parser.rb', line 68 def parse_directive(state) line = state.current_line match = line.match(/\A {0,3}:::([A-Za-z][\w-]*)(?:\s+(.*?))?\s*\z/) return false unless match closing_line = find_directive_closing_line(state, match[1]) return false unless closing_line start_line = state.line content = state.raw_lines[start_line..closing_line].join("\n") content += "\n" if closing_line < state.lines.length - 1 || state.src.end_with?("\n") state.tokens << Token.new( type: :directive, content: content, meta: { name: match[1], title: match[2] }, map: [start_line, closing_line + 1] ) state.line = closing_line + 1 true end |
#parse_fence(state) ⇒ Object
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
# File 'lib/mdlint/parser/block_parser.rb', line 314 def parse_fence(state) line = state.current_line match = fence_match(line) return false unless match marker = match[1] info = match[2].strip fence_char = marker[0] fence_length = marker.length opening_indent = line[/\A */].length start_line = state.line state.next_line content_lines = [] until state.eof? current = state.current_line break if state.line == state.lines.length - 1 && current.empty? close_match = current.match(/\A {0,3}#{fence_char}{#{fence_length},}\s*\z/) if close_match state.next_line break end content_lines << strip_fence_indent(state.raw_line, opening_indent) state.next_line end state.tokens << Token.new( type: :fence, tag: "code", content: content_lines.join("\n") + (content_lines.any? ? "\n" : ""), markup: marker, info: info, map: [start_line, state.line] ) true end |
#parse_footnote_definition(state) ⇒ Object
993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 |
# File 'lib/mdlint/parser/block_parser.rb', line 993 def parse_footnote_definition(state) match = state.current_line.match(FOOTNOTE_DEF_REGEXP) return false unless match start_line = state.line raw_lines = [state.current_line] content = match[2] state.next_line while !state.eof? && state.current_line.match?(/\A {4}/) raw_lines << state.current_line content = "#{content}\n#{state.current_line.sub(/\A {4}/, "")}" state.next_line end state.tokens << Token.new( type: :footnote_definition, content: raw_lines.join("\n") + "\n", attrs: { label: match[1].downcase, content: content }, map: [start_line, state.line] ) true end |
#parse_front_matter(state) ⇒ Object
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 |
# File 'lib/mdlint/parser/block_parser.rb', line 175 def parse_front_matter(state) return false unless state.line.zero? opener = state.current_line format, closing_marker = front_matter_markers(opener) return false unless format closing_line = state.lines[(state.line + 1)..]&.index do |line| line.strip == closing_marker end return false unless closing_line closing_line += state.line + 1 start_line = state.line raw_lines = state.raw_lines[start_line..closing_line] payload = state.lines[(start_line + 1)...closing_line].to_a return false unless front_matter_payload?(format, payload) content = raw_lines.join("\n") content += "\n" if closing_line < state.lines.length - 1 || state.src.end_with?("\n") state.tokens << Token.new( type: :front_matter, content: content, meta: { format: format, delimiter: closing_marker }, map: [start_line, closing_line + 1] ) state.line = closing_line + 1 true end |
#parse_hr(state) ⇒ Object
354 355 356 357 358 359 360 361 362 363 364 365 366 367 |
# File 'lib/mdlint/parser/block_parser.rb', line 354 def parse_hr(state) line = state.current_line return false unless line.match?(HR_REGEXP) state.tokens << Token.new( type: :hr, tag: "hr", markup: line.strip[0], map: [state.line, state.line + 1] ) state.next_line true end |
#parse_html_block(state) ⇒ Object
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 |
# File 'lib/mdlint/parser/block_parser.rb', line 782 def parse_html_block(state) line = state.current_line return false unless html_block_start?(line) start_line = state.line content_lines = [] if line.match?(HTML_BLOCK_START_2) until state.eof? current_line = state.current_line content_lines << current_line state.next_line break if current_line.include?("-->") end elsif line.match?(HTML_BLOCK_START_1) tag = line[/\A {0,3}<([A-Za-z][A-Za-z0-9]*)/i, 1] until state.eof? current_line = state.current_line content_lines << state.raw_line state.next_line break if current_line.match?(%r{</#{Regexp.escape(tag)}\s*>}i) end elsif line.match?(HTML_BLOCK_START_3) until state.eof? current_line = state.current_line content_lines << state.raw_line state.next_line break if current_line.include?("?>") end elsif line.match?(HTML_BLOCK_START_5) until state.eof? current_line = state.current_line content_lines << state.raw_line state.next_line break if current_line.include?("]]>") end else until state.eof? current_line = state.current_line content_lines << state.raw_line state.next_line break if line.match?(HTML_BLOCK_START_7) && current_line.match?(%r{</[A-Z][A-Za-z0-9]*(?:\.[A-Za-z0-9_-]+)?>}) break if state.blank_line? end end state.tokens << Token.new( type: :html_block, content: content_lines.join("\n") + "\n", map: [start_line, state.line] ) true end |
#parse_list_items(state, pattern, _marker, base_indent = 0) ⇒ Object
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 |
# File 'lib/mdlint/parser/block_parser.rb', line 499 def parse_list_items(state, pattern, _marker, base_indent = 0) while !state.eof? line = state.current_line match = line.match(pattern) break unless match break if line.match?(HR_REGEXP) && match[1].length <= base_indent item_start = state.line content = line.sub(pattern, "") state.tokens << Token.new( type: :list_item_open, tag: "li", nesting: 1, level: state.level, attrs: task_attributes(content).merge(tight: true), map: [item_start, nil] ) item_token_index = state.tokens.length - 1 state.level += 1 state.next_line content = strip_task_marker(content) item_content_lines = [content] nested_lines = [] nested_start_line = state.line while !state.eof? && !state.blank_line? && !state.current_line.match?(pattern) if nested_lines.any? && state.current_line.match?(/\A\s+/) nested_lines << dedent_list_line(state.current_line, base_indent) state.next_line elsif state.current_line.match?(/\A\s+/) item_content_lines << state.raw_line.sub(/\A\s+/, "") state.next_line else break end end while !state.eof? && !state.blank_line? nested_match = state.current_line.match(pattern) break unless nested_match && nested_match[1].length > base_indent nested_lines << dedent_list_line(state.current_line, base_indent) state.next_line while !state.eof? && !state.blank_line? && !state.current_line.match?(pattern) break unless state.current_line.match?(/\A\s+/) nested_lines << dedent_list_line(state.current_line, base_indent) state.next_line end end loose_content = collect_loose_item_content(state, pattern, base_indent, nested_lines, match[0].length) paragraph_content = item_content_lines.join("\n").strip if paragraph_content.match?(HR_REGEXP) state.tokens << Token.new(type: :hr, tag: "hr", markup: paragraph_content.strip[0], map: [item_start, state.line]) paragraph_content = "" end unless paragraph_content.empty? state.tokens << Token.new( type: :paragraph_open, tag: "p", nesting: 1, level: state.level, map: [item_start, state.line] ) state.tokens << Token.new( type: :inline, content: paragraph_content, level: state.level + 1, map: [item_start, state.line] ) state.tokens << Token.new( type: :paragraph_close, tag: "p", nesting: -1, level: state.level ) end append_nested_tokens(state, nested_lines, nested_start_line) append_loose_paragraph(state, loose_content, item_start) if loose_content state.level -= 1 state.tokens[item_token_index].map[1] = state.line state.tokens << Token.new( type: :list_item_close, tag: "li", nesting: -1, level: state.level ) state.skip_blank_lines end end |
#parse_math_block(state) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/mdlint/parser/block_parser.rb', line 89 def parse_math_block(state) return false unless state.current_line.match?(MATH_BLOCK_REGEXP) start_line = state.line content_lines = [state.current_line] state.next_line until state.eof? content_lines << state.raw_line state.next_line break if content_lines.last.match?(MATH_BLOCK_REGEXP) end state.tokens << Token.new( type: :math_block, content: content_lines.join("\n") + "\n", map: [start_line, state.line] ) true end |
#parse_ordered_list(state) ⇒ Object
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 |
# File 'lib/mdlint/parser/block_parser.rb', line 462 def parse_ordered_list(state) line = state.current_line match = line.match(ORDERED_LIST_REGEXP) return false unless match start_num = match[2].to_i delimiter = match[3] start_line = state.line state.tokens << Token.new( type: :ordered_list_open, tag: "ol", nesting: 1, level: state.level, markup: delimiter, attrs: { start: start_num, tight: true }, map: [start_line, nil] ) list_token_index = state.tokens.length - 1 state.level += 1 parse_ordered_list_items(state, delimiter, match[1].length) state.level -= 1 state.tokens[list_token_index].map[1] = state.line state.tokens << Token.new( type: :ordered_list_close, tag: "ol", nesting: -1, level: state.level, markup: delimiter ) true end |
#parse_ordered_list_items(state, delimiter, base_indent = 0) ⇒ Object
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 |
# File 'lib/mdlint/parser/block_parser.rb', line 601 def parse_ordered_list_items(state, delimiter, base_indent = 0) pattern = /\A( {0,3})(\d{1,9})([#{Regexp.escape(delimiter)}])\s+/ while !state.eof? line = state.current_line match = line.match(pattern) break unless match break if line.match?(HR_REGEXP) && match[1].length <= base_indent item_start = state.line content = line.sub(pattern, "") state.tokens << Token.new( type: :list_item_open, tag: "li", nesting: 1, level: state.level, attrs: task_attributes(content).merge(tight: true), map: [item_start, nil] ) item_token_index = state.tokens.length - 1 state.level += 1 state.next_line content = strip_task_marker(content) item_content_lines = [content] nested_lines = [] nested_start_line = state.line while !state.eof? && !state.blank_line? && !state.current_line.match?(pattern) if nested_lines.any? && state.current_line.match?(/\A\s+/) nested_lines << dedent_list_line(state.current_line, base_indent) state.next_line elsif state.current_line.match?(/\A\s+/) item_content_lines << state.raw_line.sub(/\A\s+/, "") state.next_line else break end end while !state.eof? && !state.blank_line? nested_match = state.current_line.match(pattern) break unless nested_match && nested_match[1].length > base_indent nested_lines << dedent_list_line(state.current_line, base_indent) state.next_line while !state.eof? && !state.blank_line? && !state.current_line.match?(pattern) break unless state.current_line.match?(/\A\s+/) nested_lines << dedent_list_line(state.current_line, base_indent) state.next_line end end loose_content = collect_loose_item_content(state, pattern, base_indent, nested_lines, match[0].length) paragraph_content = item_content_lines.join("\n").strip if paragraph_content.match?(HR_REGEXP) state.tokens << Token.new(type: :hr, tag: "hr", markup: paragraph_content.strip[0], map: [item_start, state.line]) paragraph_content = "" end unless paragraph_content.empty? state.tokens << Token.new( type: :paragraph_open, tag: "p", nesting: 1, level: state.level, map: [item_start, state.line] ) state.tokens << Token.new( type: :inline, content: paragraph_content, level: state.level + 1, map: [item_start, state.line] ) state.tokens << Token.new( type: :paragraph_close, tag: "p", nesting: -1, level: state.level ) end append_nested_tokens(state, nested_lines, nested_start_line) append_loose_paragraph(state, loose_content, item_start) if loose_content state.level -= 1 state.tokens[item_token_index].map[1] = state.line state.tokens << Token.new( type: :list_item_close, tag: "li", nesting: -1, level: state.level ) state.skip_blank_lines end end |
#parse_paragraph(state) ⇒ Object
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 |
# File 'lib/mdlint/parser/block_parser.rb', line 1016 def parse_paragraph(state) return false if state.blank_line? start_line = state.line content_lines = [] while !state.eof? && !state.blank_line? line = state.current_line break if line.match?(ATX_HEADING_REGEXP) || line.match?(MATH_BLOCK_REGEXP) || fence_match(line) || line.match?(HR_REGEXP) || line.match?(BLOCKQUOTE_REGEXP) || line.match?(BULLET_LIST_REGEXP) || line.match?(ORDERED_LIST_REGEXP) || (html_block_start?(line) && !line.match?(HTML_BLOCK_START_7)) || (reference_definition_candidate?(state) && content_lines.empty?) || line.match?(FOOTNOTE_DEF_REGEXP) if state.peek_line&.match?(SETEXT_HEADING_REGEXP) break if content_lines.any? end content_lines << if content_lines.empty? state.raw_line.sub(/\A {0,3}/, "") else state.raw_line.sub(/\A\s+/, "") end state.next_line end return false if content_lines.empty? state.tokens << Token.new( type: :paragraph_open, tag: "p", nesting: 1, level: state.level, map: [start_line, state.line] ) state.tokens << Token.new( type: :inline, content: content_lines.join("\n").strip.gsub(/(?<! ) \n/, "\n"), level: state.level + 1, map: [start_line, state.line] ) state.tokens << Token.new( type: :paragraph_close, tag: "p", nesting: -1, level: state.level ) true end |
#parse_reference_definition(state) ⇒ Object
879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 |
# File 'lib/mdlint/parser/block_parser.rb', line 879 def parse_reference_definition(state) line = state.raw_line match = line.match(REFERENCE_DEF_REGEXP) return false unless match label = normalize_reference_label(match[1]) tail = line[match[0].length..].to_s.strip parsed = parse_reference_tail(tail) consumed = 1 if parsed.nil? && tail.empty? url_line = state.peek_line return false if url_line.nil? || state.blank_line?(state.line + 1) parsed = parse_reference_tail(url_line.to_s.strip) consumed += 1 if parsed end if parsed && parsed[1].nil? title_line = state.peek_line(consumed) if title_line && !state.blank_line?(state.line + consumed) && title_line.match?(/\A\s+/) title = parse_reference_title(title_line.to_s.strip) parsed = [parsed[0], title] if title consumed += 1 if title end end return false unless parsed url, title = parsed state.tokens << Token.new( type: :reference_definition, attrs: { label: label, url: url, title: title }.compact, map: [state.line, state.line + 1] ) state.line += consumed true end |
#parse_reference_tail(tail) ⇒ Object
938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 |
# File 'lib/mdlint/parser/block_parser.rb', line 938 def parse_reference_tail(tail) value = tail.to_s return nil if value.empty? if value.start_with?("<") closing = value.index(">", 1) return nil unless closing url = value[1...closing] raw_remainder = value[(closing + 1)..].to_s return nil unless raw_remainder.empty? || raw_remainder.match?(/\A\s/) remainder = raw_remainder.strip else index = 0 depth = 0 while index < value.length character = value[index] if character == "\\" index += 2 next end if character == "(" depth += 1 elsif character == ")" break if depth.zero? depth -= 1 elsif character.match?( /\s/ ) && depth.zero? break end index += 1 end return nil if index.zero? url = value[0...index] remainder = value[index..].to_s.strip end return [url, nil] if remainder.empty? title = parse_reference_title(remainder) title ? [url, title] : nil end |
#parse_reference_title(value) ⇒ Object
983 984 985 986 987 988 989 990 991 |
# File 'lib/mdlint/parser/block_parser.rb', line 983 def parse_reference_title(value) return nil unless value.length >= 2 opener = value[0] closer = { '"' => '"', "'" => "'", "(" => ")" }[opener] return nil unless closer && value.end_with?(closer) value[1...-1] end |
#parse_setext_heading(state) ⇒ Object
266 267 268 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 309 310 311 312 |
# File 'lib/mdlint/parser/block_parser.rb', line 266 def parse_setext_heading(state) line = state.current_line return false if line.match?(/\A\s*\z/) underline_index = state.line + 1 while underline_index < state.lines.length && !state.blank_line?(underline_index) break if state.lines[underline_index].match?(SETEXT_HEADING_REGEXP) break if block_boundary?(state.lines[underline_index]) underline_index += 1 end return false if underline_index >= state.lines.length match = state.lines[underline_index].match(SETEXT_HEADING_REGEXP) return false unless match level = match[1][0] == "=" ? 1 : 2 content = state.lines[state.line...underline_index].join("\n").strip start_line = state.line state.tokens << Token.new( type: :heading_open, tag: "h#{level}", nesting: 1, level: state.level, markup: match[1][0], map: [start_line, underline_index + 1] ) state.tokens << Token.new( type: :inline, content: content, level: state.level + 1, map: [start_line, underline_index] ) state.tokens << Token.new( type: :heading_close, tag: "h#{level}", nesting: -1, level: state.level, markup: match[1][0] ) state.line = underline_index + 1 true end |
#parse_table(state) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/mdlint/parser/block_parser.rb', line 109 def parse_table(state) return false unless @dialect.feature?(:tables) return false unless table_delimiter?(state.peek_line) return false unless state.current_line.include?("|") start_line = state.line header = split_table_row(state.current_line) alignments = split_table_row(state.peek_line).map { |cell| table_alignment(cell) } rows = [header] state.next_line state.next_line while !state.eof? && table_row?(state.current_line) rows << split_table_row(state.current_line) state.next_line end state.tokens << Token.new( type: :table, meta: { rows: rows, alignments: alignments }, map: [start_line, state.line] ) true end |
#reference_definition_candidate?(state) ⇒ Boolean
922 923 924 925 926 927 928 929 930 931 932 |
# File 'lib/mdlint/parser/block_parser.rb', line 922 def reference_definition_candidate?(state) match = state.raw_line.match(REFERENCE_DEF_REGEXP) return false unless match tail = state.raw_line[match[0].length..].to_s.strip return true if parse_reference_tail(tail) return false unless tail.empty? next_line = state.peek_line next_line && !state.blank_line?(state.line + 1) && parse_reference_tail(next_line.to_s.strip) end |
#split_table_row(line) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/mdlint/parser/block_parser.rb', line 145 def split_table_row(line) value = line.to_s.strip value = value[1..] if value.start_with?("|") value = value[0...-1] if value.end_with?("|") && !value.end_with?("\\|") cells = [] current = +"" escaped = false value.each_char do |character| if character == "|" && !escaped cells << current.strip current = +"" else current << character end escaped = character == "\\" && !escaped escaped = false if character != "\\" end cells << current.strip cells end |
#strip_code_indent(raw_line, expanded_line) ⇒ String
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 |
# File 'lib/mdlint/parser/block_parser.rb', line 1103 def strip_code_indent(raw_line, ) return raw_line unless .start_with?(" ") consumed = 0 index = 0 raw_line.each_char do |character| break if consumed >= 4 consumed += character == "\t" ? 4 - (consumed % 4) : 1 index += 1 end raw_line[index..] || "" end |
#strip_fence_indent(raw_line, indent) ⇒ String
1140 1141 1142 1143 1144 |
# File 'lib/mdlint/parser/block_parser.rb', line 1140 def strip_fence_indent(raw_line, indent) return raw_line if indent.zero? raw_line.sub(/\A {0,#{indent}}/, "") end |
#strip_task_marker(content) ⇒ Object
1155 1156 1157 1158 1159 |
# File 'lib/mdlint/parser/block_parser.rb', line 1155 def strip_task_marker(content) return content unless @dialect.feature?(:task_lists) content.sub(/\A\[[ xX]\]\s+/, "") end |
#table_alignment(cell) ⇒ Object
166 167 168 169 170 171 172 173 |
# File 'lib/mdlint/parser/block_parser.rb', line 166 def table_alignment(cell) value = cell.strip return :center if value.start_with?(":") && value.end_with?(":") return :left if value.start_with?(":") return :right if value.end_with?(":") nil end |
#table_delimiter?(line) ⇒ Boolean
134 135 136 137 138 139 |
# File 'lib/mdlint/parser/block_parser.rb', line 134 def table_delimiter?(line) return false unless line cells = split_table_row(line) cells.length > 0 && cells.all? { |cell| cell.match?(/\A\s*:?-{3,}:?\s*\z/) } end |
#table_row?(line) ⇒ Boolean
141 142 143 |
# File 'lib/mdlint/parser/block_parser.rb', line 141 def table_row?(line) line && line.include?("|") && !line.strip.empty? end |
#task_attributes(content) ⇒ Object
1146 1147 1148 1149 1150 1151 1152 1153 |
# File 'lib/mdlint/parser/block_parser.rb', line 1146 def task_attributes(content) return {} unless @dialect.feature?(:task_lists) match = content.match(/\A\[([ xX])\]\s+/) return {} unless match { task: true, checked: match[1].downcase == "x" } end |