class Redcarpet::Confluence

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

Constants

CODE_MACRO_LANGUAGES

Internal: Languages supported by the code macro.

LIST_ITEM_DELIMITER

Internal: List item delimiters used by Confluence.

RESERVED_SEQUENCES

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

Public Instance Methods

block_code(code, language) click to toggle source
# 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) click to toggle source
# File lib/redcarpet/confluence.rb, line 24
def block_quote(text)
  macro(:quote, text)
end
codespan(code) click to toggle source
# File lib/redcarpet/confluence.rb, line 28
def codespan(code)
  "{{#{escape_reserved_sequences(code)}}}"
end
double_emphasis(text) click to toggle source
# File lib/redcarpet/confluence.rb, line 32
def double_emphasis(text)
  "*#{text}*"
end
emphasis(text) click to toggle source
# File lib/redcarpet/confluence.rb, line 36
def emphasis(text)
  "_#{text}_"
end
header(title, level) click to toggle source
# File lib/redcarpet/confluence.rb, line 40
def header(title, level)
  "\n\nh#{level}. #{title}"
end
hrule() click to toggle source
# File lib/redcarpet/confluence.rb, line 44
def hrule
  "\n\n----"
end
image(link, title, alt) click to toggle source
# 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
linebreak() click to toggle source
# File lib/redcarpet/confluence.rb, line 52
def linebreak
  "\n"
end
list(content, list_type) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/redcarpet/confluence.rb, line 80
def normal_text(text)
  escape_reserved_sequences(text)
end
paragraph(text) click to toggle source
# File lib/redcarpet/confluence.rb, line 84
def paragraph(text)
  "\n\n#{text}"
end
strikethrough(text) click to toggle source
# File lib/redcarpet/confluence.rb, line 88
def strikethrough(text)
  "-#{text}-"
end
superscript(text) click to toggle source
# File lib/redcarpet/confluence.rb, line 92
def superscript(text)
  " ^#{text}^ "
end
table(header, body) click to toggle source
# File lib/redcarpet/confluence.rb, line 96
def table(header, body)
  "\n#{header.gsub('|', '||')}#{body}"
end
table_cell(text, align) click to toggle source
# File lib/redcarpet/confluence.rb, line 104
def table_cell(text, align)
  "#{text}|"
end
table_row(text) click to toggle source
# File lib/redcarpet/confluence.rb, line 100
def table_row(text)
  "\n|#{text}"
end
triple_emphasis(text) click to toggle source
# File lib/redcarpet/confluence.rb, line 108
def triple_emphasis(text)
  "_*#{text}*_"
end

Private Instance Methods

args_string(args) click to toggle source

Internal: Creates an argument String meant for a macro.

Arguments are key-value pairs preceded by and separated by a pipe ('|').

Returns the argument String

# File lib/redcarpet/confluence.rb, line 140
def args_string(args)
  # format macro arguments string
  args_string = ''
  args.each do |arg, value|
    args_string << "|#{arg}=#{value}" if value && !value.empty?
  end
  args_string
end
escape_reserved_sequences(text) click to toggle source

Internal: Backslash-escapes all Confluence-reserved character sequences.

Returns a copy of the text with all Confluence-reserved character sequences escaped with a backslash.

# File lib/redcarpet/confluence.rb, line 117
def escape_reserved_sequences(text)
  reserved_sequences_regexp = /(?<reserved_sequence>#{RESERVED_SEQUENCES.map { |c| Regexp.escape(c) }.join('|')})/
  text ? text.gsub(reserved_sequences_regexp, '\\\\\k<reserved_sequence>') : text
end
macro(name, content, args = {}) click to toggle source

Internal: Wraps content in the named macro.

name - The name of the macro to use. content - The content to go between the macro tags. Nil will act the same as the empty string. args - Optional Hash of arguments to pass to the macro.

Returns a string with the given content wrapped in the named macro.

# File lib/redcarpet/confluence.rb, line 129
def macro(name, content, args = {})
  # separate the macro name from the argument list with a colon
  arguments = args_string(args).sub('|', ':')
  "\n\n{#{name}#{arguments}}#{content}{#{name}}"
end