Class: Caramelize::Wikka2Markdown
- Inherits:
-
Object
- Object
- Caramelize::Wikka2Markdown
- Defined in:
- lib/caramelize/filters/wikka_to_markdown.rb
Instance Attribute Summary collapse
-
#source_body ⇒ Object
readonly
Returns the value of attribute source_body.
Instance Method Summary collapse
-
#initialize(source_body) ⇒ Wikka2Markdown
constructor
A new instance of Wikka2Markdown.
- #replace_code_block ⇒ Object
- #replace_formatting ⇒ Object
- #replace_headlines ⇒ Object
- #replace_links ⇒ Object
- #replace_lists ⇒ Object
- #replace_wiki_links ⇒ Object
- #run ⇒ Object
- #target_body ⇒ Object
Constructor Details
#initialize(source_body) ⇒ Wikka2Markdown
Returns a new instance of Wikka2Markdown.
5 6 7 |
# File 'lib/caramelize/filters/wikka_to_markdown.rb', line 5 def initialize(source_body) @source_body = source_body end |
Instance Attribute Details
#source_body ⇒ Object (readonly)
Returns the value of attribute source_body.
3 4 5 |
# File 'lib/caramelize/filters/wikka_to_markdown.rb', line 3 def source_body @source_body end |
Instance Method Details
#replace_code_block ⇒ Object
57 58 59 60 61 |
# File 'lib/caramelize/filters/wikka_to_markdown.rb', line 57 def replace_code_block target_body.gsub!(/^%%\s(.*?)%%\s?/m) do $1.gsub(/^/, ' ') end end |
#replace_formatting ⇒ Object
31 32 33 34 35 36 |
# File 'lib/caramelize/filters/wikka_to_markdown.rb', line 31 def replace_formatting target_body.gsub!(/(\*\*)(.*?)(\*\*)/) {|s| '**' + $2 + '**' } #bold target_body.gsub!(/(\/\/)(.*?)(\/\/)/, '*\2*') #italic target_body.gsub!(/(__)(.*?)(__)/) {|s| '<u>' + $2 + '</u>'} #underline target_body.gsub!(/(---)/, ' ') #forced linebreak end |
#replace_headlines ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/caramelize/filters/wikka_to_markdown.rb', line 23 def replace_headlines target_body.gsub!(/(======)(.*?)(======)/ ) {|s| '# ' + $2 } #h1 target_body.gsub!(/(=====)(.*?)(=====)/) {|s| '## ' + $2 } #h2 target_body.gsub!(/(====)(.*?)(====)/) {|s| '### ' + $2 } #h3 target_body.gsub!(/(===)(.*?)(===)/) {|s| '#### ' + $2 } #h4 target_body.gsub!(/(==)(.*?)(==)/) {|s| '##### ' + $2 } #h5 end |
#replace_links ⇒ Object
51 52 53 54 55 |
# File 'lib/caramelize/filters/wikka_to_markdown.rb', line 51 def replace_links target_body.gsub!(/[\[]{2}((\w+):[\S][^\| ]*)[\| ](.*)[\]]{2}/, '[\3](\1)') target_body.gsub!(/[\[]{2}((\w+):.*)[\]]{2}/, '<\1>') end |
#replace_lists ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/caramelize/filters/wikka_to_markdown.rb', line 38 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 |
#replace_wiki_links ⇒ Object
47 48 49 |
# File 'lib/caramelize/filters/wikka_to_markdown.rb', line 47 def replace_wiki_links target_body.gsub!(/[\[]{2}(\w+)[\s|](.+?)[\]]{2}/, '[[\2|\1]]') end |
#run ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/caramelize/filters/wikka_to_markdown.rb', line 9 def run # TODO images: ({{image)(url\=?)?(.*)(}}) # markdown:  replace_headlines replace_formatting replace_lists replace_wiki_links replace_links replace_code_block target_body end |
#target_body ⇒ Object
63 64 65 |
# File 'lib/caramelize/filters/wikka_to_markdown.rb', line 63 def target_body @target_body ||= source_body.dup end |