Class: Md2conf::ConfluenceUtil

Inherits:
Object
  • Object
show all
Defined in:
lib/md2conf.rb

Overview

ConfluenceUtil class contains various helpers for processing XHTML generated by redcarpet gem.

Instance Method Summary collapse

Constructor Details

#initialize(html, max_toc_level, config_file) ⇒ ConfluenceUtil

Returns a new instance of ConfluenceUtil.

Parameters:

  • html (String)

    XHTML rendered by redcarpet gem (must have fenced code blocks).

  • max_toc_level (Integer)

    Table of Contents maximum header depth.



15
16
17
18
19
20
21
22
# File 'lib/md2conf.rb', line 15

def initialize(html, max_toc_level, config_file)
  @html          = html
  @max_toc_level = max_toc_level
  return unless File.file?(File.expand_path(config_file))
  @config = YAML.load_file(File.expand_path(config_file))
  return unless @config.key? 'macros'
  @macros = @config['macros']
end

Instance Method Details

#add_tocObject

Add Table of Contents Confluence tag at the beginning.

Use @max_toc_level class variable to specify maximum header depth.



157
158
159
160
161
162
163
164
165
# File 'lib/md2conf.rb', line 157

def add_toc
  return if @max_toc_level == 0
  @html = "    <ac:structured-macro ac:name=\"toc\">\n      <ac:parameter ac:name=\"maxLevel\">\#{@max_toc_level}</ac:parameter>\n    </ac:structured-macro>\n    \#{@html}\n  HTML\nend\n"

#convert_info_macrosObject

Convert Info macros to Confluence-friendly format:

Examples:

Regular informational message

> My info message

A Note (‘Note: ` will be removed from the message)

> Note: An important note

A Warning (‘Warning: ` will be removed from the message)

> Warning: An extremely important warning


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/md2conf.rb', line 102

def convert_info_macros
  confluence_code = "    <ac:structured-macro ac:name=\"%<macro_name>s\">\n      <ac:rich-text-body>\n        %<quote>s\n      </ac:rich-text-body>\n    </ac:structured-macro>\n  HTML\n\n  @html.scan(%r{<blockquote>(.*?)</blockquote>}m).each do |quote|\n    quote = quote.first\n    if quote.include? 'Note: '\n      quote_new  = quote.strip.sub 'Note: ', ''\n      macro_name = 'note'\n    elsif quote.include? 'Warning: '\n      quote_new  = quote.strip.sub 'Warning: ', ''\n      macro_name = 'warning'\n    else\n      quote_new  = quote.strip\n      macro_name = 'info'\n    end\n    @html.sub! \"<blockquote>\#{quote}</blockquote>\", format(confluence_code, macro_name: macro_name, quote: quote_new)\n  end\nend\n"

#parseObject

Launch all internal parsers.



25
26
27
28
29
30
31
32
33
# File 'lib/md2conf.rb', line 25

def parse
  process_macros if @macros
  process_mentions
  convert_info_macros
  process_code_blocks
  add_toc

  @html
end

#process_code_blocksObject

Convert regular code blocks to Confluence code blocks. Language type is also supported.

puppet is currently replaced by ruby language type.



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/md2conf.rb', line 131

def process_code_blocks
  @html.scan(%r{<pre><code.*?>.*?</code></pre>}m).each do |codeblock|
    content = codeblock.match(%r{<pre><code.*?>(.*?)</code></pre>}m)[1]
    lang    = codeblock.match(/code class="(.*)"/)
    lang    = if lang.nil?
      'none'
    else
      lang[1].sub('puppet', 'ruby')
    end

    confluence_code = "      <ac:structured-macro ac:name=\"code\">\n        <ac:parameter ac:name=\"theme\">RDark</ac:parameter>\n        <ac:parameter ac:name=\"linenumbers\">true</ac:parameter>\n        <ac:parameter ac:name=\"language\">\#{lang}</ac:parameter>\n        <ac:plain-text-body><![CDATA[\#{CGI.unescape_html content}]]></ac:plain-text-body>\n      </ac:structured-macro>\n    HTML\n\n    @html.sub! codeblock, confluence_code\n  end\nend\n"

#process_macrosObject

Process custom macros. Macro definitions should be placed in ‘~/.md2conf.yaml`. Format is described in the README.

Macros are blocks that are contained in curly braces like this: ‘JIRA:52837`.



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
# File 'lib/md2conf.rb', line 39

def process_macros
  html_new      = ''
  last_position = 0
  @html.scan(/{(.*?)}/) do |macro|
    next if inside_code_block Regexp.last_match.pre_match
    macro_name = macro.first.split(':')[0]
    macro_arg  = macro.first.split(':')[1]

    confluence_code = if @macros.include? macro_name
      @macros[macro_name] % { arg: macro_arg }
    else
      "<code>#{macro.first}</code>"
    end

    since_last_match = @html[last_position..Regexp.last_match.begin(0) - 1]
    html_new << "#{since_last_match}#{confluence_code}"
    last_position = Regexp.last_match.end(0)
  end

  # Did we have at least one match?
  return unless Regexp.last_match
  @html = html_new << if inside_code_block Regexp.last_match.pre_match
    @html[last_position..-1]
  else
    Regexp.last_match.post_match
  end
end

#process_mentionsObject

Process username mentions.

Everything that starts with an ‘@` and is not enclosed in a code block will be converted to a valid Confluence username.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/md2conf.rb', line 71

def process_mentions
  html_new      = ''
  last_position = 0
  @html.scan(/@(\w+)/) do |mention|
    next if inside_code_block Regexp.last_match.pre_match

    confluence_code  = "<ac:link><ri:user ri:username=\"#{mention.first}\"/></ac:link>"
    since_last_match = @html[last_position..Regexp.last_match.begin(0) - 1]
    html_new << "#{since_last_match}#{confluence_code}"
    last_position = Regexp.last_match.end(1)
  end

  # Did we have at least one match?
  return unless Regexp.last_match
  @html = html_new << if inside_code_block Regexp.last_match.pre_match
    @html[last_position..-1]
  else
    Regexp.last_match.post_match
  end
end