Created
May 16, 2015 13:05
-
-
Save balaam/7abcf6409281baa15728 to your computer and use it in GitHub Desktop.
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
| #!/opt/local/bin/lua | |
| local addFigs = true | |
| -- print("Markdown -> TeX") | |
| -- print("...") | |
| -- os.execute('pandoc -s ./test.md -o ./middle.tex --latex-engine="xelatex" -V --highlight-style espresso documentclass="report"') | |
| -- print("Done.") | |
| -- print(" ") | |
| -- print("TeX -> PDF") | |
| -- print("...") | |
| -- os.execute('pandoc -s ./middle.tex -o ./output.pdf --latex-engine="xelatex" -V --highlight-style espresso documentclass="report"') | |
| -- print("Done.") | |
| local command = 'pandoc %s --template=template.tex --variable mainfont=Verdana --variable monofont=Inconsolata -s --highlight-style espresso --latex-engine=xelatex -V documentclass="report" -s -o %s' | |
| local commandToTex = 'pandoc %s --template=template.tex --variable mainfont=Verdana --variable monofont=Inconsolata -s --latex-engine=xelatex -V documentclass="report" -s -o %s' | |
| executef = function(s, ...) os.execute(string.format(s, ...)) end | |
| os.execute("mkdir ./tmp > /dev/null 2>&1") | |
| book = | |
| { | |
| title = "../title.txt", | |
| metadata = "../metadata.xml", | |
| chapters = | |
| { | |
| -- 01 Intro | |
| { | |
| "./test1.md", | |
| "./test2.md" | |
| }, | |
| { | |
| "./test3.md", | |
| } | |
| } | |
| } | |
| local img = | |
| { | |
| count = {}, | |
| refToNumMap = {} | |
| } | |
| local code = | |
| { | |
| count = {}, | |
| refToNumMap = {} | |
| } | |
| function ExtractRef(s) | |
| local m = "#(.-)#" | |
| local tag = nil | |
| s = s:gsub(m, | |
| function(first) | |
| tag = first | |
| return "" | |
| end, 1) | |
| return s, tag | |
| end | |
| function LinkRefs(filename) | |
| local f = io.open(filename, "r") | |
| assert(f) | |
| local t = f:read("*all") | |
| f:close() | |
| f = io.open(filename, "w") | |
| local mFig = "\\fig{(.-)}" | |
| t = t:gsub(mFig, | |
| function(tag) | |
| local ref = img.refToNumMap[tag] | |
| if not ref then print("Warning unknown figure", tag) end | |
| return string.format("Figure %s", ref) | |
| end) | |
| local mRef = "\\ref{(.-)}" | |
| t = t:gsub(mRef, | |
| function(tag) | |
| local ref = code.refToNumMap[tag] | |
| if not ref then print("Warning unknown listing", tag) end | |
| return string.format("Listing %s", ref) | |
| end) | |
| f:write(t) | |
| f:close() | |
| end | |
| function Escape(s) | |
| local s = s:gsub("_", "\\_") | |
| s = s:gsub("`", "") | |
| return s | |
| end | |
| function SetupRef(comment, section, c) | |
| local realComment, ref = ExtractRef(comment) | |
| c.count[section] = c.count[section] or 1 | |
| local figNum = string.format("%d.%d", section, c.count[section]) | |
| if ref then | |
| if c.refToNumMap[ref] then | |
| print("Warning ref already in use", ref, c.refToNumMap[ref]) | |
| end | |
| c.refToNumMap[ref] = figNum | |
| end | |
| c.count[section] = c.count[section] + 1 | |
| return string.format("%s: %s", figNum, Escape(realComment)) | |
| end | |
| function AddImgRefs(t, k) | |
| return string.gsub(t, "!%[(.-)]%s*%((.-)%)%s*\\", | |
| function(comment, path) | |
| local realComment = comment | |
| realComment, ref = ExtractRef(realComment) | |
| if addFigs then | |
| realComment = SetupRef(comment, k, img) | |
| end | |
| local ret = "\\\n\\begin{center} \\emph{Figure %s} \\end{center}\n\n" | |
| return string.format(ret, comment, path, realComment) | |
| end) | |
| end | |
| function AddCodeRef(t, k) | |
| return t:gsub('~~~{%s*caption%s*=%s*"(.-)".-}(.-)~~~', | |
| function(comment, snippet) | |
| local realComment = comment | |
| if addFigs then | |
| realComment = SetupRef(comment, k, code) | |
| end | |
| local ret = '~~~{caption="%s" .lua} %s~~~\n\\begin{center} \\emph{Listing %s} \\end{center}\n\n' | |
| return string.format(ret, comment, snippet, realComment) | |
| end) | |
| end | |
| function ModifySectionFile(filename, k) | |
| local f = io.open(filename, "r") | |
| assert(f) | |
| local t = f:read("*all") | |
| f:close() | |
| f = io.open(filename, "w") | |
| t = AddImgRefs(t, k) | |
| t = AddCodeRef(t, k) | |
| f:write(t) | |
| f:close() | |
| end | |
| -- Each section is gathered up into one large file | |
| -- called section_X | |
| -- These merged sections are then gathered up into a | |
| -- master file that's all the data in the book | |
| -- | |
| local master_flat = {} | |
| for k, chapter in ipairs(book.chapters) do | |
| local section = {} | |
| for _, path in ipairs(chapter) do | |
| table.insert(section, path) | |
| end | |
| local file_list = table.concat(section, ' ') | |
| local filename = string.format("./tmp/section_%d", k) | |
| executef("cat %s > %s", file_list, filename) | |
| ModifySectionFile(filename, k) | |
| table.insert(master_flat, filename) | |
| end | |
| local source_file_list = table.concat(master_flat, ' ') | |
| executef("cat %s > ./tmp/mega.md", source_file_list) | |
| print("Mega file created.") | |
| for k, v in pairs(img.refToNumMap) do | |
| print('img', k ,v) | |
| end | |
| for k, v in pairs(code.refToNumMap) do | |
| print('code', k ,v) | |
| end | |
| -- We replace all the \fig{} commads with Figure ?? | |
| -- Replace \ref{} with Listing ?? | |
| LinkRefs("./tmp/mega.md") | |
| os.execute(string.format(command, "./tmp/mega.md", "output_md.pdf")) | |
| -- print("Markdown -> TeX") | |
| -- print("...") | |
| -- os.execute(string.format(commandToTex, "test.md", "middle.tex")) | |
| -- print("Done.") | |
| -- print(" ") | |
| -- print("TeX -> PDF") | |
| -- os.execute(string.format(command, "middle.tex", "output.pdf")) | |
| -- print("Done.") | |
| -- print("Markdown -> PDF") | |
| -- os.execute(string.format(command, "test.md", "output_md.pdf")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment