Class: Redcarpet::Confluence

Inherits:
Render::Base
  • Object
show all
Defined in:
lib/redcarpet/confluence.rb

Overview

Public: A Redcarpet renderer to convert Markdown to Confluence syntax.

Constant Summary collapse

CODE_MACRO_LANGUAGES =

Internal: Languages supported by the code macro.

%w(
  actionscript actionscript3 bash csharp coldfusion cpp css delphi diff erlang groovy html java javafx javascript
  perl php powershell python ruby scala sql vb xhtml xml
)
LIST_ITEM_DELIMITER =

Internal: List item delimiters used by Confluence.

%w( # * )
RESERVED_SEQUENCES =

Internal: Reserved character sequences in Confluence that need to be escaped when not intended to be used.

%w( { } [ ] - + * _ ^ ~ ?? bq. )

Instance Method Summary collapse

Instance Method Details

#block_code(code, language) ⇒ Object



19
20
21
22
# File 'lib/redcarpet/confluence.rb', line 19

def block_code(code, language)
  supported_language = CODE_MACRO_LANGUAGES.find { |lang| lang == language }
  macro(:code, code, lang: supported_language || :none)
end

#block_quote(text) ⇒ Object



24
25
26
# File 'lib/redcarpet/confluence.rb', line 24

def block_quote(text)
  macro(:quote, text)
end

#codespan(code) ⇒ Object



28
29
30
# File 'lib/redcarpet/confluence.rb', line 28

def codespan(code)
  "{{#{escape_reserved_sequences(code)}}}"
end

#double_emphasis(text) ⇒ Object



32
33
34
# File 'lib/redcarpet/confluence.rb', line 32

def double_emphasis(text)
  "*#{text}*"
end

#emphasis(text) ⇒ Object



36
37
38
# File 'lib/redcarpet/confluence.rb', line 36

def emphasis(text)
  "_#{text}_"
end

#header(title, level) ⇒ Object



40
41
42
# File 'lib/redcarpet/confluence.rb', line 40

def header(title, level)
  "\n\nh#{level}. #{title}"
end

#hruleObject



44
45
46
# File 'lib/redcarpet/confluence.rb', line 44

def hrule
  "\n\n----"
end

#image(link, title, alt) ⇒ Object



48
49
50
# File 'lib/redcarpet/confluence.rb', line 48

def image(link, title, alt)
  "!#{link}#{args_string(alt: escape_reserved_sequences(alt), title: escape_reserved_sequences(title))}!"
end

#linebreakObject



52
53
54
# File 'lib/redcarpet/confluence.rb', line 52

def linebreak
  "\n"
end


56
57
58
59
60
# File 'lib/redcarpet/confluence.rb', line 56

def link(link, title, content)
  link = "[#{content}|#{link}]"
  link.insert(-2, "|#{escape_reserved_sequences(title)}") if title && !title.empty?
  link
end

#list(content, list_type) ⇒ Object



62
63
64
65
66
# File 'lib/redcarpet/confluence.rb', line 62

def list(content, list_type)
  nested_list_prefix_regexp = /\n\n(?<matched_list_item_delimiter>#{LIST_ITEM_DELIMITER.map { |c| Regexp.escape(c) }.join('|')})/
  nested_list_content = content.gsub(nested_list_prefix_regexp, '\k<matched_list_item_delimiter>')
  "\n\n#{nested_list_content}"
end

#list_item(content, list_type) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/redcarpet/confluence.rb', line 68

def list_item(content, list_type)
  list_item_delimiter_regexp = /\n(?<matched_list_item_delimiter>#{LIST_ITEM_DELIMITER.map { |c| Regexp.escape(c) }.join('|')})/
  list_item_delimiter = nil
  case list_type
  when :ordered
    list_item_delimiter = '#'
  when :unordered
    list_item_delimiter = '*'
  end
  "#{list_item_delimiter} #{content.gsub(list_item_delimiter_regexp, "\n#{list_item_delimiter}\\k<matched_list_item_delimiter>")}"
end

#normal_text(text) ⇒ Object



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

def normal_text(text)
  escape_reserved_sequences(text)
end

#paragraph(text) ⇒ Object



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

def paragraph(text)
  "\n\n#{text}"
end

#strikethrough(text) ⇒ Object



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

def strikethrough(text)
  "-#{text}-"
end

#superscript(text) ⇒ Object



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

def superscript(text)
  " ^#{text}^ "
end

#table(header, body) ⇒ Object



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

def table(header, body)
  "\n#{header.gsub('|', '||')}#{body}"
end

#table_cell(text, align) ⇒ Object



104
105
106
# File 'lib/redcarpet/confluence.rb', line 104

def table_cell(text, align)
  "#{text}|"
end

#table_row(text) ⇒ Object



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

def table_row(text)
  "\n|#{text}"
end

#triple_emphasis(text) ⇒ Object



108
109
110
# File 'lib/redcarpet/confluence.rb', line 108

def triple_emphasis(text)
  "_*#{text}*_"
end