Class: MarkupNode

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/assets/lib/markup_node.rb

Overview

MarkupNode is an wrapper introduced to simplify

producing Pandoc markdown output.
It's a delegator to the Node class

Constant Summary collapse

@@macros =

register available macros

Array.new.tap do |ary|
  ary << SkipMacro.new
  ary << ListMacro.new
  ary << TreeMacro.new
  ary << EvalMacro.new
  ary.freeze
end

Instance Method Summary collapse

Instance Method Details

#bodyString

Returns output text for node.body.

Returns:

  • (String)

    output text for node.body



46
47
48
49
50
51
52
# File 'lib/assets/lib/markup_node.rb', line 46

def body
  String.new(super).tap do |txt|
    process_links!(txt)
    process_macro!(txt)
    txt.gsub!(/^$\n{2,}/, "\n")
  end
end

Returns output text for link [[node.id]].

Returns:

  • (String)

    output text for link [[node.id]]



55
56
57
58
59
# File 'lib/assets/lib/markup_node.rb', line 55

def link(ref)
  node = root.find{|n| n.id == ref}
  return "[#{ref}](#unknown)" unless node
  "[#{node.title}](##{url(ref)})"
end

#markupObject



18
19
20
# File 'lib/assets/lib/markup_node.rb', line 18

def markup
  [title, meta, body].select{|part| !part.empty?}.join("\n\n")
end

#metaString

Returns output text of node.meta.

Returns:

  • (String)

    output text of node.meta



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/assets/lib/markup_node.rb', line 30

def meta
  return '' if nesting_level == 0
  return '' if super[:skip_meta]

  hsh = {id: id}.merge(super)
  hsh.delete(:order_index)
  hsh.delete(:filename)
  hsh.delete(:parent)
  [].tap{|ary|
    ary << "Attribute | Value"
    ary << "--------- | -----"
    hsh.each{|k,v| ary << "#{k} | #{v}"}
  }.join("\n")
end

#titleString

Returns output text of node.title.

Returns:

  • (String)

    output text of node.title



23
24
25
26
27
# File 'lib/assets/lib/markup_node.rb', line 23

def title
  s = super
  s = ".#{id.split(/\./).last}" if s.empty?
  "#{'#' * nesting_level} #{s} {##{url(id)}}"
end

#url(id) ⇒ String

Returns url for node.id.

Returns:

  • (String)

    url for node.id



62
63
64
65
66
67
68
# File 'lib/assets/lib/markup_node.rb', line 62

def url(id)
  r = id.start_with?(/[[:digit:]]/) ? "p#{id}" : id
  r.downcase
   .gsub(/[^A-Za-z0-9]{1,}/, '-')
   .gsub(/^-/, '')
   .gsub(/-$/, '')
end