The goal is simply to provide a way to use Pandoc in Tinderbox. In this template, Pandoc is relevant for two different tasks:
- Preview the Markdown text making use of Pandoc’s Citeproc
- Export the text of the selected notes to files — to a single or to multiple files — in different formats (
md
,epub
,pdf
,latex
,opml
,wikicode
,pptx
, etc.)
In order to do this, two things are needed: the TbxConfig
note and an export stamp
.
With the stamp you can…
- Export each one of the selected notes with or without their children (as part of each file or as part of multiple individual files). (@satikusala, this should address the issue raised in this topic).
From the TbxConfig
note you can…
- Update the preview command in case you decide to make changes to the configuration (click on the checkbox; it will quickly go back to being unchecked and the
$HTMLPreviewCommand
will have been updated). - Choose your export folder.
- Point to your bibliography file*.
- Choose your bibliography style.
- Choose the Pandoc’s conversion templates.
- Choose the converted file’s extension.
- Activate/deactivate conversion options (e.g. table of contents, citations as links to corresponding bibliographical reference).
- Choose the fonts for the preview and Export (only for pdf conversion).
- Choose from different CSS styles available and that will be automatically used in Preview.
- Include children, if the note has any, when exporting the selected notes.
- Choose to export the children of the selected notes as individual files
This template assumes that all of the writing will be done using Markdown. In case you want to use something different, you my need to make a few adjustments.
Simple Pandoc Template 01.tbx (483.8 KB)
Latest version will always be in this Github repository.
For the export stamp to work, you need to download the script below and use TbxConfig to indicate the correct location (see below).
PandocExportTbx.scpt.zip (13.0 KB)
Here is the actual script (click to unfold)
set theRevealCommand to ""
set theOpenCommand to ""
set theTemplateCommand to ""
set theText to ""
tell application "Tinderbox 8"
set TbxConfig to find note in front document with path "/TbxConfig"
set thePandocPath to evaluate TbxConfig with "$PandocPath + \" \";"
set theConvertedExt to evaluate TbxConfig with "$PandocToExtension.lowercase.replace(\"\\.\",\"\");"
set theDocxTemplate to evaluate TbxConfig with "if($PandocDocxTemplate){\" --reference-doc=\" + $PandocDocxTemplate.replace(\"~/\",\"$HOME/\").replace(\"/Users/.+?/\",\"$HOME/\").replace(\"\\s\",\"\\\\ \")}else{\"\"};"
set theLatexTemplate to evaluate TbxConfig with "if($PandocLatexTemplate){\" --template=\" + $PandocLatexTemplate.replace(\"~/\",\"$HOME/\").replace(\"/Users/.+?/\",\"$HOME/\").replace(\"\\s\",\"\\\\ \")}else{\"\"};"
set thePdfTemplate to evaluate TbxConfig with "if($PandocLatexTemplate){\" --template=\" + $PandocLatexTemplate.replace(\"~/\",\"$HOME/\").replace(\"/Users/.+?/\",\"$HOME/\").replace(\"\\s\",\"\\\\ \")}else{\"\"};"
set theMainFont to evaluate TbxConfig with "if($CSSserif){\" -V mainfont:\" + $CSSserif.replace(\"\\s\",\"\\\\ \")}else{\"\"};"
set theSansFont to evaluate TbxConfig with "if($CSSsans){\" -V sansfont:\" + $CSSsans.replace(\"\\s\",\"\\\\ \")}else{\"\"};"
set theMonoFont to evaluate TbxConfig with "if($CSSmono){\" -V monofont:\" + $CSSmono.replace(\"\\s\",\"\\\\ \")}else{\"\"};"
set theCSLFile to evaluate TbxConfig with "if($PandocCSLPath!=\"\"){\" -C --csl=\" + $PandocCSLPath.replace(\"~/\",\"$HOME/\").replace(\"/Users/.+?/\",\"$HOME/\").replace(\"\\s\",\"\\\\ \") + \" \"}else{\"\"};"
set theBibFile to evaluate TbxConfig with "if($PandocBibPath!=\"\"){\" --bibliography=\" + $PandocBibPath.replace(\"~/\",\"$HOME/\").replace(\"/Users/.+?/\",\"$HOME/\").replace(\"\\s\",\"\\\\ \") + \" \"}else{\"\"};"
set LinkMD to evaluate TbxConfig with "if($PandocLinkCitations==true){\" --metadata link-citations=true \"}else{\"\"};"
set theToc to evaluate TbxConfig with "if($PandocTableofContents==true){\" --toc \";}else{\"\"};"
set thePDFEngine to evaluate TbxConfig with "if($PandocPDFEngine!=\"\"){\" --pdf-engine=\" + $PandocPDFEngine + \" \"}else{\"\"};"
set ExportChildrenIndividually to evaluate TbxConfig with "$ExportChildrenIndividually"
set RevealConvertedFile to evaluate TbxConfig with "$RevealConvertedFile"
set OpenConvertedFile to evaluate TbxConfig with "$OpenConvertedFile"
set RunPandocOnExport to evaluate TbxConfig with "$RunPandocOnExport"
set theBibCommand to theCSLFile & theBibFile & LinkMD
set theOptCommand to theBibCommand & theToc
set theSelection to front document's selections
repeat with aNote in theSelection
set theText to evaluate aNote with "if($IncludeChildrenWithParent(\"TbxConfig\")){if($ChildCount >= 1){$Text + \"\\n\\n\" + $Text(children).replace(\";\",\"\\n\\n\")}else{$Text}}else{$Text};"
set theExportedFile to evaluate aNote with "$PandocExportFolder(\"TbxConfig\").replace(\"~/\",\"$HOME/\").replace(\"/Users/.+?/\",\"$HOME/\").replace(\"\\s\",\"\\\\ \") + \"/\" + $Name.replace(\"\\s\",\"_\").replace(\"(\\(|\\))\",\"\") + \".\""
set theExportedExt to evaluate aNote with "$PandocFromExtension(\"TbxConfig\").lowercase.replace(\"\\.\",\"\");"
set theConvertedFile to evaluate aNote with "$PandocExportFolder(\"TbxConfig\").replace(\"~/\",\"$HOME/\").replace(\"/Users/.+?/\",\"$HOME/\").replace(\"\\s\",\"\\\\ \") + \"/\" + $Name.replace(\"\\s\",\"_\").replace(\"(\\(|\\))\",\"\") + \".\";"
set theExportedFile to theExportedFile & theExportedExt
set theConvertedFile to theConvertedFile & theConvertedExt
set theInput to thePandocPath & theExportedFile
set theOutput to "-s -o " & theConvertedFile
if theConvertedExt is "pdf" then set theTemplateCommand to theMainFont & theSansFont & theMonoFont & theLatexTemplate
if theConvertedExt is "latex" then set theTemplateCommand to theMainFont & theSansFont & theMonoFont & theLatexTemplate
if theConvertedExt is "docx" then set theTemplateCommand to theDocxTemplate
if OpenConvertedFile is "true" then set theOpenCommand to " && open " & theConvertedFile
if RevealConvertedFile is "true" then set theRevealCommand to " && open " & theConvertedFile & " -R"
if theConvertedExt is "pdf" then
set theConversionCommand to "export PATH=/Library/TeX/texbin:$PATH &&" & theInput & space & theOutput & theOptCommand & theTemplateCommand & thePDFEngine & theRevealCommand
else
set theConversionCommand to theInput & space & theOutput & theOptCommand & theTemplateCommand & theRevealCommand & theOpenCommand
end if
set the clipboard to theText
do shell script "touch " & theExportedFile & " && pbpaste > " & theExportedFile
if RunPandocOnExport is "true" then do shell script theConversionCommand
if ExportChildrenIndividually is "true" then
set theChildren to notes of aNote
repeat with aNote in theChildren
set theText to evaluate aNote with "if($IncludeChildrenWithParent(\"TbxConfig\")){if($ChildCount >= 1){$Text + \"\\n\\n\" + $Text(children).replace(\";\",\"\\n\\n\")}else{$Text}}else{$Text};"
set theExportedFile to evaluate aNote with "$PandocExportFolder(\"TbxConfig\").replace(\"~/\",\"$HOME/\").replace(\"/Users/.+?/\",\"$HOME/\").replace(\"\\s\",\"\\\\ \") + \"/\" + $Name.replace(\"\\s\",\"_\").replace(\"(\\(|\\))\",\"\") + \".\""
set theExportedExt to evaluate aNote with "$PandocFromExtension(\"TbxConfig\").lowercase.replace(\"\\.\",\"\");"
set theConvertedFile to evaluate aNote with "$PandocExportFolder(\"TbxConfig\").replace(\"~/\",\"$HOME/\").replace(\"/Users/.+?/\",\"$HOME/\").replace(\"\\s\",\"\\\\ \") + \"/\" + $Name.replace(\"\\s\",\"_\").replace(\"(\\(|\\))\",\"\") + \".\";"
set theExportedFile to theExportedFile & theExportedExt
set theConvertedFile to theConvertedFile & theConvertedExt
set theInput to thePandocPath & theExportedFile
set theOutput to "-s -o " & theConvertedFile
if theConvertedExt is "pdf" then set theTemplateCommand to theMainFont & theSansFont & theMonoFont & theLatexTemplate
if theConvertedExt is "latex" then set theTemplateCommand to theMainFont & theSansFont & theMonoFont & theLatexTemplate
if theConvertedExt is "docx" then set theTemplateCommand to theDocxTemplate
if OpenConvertedFile is "true" then set theOpenCommand to " && open " & theConvertedFile
if RevealConvertedFile is "true" then set theRevealCommand to " && open " & theConvertedFile & " -R"
if theConvertedExt is "pdf" then
set theConversionCommand to "export PATH=/Library/TeX/texbin:$PATH &&" & theInput & space & theOutput & theOptCommand & theTemplateCommand & thePDFEngine & theRevealCommand
else
set theConversionCommand to theInput & space & theOutput & theOptCommand & theTemplateCommand & theRevealCommand & theOpenCommand
end if
set the clipboard to theText
do shell script "touch " & theExportedFile & " && pbpaste > " & theExportedFile
if RunPandocOnExport is "true" then do shell script theConversionCommand
end repeat
end if
end repeat
end tell
on replaceText(theString, old, new)
set {TID, text item delimiters} to {text item delimiters, old}
set theStringItems to text items of theString
set text item delimiters to new
set theString to theStringItems as text
set text item delimiters to TID
return theString
end replaceText
The script is also available at /Codes/Pandoc/Export
.
P.S.: You might need to change Tinderbox 9
to Tinderbox 8
in the tell command of the Applescript.
* If you never used Pandocs bibliography feature, consider giving it a try. It is both useful and easy to use. Especially if you are using Bibdesk.