#!/bin/awk -f # md2tex.awk # JGL yiyus - 09/04/09 # tested only with gnu awk function dotabline(){ # notes if(match($0, /^\[.+\] /)){ tbln[tblni] = $0; tblni++; return; } # header if(tbli == 0){ if(match($0, /\t/)){ print("\t\t\\begin{tabular}"); gsub(/\t/, ""); sub(/^/, "\t\t{"); sub(/$/, "}"); print; tbli++; return; } else if(tblni == 0){ print("\t\t\\begin{tabular}"); } } # horizontal lines if(match($0, /^[ \t]*-+[ \t]*$/)){ print("\t\t\\hline"); return; } # new table if(match($0, /^$/)){ print("\t\t\\end{tabular}"); print("\n\\phantom{VSPACE}\n"); tbli = 0; return; } # end of row if(!match($0, /[ \t]+$/)) sub(/$/, "\\\\"); gsub(/\t+/, " \\& "); sub(/^/, "\t\t"); print; } function dotblfile(file){ if(system("test -f " file) != 0) return; tbli = 0; tblni = 0; print("\t\t\\begin{threeparttable}"); while(getline <(file)) dotabline(); close(file); if(tbli != 0) print("\t\t\\end{tabular}"); if(tblni > 0) print("\t\t\\begin{tablenotes}\\scriptsize{"); for(i = 0; i < tblni; i++) print("\t\t\\item " tbln[i]); if(tblni > 0) print("\t\t}\\end{tablenotes}"); print("\t\t\\end{threeparttable}"); return; } BEGIN{ env = "none"; text = ""; } # inline # cite /\[\[/{ pcenv = env; env = "cite"; gsub(/\[\[/, "\\cite{"); } /\]\]/{ if(env = "cite"){ gsub(/]]/, "}"); env = pcenv; } } # footnotes /\(\(/{ pfenv = env; env = "foot"; gsub(/\(\(/, "\\footnote{"); } /))/{ if(env = "foot"){ gsub(/))/, "}"); env = pfenv; } } # emphasis /\*\*/{ if(env != "eqn"){ while(match($0, /\*\*/) != 0){ if(env == "emph"){ sub(/\*\*/, "}"); env = peenv; } else{ sub(/\*\*/, "\\emph{"); peenv = env; env = "emph"; } } } } # reference /\[\(/{ prenv = env; env = "ref"; gsub(/\[\(/, "\\ref{"); } /\)\]/{ if(env = "ref"){ gsub(/)]/, "}"); env = prenv; } } # # paragraph /^$/{ if(env == "tabular}}\\end{center}\\end{table"){ print "\\end{tabular}}"; if(tblni > 0) print("\t\t\\begin{tablenotes}\\scriptsize{"); for(i = 0; i < tblni; i++) print("\t\t\\item " tbln[i]); if(tblni > 0) print("\t\t}\\end{tablenotes}"); tblni = 0; print("\t\t\\end{threeparttable}\\end{center}"); print "\t\\caption[" text "]{"; print "\t\\label{tbl:" table "}"; print "\t\\scriptsize{" text "}}"; print "\\end{table}"; env = "none"; text = ""; }else if(env != "none" && env != "cite"){ if(text){ if(env == "figure" || env == "table") print "\t\\scriptsize{" text "}}"; else print text; text=""; } print "\\end{" env "}"; if(env != "align") print "\n"; env = "none"; } if (text) print text "\n"; text = ""; tbli = 0; next; } # numbered equations /^EQN/{ if(text) print text; text=""; equation = $2; $1 = $2 = ""; if(NF>2) print "\\begin{equation}"; else print "\\begin{align}" print "\\label{eqn:" equation "}"; if(NF>2){ print "\t" substr($0,3); print "\\end{equation}"; } else { env = "align"; } next; } # floats /^FIG/{ chname = FILENAME; sub(".*/","",chname); sub(".txt$","",chname); split($2, figure, /\[|\]/); $1 = $2 = ""; text = substr($0,3); if(figure[2]) width = figure[2]; else width = "0.9"; # width = "0.5"; print "\\begin{figure}[h]" # print "\\begin{figure}[htb]" print "\t\\begin{center}"; print "\t\t\\includegraphics[width=" width "\\textwidth]{../img/" figure[1] "}"; print "\t\\end{center}"; print "\t\\caption[" text "]{"; print "\t\\label{fig:" figure[1] "}"; env = "figure"; next; } /^GRF/{ graph = $2; $1 = $2 = ""; text = substr($0,3); print "\\begin{figure}[htb]" print "\t\\begin{center}"; print "\t\\scriptsize"; print "\t\t\\input{" graph ".tex}"; print "\t\\normalsize"; print "\t\\end{center}"; print "\t\\caption[" text "]{"; print "\t\\label{grf:" graph "}"; env = "figure"; next; } /^TBL[^\*]/{ table = $2; $1 = $2 = ""; text = substr($0,3); print "\\begin{table}[htb]" print "\t\\begin{center}"; dotblfile("../tbl/" table ".txt"); print "\t\\end{center}"; print "\t\\caption[" text "]{"; print "\t\\label{tbl:" table "}"; env = "table"; next; } # tabular /^TBL\*/{ table = $2; $1 = $2 = ""; text = substr($0,3); print "\\begin{table}[htb]\\begin{center}" print "\\begin{threeparttable}"; print("{\\small"); env = "tabular}}\\end{center}\\end{table"; next; } env == "tabular}}\\end{center}\\end{table"{ dotabline(); next; } # headers /^=+$/{ # print "\\afterpage{\\clearpage}"; # clear floats print "\\section{" text "}\\label{sec:" text "}\n"; text = ""; next; } /^-+$/{ print "\\subsection{" text "}\n"; text = ""; next; } /^_+$/{ print "\\subsubsection{" text "}"; text = ""; next; } # unordered lists /^[+*-] /{ if(env == "none"){ env = "itemize"; print "\\begin{itemize}"; } print "\\item " substr($0, 3); next; } # ordered lists /^[0-9#]./{ if(env == "none"){ env = "enumerate"; print "\\begin{enumerate}"; } print "\\item " substr($0, 4); next; } # default //{ if(text){ if (env == "align") text = text "\n" $0; else text = text " " $0; } else text = $0; } END{ # print "\\clearpage"; # floats before bibliography # print "\\bibliographystyle{unsrtnat}\n\\bibliography{" ARGV[1] "}\n"; # print "\\bibliographystyle{unsrtnat}\n\\bibliography{bib.txt}\n"; }