class Kramdown::Parser::Kramdown
Constants
- ALD_TYPE_ANY
- ALD_TYPE_STYLE_ATTR
Public Instance Methods
parse_attribute_list(str, opts)
click to toggle source
Parse the string str
and extract all attributes and add all found attributes to the hash opts
.
# File lib/darkmouun/kramdown/parser/kramdown/extensions.rb, line 15 def parse_attribute_list(str, opts) return if str.strip.empty? || str.strip == ':' style_attr = [] attrs = str.scan(ALD_TYPE_ANY) attrs.each do |key, sep, val, style_key, style_val, ref, id_and_or_class, _, _| if ref (opts[:refs] ||= []) << ref elsif id_and_or_class id_and_or_class.scan(ALD_TYPE_ID_OR_CLASS).each do |id_attr, class_attr| if class_attr opts[IAL_CLASS_ATTR] = "#{opts[IAL_CLASS_ATTR]} #{class_attr}".lstrip else opts['id'] = id_attr end end elsif style_key style_attr << (style_key + style_val) else val.gsub!(/\\(\}|#{sep})/, "\\1") if /\Astyle\Z/i =~ key style_attr << val else opts[key] = val end end end (opts['style'] = style_attr.join(' ')) unless style_attr.empty? warning("No or invalid attributes found in IAL/ALD content: #{str}") if attrs.empty? end
parse_link()
click to toggle source
Parse the link at the current scanner position. This method is used to parse normal links as well as image links, plain spans.
# File lib/darkmouun/kramdown/parser/kramdown/link.rb, line 17 def parse_link start_line_number = @src.current_line_number result = @src.scan(LINK_START) cur_pos = @src.pos saved_pos = @src.save_pos link_type = (result =~ /^!/ ? :img : :a) # no nested links allowed if link_type == :a && (@tree.type == :img || @tree.type == :a || @stack.any? {|t, _| t && (t.type == :img || t.type == :a) }) add_text(result) return end el = Element.new(link_type, nil, nil, location: start_line_number) count = 1 found = parse_spans(el, LINK_BRACKET_STOP_RE) do count += (@src[1] ? -1 : 1) count - el.children.select {|c| c.type == :img }.size == 0 end unless found @src.revert_pos(saved_pos) add_text(result) return end alt_text = extract_string(cur_pos...@src.pos, @src).gsub(ESCAPED_CHARS, '\1') @src.scan(LINK_BRACKET_STOP_RE) # reference style link or no link url if @src.scan(LINK_INLINE_ID_RE) || !@src.check(/\(/) emit_warning = !@src[1] link_id = normalize_link_id(@src[1] || alt_text) if @link_defs.key?(link_id) link_def = @link_defs[link_id] add_link(el, link_def[0], link_def[1], alt_text, link_def[2] && link_def[2].options[:ial]) else if emit_warning warning("No link definition for link ID '#{link_id}' found on line #{start_line_number}") end @src.revert_pos(saved_pos) if @src.check(/./) == ']' add_text(result) else parse_span end end return end # link url in parentheses if @src.scan(/\(<(.*?)>/) link_url = @src[1] if @src.scan(/\)/) add_link(el, link_url, nil, alt_text) return end else link_url = +'' nr_of_brackets = 0 while (temp = @src.scan_until(LINK_PAREN_STOP_RE)) link_url << temp if @src[2] nr_of_brackets -= 1 break if nr_of_brackets == 0 elsif @src[1] nr_of_brackets += 1 else break end end link_url = link_url[1..-2] link_url.strip! if nr_of_brackets == 0 add_link(el, link_url, nil, alt_text) return end end if @src.scan(LINK_INLINE_TITLE_RE) add_link(el, link_url, @src[2], alt_text) else @src.revert_pos(saved_pos) add_text(result) end end
parse_span()
click to toggle source
Parse the span at the current location.
# File lib/darkmouun/kramdown/parser/kramdown/span.rb, line 9 def parse_span start_line_number = @src.current_line_number saved_pos = @src.save_pos span_start = /(?:\[\s*?)/ result = @src.scan(span_start) stop_re = /(?:\s*?\])/ el = Element.new(:span, nil, nil, :location => start_line_number) found = parse_spans(el, stop_re) do el.children.size > 0 end if found @src.scan(stop_re) @tree.children << el else @src.revert_pos(saved_pos) @src.pos += result.length add_text(result) end end