Conversion between textile and markdown

Differences

|element|textile|markdown|
|-|-|-|
|headers|h1. header|# header #|
|inline code|@printf()@|`printf()`|
|links|"link":URL|[link](URL)|
|bold|bold|bold|

Tables

Textile

|_. h1 |_. h2 |
|   i1 |   i2 |

Markdown

| h1 | h2 |
| -- | -- |
| c1 | c2 |

Code

Textile

<pre><code>
    printf();
</code></pre>

Markdown

<4 spaces>printf();

Conversion script (Ruby)

#!/usr/local/bin/ruby -w

@code = false

STDIN.each { |line|

  # h3. xxx => ### xxx ###
  re = /^(h|H)[0-9]. /
  if line =~ re then
    hn =line.match(re)[0].match(/[0-9]/)[0].to_i
    rs = ""
    hn.times { |i| rs << "#" }
    line.sub!(/^(h|H)[0-9]\./, rs)
    line.strip!
    line = line << " #{rs}"
  end

  # ... "link":URL ... => ... [link](URL) ...
  re = /"(\w|\s)+":(http(s)?:\/\/)(\w|\.|\/|-|:|\?|=|\+|&)+/
  if line =~ re then
    line.gsub!(re) { |s|
      s.sub(s) {
        s.sub!(/"/,'[')
        s.sub!(/":/,'](')
        s << ')'
        s
      }
    }
  end

  if line =~ /.*<code>/ then @code = true
  elsif line =~ /.*<\/code>/ then @code = false
  else
    line.strip!
    if @code then line = "    " << line end
    puts line
  end

}

wiki-textile-2007-11-28-20-10-34.zip