Module: MaRuKu::Helpers

Included in:
In::Markdown::BlockLevelParser, In::Markdown::SpanLevelParser, MDElement
Defined in:
lib/maruku/helpers.rb,
lib/maruku.rb

Overview

A collection of helper functions for creating Markdown elements. They hide the particular internal representations.

Always use these rather than creating an MDElement directly.

Instance Method Summary collapse

Instance Method Details

#md_abbr(abbr, title) ⇒ Object



60
61
62
# File 'lib/maruku/helpers.rb', line 60

def md_abbr(abbr, title)
  md_el(:abbr, abbr, :title => title)
end

#md_abbr_def(abbr, text, al = nil) ⇒ Object



56
57
58
# File 'lib/maruku/helpers.rb', line 56

def md_abbr_def(abbr, text, al=nil)
  md_el(:abbr_def, [], { :abbr => abbr, :text => text }, al)
end

#md_ald(id, al) ⇒ Object

Attribute list definition



152
153
154
# File 'lib/maruku/helpers.rb', line 152

def md_ald(id, al)
  md_el(:ald, [], :ald_id => id, :ald => al)
end

#md_brObject



98
99
100
# File 'lib/maruku/helpers.rb', line 98

def md_br
  md_el(:linebreak, [], {}, nil)
end

#md_code(code, al = nil) ⇒ Object

Inline code



35
36
37
# File 'lib/maruku/helpers.rb', line 35

def md_code(code, al=nil)
  md_el(:inline_code, [], { :raw_code => code }, al)
end

#md_codeblock(source, lang = nil, al = nil) ⇒ Object

Code block



40
41
42
# File 'lib/maruku/helpers.rb', line 40

def md_codeblock(source, lang=nil, al=nil)
  md_el(:code, [], { :raw_code => source, :lang => lang }, al)
end

#md_el(node_type, children = [], meta = {}, al = nil) ⇒ Object

Parameters:



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/maruku/helpers.rb', line 12

def md_el(node_type, children=[], meta={}, al=nil)
  children = Array(children)

  first = children.first
  if first && first.is_a?(MDElement) && first.node_type == :ial
    if al
      al += first.ial
    else
      al = first.ial
    end
    children.shift
  end

  e = MDElement.new(node_type, children, meta, al)
  e.doc = @doc
  e
end

#md_em(children, al = nil) ⇒ Object



94
95
96
# File 'lib/maruku/helpers.rb', line 94

def md_em(children, al=nil)
  md_el(:emphasis, children, {}, al)
end

#md_email(email, al = nil) ⇒ Object

