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) ⇒ 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.



14
15
16
17
# File 'lib/md2conf.rb', line 14

def initialize(html, max_toc_level)
  @html          = html
  @max_toc_level = max_toc_level
end

Instance Method Details

#parseObject

Launch all internal parsers.



20
21
22
23
24
25
26
27
# File 'lib/md2conf.rb', line 20

def parse
  process_mentions
  convert_info_macros
  process_code_blocks
  add_toc

  @html
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.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/md2conf.rb', line 33

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