File: inspect_element.rb

package info (click to toggle)
ruby-maruku 0.7.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, forky, sid, trixie
  • size: 1,304 kB
  • sloc: ruby: 5,741; xml: 235; makefile: 2
file content (60 lines) | stat: -rw-r--r-- 2,015 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
module MaRuKu
  class MDElement
    INSPECT_FORMS = {
      :paragraph          => ["par",      :children],
      :footnote_reference => ["foot_ref", :footnote_id],
      :entity             => ["entity",   :entity_name],
      :email_address      => ["email",    :email],
      :inline_code        => ["code",     :raw_code],
      :raw_html           => ["html",     :raw_html],
      :emphasis           => ["em",       :children],
      :strong             => ["strong",   :children],
      :immediate_link     => ["url",      :url],
      :image              => ["image",    :children, :ref_id],
      :im_image           => ["im_image", :children, :url, :title],
      :link               => ["link",     :children, :ref_id],
      :im_link            => ["im_link",  :children, :url, :title],
      :ref_definition     => ["ref_def",  :ref_id, :url, :title],
      :ial                => ["ial",      :ial],
      :li                 => ["li",       :children, :want_my_paragraph]
    }

    # Outputs the document AST as calls to document helpers.
    # (this should be `eval`-able to get a copy of the original element).
    def inspect
      if INSPECT_FORMS.has_key? @node_type
        name, *params = INSPECT_FORMS[@node_type]

        params = params.map do |p|
          if p == :children
            children_inspect
          else
            send(p).inspect
          end
        end
        params << @al.inspect if @al && !@al.empty?
      else
        name = 'el'
        params = [self.node_type.inspect, children_inspect]
        params << @meta_priv.inspect unless @meta_priv.empty? && self.al.empty?
        params << self.al.inspect unless self.al.empty?
      end

      "md_#{name}(#{params.join(', ')})"
    end

    private

    def children_inspect
      kids = @children.map(&:inspect)
      return kids.first if kids.size == 1

      comma = kids.join(", ")
      if comma.size < 70
        "[#{comma}]"
      else
        "[\n\t#{kids.join(",\n\t")}\n]"
      end
    end
  end
end