Created
August 9, 2011 09:42
-
-
Save osima/1133674 to your computer and use it in GitHub Desktop.
Simple converter from markdown to textile.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.regex.* | |
def br = System.getProperty('line.separator') | |
def getNoteBody = { | |
def out = ''<<'' | |
def noteHeaderEnd=false | |
new StringReader(it).each{ | |
if( noteHeaderEnd==false && (it =~ /^#[^#]/).find() ){ noteHeaderEnd=true } | |
if( noteHeaderEnd==true ){ out << it << br } | |
} | |
out.toString() | |
} | |
def toTextile = { line-> | |
def m1 = (line =~ /^(.*)<(http.*?)>(.*)$/) | |
if( m1.find() ){ | |
line = "${m1.group(1)}[${m1.group(2)}]${m1.group(3)}" | |
} | |
line = line.replaceAll(/^#### /,'h4. ') | |
line = line.replaceAll(/^### /, 'h3. ') | |
line = line.replaceAll(/^## /, 'h2. ') | |
line = line.replaceAll(/^# /, 'h1. ') | |
line = line.replaceAll(/^- /, '* ') | |
line = line.replaceAll(/^ - /, '** ') | |
line = line.replaceAll(/^ - /, '*** ') | |
line = line.replaceAll(/^ - /, '**** ') | |
line = line.replaceAll(/^1. /, '# ') | |
line = line.replaceAll(/^ 1. /, '## ') | |
line = line.replaceAll(/^ 1. /, '### ') | |
line = line.replaceAll(/^ 1. /, '#### ') | |
line | |
} | |
def inputFile = new File(args[0]) | |
def outputFile = new File(args[1]) | |
def text = inputFile.getText('UTF-8') | |
text = getNoteBody(text) | |
text = text.replace('{console}','{code}') | |
def out = ''<<'' | |
boolean inCodeBlock=false | |
new StringReader(text).each{ line-> | |
boolean onCodeMacro=false | |
def pat = Pattern.compile('^\\{code\\}') | |
def m1 = pat.matcher(line) | |
if( inCodeBlock==false ){ | |
if( m1.find() ){ | |
inCodeBlock=true | |
onCodeMacro=true | |
} | |
} | |
else{ | |
if( m1.find() ){ | |
inCodeBlock=false | |
onCodeMacro=true | |
} | |
} | |
if( onCodeMacro ) out << line << br | |
else { | |
if( inCodeBlock ){ | |
out << line << br | |
} | |
else{ | |
out << toTextile(line) << br | |
} | |
} | |
} | |
def w = outputFile.newWriter('UTF-8') | |
w.print out.toString() | |
w.close() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment