class Kramdown::Parser::Kramdown
Used for parsing a document in kramdown format.
If you want to extend the functionality of the parser, you need to do the following:
-
Create a new subclass
-
add the needed parser methods
-
modify the @block_parsers and @span_parsers variables and add the names of your parser methods
Here is a small example for an extended parser class that parses ERB style tags as raw text if they are used as span-level elements (an equivalent block-level parser should probably also be made to handle the block case):
require 'kramdown/parser/kramdown' class Kramdown::Parser::ERBKramdown < Kramdown::Parser::Kramdown def initialize(source, options) super @span_parsers.unshift(:erb_tags) end ERB_TAGS_START = /<%.*?%>/ def parse_erb_tags @src.pos += @src.matched_size @tree.children << Element.new(:raw, @src.matched) end define_parser(:erb_tags, ERB_TAGS_START, '<%') end
The new parser can be used like this:
require 'kramdown/document' # require the file with the above parser class Kramdown::Document.new(input_text, :input => 'ERBKramdown').to_html
Constants
- ABBREV_DEFINITION_START
- ACHARS
- ALD_ANY_CHARS
- ALD_CLASS_NAME
- ALD_ID_CHARS
- ALD_ID_NAME
- ALD_START
- ALD_TYPE_ANY
- ALD_TYPE_CLASS_NAME
- ALD_TYPE_ID_NAME
- ALD_TYPE_ID_OR_CLASS
- ALD_TYPE_ID_OR_CLASS_MULTI
- ALD_TYPE_KEY_VALUE_PAIR
- ALD_TYPE_REF
- ATX_HEADER_START
- AUTOLINK_START
- AUTOLINK_START_STR
- BLANK_LINE
- BLOCKQUOTE_START
- BLOCK_BOUNDARY
- BLOCK_EXTENSIONS_START
- BLOCK_MATH_START
- CODEBLOCK_MATCH
- CODEBLOCK_START
- CODESPAN_DELIMITER
- DEFINITION_LIST_START
- Data
Struct class holding all the needed data for one block/span-level parser method.
- EMPHASIS_START
- EOB_MARKER
- ESCAPED_CHARS
- EXT_BLOCK_START
- EXT_BLOCK_STOP_STR
- EXT_SPAN_START
- EXT_START_STR
- EXT_STOP_STR
- FENCED_CODEBLOCK_MATCH
- FENCED_CODEBLOCK_START
- FOOTNOTE_DEFINITION_START
- FOOTNOTE_MARKER_START
- HEADER_ID
- HR_START
- HTML_BLOCK_START
- HTML_MARKDOWN_ATTR_MAP
Mapping of markdown attribute value to content model. I.e. :raw when “0”, :default when “1” (use default content model for the HTML element), :span when “span”, :block when block and for everything else
nilis returned.- HTML_SPAN_START
- IAL_BLOCK
- IAL_BLOCK_START
- IAL_CLASS_ATTR
- IAL_SPAN_START
- INDENT
Regexp for matching indentation (one tab or four spaces)
- INLINE_MATH_START
- LAZY_END
- LAZY_END_HTML_SPAN_ELEMENTS
- LAZY_END_HTML_START
- LAZY_END_HTML_STOP
- LINE_BREAK
- LINK_BRACKET_STOP_RE
- LINK_DEFINITION_START
- LINK_EMPHASIS_CONTEXT
- LINK_INLINE_ID_RE
- LINK_INLINE_TITLE_RE
- LINK_PAREN_STOP_RE
- LINK_START
- LIST_ITEM_IAL
- LIST_ITEM_IAL_CHECK
- LIST_START
- LIST_START_OL
- LIST_START_UL
- OPT_SPACE
Regexp for matching the optional space (zero or up to three spaces)
- PARAGRAPH_END
- PARAGRAPH_MATCH
- PARAGRAPH_START
- PARSE_FIRST_LIST_LINE_REGEXP_CACHE
- PATTERN_TAIL
- SETEXT_HEADER_START
- SMART_QUOTES_RE
- SPAN_EXTENSIONS_START
- SQ_CLOSE
- SQ_PUNCT
- SQ_RULES
- SQ_SUBSTS
- TABLE_FSEP_LINE
- TABLE_HSEP_ALIGN
- TABLE_LINE
- TABLE_PIPE_CHECK
- TABLE_ROW_LINE
- TABLE_SEP_LINE
- TABLE_START
- TRAILING_WHITESPACE
- TYPOGRAPHIC_SYMS
- TYPOGRAPHIC_SYMS_RE
- TYPOGRAPHIC_SYMS_SUBST
Protected Class Methods
Add a parser method
-
with the given
name, -
using
start_reas start regexp -
and, for span parsers,
span_startas a String that can be used in a regexp and which identifies the starting character(s)
to the registry. The method name is automatically derived from the name or can explicitly be set by using the meth_name parameter.
# File lib/kramdown/parser/kramdown.rb 335 def self.define_parser(name, start_re, span_start = nil, meth_name = "parse_#{name}") 336 raise "A parser with the name #{name} already exists!" if @@parsers.key?(name) 337 @@parsers[name] = Data.new(name, start_re, span_start, meth_name) 338 end
Return true if there is a parser called name.
# File lib/kramdown/parser/kramdown.rb 346 def self.has_parser?(name) 347 @@parsers.key?(name) 348 end
Return the Data structure for the parser name.
# File lib/kramdown/parser/kramdown.rb 341 def self.parser(name = nil) 342 @@parsers[name] 343 end
Private Class Methods
Create a new Kramdown parser object with the given options.
Kramdown::Parser::Base::new
# File lib/kramdown/parser/kramdown.rb 64 def initialize(source, options) 65 super 66 67 reset_env 68 69 @alds = {} 70 @footnotes = {} 71 @link_defs = {} 72 update_link_definitions(@options[:link_defs]) 73 74 @block_parsers = [:blank_line, :codeblock, :codeblock_fenced, :blockquote, :atx_header, 75 :horizontal_rule, :list, :definition_list, :block_html, :setext_header, 76 :block_math, :table, :footnote_definition, :link_definition, 77 :abbrev_definition, :block_extensions, :eob_marker, :paragraph] 78 @span_parsers = [:emphasis, :codespan, :autolink, :span_html, :footnote_marker, :link, 79 :smart_quotes, :inline_math, :span_extensions, :html_entity, 80 :typographic_syms, :line_break, :escaped_chars] 81 82 @span_pattern_cache ||= Hash.new {|h, k| h[k] = {} } 83 @stack_counter = 0 84 end
Initialize the parser object with the source string and the parsing options.
The @root element, the @warnings array and @text_type (specifies the default type for newly created text nodes) are automatically initialized.
# File lib/kramdown/parser/base.rb 51 def initialize(source, options) 52 @source = source 53 @options = Kramdown::Options.merge(options) 54 @root = Element.new(:root, nil, nil, encoding: (source.encoding rescue nil), location: 1, 55 options: {}, abbrev_defs: {}, abbrev_attr: {}) 56 57 @root.options[:abbrev_defs].default_proc = @root.options[:abbrev_attr].default_proc = 58 lambda do |h, k| 59 k_mod = k.gsub(/[\p{Z}\t\r\n\f\v]+/m, " ") 60 k != k_mod ? h[k_mod] : nil 61 end 62 @warnings = [] 63 @text_type = :text 64 end
Public Instance Methods
This helper methods adds the approriate attributes to the element el of type a or img and the element itself to the @tree.
# File lib/kramdown/parser/kramdown/link.rb 38 def add_link(el, href, title, alt_text = nil, ial = nil) 39 el.options[:ial] = ial 40 update_attr_with_ial(el.attr, ial) if ial 41 if el.type == :a 42 el.attr['href'] = href 43 else 44 el.attr['src'] = href 45 el.attr['alt'] ||= alt_text 46 el.children.clear 47 end 48 el.attr['title'] = title if title 49 @tree.children << el 50 end
Return true if we are after a block boundary.
# File lib/kramdown/parser/kramdown/block_boundary.rb 20 def after_block_boundary? 21 last_child = @tree.children.last 22 !last_child || last_child.type == :blank || 23 (last_child.type == :eob && last_child.value.nil?) || @block_ial 24 end
Return true if we are before a block boundary.
# File lib/kramdown/parser/kramdown/block_boundary.rb 27 def before_block_boundary? 28 @src.match?(self.class::BLOCK_BOUNDARY) 29 end
Correct abbreviation attributes.
# File lib/kramdown/parser/kramdown/abbreviation.rb 33 def correct_abbreviations_attributes 34 @root.options[:abbrev_attr].keys.each do |k| 35 @root.options[:abbrev_attr][k] = @root.options[:abbrev_attr][k].attr 36 end 37 end
# File lib/kramdown/parser/kramdown/extensions.rb 97 def handle_extension(name, opts, body, type, line_no = nil) 98 case name 99 when 'comment' 100 if body.kind_of?(String) 101 @tree.children << Element.new(:comment, body, nil, category: type, location: line_no) 102 end 103 true 104 when 'nomarkdown' 105 if body.kind_of?(String) 106 @tree.children << Element.new(:raw, body, nil, category: type, 107 location: line_no, type: opts['type'].to_s.split(/\s+/)) 108 end 109 true 110 when 'options' 111 opts.select do |k, v| 112 k = k.to_sym 113 if Kramdown::Options.defined?(k) 114 if @options[:forbidden_inline_options].include?(k) || 115 k == :forbidden_inline_options 116 warning("Option #{k} may not be set inline") 117 next false 118 end 119 120 begin 121 val = Kramdown::Options.parse(k, v) 122 @options[k] = val 123 (@root.options[:options] ||= {})[k] = val 124 rescue StandardError 125 end 126 false 127 else 128 true 129 end 130 end.each do |k, _v| 131 warning("Unknown kramdown option '#{k}'") 132 end 133 @tree.children << new_block_el(:eob, :extension) if type == :block 134 true 135 else 136 false 137 end 138 end
# File lib/kramdown/parser/kramdown/html.rb 25 def handle_kramdown_html_tag(el, closed, handle_body) 26 if @block_ial 27 el.options[:ial] = @block_ial 28 @block_ial = nil 29 end 30 31 content_model = if @tree.type != :html_element || @tree.options[:content_model] != :raw 32 (@options[:parse_block_html] ? HTML_CONTENT_MODEL[el.value] : :raw) 33 else 34 :raw 35 end 36 if (val = HTML_MARKDOWN_ATTR_MAP[el.attr.delete('markdown')]) 37 content_model = (val == :default ? HTML_CONTENT_MODEL[el.value] : val) 38 end 39 40 @src.scan(TRAILING_WHITESPACE) if content_model == :block 41 el.options[:content_model] = content_model 42 el.options[:is_closed] = closed 43 44 if !closed && handle_body 45 case content_model 46 when :block 47 unless parse_blocks(el) 48 warning("Found no end tag for '#{el.value}' (line #{el.options[:location]}) - auto-closing it") 49 end 50 when :span 51 curpos = @src.pos 52 if @src.scan_until(/(?=<\/#{el.value}\s*>)/mi) 53 add_text(extract_string(curpos...@src.pos, @src), el) 54 @src.scan(HTML_TAG_CLOSE_RE) 55 else 56 add_text(@src.rest, el) 57 @src.terminate 58 warning("Found no end tag for '#{el.value}' (line #{el.options[:location]}) - auto-closing it") 59 end 60 else 61 parse_raw_html(el) {|iel, ic, ih| handle_kramdown_html_tag(iel, ic, ih) } 62 end 63 unless @tree.type == :html_element && @tree.options[:content_model] == :raw 64 @src.scan(TRAILING_WHITESPACE) 65 end 66 end 67 end
Normalize the link identifier.
# File lib/kramdown/parser/kramdown/link.rb 16 def normalize_link_id(id) 17 id.gsub(/\s+/, ' ').downcase 18 end
# File lib/kramdown/parser/kramdown/paragraph.rb 55 def paragraph_end 56 self.class::PARAGRAPH_END 57 end
The source string provided on initialization is parsed into the @root element.
# File lib/kramdown/parser/kramdown.rb 88 def parse 89 configure_parser 90 parse_blocks(@root, adapt_source(source)) 91 update_tree(@root) 92 correct_abbreviations_attributes 93 replace_abbreviations(@root) 94 @footnotes.each do |_name, data| 95 update_tree(data[:content]) 96 replace_abbreviations(data[:content]) 97 end 98 footnote_count = 0 99 @footnotes.each do |name, data| 100 (footnote_count += 1; next) if data.key?(:marker) 101 line = data[:content].options[:location] 102 warning("Footnote definition for '#{name}' on line #{line} is unreferenced - ignoring") 103 end 104 @root.options[:footnote_count] = footnote_count 105 end
Parse the link definition at the current location.
# File lib/kramdown/parser/kramdown/abbreviation.rb 16 def parse_abbrev_definition 17 start_line_number = @src.current_line_number 18 @src.pos += @src.matched_size 19 abbrev_id, abbrev_text = @src[1], @src[2] 20 abbrev_text.strip! 21 if @root.options[:abbrev_defs][abbrev_id] 22 warning("Duplicate abbreviation ID '#{abbrev_id}' on line #{start_line_number} " \ 23 "- overwriting") 24 end 25 @tree.children << new_block_el(:eob, :abbrev_def) 26 @root.options[:abbrev_defs][abbrev_id] = abbrev_text 27 @root.options[:abbrev_attr][abbrev_id] = @tree.children.last 28 true 29 end
Parse the string str and extract all attributes and add all found attributes to the hash opts.
# File lib/kramdown/parser/kramdown/extensions.rb 17 def parse_attribute_list(str, opts) 18 return if str.strip.empty? || str.strip == ':' 19 attrs = str.scan(ALD_TYPE_ANY) 20 attrs.each do |key, sep, val, ref, id_and_or_class, _, _, unmatched| 21 if ref 22 (opts[:refs] ||= []) << ref 23 elsif id_and_or_class 24 id_and_or_class.scan(ALD_TYPE_ID_OR_CLASS).each do |id_attr, class_attr| 25 if class_attr 26 opts[IAL_CLASS_ATTR] = "#{opts[IAL_CLASS_ATTR]} #{class_attr}".lstrip 27 else 28 opts['id'] = id_attr 29 end 30 end 31 elsif unmatched 32 warning("IAL/ALD contains invalid part: #{unmatched}") 33 else 34 val.gsub!(/\\(\}|#{sep})/, "\\1") 35 opts[key] = val 36 end 37 end 38 warning("No or invalid attributes found in IAL/ALD content: #{str}") if attrs.empty? 39 end
Parse the Atx header at the current location.
# File lib/kramdown/parser/kramdown/header.rb 31 def parse_atx_header 32 return false unless after_block_boundary? 33 text, id = parse_header_contents 34 text.sub!(/(?<!\\)#+\z/, '') && text.rstrip! 35 return false if text.empty? 36 add_header(@src["level"].length, text, id) 37 true 38 end
Parse the autolink at the current location.
# File lib/kramdown/parser/kramdown/autolink.rb 18 def parse_autolink 19 start_line_number = @src.current_line_number 20 @src.pos += @src.matched_size 21 href = (@src[2].nil? ? "mailto:#{@src[1]}" : @src[1]) 22 el = Element.new(:a, nil, {'href' => href}, location: start_line_number) 23 add_text(@src[1].sub(/^mailto:/, ''), el) 24 @tree.children << el 25 end
Parse the blank line at the current postition.
# File lib/kramdown/parser/kramdown/blank_line.rb 16 def parse_blank_line 17 @src.pos += @src.matched_size 18 if (last_child = @tree.children.last) && last_child.type == :blank 19 last_child.value << @src.matched 20 else 21 @tree.children << new_block_el(:blank, @src.matched) 22 end 23 true 24 end
Parse one of the block extensions (ALD, block IAL or generic extension) at the current location.
# File lib/kramdown/parser/kramdown/extensions.rb 165 def parse_block_extensions 166 if @src.scan(ALD_START) 167 parse_attribute_list(@src[2], @alds[@src[1]] ||= {}) 168 @tree.children << new_block_el(:eob, :ald) 169 true 170 elsif @src.match?(EXT_BLOCK_START) 171 parse_extension_start_tag(:block) 172 elsif @src.scan(IAL_BLOCK_START) 173 if (last_child = @tree.children.last) && last_child.type != :blank && 174 (last_child.type != :eob || 175 [:link_def, :abbrev_def, :footnote_def].include?(last_child.value)) 176 parse_attribute_list(@src[1], last_child.options[:ial] ||= {}) 177 @tree.children << new_block_el(:eob, :ial) unless @src.match?(IAL_BLOCK_START) 178 else 179 parse_attribute_list(@src[1], @block_ial ||= {}) 180 end 181 true 182 else 183 false 184 end 185 end
Parse the HTML at the current position as block-level HTML.
# File lib/kramdown/parser/kramdown/html.rb 72 def parse_block_html 73 line = @src.current_line_number 74 if @src.skip(/<!--/) && (r = @src.exist?(/-->/); @src.pos -= 4; r) 75 result = @src.scan(HTML_COMMENT_RE) 76 @tree.children << Element.new(:xml_comment, result, nil, category: :block, location: line) 77 @src.scan(TRAILING_WHITESPACE) 78 true 79 elsif @src.match?(/^#{OPT_SPACE}#{HTML_TAG_RE}/o) && !HTML_SPAN_ELEMENTS.include?(@src[1].downcase) 80 @src.pos += @src.matched_size 81 handle_html_start_tag(line) {|iel, ic, ih| handle_kramdown_html_tag(iel, ic, ih) } 82 Kramdown::Parser::Html::ElementConverter.convert(@root, @tree.children.last) if @options[:html_to_native] 83 true 84 elsif @src.match?(/^#{OPT_SPACE}#{HTML_TAG_CLOSE_RE}/o) && !HTML_SPAN_ELEMENTS.include?(@src[1].downcase) 85 name = @src[1].downcase 86 87 if @tree.type == :html_element && @tree.value == name 88 @src.pos += @src.matched_size 89 throw :stop_block_parsing, :found 90 else 91 false 92 end 93 else 94 false 95 end 96 end
Parse the math block at the current location.
# File lib/kramdown/parser/kramdown/math.rb 18 def parse_block_math 19 start_line_number = @src.current_line_number 20 if !after_block_boundary? 21 return false 22 elsif @src[1] 23 @src.scan(/^#{OPT_SPACE}\\/o) if @src[3] 24 return false 25 end 26 27 saved_pos = @src.save_pos 28 @src.pos += @src.matched_size 29 data = @src[2].strip 30 if before_block_boundary? 31 @tree.children << new_block_el(:math, data, nil, category: :block, location: start_line_number) 32 true 33 else 34 @src.revert_pos(saved_pos) 35 false 36 end 37 end
Parse the blockquote at the current location.
# File lib/kramdown/parser/kramdown/blockquote.rb 20 def parse_blockquote 21 start_line_number = @src.current_line_number 22 result = @src.scan(PARAGRAPH_MATCH) 23 until @src.match?(self.class::LAZY_END) 24 result << @src.scan(PARAGRAPH_MATCH) 25 end 26 result.gsub!(BLOCKQUOTE_START, '') 27 28 el = new_block_el(:blockquote, nil, nil, location: start_line_number) 29 @tree.children << el 30 parse_blocks(el, result) 31 true 32 end
Parse the indented codeblock at the current location.
# File lib/kramdown/parser/kramdown/codeblock.rb 22 def parse_codeblock 23 start_line_number = @src.current_line_number 24 data = @src.scan(self.class::CODEBLOCK_MATCH) 25 data.gsub!(/\n( {0,3}\S)/, ' \\1') 26 data.gsub!(INDENT, '') 27 @tree.children << new_block_el(:codeblock, data, nil, location: start_line_number) 28 true 29 end
Parse the fenced codeblock at the current location.
# File lib/kramdown/parser/kramdown/codeblock.rb 36 def parse_codeblock_fenced 37 if @src.match?(self.class::FENCED_CODEBLOCK_MATCH) 38 start_line_number = @src.current_line_number 39 @src.pos += @src.matched_size 40 el = new_block_el(:codeblock, @src[5], nil, location: start_line_number, fenced: true) 41 lang = @src[3].to_s.strip 42 unless lang.empty? 43 el.options[:lang] = lang 44 el.attr['class'] = "language-#{@src[4]}" 45 end 46 @tree.children << el 47 true 48 else 49 false 50 end 51 end
Parse the codespan at the current scanner location.
# File lib/kramdown/parser/kramdown/codespan.rb 16 def parse_codespan 17 start_line_number = @src.current_line_number 18 result = @src.scan(CODESPAN_DELIMITER) 19 simple = (result.length == 1) 20 saved_pos = @src.save_pos 21 22 if simple && @src.pre_match =~ /\s\Z|\A\Z/ && @src.match?(/\s/) 23 add_text(result) 24 return 25 end 26 27 # assign static regex to avoid allocating the same on every instance 28 # where +result+ equals a single-backtick. Interpolate otherwise. 29 if result == '`' 30 scan_pattern = /`/ 31 str_sub_pattern = /`\Z/ 32 else 33 scan_pattern = /#{result}/ 34 str_sub_pattern = /#{result}\Z/ 35 end 36 37 if (text = @src.scan_until(scan_pattern)) 38 text.sub!(str_sub_pattern, '') 39 unless simple 40 text = text[1..-1] if text[0..0] == ' ' 41 text = text[0..-2] if text[-1..-1] == ' ' 42 end 43 @tree.children << Element.new(:codespan, text, nil, { 44 codespan_delimiter: result, 45 location: start_line_number, 46 }) 47 48 else 49 @src.revert_pos(saved_pos) 50 add_text(result) 51 end 52 end
Parse the ordered or unordered list at the current location.
# File lib/kramdown/parser/kramdown/list.rb 154 def parse_definition_list 155 children = @tree.children 156 if !children.last || (children.length == 1 && children.last.type != :p) || 157 (children.length >= 2 && children[-1].type != :p && 158 (children[-1].type != :blank || children[-1].value != "\n" || children[-2].type != :p)) 159 return false 160 end 161 162 first_as_para = false 163 deflist = new_block_el(:dl) 164 para = @tree.children.pop 165 if para.type == :blank 166 para = @tree.children.pop 167 first_as_para = true 168 end 169 # take location from preceding para which is the first definition term 170 deflist.options[:location] = para.options[:location] 171 para.children.first.value.split("\n").each do |term| 172 el = Element.new(:dt, nil, nil, location: @src.current_line_number) 173 term.sub!(self.class::LIST_ITEM_IAL) do 174 parse_attribute_list($1, el.options[:ial] ||= {}) 175 '' 176 end 177 el.options[:raw_text] = term 178 el.children << Element.new(:raw_text, term) 179 deflist.children << el 180 end 181 deflist.options[:ial] = para.options[:ial] 182 183 item = nil 184 content_re, lazy_re, indent_re = nil 185 def_start_re = DEFINITION_LIST_START 186 last_is_blank = false 187 until @src.eos? 188 start_line_number = @src.current_line_number 189 if @src.scan(def_start_re) 190 item = Element.new(:dd, nil, nil, location: start_line_number) 191 item.options[:first_as_para] = first_as_para 192 item.value, indentation, content_re, lazy_re, indent_re = 193 parse_first_list_line(@src[1].length, @src[2]) 194 deflist.children << item 195 196 item.value.sub!(self.class::LIST_ITEM_IAL) do |_match| 197 parse_attribute_list($1, item.options[:ial] ||= {}) 198 '' 199 end 200 201 def_start_re = fetch_pattern(:dl, indentation) 202 first_as_para = false 203 last_is_blank = false 204 elsif @src.match?(EOB_MARKER) 205 break 206 elsif (result = @src.scan(content_re)) || (!last_is_blank && (result = @src.scan(lazy_re))) 207 result.sub!(/^(\t+)/) { " " * ($1 ? 4 * $1.length : 0) } 208 result.sub!(indent_re, '') 209 item.value << result 210 first_as_para = false 211 last_is_blank = false 212 elsif (result = @src.scan(BLANK_LINE)) 213 first_as_para = true 214 item.value << result 215 last_is_blank = true 216 else 217 break 218 end 219 end 220 221 last = nil 222 deflist.children.each do |it| 223 next if it.type == :dt 224 225 parse_blocks(it, it.value) 226 it.value = nil 227 it_children = it.children 228 next if it_children.empty? 229 230 last = (it_children.last.type == :blank ? it_children.pop : nil) 231 232 if it_children.first && it_children.first.type == :p && !it.options.delete(:first_as_para) 233 it_children.first.children.first.value << "\n" if it_children.size > 1 234 it_children.first.options[:transparent] = true 235 end 236 end 237 238 children = @tree.children 239 if children.length >= 1 && children.last.type == :dl 240 children[-1].children.concat(deflist.children) 241 elsif children.length >= 2 && children[-1].type == :blank && 242 children[-2].type == :dl 243 children.pop 244 children[-1].children.concat(deflist.children) 245 else 246 children << deflist 247 end 248 249 children << last if last 250 251 true 252 end
Parse the emphasis at the current location.
# File lib/kramdown/parser/kramdown/emphasis.rb 16 def parse_emphasis 17 start_line_number = @src.current_line_number 18 saved_pos = @src.save_pos 19 20 result = @src.scan(EMPHASIS_START) 21 element = (result.length == 2 ? :strong : :em) 22 type = result[0..0] 23 24 if (type == '_' && (tmp = @src.pre_match).match?(/[[:alnum:]]-?[[:alnum:]]*_*\z/, [tmp.size - 50, 0].max)) || 25 @src.match?(/\s/) || 26 @tree.type == element || @stack.any? {|el, _| el.type == element } 27 add_text(result) 28 return 29 end 30 31 warnings_pos = @warnings.size 32 sub_parse = lambda do |delim, elem| 33 el = Element.new(elem, nil, nil, location: start_line_number) 34 stop_re = /#{Regexp.escape(delim)}/ 35 found = parse_spans(el, stop_re) do 36 (@src.pre_match[-1, 1] !~ /\s/) && 37 (elem != :em || !@src.match?(/#{Regexp.escape(delim * 2)}(?!#{Regexp.escape(delim)})/)) && 38 (type != '_' || !@src.match?(/#{Regexp.escape(delim)}[[:alnum:]]/)) && !el.children.empty? 39 end 40 [found, el, stop_re] 41 end 42 43 found, el, stop_re = sub_parse.call(result, element) 44 if !found && element == :strong && @tree.type != :em 45 @src.revert_pos(saved_pos) 46 @src.pos += 1 47 found, el, stop_re = sub_parse.call(type, :em) 48 end 49 if found 50 # Useful for implementing underlines. 51 el.options[:char] = type 52 53 @src.scan(stop_re) 54 @tree.children << el 55 else 56 @warnings.slice!(0...warnings_pos) 57 @src.revert_pos(saved_pos) 58 @src.pos += result.length 59 add_text(result) 60 end 61 end
Parse the EOB marker at the current location.
# File lib/kramdown/parser/kramdown/eob.rb 16 def parse_eob_marker 17 @src.pos += @src.matched_size 18 @tree.children << new_block_el(:eob) 19 true 20 end
Parse the backslash-escaped character at the current location.
# File lib/kramdown/parser/kramdown/escaped_chars.rb 16 def parse_escaped_chars 17 @src.pos += @src.matched_size 18 add_text(@src[1]) 19 end
Parse the generic extension at the current point. The parameter type can either be :block or :span depending whether we parse a block or span extension tag.
# File lib/kramdown/parser/kramdown/extensions.rb 55 def parse_extension_start_tag(type) 56 saved_pos = @src.save_pos 57 start_line_number = @src.current_line_number 58 @src.pos += @src.matched_size 59 60 error_block = lambda do |msg| 61 warning(msg) 62 @src.revert_pos(saved_pos) 63 add_text(@src.getch) if type == :span 64 false 65 end 66 67 if @src[4] || @src.matched == '{:/}' 68 name = (@src[4] ? "for '#{@src[4]}' " : '') 69 return error_block.call("Invalid extension stop tag #{name} found on line " \ 70 "#{start_line_number} - ignoring it") 71 end 72 73 ext = @src[1] 74 opts = {} 75 body = nil 76 parse_attribute_list(@src[2] || '', opts) 77 78 unless @src[3] 79 stop_re = (type == :block ? /#{EXT_BLOCK_STOP_STR % ext}/ : /#{EXT_STOP_STR % ext}/) 80 if (result = @src.scan_until(stop_re)) 81 body = result.sub!(stop_re, '') 82 body.chomp! if type == :block 83 else 84 return error_block.call("No stop tag for extension '#{ext}' found on line " \ 85 "#{start_line_number} - ignoring it") 86 end 87 end 88 89 if handle_extension(ext, opts, body, type, start_line_number) 90 true 91 else 92 error_block.call("Invalid extension with name '#{ext}' specified on line " \ 93 "#{start_line_number} - ignoring it") 94 end 95 end
Used for parsing the first line of a list item or a definition, i.e. the line with list item marker or the definition marker.
# File lib/kramdown/parser/kramdown/list.rb 33 def parse_first_list_line(indentation, content) 34 if content.match?(self.class::LIST_ITEM_IAL_CHECK) 35 indentation = 4 36 else 37 while content.match?(/^ *\t/) 38 temp = content.scan(/^ */).first.length + indentation 39 content.sub!(/^( *)(\t+)/) { $1 << " " * (4 - (temp % 4) + ($2.length - 1) * 4) } 40 end 41 indentation += content[/^ */].length 42 end 43 content.sub!(/^\s*/, '') 44 45 [content, indentation, *PARSE_FIRST_LIST_LINE_REGEXP_CACHE[indentation]] 46 end
Parse the foot note definition at the current location.
# File lib/kramdown/parser/kramdown/footnote.rb 20 def parse_footnote_definition 21 start_line_number = @src.current_line_number 22 @src.pos += @src.matched_size 23 24 el = Element.new(:footnote_def, nil, nil, location: start_line_number) 25 parse_blocks(el, @src[2].gsub(INDENT, '')) 26 if @footnotes[@src[1]] 27 warning("Duplicate footnote name '#{@src[1]}' on line #{start_line_number} - overwriting") 28 end 29 @tree.children << new_block_el(:eob, :footnote_def) 30 (@footnotes[@src[1]] = {})[:content] = el 31 @footnotes[@src[1]][:eob] = @tree.children.last 32 true 33 end
Parse the footnote marker at the current location.
# File lib/kramdown/parser/kramdown/footnote.rb 39 def parse_footnote_marker 40 start_line_number = @src.current_line_number 41 @src.pos += @src.matched_size 42 fn_def = @footnotes[@src[1]] 43 if fn_def 44 if fn_def[:eob] 45 update_attr_with_ial(fn_def[:eob].attr, fn_def[:eob].options[:ial] || {}) 46 fn_def[:attr] = fn_def[:eob].attr 47 fn_def[:options] = fn_def[:eob].options 48 fn_def.delete(:eob) 49 end 50 fn_def[:marker] ||= [] 51 fn_def[:marker].push(Element.new(:footnote, fn_def[:content], fn_def[:attr], 52 fn_def[:options].merge(name: @src[1], location: start_line_number))) 53 @tree.children << fn_def[:marker].last 54 else 55 warning("Footnote definition for '#{@src[1]}' not found on line #{start_line_number}") 56 add_text(@src.matched) 57 end 58 end
Parse the horizontal rule at the current location.
# File lib/kramdown/parser/kramdown/horizontal_rule.rb 16 def parse_horizontal_rule 17 start_line_number = @src.current_line_number 18 @src.pos += @src.matched_size 19 @tree.children << new_block_el(:hr, nil, nil, location: start_line_number) 20 true 21 end
Parse the HTML entity at the current location.
# File lib/kramdown/parser/kramdown/html_entity.rb 16 def parse_html_entity 17 start_line_number = @src.current_line_number 18 @src.pos += @src.matched_size 19 begin 20 value = ::Kramdown::Utils::Entities.entity(@src[1] || @src[2]&.to_i || @src[3].hex) 21 @tree.children << Element.new(:entity, value, 22 nil, original: @src.matched, location: start_line_number) 23 rescue ::Kramdown::Error 24 @tree.children << Element.new(:entity, ::Kramdown::Utils::Entities.entity('amp'), 25 nil, location: start_line_number) 26 add_text(@src.matched[1..-1]) 27 end 28 end
Parse the inline math at the current location.
# File lib/kramdown/parser/kramdown/math.rb 43 def parse_inline_math 44 start_line_number = @src.current_line_number 45 @src.pos += @src.matched_size 46 @tree.children << Element.new(:math, @src[1].strip, nil, category: :span, location: start_line_number) 47 end
Parse the line break at the current location.
# File lib/kramdown/parser/kramdown/line_break.rb 16 def parse_line_break 17 @tree.children << Element.new(:br, nil, nil, location: @src.current_line_number) 18 @src.pos += @src.matched_size 19 end
Parse the link at the current scanner position. This method is used to parse normal links as well as image links.
# File lib/kramdown/parser/kramdown/link.rb 61 def parse_link 62 start_line_number = @src.current_line_number 63 start_pos = @src.pos 64 result = @src.scan(LINK_START) 65 cur_pos = @src.pos 66 saved_pos = @src.save_pos 67 68 link_type = (result.match?(/^!/) ? :img : :a) 69 70 # no nested links allowed 71 if link_type == :a && (@tree.type == :img || @tree.type == :a || 72 @stack.any? {|t, _| t && (t.type == :img || t.type == :a) }) 73 add_text(result) 74 return 75 end 76 77 # Failed bracket scans are cached per source position to avoid quadratic or even exponential 78 # runtime on unclosed brackets. Since the scan result depends on whether emphasis may be 79 # parsed, the emphasis context (stored in the two lowest bits) is part of the cache key. 80 unless @link_scan_failures_src.equal?(@src) 81 @link_scan_failures_src, @link_scan_failures = @src, {} 82 end 83 context = @stack.inject(LINK_EMPHASIS_CONTEXT[@tree.type]) do |memo, (t, _)| 84 memo | LINK_EMPHASIS_CONTEXT[t&.type] 85 end 86 start_key = (start_pos << 2) | context 87 if @link_scan_failures[start_key] 88 @src.terminate if @tree.type == :a || @tree.type == :img 89 add_text(result) 90 return 91 end 92 93 el = Element.new(link_type, nil, nil, location: start_line_number) 94 95 count = 1 96 brackets = [] 97 found = parse_spans(el, LINK_BRACKET_STOP_RE) do 98 closing = @src[1] 99 count += (closing ? -1 : 1) 100 level = count - el.children.count {|c| c.type == :img } 101 brackets << [(closing || @src.matched_size == 2) ? nil : @src.pos, level] 102 level == 0 103 end 104 unless found 105 # An opening bracket found during the scan can only start a link if a later bracket has a 106 # level of one less than the opening bracket. 107 levels = {} 108 brackets.reverse_each do |pos, level| 109 @link_scan_failures[(pos << 2) | context] = true if pos && !levels[level - 1] 110 levels[level] = true 111 end 112 @link_scan_failures[start_key] = true 113 @src.revert_pos(saved_pos) 114 @src.terminate if @tree.type == :a || @tree.type == :img 115 add_text(result) 116 return 117 end 118 alt_text = extract_string(cur_pos...@src.pos, @src).gsub(ESCAPED_CHARS, '\1') 119 @src.scan(LINK_BRACKET_STOP_RE) 120 121 # reference style link or no link url 122 if @src.scan(LINK_INLINE_ID_RE) || !@src.match?(/\(/) 123 emit_warning = !@src[1] 124 link_id = normalize_link_id(@src[1] || alt_text) 125 if @link_defs.key?(link_id) 126 link_def = @link_defs[link_id] 127 add_link(el, link_def[0], link_def[1], alt_text, 128 link_def[2] && link_def[2].options[:ial]) 129 else 130 if emit_warning 131 warning("No link definition for link ID '#{link_id}' found on line #{start_line_number}") 132 end 133 @src.revert_pos(saved_pos) 134 add_text(result) 135 end 136 return 137 end 138 139 # link url in parentheses 140 if @src.scan(/\(<(.*?)>/) 141 link_url = @src[1] 142 if @src.scan(/\)/) 143 add_link(el, link_url, nil, alt_text) 144 return 145 end 146 else 147 link_url = +'' 148 nr_of_brackets = 0 149 while (temp = @src.scan_until(LINK_PAREN_STOP_RE)) 150 link_url << temp 151 if @src[2] 152 nr_of_brackets -= 1 153 break if nr_of_brackets == 0 154 elsif @src[1] 155 nr_of_brackets += 1 156 else 157 break 158 end 159 end 160 link_url = link_url[1..-2] 161 link_url.strip! 162 163 if nr_of_brackets == 0 164 add_link(el, link_url, nil, alt_text) 165 return 166 end 167 end 168 169 if @src.scan(LINK_INLINE_TITLE_RE) 170 add_link(el, link_url, @src[2], alt_text) 171 else 172 @src.revert_pos(saved_pos) 173 add_text(result) 174 end 175 end
Parse the link definition at the current location.
# File lib/kramdown/parser/kramdown/link.rb 23 def parse_link_definition 24 return false if @src[3].to_s.match?(/[ \t]+["']/) 25 @src.pos += @src.matched_size 26 link_id, link_url, link_title = normalize_link_id(@src[1]), @src[2] || @src[3], @src[5] 27 if @link_defs[link_id] 28 warning("Duplicate link ID '#{link_id}' on line #{@src.current_line_number} - overwriting") 29 end 30 @tree.children << new_block_el(:eob, :link_def) 31 @link_defs[link_id] = [link_url, link_title, @tree.children.last] 32 true 33 end
Parse the ordered or unordered list at the current location.
# File lib/kramdown/parser/kramdown/list.rb 55 def parse_list 56 start_line_number = @src.current_line_number 57 type, list_start_re = (@src.match?(LIST_START_UL) ? [:ul, LIST_START_UL] : [:ol, LIST_START_OL]) 58 list = new_block_el(type, nil, nil, location: start_line_number) 59 60 item = nil 61 content_re, lazy_re, indent_re = nil 62 eob_found = false 63 nested_list_found = false 64 last_is_blank = false 65 until @src.eos? 66 start_line_number = @src.current_line_number 67 if last_is_blank && @src.match?(HR_START) 68 break 69 elsif @src.scan(EOB_MARKER) 70 eob_found = true 71 break 72 elsif @src.scan(list_start_re) 73 list.options[:first_list_marker] ||= @src[1].strip 74 item = Element.new(:li, nil, nil, location: start_line_number) 75 item.value, indentation, content_re, lazy_re, indent_re = 76 parse_first_list_line(@src[1].length, @src[2]) 77 list.children << item 78 79 item.value.sub!(self.class::LIST_ITEM_IAL) do 80 parse_attribute_list($1, item.options[:ial] ||= {}) 81 '' 82 end 83 84 list_start_re = fetch_pattern(type, indentation) 85 nested_list_found = (item.value =~ LIST_START) 86 last_is_blank = false 87 item.value = [item.value] 88 elsif (result = @src.scan(content_re)) || (!last_is_blank && (result = @src.scan(lazy_re))) 89 result.sub!(/^(\t+)/) { " " * 4 * $1.length } 90 indentation_found = result.sub!(indent_re, '') 91 if !nested_list_found && indentation_found && result =~ LIST_START 92 item.value << +'' 93 nested_list_found = true 94 elsif nested_list_found && !indentation_found && result =~ LIST_START 95 result = " " * (indentation + 4) << result 96 end 97 item.value.last << result 98 last_is_blank = false 99 elsif (result = @src.scan(BLANK_LINE)) 100 nested_list_found = true 101 last_is_blank = true 102 item.value.last << result 103 else 104 break 105 end 106 end 107 108 @tree.children << list 109 110 last = nil 111 list.children.each do |it| 112 temp = Element.new(:temp, nil, nil, location: it.options[:location]) 113 114 env = save_env 115 location = it.options[:location] 116 it.value.each do |val| 117 @src = ::Kramdown::Utils::StringScanner.new(val, location) 118 parse_blocks(temp) 119 location = @src.current_line_number 120 end 121 restore_env(env) 122 123 it.children = temp.children 124 it.value = nil 125 126 it_children = it.children 127 next if it_children.empty? 128 129 # Handle the case where an EOB marker is inserted by a block IAL for the first paragraph 130 it_children.delete_at(1) if it_children.first.type == :p && 131 it_children.length >= 2 && it_children[1].type == :eob && it_children.first.options[:ial] 132 133 if it_children.first.type == :p && 134 (it_children.length < 2 || it_children[1].type != :blank || 135 (it == list.children.last && it_children.length == 2 && !eob_found)) && 136 (list.children.last != it || list.children.size == 1 || 137 list.children[0..-2].any? {|cit| !cit.children.first || cit.children.first.type != :p || cit.children.first.options[:transparent] }) 138 it_children.first.children.first.value << "\n" if it_children.size > 1 && it_children[1].type != :blank 139 it_children.first.options[:transparent] = true 140 end 141 142 last = (it_children.last.type == :blank ? it_children.pop : nil) 143 end 144 145 @tree.children << last if !last.nil? && !eob_found 146 147 true 148 end
Parse the paragraph at the current location.
# File lib/kramdown/parser/kramdown/paragraph.rb 30 def parse_paragraph 31 pos = @src.pos 32 start_line_number = @src.current_line_number 33 result = @src.scan(PARAGRAPH_MATCH) 34 until @src.match?(paragraph_end) 35 result << @src.scan(PARAGRAPH_MATCH) 36 end 37 result.rstrip! 38 if (last_child = @tree.children.last) && last_child.type == :p 39 last_item_in_para = last_child.children.last 40 if last_item_in_para && last_item_in_para.type == @text_type 41 joiner = (extract_string((pos - 3)...pos, @src) == " \n" ? " \n" : "\n") 42 last_item_in_para.value << joiner << result 43 else 44 add_text(result, last_child) 45 end 46 else 47 @tree.children << new_block_el(:p, nil, nil, location: start_line_number) 48 result.lstrip! 49 add_text(result, @tree.children.last) 50 end 51 true 52 end
Parse the Setext header at the current location.
# File lib/kramdown/parser/kramdown/header.rb 19 def parse_setext_header 20 return false unless after_block_boundary? 21 text, id = parse_header_contents 22 return false if text.empty? 23 add_header(@src["level"] == '-' ? 2 : 1, text, id) 24 true 25 end
Parse the smart quotes at current location.
# File lib/kramdown/parser/kramdown/smart_quotes.rb 157 def parse_smart_quotes 158 start_line_number = @src.current_line_number 159 substs = SQ_RULES.find {|reg, _subst| @src.scan(reg) }[1] 160 substs.each do |subst| 161 if subst.kind_of?(Integer) 162 add_text(@src[subst]) 163 else 164 val = SQ_SUBSTS[[subst, @src[subst.to_s[-1, 1].to_i]]] || subst 165 @tree.children << Element.new(:smart_quote, val, nil, location: start_line_number) 166 end 167 end 168 end
Parse the extension span at the current location.
# File lib/kramdown/parser/kramdown/extensions.rb 193 def parse_span_extensions 194 if @src.match?(EXT_SPAN_START) 195 parse_extension_start_tag(:span) 196 elsif @src.match?(IAL_SPAN_START) 197 if (last_child = @tree.children.last) && last_child.type != :text 198 @src.pos += @src.matched_size 199 attr = {} 200 parse_attribute_list(@src[1], attr) 201 update_ial_with_ial(last_child.options[:ial] ||= {}, attr) 202 update_attr_with_ial(last_child.attr, attr) 203 else 204 warning("Found span IAL after text - ignoring it") 205 add_text(@src.getch) 206 end 207 else 208 add_text(@src.getch) 209 end 210 end
Parse the HTML at the current position as span-level HTML.
# File lib/kramdown/parser/kramdown/html.rb 102 def parse_span_html 103 line = @src.current_line_number 104 if @src.skip(/<!--/) && (r = @src.exist?(/-->/); @src.pos -= 4; r) 105 result = @src.scan(HTML_COMMENT_RE) 106 @tree.children << Element.new(:xml_comment, result, nil, category: :span, location: line) 107 elsif @src.skip(/<!\[CDATA\[/) && (r = @src.exist?(/\]\]>/); @src.pos -= 9; r) 108 @src.scan(HTML_CDATA_RE) 109 add_text(escape_html(@src[1])) 110 elsif (result = @src.scan(HTML_TAG_CLOSE_RE)) 111 warning("Found invalidly used HTML closing tag for '#{@src[1]}' on line #{line}") 112 add_text(result) 113 elsif (result = @src.scan(HTML_TAG_RE)) 114 tag_name = @src[1] 115 tag_name.downcase! if HTML_ELEMENT[tag_name.downcase] 116 if HTML_BLOCK_ELEMENTS.include?(tag_name) 117 warning("Found block HTML tag '#{tag_name}' in span-level text on line #{line}") 118 add_text(result) 119 return 120 end 121 122 attrs = parse_html_attributes(@src[2], line, HTML_ELEMENT[tag_name]) 123 attrs.each_value {|value| value.gsub!(/\n+/, ' ') unless value.empty? } 124 125 do_parsing = if HTML_CONTENT_MODEL[tag_name] == :raw || @tree.options[:content_model] == :raw 126 false 127 else 128 @options[:parse_span_html] 129 end 130 if (val = HTML_MARKDOWN_ATTR_MAP[attrs.delete('markdown')]) 131 case val 132 when :block 133 warning("Cannot use block-level parsing in span-level HTML tag (line #{line}) " \ 134 "- using default mode") 135 when :span 136 do_parsing = true 137 when :default 138 do_parsing = HTML_CONTENT_MODEL[tag_name] != :raw 139 when :raw 140 do_parsing = false 141 end 142 end 143 144 el = Element.new(:html_element, tag_name, attrs, category: :span, location: line, 145 content_model: (do_parsing ? :span : :raw), is_closed: !@src[4].nil?) 146 @tree.children << el 147 stop_re = /<\/#{Regexp.escape(tag_name)}\s*>/ 148 stop_re = Regexp.new(stop_re.source, Regexp::IGNORECASE) if HTML_ELEMENT[tag_name] 149 if !@src[4] && !HTML_ELEMENTS_WITHOUT_BODY.include?(el.value) 150 if parse_spans(el, stop_re, (do_parsing ? nil : [:span_html])) 151 @src.scan(stop_re) 152 else 153 warning("Found no end tag for '#{el.value}' (line #{line}) - auto-closing it") 154 add_text(@src.rest, el) 155 @src.terminate 156 end 157 end 158 Kramdown::Parser::Html::ElementConverter.convert(@root, el) if @options[:html_to_native] 159 else 160 add_text(@src.getch) 161 end 162 end
Parse the table at the current location.
# File lib/kramdown/parser/kramdown/table.rb 24 def parse_table 25 return false unless after_block_boundary? 26 27 saved_pos = @src.save_pos 28 orig_pos = @src.pos 29 table = new_block_el(:table, nil, nil, alignment: [], location: @src.current_line_number) 30 leading_pipe = (@src.check(TABLE_LINE) =~ /^\s*\|/) 31 @src.scan(TABLE_SEP_LINE) 32 33 rows = [] 34 has_footer = false 35 columns = 0 36 37 add_container = lambda do |type, force| 38 if !has_footer || type != :tbody || force 39 cont = Element.new(type) 40 cont.children, rows = rows, [] 41 table.children << cont 42 end 43 end 44 45 until @src.eos? 46 break unless @src.match?(TABLE_LINE) 47 if @src.scan(TABLE_SEP_LINE) 48 if rows.empty? 49 # nothing to do, ignoring multiple consecutive separator lines 50 elsif table.options[:alignment].empty? && !has_footer 51 add_container.call(:thead, false) 52 table.options[:alignment] = @src[1].scan(TABLE_HSEP_ALIGN).map do |left, right| 53 (left.empty? && right.empty? && :default) || (right.empty? && :left) || 54 (left.empty? && :right) || :center 55 end 56 else # treat as normal separator line 57 add_container.call(:tbody, false) 58 end 59 elsif @src.scan(TABLE_FSEP_LINE) 60 add_container.call(:tbody, true) unless rows.empty? 61 has_footer = true 62 elsif @src.scan(TABLE_ROW_LINE) 63 trow = Element.new(:tr) 64 65 # parse possible code spans on the line and correctly split the line into cells 66 env = save_env 67 cells = [] 68 @src[1].split(/(<code.*?>.*?<\/code>)/).each_with_index do |str, i| 69 if i.odd? 70 (cells.empty? ? cells : cells.last) << str 71 else 72 reset_env(src: Kramdown::Utils::StringScanner.new(str, @src.current_line_number)) 73 root = Element.new(:root) 74 parse_spans(root, nil, [:codespan]) 75 76 root.children.each do |c| 77 if c.type == :raw_text 78 f, *l = c.value.split(/(?<!\\)\|/, -1).map {|t| t.gsub(/\\\|/, '|') } 79 (cells.empty? ? cells : cells.last) << f 80 cells.concat(l) 81 else 82 delim = (c.value.scan(/`+/).max || '') + '`' 83 tmp = +"#{delim}#{' ' if delim.size > 1}#{c.value}#{' ' if delim.size > 1}#{delim}" 84 (cells.empty? ? cells : cells.last) << tmp 85 end 86 end 87 end 88 end 89 restore_env(env) 90 91 cells.shift if leading_pipe && cells.first.strip.empty? 92 cells.pop if cells.last.strip.empty? 93 cells.each do |cell_text| 94 tcell = Element.new(:td) 95 tcell.children << Element.new(:raw_text, cell_text.strip) 96 trow.children << tcell 97 end 98 columns = [columns, cells.length].max 99 rows << trow 100 else 101 break 102 end 103 end 104 105 unless before_block_boundary? 106 @src.revert_pos(saved_pos) 107 return false 108 end 109 110 # Parse all lines of the table with the code span parser 111 env = save_env 112 l_src = ::Kramdown::Utils::StringScanner.new(extract_string(orig_pos...(@src.pos - 1), @src), 113 @src.current_line_number) 114 reset_env(src: l_src) 115 root = Element.new(:root) 116 parse_spans(root, nil, [:codespan, :span_html]) 117 restore_env(env) 118 119 # Check if each line has at least one unescaped pipe that is not inside a code span/code 120 # HTML element 121 # Note: It doesn't matter that we parse *all* span HTML elements because the row splitting 122 # algorithm above only takes <code> elements into account! 123 pipe_on_line = false 124 while (c = root.children.shift) 125 next unless (lines = c.value) 126 lines = lines.split("\n") 127 if c.type == :codespan 128 if lines.size > 2 || (lines.size == 2 && !pipe_on_line) 129 break 130 elsif lines.size == 2 && pipe_on_line 131 pipe_on_line = false 132 end 133 else 134 break if lines.size > 1 && !pipe_on_line && lines.first !~ /^#{TABLE_PIPE_CHECK}/o 135 pipe_on_line = (lines.size > 1 ? false : pipe_on_line) || (lines.last =~ /^#{TABLE_PIPE_CHECK}/o) 136 end 137 end 138 @src.revert_pos(saved_pos) and return false unless pipe_on_line 139 140 add_container.call(has_footer ? :tfoot : :tbody, false) unless rows.empty? 141 142 if table.children.none? {|el| el.type == :tbody } 143 warning("Found table without body on line #{table.options[:location]} - ignoring it") 144 @src.revert_pos(saved_pos) 145 return false 146 end 147 148 # adjust all table rows to have equal number of columns, same for alignment defs 149 table.children.each do |kind| 150 kind.children.each do |row| 151 (columns - row.children.length).times do 152 row.children << Element.new(:td) 153 end 154 end 155 end 156 if table.options[:alignment].length > columns 157 table.options[:alignment] = table.options[:alignment][0...columns] 158 else 159 table.options[:alignment] += [:default] * (columns - table.options[:alignment].length) 160 end 161 162 @tree.children << table 163 164 true 165 end
Parse the typographic symbols at the current location.
# File lib/kramdown/parser/kramdown/typographic_symbol.rb 21 def parse_typographic_syms 22 start_line_number = @src.current_line_number 23 @src.pos += @src.matched_size 24 val = TYPOGRAPHIC_SYMS_SUBST[@src.matched] 25 if val.kind_of?(Symbol) 26 @tree.children << Element.new(:typographic_sym, val, nil, location: start_line_number) 27 elsif @src.matched == '\\<<' 28 @tree.children << Element.new(:entity, ::Kramdown::Utils::Entities.entity('lt'), 29 nil, location: start_line_number) 30 @tree.children << Element.new(:entity, ::Kramdown::Utils::Entities.entity('lt'), 31 nil, location: start_line_number) 32 else 33 @tree.children << Element.new(:entity, ::Kramdown::Utils::Entities.entity('gt'), 34 nil, location: start_line_number) 35 @tree.children << Element.new(:entity, ::Kramdown::Utils::Entities.entity('gt'), 36 nil, location: start_line_number) 37 end 38 end
Replace the abbreviation text with elements.
# File lib/kramdown/parser/kramdown/abbreviation.rb 40 def replace_abbreviations(el, regexps = nil) 41 return if @root.options[:abbrev_defs].empty? 42 unless regexps 43 sorted_abbrevs = @root.options[:abbrev_defs].keys.sort {|a, b| b.length <=> a.length } 44 regexps = [Regexp.union(*sorted_abbrevs.map do |k| 45 /#{Regexp.escape(k).gsub(/\\\s/, "[\\t\\r\\n\\f\\v\\p{Z}]+").force_encoding(Encoding::UTF_8)}/ 46 end)] 47 regexps << /(?=(?:\W|^)#{regexps.first}(?!\w))/ # regexp should only match on word boundaries 48 end 49 el.children.map! do |child| 50 if child.type == :text && el.options[:content_model] != :raw 51 if child.value.match?(regexps.first) 52 result = [] 53 strscan = Kramdown::Utils::StringScanner.new(child.value, child.options[:location]) 54 text_lineno = strscan.current_line_number 55 while (temp = strscan.scan_until(regexps.last)) 56 abbr_lineno = strscan.current_line_number 57 abbr = strscan.scan(regexps.first) # begin of line case of abbr with \W char as first one 58 if abbr.nil? 59 temp << strscan.scan(/\W|^/) 60 abbr = strscan.scan(regexps.first) 61 end 62 result << Element.new(:text, temp, nil, location: text_lineno) 63 result << Element.new(:abbreviation, abbr, nil, location: abbr_lineno) 64 text_lineno = strscan.current_line_number 65 end 66 result << Element.new(:text, strscan.rest, nil, location: text_lineno) 67 else 68 child 69 end 70 else 71 replace_abbreviations(child, regexps) 72 child 73 end 74 end.flatten! 75 end
Update the ial with the information from the inline attribute list opts.
# File lib/kramdown/parser/kramdown/extensions.rb 42 def update_ial_with_ial(ial, opts) 43 (ial[:refs] ||= []).concat(opts[:refs]) if opts.key?(:refs) 44 opts.each do |k, v| 45 if k == IAL_CLASS_ATTR 46 ial[k] = "#{ial[k]} #{v}".lstrip 47 elsif k.kind_of?(String) 48 ial[k] = v 49 end 50 end 51 end
Protected Instance Methods
# File lib/kramdown/parser/kramdown/header.rb 58 def add_header(level, text, id) 59 start_line_number = @src.current_line_number 60 @src.pos += @src.matched_size 61 el = new_block_el(:header, nil, nil, level: level, raw_text: text, location: start_line_number) 62 add_text(text, el) 63 el.attr['id'] = id if id 64 @tree.children << el 65 end
Adapt the object to allow parsing like specified in the options.
# File lib/kramdown/parser/kramdown.rb 121 def configure_parser 122 @parsers = {} 123 (@block_parsers + @span_parsers).each do |name| 124 if self.class.has_parser?(name) 125 @parsers[name] = self.class.parser(name) 126 else 127 raise Kramdown::Error, "Unknown parser: #{name}" 128 end 129 end 130 @span_start, @span_start_re = span_parser_regexps 131 end
Create a new block-level element, taking care of applying a preceding block IAL if it exists. This method should always be used for creating a block-level element!
# File lib/kramdown/parser/kramdown.rb 312 def new_block_el(*args) 313 el = Element.new(*args) 314 if @block_ial 315 el.options[:ial] = @block_ial 316 @block_ial = nil 317 end 318 el 319 end
Parse all block-level elements in text into the element el.
# File lib/kramdown/parser/kramdown.rb 140 def parse_blocks(el, text = nil) 141 @stack_counter += 1 142 raise Kramdown::Error, "Elements nested too deep at block level" if @stack_counter > 100 143 @stack.push([@tree, @src, @block_ial]) 144 @tree, @block_ial = el, nil 145 @src = (text.nil? ? @src : ::Kramdown::Utils::StringScanner.new(text, el.options[:location])) 146 147 status = catch(:stop_block_parsing) do 148 until @src.eos? 149 @block_parsers.any? do |name| 150 if @src.match?(@parsers[name].start_re) 151 send(@parsers[name].method) 152 else 153 false 154 end 155 end || begin 156 warning('Warning: this should not occur - no block parser handled the line') 157 add_text(@src.scan(/.*\n/)) 158 end 159 end 160 end 161 162 @tree, @src, @block_ial = *@stack.pop 163 @stack_counter -= 1 164 status 165 end
Returns header text and optional ID.
# File lib/kramdown/parser/kramdown/header.rb 46 def parse_header_contents 47 text = @src["contents"] 48 text.rstrip! 49 id_match = HEADER_ID.match(text) 50 if id_match 51 id = id_match["id"] 52 text = text[0...-id_match[0].length] 53 text.rstrip! 54 end 55 [text, id] 56 end
Parse all span-level elements in the source string of @src into el.
If the parameter stop_re (a regexp) is used, parsing is immediately stopped if the regexp matches and if no block is given or if a block is given and it returns true.
The parameter parsers can be used to specify the (span-level) parsing methods that should be used for parsing.
The parameter text_type specifies the type which should be used for created text nodes.
# File lib/kramdown/parser/kramdown.rb 218 def parse_spans(el, stop_re = nil, parsers = nil, text_type = @text_type) 219 @stack_counter += 1 220 raise Kramdown::Error, "Elements nested too deep at span level" if @stack_counter > 100 221 @stack.push([@tree, @text_type]) unless @tree.nil? 222 @tree, @text_type = el, text_type 223 224 span_start = @span_start 225 span_start_re = @span_start_re 226 span_start, span_start_re = span_parser_regexps(parsers) if parsers 227 parsers ||= @span_parsers 228 229 used_re = (stop_re.nil? ? span_start_re : span_pattern_cache(stop_re, span_start)) 230 stop_re_found = false 231 while !@src.eos? && !stop_re_found 232 if (result = @src.scan_until(used_re)) 233 add_text(result) 234 if stop_re && @src.match?(stop_re) 235 stop_re_found = (block_given? ? yield : true) 236 end 237 processed = parsers.any? do |name| 238 if @src.match?(@parsers[name].start_re) 239 send(@parsers[name].method) 240 true 241 else 242 false 243 end 244 end unless stop_re_found 245 add_text(@src.getch) if !processed && !stop_re_found 246 else 247 (add_text(@src.rest); @src.terminate) unless stop_re 248 break 249 end 250 end 251 252 @tree, @text_type = @stack.pop 253 @stack_counter -= 1 254 255 stop_re_found 256 end
Reset the current parsing environment. The parameter env can be used to set initial values for one or more environment variables.
# File lib/kramdown/parser/kramdown.rb 260 def reset_env(opts = {}) 261 opts = {text_type: :raw_text, stack: []}.merge(opts) 262 @src = opts[:src] 263 @tree = opts[:tree] 264 @block_ial = opts[:block_ial] 265 @stack = opts[:stack] 266 @text_type = opts[:text_type] 267 end
Restore the current parsing environment.
# File lib/kramdown/parser/kramdown.rb 275 def restore_env(env) 276 @src, @tree, @block_ial, @stack, @text_type = *env 277 end
Return the current parsing environment.
# File lib/kramdown/parser/kramdown.rb 270 def save_env 271 [@src, @tree, @block_ial, @stack, @text_type] 272 end
Create the needed span parser regexps.
# File lib/kramdown/parser/kramdown.rb 134 def span_parser_regexps(parsers = @span_parsers) 135 span_start = /#{parsers.map {|name| @parsers[name].span_start }.join('|')}/ 136 [span_start, /(?=#{span_start})/] 137 end
Update the given attributes hash attr with the information from the inline attribute list ial and all referenced ALDs.
# File lib/kramdown/parser/kramdown.rb 281 def update_attr_with_ial(attr, ial) 282 ial[:refs]&.each do |ref| 283 update_attr_with_ial(attr, ref) if (ref = @alds[ref]) 284 end 285 ial.each do |k, v| 286 if k == IAL_CLASS_ATTR 287 attr[k] = "#{attr[k]} #{v}".lstrip 288 elsif k.kind_of?(String) 289 attr[k] = v 290 end 291 end 292 end
Update the parser specific link definitions with the data from link_defs (the value of the :link_defs option).
The parameter link_defs is a hash where the keys are possibly unnormalized link IDs and the values are two element arrays consisting of the link target and a title (can be nil).
# File lib/kramdown/parser/kramdown.rb 116 def update_link_definitions(link_defs) 117 link_defs.each {|k, v| @link_defs[normalize_link_id(k)] = v } 118 end
Update the raw text for automatic ID generation.
# File lib/kramdown/parser/kramdown.rb 295 def update_raw_text(item) 296 raw_text = +'' 297 298 append_text = lambda do |child| 299 if child.type == :text 300 raw_text << child.value 301 else 302 child.children.each {|c| append_text.call(c) } 303 end 304 end 305 306 append_text.call(item) 307 item.options[:raw_text] = raw_text 308 end
Update the tree by parsing all :raw_text elements with the span-level parser (resets the environment) and by updating the attributes from the IALs.
# File lib/kramdown/parser/kramdown.rb 169 def update_tree(element) 170 last_blank = nil 171 element.children.map! do |child| 172 case child.type 173 when :raw_text 174 last_blank = nil 175 reset_env(src: ::Kramdown::Utils::StringScanner.new(child.value, element.options[:location]), 176 text_type: :text) 177 parse_spans(child) 178 child.children 179 when :eob 180 update_attr_with_ial(child.attr, child.options[:ial]) if child.options[:ial] 181 [] 182 when :blank 183 if last_blank 184 last_blank.value << child.value 185 [] 186 else 187 last_blank = child 188 child 189 end 190 else 191 last_blank = nil 192 update_tree(child) 193 update_attr_with_ial(child.attr, child.options[:ial]) if child.options[:ial] 194 # DEPRECATED: option auto_id_stripping will be removed in 2.0 because then this will be 195 # the default behaviour 196 if child.type == :dt || (child.type == :header && @options[:auto_id_stripping]) 197 update_raw_text(child) 198 end 199 child 200 end 201 end.flatten! 202 end
Private Instance Methods
precomputed patterns for indentations 1..4 and fallback expression to compute pattern when indentation is outside the 1..4 range.
# File lib/kramdown/parser/kramdown/list.rb 259 def fetch_pattern(type, indentation) 260 case type 261 when :ul 262 case indentation 263 when 1 then /^( {0}[+*-])(#{PATTERN_TAIL})/o 264 when 2 then /^( {0,1}[+*-])(#{PATTERN_TAIL})/o 265 when 3 then /^( {0,2}[+*-])(#{PATTERN_TAIL})/o 266 else /^( {0,3}[+*-])(#{PATTERN_TAIL})/o 267 end 268 when :ol 269 case indentation 270 when 1 then /^( {0}\d+\.)(#{PATTERN_TAIL})/o 271 when 2 then /^( {0,1}\d+\.)(#{PATTERN_TAIL})/o 272 when 3 then /^( {0,2}\d+\.)(#{PATTERN_TAIL})/o 273 else /^( {0,3}\d+\.)(#{PATTERN_TAIL})/o 274 end 275 when :dl 276 case indentation 277 when 1 then /^( {0}:)(#{PATTERN_TAIL})/o 278 when 2 then /^( {0,1}:)(#{PATTERN_TAIL})/o 279 when 3 then /^( {0,2}:)(#{PATTERN_TAIL})/o 280 else /^( {0,3}:)(#{PATTERN_TAIL})/o 281 end 282 end 283 end
# File lib/kramdown/parser/kramdown.rb 204 def span_pattern_cache(stop_re, span_start) 205 @span_pattern_cache[stop_re][span_start] ||= /(?=#{Regexp.union(stop_re, span_start)})/ 206 end