An email to be linkified (e.g. ‘<[email protected]>` or `<[email protected]>`).



121
122
123
# File 'lib/maruku/helpers.rb', line 121

def md_email(email, al=nil)
  md_el(:email_address, [], { :email => email }, al)
end

#md_emstrong(children, al = nil) ⇒ Object



110
111
112
# File 'lib/maruku/helpers.rb', line 110

def md_emstrong(children, al=nil)
  md_strong(md_em(children), al)
end

#md_entity(entity_name, al = nil) ⇒ Object



125
126
127
# File 'lib/maruku/helpers.rb', line 125

def md_entity(entity_name, al=nil)
  md_el(:entity, [], { :entity_name => entity_name }, al)
end

#md_foot_ref(ref_id, al = nil) ⇒ Object

Markdown extra



130
131
132
# File 'lib/maruku/helpers.rb', line 130

def md_foot_ref(ref_id, al=nil)
  md_el(:footnote_reference, [], { :footnote_id => ref_id }, al)
end

#md_footnote(footnote_id, children, al = nil) ⇒ Object



52
53
54
# File 'lib/maruku/helpers.rb', line 52

def md_footnote(footnote_id, children, al=nil)
  md_el(:footnote, children, { :footnote_id => footnote_id }, al)
end

#md_header(level, children, al = nil) ⇒ Object



30
31
32
# File 'lib/maruku/helpers.rb', line 30

def md_header(level, children, al=nil)
  md_el(:header, children, { :level => level }, al)
end

#md_hruleObject



102
103
104
# File 'lib/maruku/helpers.rb', line 102

def md_hrule
  md_el(:hrule, [], {}, nil)
end

#md_html(raw_html, al = nil) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/maruku/helpers.rb', line 64

def md_html(raw_html, al=nil)
  e = MDHTMLElement.new(:raw_html, [], :raw_html => raw_html)
  e.doc = @doc
  begin
    # Set this as an attribute so it doesn't get included
    # in metadata comparisons
    e.parsed_html = MaRuKu::HTMLFragment.new(raw_html)
  rescue => ex
    maruku_recover "Maruku cannot parse this block of HTML/XML:\n" +
      raw_html.gsub(/^/, '|').rstrip + "\n" + ex.to_s
  end
  e
end

#md_ial(al) ⇒ Object

inline attribute list



146
147
148
149
# File 'lib/maruku/helpers.rb', line 146

def md_ial(al)
  al = Maruku::AttributeList.new(al) unless al.is_a?(Maruku::AttributeList)
  md_el(:ial, [], :ial => al)
end

#md_im_image(children, url, title = nil, al = nil) ⇒ Object



90
91
92
# File 'lib/maruku/helpers.rb', line 90

def md_im_image(children, url, title=nil, al=nil)
  md_el(:im_image, children, { :url => url, :title => title }, al)
end


82
83
84
# File 'lib/maruku/helpers.rb', line 82

def md_im_link(children, url, title = nil, al=nil)
  md_el(:im_link, children, { :url => url, :title => title }, al)
end

#md_image(children, ref_id, al = nil) ⇒ Object



86
87
88
# File 'lib/maruku/helpers.rb', line 86

def md_image(children, ref_id, al=nil)
  md_el(:image, children, { :ref_id => ref_id }, al)
end

#md_li(children, want_my_par = false, al = nil) ⇒ Object



48
49
50
# File 'lib/maruku/helpers.rb', line 48

def md_li(children, want_my_par=false, al=nil)
  md_el(:li, children, { :want_my_paragraph => want_my_par }, al)
end


78
79
80
# File 'lib/maruku/helpers.rb', line 78

def md_link(children, ref_id, al=nil)
  md_el(:link, children, { :ref_id => ref_id }, al)
end

#md_par(children, al = nil) ⇒ Object



134
135
136
# File 'lib/maruku/helpers.rb', line 134

def md_par(children, al=nil)
  md_el(:paragraph, children, {}, al)
end

#md_quote(children, al = nil) ⇒ Object



44
45
46
# File 'lib/maruku/helpers.rb', line 44

def md_quote(children, al=nil)
  md_el(:quote, children, {}, al)
end

#md_ref_def(ref_id, url, title = nil, meta = {}, al = nil) ⇒ Object

A definition of a reference (e.g. ‘[1]: url [properties]`).



139
140
141
142
143
# File 'lib/maruku/helpers.rb', line 139

def md_ref_def(ref_id, url, title=nil, meta={}, al=nil)
  all_meta = meta.merge({ :url => url, :ref_id => ref_id })
  all_meta[:title] ||= title
  md_el(:ref_definition, [], all_meta, al)
end

#md_strong(children, al = nil) ⇒ Object



106
107
108
# File 'lib/maruku/helpers.rb', line 106

def md_strong(children, al=nil)
  md_el(:strong, children, {}, al)
end

#md_url(url, al = nil) ⇒ Object

A URL to be linkified (e.g. ‘<www.example.com/>`).



115
116
117
# File 'lib/maruku/helpers.rb', line 115

def md_url(url, al=nil)
  md_el(:immediate_link, [], { :url => url }, al)
end

#md_xml_instr(target, code) ⇒ Object

A server directive (e.g. ‘<?target code… ?>`)



157
158
159
# File 'lib/maruku/helpers.rb', line 157

def md_xml_instr(target, code)
  md_el(:xml_instr, [], :target => target, :code => code)
end