Skip to content

Instantly share code, notes, and snippets.

@alanzchen
Created May 27, 2021 06:52
Show Gist options
  • Save alanzchen/a8b50c56536f9d8f31a822d85ab2d4c0 to your computer and use it in GitHub Desktop.
Save alanzchen/a8b50c56536f9d8f31a822d85ab2d4c0 to your computer and use it in GitHub Desktop.
-- Convert Markdown documents to Pandoc PDFs (using XeTeX)
-- Created by Christian Grunenberg on Mon Dec 01 2008.
-- Copyright (c) 2008-2011. All rights reserved.
-- Slightly changed by Silverstone on March 18 2019,
-- All copyrights go to great DEVONtech Team ;)
-- Repurposed by Alan Chen on May 27 2021
tell application id "DNtp"
try
set theSelection to the selection
if theSelection is not {} then
show progress indicator "Converting..." steps (count of theSelection)
repeat with theRecord in theSelection
set theName to (name of theRecord) as string
step progress indicator theName
if cancelled progress then exit repeat
set theType to type of theRecord
if theType is not group and theType is not feed and theType is not smart group then
set theSource to path of theRecord
-- Setup Your Temporary Folder Here:
set Path_to_MD to "/tmp/" & theName & ".md"
set theOutput to "/tmp/" & theName & ".pdf"
set theURL to URL of theRecord
do shell script "perl -i -pe's/src=\"\\/\\//src=\"http:\\/\\//g' \"" & theSource & "\""
do shell script "/usr/local/bin/pandoc \"" & theSource & "\" -o \"" & Path_to_MD & "\" "
-- Construct your personal command line options here:
do shell script "export PATH=/Library/TeX/texbin:$PATH && /usr/local/bin/pandoc \"" & Path_to_MD & "\" --lua-filter=\"/Users/alan/Library/Application Scripts/com.devon-technologies.think3/Menu/Convert/rm_attr.lua\" -V geometry:a5paper,margin=2cm -V mainfont=\"New York Small\" -M title=\"" & theName & "\" -o \"" & theOutput & "\" --pdf-engine=xelatex"
try
set theParents to parents of theRecord
set thePDF to import theOutput to (item 1 of theParents) name theName
repeat with i from 2 to (count of theParents)
replicate record thePDF to (item i of theParents)
end repeat
set URL of thePDF to URL of theRecord
set creation date of thePDF to creation date of theRecord
set modification date of thePDF to modification date of theRecord
set comment of thePDF to comment of theRecord
set label of thePDF to label of theRecord
end try
-- tell application "Finder" to delete Path_to_MD as POSIX file
tell application "Finder" to delete theOutput as POSIX file
end if
end repeat
hide progress indicator
end if
on error error_message number error_number
hide progress indicator
if the error_number is not -128 then display alert "DEVONthink Pro" message error_message as warning
end try
end tell
function remove_attr (x)
if x.attr then
x.attr = pandoc.Attr()
return x
end
end
return {{Inline = remove_attr, Block = remove_attr}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment