Class: Caramelize::Wikka2Markdown

Inherits:
Object
  • Object
show all
Defined in:
lib/caramelize/filters/wikka_to_markdown.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_body) ⇒ Wikka2Markdown

Returns a new instance of Wikka2Markdown.



7
8
9
# File 'lib/caramelize/filters/wikka_to_markdown.rb', line 7

def initialize(source_body)
  @source_body = source_body
end

Instance Attribute Details

#source_bodyObject (readonly)

Returns the value of attribute source_body.



5
6
7
# File 'lib/caramelize/filters/wikka_to_markdown.rb', line 5

def source_body
  @source_body
end

Instance Method Details

#replace_code_blockObject



64
65
66
67
# File 'lib/caramelize/filters/wikka_to_markdown.rb', line 64

def replace_code_block
  target_body.gsub!(/^%%\(?(\w+)\)?\s(.*?)%%\s?/m) { "```#{::Regexp.last_match(1)}\n#{::Regexp.last_match(2)}```\n" }
  target_body.gsub!(/^%%\s(.*?)%%\s?/m) { "```\n#{::Regexp.last_match(1)}```\n" }
end

#replace_formattingObject



34
35
36
37
38
39
# File 'lib/caramelize/filters/wikka_to_markdown.rb', line 34

def replace_formatting
  target_body.gsub!(/(\*\*)(.*?)(\*\*)/) { |_s| "**#{::Regexp.last_match(2)}**" } # bold
  target_body.gsub!(%r{(//)(.*?)(//)}, '*\2*') # italic
  target_body.gsub!(/(__)(.*?)(__)/) { |_s| "<u>#{::Regexp.last_match(2)}</u>" } # underline
  target_body.gsub!(/(---)/, '  ') # forced linebreak
end

#replace_headlinesObject



26
27
28
29
30
31
32
# File 'lib/caramelize/filters/wikka_to_markdown.rb', line 26

def replace_headlines
  target_body.gsub!(/(======)(.*?)(======)/) { |_s| "# #{::Regexp.last_match(2)}" } # h1
  target_body.gsub!(/(=====)(.*?)(=====)/) { |_s| "## #{::Regexp.last_match(2)}" } # h2
  target_body.gsub!(/(====)(.*?)(====)/) { |_s| "### #{::Regexp.last_match(2)}" } # h3
  target_body.gsub!(/(===)(.*?)(===)/) { |_s| "#### #{::Regexp.last_match(2)}" } # h4
  target_body.gsub!(/(==)(.*?)(==)/) { |_s| "##### #{::Regexp.last_match(2)}" } # h5
end

#replace_inline_codeObject



60
61
62
# File 'lib/caramelize/filters/wikka_to_markdown.rb', line 60

def replace_inline_code
  target_body.gsub!(/(%%)(.*?)(%%)/) { |_s| "`#{::Regexp.last_match(2)}`" } # h1
end


54
55
56
57
58
# File 'lib/caramelize/filters/wikka_to_markdown.rb', line 54

def replace_links
  target_body.gsub!(/\[{2}((\w+):\S[^| ]*)[| ](.*)\]{2}/,
                    '[\3](\1)')
  target_body.gsub!(/\[{2}((\w+):.*)\]{2}/, '<\1>')
end

#replace_listsObject



41
42
43
44
45
46
47
48
# File 'lib/caramelize/filters/wikka_to_markdown.rb', line 41

def replace_lists
  target_body.gsub!(/(\t-\s?)(.*)/, '- \2')    # unordered list
  target_body.gsub!(/(~-\s?)(.*)/, '- \2')     # unordered list
  target_body.gsub!(/(    -\s?)(.*)/, '- \2')     # unordered list

  target_body.gsub!(/(~1\)\s?)(.*)/, '1. \2')     # unordered list
  # TODO ordered lists
end


50
51
52
# File 'lib/caramelize/filters/wikka_to_markdown.rb', line 50

def replace_wiki_links
  target_body.gsub!(/\[{2}(\w+)[\s|](.+?)\]{2}/, '[[\2|\1]]')
end

#runObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/caramelize/filters/wikka_to_markdown.rb', line 11

def run
  # TODO: images: ({{image)(url\=?)?(.*)(}})
  # markdown: ![Tux, the Linux mascot](/assets/images/tux.png)

  replace_headlines
  replace_formatting
  replace_lists
  replace_wiki_links
  replace_links
  replace_inline_code
  replace_code_block

  target_body
end

#target_bodyObject



69
70
71
# File 'lib/caramelize/filters/wikka_to_markdown.rb', line 69

def target_body
  @target_body ||= source_body.dup
end