Permalink
Browse files

Initial commit of MoinMoin2Markdown transformation

This is going to be a 80/20 solution for the
migration of the CouchDB wiki pages from the
MoinMoin-Syntax to Markdown.
  • Loading branch information...
tisba committed Mar 26, 2010
1 parent cafd2dd commit e6e7d3237e230dcd5a76fe3dfb1af5796f7faf08
Showing with 73 additions and 0 deletions.
  1. +73 −0 bin/mm2md.rb
View
@@ -0,0 +1,73 @@
+require "rubygems"
+
+input = IO.readlines('../wiki/HTTP_database_API.mm','').to_s
+
+def substitute_pattern_in_string_with(pattern, string)
+ string.scan(pattern).flatten.inject({}) do |acc, matched|
+ acc[matched] = yield matched
+ acc
+ end.each do |matched, substitute|
+ string.gsub!(matched, substitute)
+ end
+
+ return string
+end
+
+transformations = [
+ { :regexp => /=+\s*?$/, :replacement => '' }, # headlines
+
+ # { :regexp => /^(?:(\{\{\{)|(\}\}\}))\s*?$/,:replacement => '....' }, # blocks (extra line)
+ { :regexp => /\{\{\{(.*?)\}\}\}/, :replacement => '`\1`' }, # blocks (inline)
+
+ { :regexp => /'''([^']+)'''/, :replacement => '**\1**' }, # bold
+ { :regexp => /''([^']+)''/, :replacement => '*\1*' }, # italic
+
+ { :regexp => /_\_(\S)/, :replacement => '_\_\1' }, # escaping (italic)
+
+ { :regexp => /<<TableOfContents\(.\)>>/, :replacement => '' } # drop some stuff
+ ]
+
+# do the basic stuff
+output = transformations.inject(input) do |txt, pattern|
+ txt.gsub(pattern[:regexp], pattern[:replacement])
+end
+
+# transform headlines
+output = substitute_pattern_in_string_with(/^=+/, output) do |matched_string|
+ "#" * matched_string.length
+end
+
+# doing blocks
+output = substitute_pattern_in_string_with(/\{\{\{.*?\}\}\}/m, output) do |matched_block|
+ block = matched_block.split("\n")
+ block.shift
+ block.pop
+ "\n" << block.each.inject("") do |acc, line|
+ acc << " " << line << "\n"
+ end
+
+end
+
+puts output
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

0 comments on commit e6e7d32

Please sign in to comment.