Skip to content

Instantly share code, notes, and snippets.

@dmerand
Created April 18, 2012 15:30
Show Gist options
  • Save dmerand/2414353 to your computer and use it in GitHub Desktop.
Save dmerand/2414353 to your computer and use it in GitHub Desktop.
BASH/AWK/SED script to convert Dokuwiki to Latex format. Flawed in many ways, but functional
#! /bin/sh
#doku-tex - take a dokuwiki/notational velocty syntax file, and convert it to latex
#author: donald l. merand
echo "\documentclass{article}"
echo "\pagestyle{empty}"
echo "\setlength{\parindent}{0pt}"
echo "\setlength{\parskip}{1ex plus 0.5ex minus 0.2ex}"
echo "\usepackage{ulem}"
echo "\usepackage[pdftex]{hyperref}"
echo "\hypersetup{colorlinks=true}"
echo "\setcounter{secnumdepth}{-1}"
echo "\\\begin{document}"
echo ""
awk '
/^ [\*|-] / {
sub(/^ [\*|-] /, "\t\t\t\\item ")
if (list4) { print $0 } else {
list4 = 1
print "\t\t\t\\begin{itemize}"
print $0
}
next
}
list4 {
list4=0
print "\t\t\t\\end{itemize}"
}
/^ [\*|-] / {
sub(/^ [\*|-] /, "\t\t\\item ")
if (list3) { print $0 } else {
list3 = 1
print "\t\t\\begin{itemize}"
print $0
}
next
}
list3 {
list3=0
print "\t\t\\end{itemize}"
}
/^ [\*|-] / {
sub(/^ [\*|-] /, "\t\\item ")
if (list2) { print $0 } else {
list2 = 1
print "\t\\begin{itemize}"
print $0
}
next
}
list2 {
list2=0
print "\t\\end{itemize}"
}
/^ [\*|-] / {
sub(/^ [\*|-] /, "\\item ")
if (list1) { print $0 } else {
list1 = 1
print "\\begin{itemize}"
print $0
}
next
}
list1 {
list1=0
print "\\end{itemize}"
}
#default case
{ print }
#catch itemize at the end of the file
END { if (list1==1 || list2==2 || list3==2 || list==2) print "\\end{itemize}" }
' "$1" |
sed -e "
s/======\(.*\)======/\\\section{\1}/
s/=====\(.*\)=====/\\\subsection{\1}/
s/====\(.*\)====/\\\subsubsection{\1}/
#remaining headers do not translate to latex. Just make them bold
s/==*\(.*\)==*/\\\textbf\{\1\}/g
#bold, underline, italic
s/\*\*\(.*\)\*\*/\\\textbf\{\1\}/g
s/__\(.*\)__/\\\underline\{\1\}/g
s|\/\/\(.*\)\/\/|\\\emph\{\1\}|g
#footnotes
s|((\(.*\)))|\\\footnote\{\1\}|g
#ellipses
s|\.\.\.|\\\ldots |g
#double-single quotes (remember LONGEST MATCH)
s|''\(.*\)''|\`\`\1''|g
#quote character
s|"\""\(.*\)"\""|\`\`\1''|g
#arrows
s|->|---|g
#underscores do not work unless escaped
s|\_|\\\_|g
#special cases for @done for strikethrough
s|item \(.*\)@done$|\\item \\\sout\{\1\}|g
s|\(.*\)@done$|\\\sout\{\1\}|g
#fractions, numbers
s| \([0-9][0-9]*\)/\([0-9][0-9]*\) | \$\\\frac\{\1\}\{\2\}\$ |g
s| \([0-9][0-9]*\) | \$\1\$ |g
s|\$ \$||g
#link. no special character at end. matches file:///syntax, and allows spaces as a result
s?\(https*:\/\/[-a-zA-Z0-9.&_=/\\\?]*[a-zA-Z0-9]\)? \\\url\{\1\}?
s?file:\/\/\/\([-a-zA-Z0-9 .\/\\]*\)? \\\url\{\/\1\}?
"
echo "\\end{document}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment