Skip to content

Commit

Permalink
Initial commit of MoinMoin2Markdown transformation
Browse files Browse the repository at this point in the history
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
Sebastian Cohnen committed Mar 26, 2010
1 parent cafd2dd commit e6e7d32
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions bin/mm2md.rb
@@ -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.