Last active
August 29, 2015 14:20
-
-
Save danhaywood/60afd4905bf58e9408bf to your computer and use it in GitHub Desktop.
A Groovy script to convert pragprog PML (a DocBook-like language) to Asciidoc
This file contains 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
def replacements = [ | |
"<bookname[^>]*>([^<]+?)</bookname>":" '\$1'" | |
,"<acronym>([^<]+?)</acronym>":" _\$1_" | |
,"<emph>([^<]+?)</emph>":" *\$1*" | |
,"<url>([^<]+?)</url>":"link:\$1[]" | |
,"<keystroke>([^<]+?)</keystroke>":"kbd:[\$1]" | |
,"<variablename>([^<]*?)</variablename>":"`\$1`" | |
,"<classname>([^<]+?)</classname>":"`\$1`" | |
,"<dirname>([^<]+?)</dirname>":"`\$1`" | |
,"<filename>([^<]+?)</filename>":"`\$1`" | |
,"<cf>([^<]+?)</cf>":"`\$1`" | |
,"<inlinecode>([^<]+?)</inlinecode>":"`\$1`" | |
,"<stringinfile>([^<]+?)</stringinfile>":"`\$1`" | |
,"<firstuse>([^<]+?)</firstuse>":"*_\$1_*" | |
,"<ldots.*?/>":"…" | |
,"``([^']*)''":"\"\$1\"" | |
,"<pref[^l]+linkend=\"(.*?)\".*?/>":"<<\$1>>" | |
,"<ii.*?>.+?</ii>":"" | |
,"<i.*?>[^<]+?</i>":"" | |
,"<layout[^>]*>(.+?)</layout>":"" | |
,"<chapter[^>]*>([^<]*)<title>([^<]+?)</title>":"= \$2\n\n" | |
,"</chapter>":"" | |
,"<sect1[^>]*?>([^<]*)<title>([^<]+?)</title>":"== \$2\n\n" | |
,"</sect1>":"" | |
,"<sect2[^>]*?>([^<]*)<title>([^<]+?)</title>":"=== \$2\n\n" | |
,"</sect2>":"" | |
,"<sect3[^>]*?>([^<]*)<title>([^<]+?)</title>":"==== \$2\n\n" | |
,"</sect3>":"" | |
,"<sect4[^>]*?>([^<]*)<title>([^<]+?)</title>":"===== \$2\n\n" | |
,"</sect4>":"" | |
,"<li>\n[\\s]*([\\S]*)":"* \$1" | |
,"</li>":"" | |
,"<ul.*?>":"" | |
,"</ul>":"" | |
,"<ol>":"" | |
,"</ol>":"" | |
,"<p>":"" | |
,"</p>":"" | |
,"<footnote>([^<]*)</footnote>":"\n\n[TIP]\n====\n\$1\n====\n\n" | |
,"<figure.*?>([^<]*)<title>([^<]*)</title>":"== \n\n.\$2" | |
,"</figure>":"" | |
,"<imagedata.+?fileref=\"(.+?)\"[^/]*?/>":"image::\$1[]" | |
,"<code.+?file=\"(.+?)\"[^>]*?>.*?</code>":"[source,java]\n----\ninclude::\$1\n----" | |
,"<joeasks>([^<]*)<title>([^<]*)</title>":"== \n\n.\$2\n****" | |
,"</joeasks>":"****" | |
,"<ddd>([^<]*)<title>([^<]*)</title>([^<]*)</ddd>":"== \n\n.\$2\n****\$3\n****\n\n" | |
,"<!--[^-]+?-->":"" | |
,"<!--.+?-->":"" | |
] | |
def stripLeadingSpaces(str) { | |
lines=str.split("\n") | |
buf="" | |
lines.each{line -> buf+= line.trim() +"\n" } | |
return buf | |
} | |
def currentDir = new File("."); | |
currentDir.eachFileRecurse { file -> | |
if (file.path.endsWith(".pml")) { | |
adocFile = new File(file.path.replaceAll("[.]pml",".adoc")) | |
println("${file.path} ---> ${adocFile.path}" ) | |
def fileText = file.text; | |
replacements.each { from, to -> | |
//println( from + " ---> " + to) | |
p = java.util.regex.Pattern.compile(from) | |
m = p.matcher(fileText); | |
newFileText = m.replaceAll(to) | |
fileText = newFileText; | |
} | |
// strip off leading spaces | |
newFileText = stripLeadingSpaces(newFileText) | |
adocFile.write(newFileText); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment