Class: Kramdown::Converter::Confluence

Inherits:
Base
  • Object
show all
Defined in:
lib/markdown2confluence/convertor/confluence.rb

Overview

Converts a Kramdown::Document to Confluence ML

You can customize the Confluence converter by sub-classing it and overriding the convert_NAME methods. Each such method takes the following parameters:

el

The element of type NAME to be converted.

indent

A number representing the current amount of spaces for indent (only used for block-level elements).

The return value of such a method has to be a string containing the element el formatted as Confluence element.

Constant Summary collapse

DISPATCHER =

The mapping of element type to conversion method.

Hash.new {|h,k| h[k] = "convert_#{k}"}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, options) ⇒ Confluence

Initialize the Confluence converter with the given Kramdown document doc.



45
46
47
48
49
# File 'lib/markdown2confluence/convertor/confluence.rb', line 45

def initialize(root, options)
  super
  @indent = 2
  @stack = []
end

Instance Attribute Details

#indentObject

The amount of indentation used when nesting Confluence tags.



42
43
44
# File 'lib/markdown2confluence/convertor/confluence.rb', line 42

def indent
  @indent
end

Instance Method Details

#convert(el, indent = -@indent)) ⇒ Object

Dispatch the conversion of the element el to a convert_TYPE method using the type of the element.



56
57
58
# File 'lib/markdown2confluence/convertor/confluence.rb', line 56

def convert(el, indent = -@indent)
  send(DISPATCHER[el.type], el, indent)
end

#convert_a(el, indent) ⇒ Object



162
163
164
165
166
# File 'lib/markdown2confluence/convertor/confluence.rb', line 162

def convert_a(el, indent)
  text   = inner(el,indent)
  link   = el.attr['href']
  "[#{text+'|' unless text.nil?}#{link}]"
end

#convert_abbreviation(el, indent) ⇒ Object



218
219
220
# File 'lib/markdown2confluence/convertor/confluence.rb', line 218

def convert_abbreviation(el, indent)
  inner(el,indent)
end

#convert_blank(el, indent) ⇒ Object



76
77
78
# File 'lib/markdown2confluence/convertor/confluence.rb', line 76

def convert_blank(el, indent)
  "\n"
end

#convert_blockquote(el, indent) ⇒ Object



88
89
90
# File 'lib/markdown2confluence/convertor/confluence.rb', line 88

def convert_blockquote(el, indent)
  "#{' '*indent}bq. #{inner(el, indent)}\n"
end

#convert_br(el, indent) ⇒ Object



158
159
160
# File 'lib/markdown2confluence/convertor/confluence.rb', line 158

def convert_br(el, indent)
  "\\"
end

#convert_codeblock(el, indent) ⇒ Object



174
175
176
# File 'lib/markdown2confluence/convertor/confluence.rb', line 174

def convert_codeblock(el, indent)
  "{code}#{el.value}{code}\n"
end

#convert_codespan(el, indent) ⇒ Object



178
179
180
181
182
183
184
# File 'lib/markdown2confluence/convertor/confluence.rb', line 178

def convert_codespan(el, indent)
  if  el.value.strip.match(/\n/)
    "{code}#{el.value}{code}\n"
  else
    "{{#{el.value.strip}}}"
  end
end

#convert_comment(el, indent) ⇒ Object



154
155
156
# File 'lib/markdown2confluence/convertor/confluence.rb', line 154

def convert_comment(el, indent)
  inner(el, indent)
end

#convert_dt(el, indent) ⇒ Object



113
114
115
# File 'lib/markdown2confluence/convertor/confluence.rb', line 113

def convert_dt(el, indent)
  inner(el, indent)
end

#convert_em(el, indent) ⇒ Object



194
195
196
# File 'lib/markdown2confluence/convertor/confluence.rb', line 194

def convert_em(el, indent)
  "_#{inner(el, indent)}_"
end

#convert_empty(el, indent) ⇒ Object Also known as: convert_tbody, convert_tfoot, convert_tr



143
144
145
# File 'lib/markdown2confluence/convertor/confluence.rb', line 143

def convert_empty(el, indent)
  ""
end

#convert_entity(el, indent) ⇒ Object



202
203
204
# File 'lib/markdown2confluence/convertor/confluence.rb', line 202

def convert_entity(el, indent)
  inner(el,indent)
end

#convert_footnote(el, indent) ⇒ Object



186
187
188
# File 'lib/markdown2confluence/convertor/confluence.rb', line 186

def convert_footnote(el, indent)
  inner(el, indent)
end

#convert_header(el, indent) ⇒ Object



92
93
94
# File 'lib/markdown2confluence/convertor/confluence.rb', line 92

def convert_header(el, indent)
  "h#{el.options[:level]}. #{inner(el, indent)}\n"
end

#convert_hr(el, indent) ⇒ Object



96
97
98
# File 'lib/markdown2confluence/convertor/confluence.rb', line 96

def convert_hr(el, indent)
  "#{' '*indent}----\n"
end

#convert_html_element(el, indent) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/markdown2confluence/convertor/confluence.rb', line 117

def convert_html_element(el, indent)
  markup=case el.value
         when "iframe" then "{iframe:src=#{el.attr["src"]}}"
         when "pre" then
           if inner(el,indent).strip.match(/\n/)
             "{code}#{inner(el,indent)}{code}"
           else
             "{{#{inner(el,indent).strip}}}"
           end
         else inner(el, indent)
         end
end

#convert_img(el, indent) ⇒ Object



168
169
170
171
172
# File 'lib/markdown2confluence/convertor/confluence.rb', line 168

def convert_img(el, indent)
  src = el.attr['src']
  alt = el.attr['alt']
  alt.to_s.empty? ? "!#{src}!" : "!#{src}|alt=#{alt}!"
end

#convert_li(el, indent) ⇒ Object Also known as: convert_dd



107
108
109
# File 'lib/markdown2confluence/convertor/confluence.rb', line 107

def convert_li(el, indent)
  "#{'-'*(indent/2)}#{inner(el, 0)}"
end

#convert_math(el, indent) ⇒ Object



214
215
216
# File 'lib/markdown2confluence/convertor/confluence.rb', line 214

def convert_math(el, indent)
  inner(el,indent)
end

#convert_p(el, indent) ⇒ Object



84
85
86
# File 'lib/markdown2confluence/convertor/confluence.rb', line 84

def convert_p(el, indent)
  "#{' '*indent}#{inner(el, indent)}\n"
end

#convert_raw(el, indent) ⇒ Object



190
191
192
# File 'lib/markdown2confluence/convertor/confluence.rb', line 190

def convert_raw(el, indent)
  inner(el, indent)
end

#convert_root(el, indent) ⇒ Object



222
223
224
# File 'lib/markdown2confluence/convertor/confluence.rb', line 222

def convert_root(el, indent)
  handle_iframes(inner(el, indent))
end

#convert_smart_quote(el, indent) ⇒ Object



210
211
212
# File 'lib/markdown2confluence/convertor/confluence.rb', line 210

def convert_smart_quote(el, indent)
  "'"
end

#convert_strong(el, indent) ⇒ Object



198
199
200
# File 'lib/markdown2confluence/convertor/confluence.rb', line 198

def convert_strong(el, indent)
  "*#{inner(el, indent)}*"
end

#convert_table(el, indent) ⇒ Object



135
136
137
# File 'lib/markdown2confluence/convertor/confluence.rb', line 135

def convert_table(el, indent)
  ""
end

#convert_td(el, indent) ⇒ Object



150
151
152
# File 'lib/markdown2confluence/convertor/confluence.rb', line 150

def convert_td(el, indent)
  inner(el, indent)
end

#convert_text(el, indent) ⇒ Object



80
81
82
# File 'lib/markdown2confluence/convertor/confluence.rb', line 80

def convert_text(el, indent)
  el.value
end

#convert_thead(el, indent) ⇒ Object



139
140
141
# File 'lib/markdown2confluence/convertor/confluence.rb', line 139

def convert_thead(el, indent)
  inner(el, indent)
end

#convert_typographic_sym(el, indent) ⇒ Object



206
207
208
# File 'lib/markdown2confluence/convertor/confluence.rb', line 206

def convert_typographic_sym(el, indent)
  inner(el,indent)
end

#convert_ul(el, indent) ⇒ Object Also known as: convert_ol, convert_dl



100
101
102
# File 'lib/markdown2confluence/convertor/confluence.rb', line 100

def convert_ul(el, indent)
  inner(el,indent)
end

#convert_xml_comment(el, indent) ⇒ Object Also known as: convert_xml_pi



130
131
132
# File 'lib/markdown2confluence/convertor/confluence.rb', line 130

def convert_xml_comment(el, indent)
  ""
end

#handle_iframes(text) ⇒ Object



226
227
228
229
230
231
232
233
234
# File 'lib/markdown2confluence/convertor/confluence.rb', line 226

def handle_iframes(text)
  markup=text.gsub(/<iframe.*iframe>/) { |match| 
    doc =Nokogiri::HTML::DocumentFragment.parse(match)
    element=doc.search('iframe').first
    attributes=element.attributes.map{ |at| "#{at[1].name}=#{at[1].value}"}
    "{iframe:#{attributes.join('|')}}#{element.text}{iframe}"
  }
  return markup
end

#inner(el, indent) ⇒ Object

Return the converted content of the children of el as a string. The parameter indent has to be the amount of indentation used for the element el.

Pushes el onto the @stack before converting the child elements and pops it from the stack afterwards.



65
66
67
68
69
70
71
72
73
74
# File 'lib/markdown2confluence/convertor/confluence.rb', line 65

def inner(el, indent)
  result = ''
  indent += @indent
  @stack.push(el)
  el.children.each do |inner_el|
    result << send(DISPATCHER[inner_el.type], inner_el, indent)
  end
  @stack.pop
  result
end