Class: Kramdown::Parser::Kramdown

Inherits:
Object
  • Object
show all
Defined in:
lib/darkmouun/kramdown/parser/kramdown/link.rb,
lib/darkmouun/kramdown/parser/kramdown/span.rb,
lib/darkmouun/kramdown/parser/kramdown/extensions.rb

Constant Summary collapse

ALD_TYPE_STYLE_ATTR =
/%((?:--)?#{ALD_ID_NAME}:)\s*?((?:\\\}|\\;|[^\};])*?;)/
ALD_TYPE_ANY =
/(?:\A|\s)(?:#{ALD_TYPE_KEY_VALUE_PAIR}|#{ALD_TYPE_STYLE_ATTR}|#{ALD_TYPE_REF}|#{ALD_TYPE_ID_OR_CLASS_MULTI})(?=\s|\Z)/

Instance Method Summary collapse

Instance Method Details

#parse_attribute_list(str, opts) ⇒ Object

Parse the string str and extract all attributes and add all found attributes to the hash opts.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/darkmouun/kramdown/parser/kramdown/extensions.rb', line 16

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 the link at the current scanner position. This method is used to parse normal links as well as image links, plain spans.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/darkmouun/kramdown/parser/kramdown/link.rb', line 18

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_spanObject

Parse the span at the current location.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/darkmouun/kramdown/parser/kramdown/span.rb', line 10

def parse_span
  start_line_number = @src.current_line_number
  saved_pos = @src.save_pos

  stop_re = /(\](?=\{\:))/

  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)
    add_text('[')
  end
end