Skip to content

Instantly share code, notes, and snippets.

@jasonsnell
Created February 25, 2013 21:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jasonsnell/5033421 to your computer and use it in GitHub Desktop.
Save jasonsnell/5033421 to your computer and use it in GitHub Desktop.
Macworld/IDG Markdown to HTML AppleScript
--Macworld Markdown to HTML script
--by Jason Snell <jsnell@macworld.com>
--Markdown and SmartyPants by John Gruber <http://daringfireball.net/projects>
--Version 10: Added Euro support and forced Unicode for output file
--Version 11: Output now opens as new document in source window.
--Version 12: Makes sure the output document is set to source language of HTML
--Version 13: Support for [[jump]] tag
--Version 14: Rewrite iLife ‘09 style tags to proper ’09 style.
--Version 15: Support pound symbol
--Version 16: Embedded SmartyPants and Markdown inside the app bundle.
--Verison 17: Removed Convert to ASCII step to allow accented characters
--Version 18: Fixes for BBEdit 9.5
--Version 19: Closes up emdashes
--Version 20: Fixes for BBEdit 10
--Version 21: ebook and email
--Version 22: edit-staging removed
--Verison 23: Moved to BBEdit package. SmartyPants and Markdown moved next door to Text Filters.
--Version 25: New CMS
--Version 26: More changes for new CMS, multisite
--The script needs to know where it is on your system
--This block figures out its path
set thePath to (path to me as text)
set AppleScript's text item delimiters to ":"
set thePathItems to text items of thePath
set AppleScript's text item delimiters to {""}
set parsedPath to items 1 thru ((count of items of thePathItems) - 2) of thePathItems
set AppleScript's text item delimiters to ":"
set parentPath to parsedPath as string
set AppleScript's text item delimiters to {""}
--Remember where SmartyPants and Markdown are, next door in Text Filters
set SmartyPants to parentPath & ":Text Filters:SmartyPants.pl"
set Markdown to parentPath & ":Text Filters:Markdown.pl"
--Copy existing document to new window for conversion
tell application "BBEdit"
set theItem to (contents of window 1 as text)
set theDocument to (make new text document of window 1)
set theVersion to version
set encoding of theDocument to "Unicode (UTF-8)"
set text of theDocument to theItem
set name of theDocument to "HTML output"
set source language of theDocument to "HTML"
end tell
--style check!
replacemeliteral("web site", "website")
replacemeliteral("e-mail", "email")
replacemeliteral("e-book", "ebook")
replacemeliteral("Ethernet", "ethernet")
replacemeliteral(" target=\"_blank\"", "")
replacemeliteral("web page", "webpage")
replacemeliteral("home page", "homepage")
replacemeliteral("NVIDIA", "Nvidia")
replaceme("(Command|Control|Option|Shift)\\+(Command|Control|Option|Shift)\\+(.)", "\\1-\\2-\\3")
replaceme("(Command|Control|Option|Shift)\\+(.)", "\\1-\\2")
--hide the pound sign
replacemeliteral("£", "[poundsymbol]")
--edit staging check
replacemeliteral("preview.www.", "www.")
-- Convert Markdown to HTML
tell application "BBEdit"
run unix filter Markdown with replacing selection
end tell
-- Use SmartyPants to convert quotes, etc. into entities
tell application "BBEdit"
run unix filter SmartyPants with replacing selection
end tell
--The great emdasherator - All hail Dan Frakes!
replaceme(" *-- *", "—")
--hides greater-thans and less-thans
replacemeliteral("&gt;", "[greaterthansymbol]")
replacemeliteral("&lt;", "[lessthansymbol]")
-- Use SmartyPants to convert quotes, etc. into entities
tell application "BBEdit"
run unix filter SmartyPants with replacing selection
end tell
tell application "BBEdit"
-- converts HTML entities back into 8-bit characters
translate html to text entity conversion true ¬
create new document false ¬
without selection only
end tell
--returns greater-thans and less-thans
replacemeliteral("[greaterthansymbol]", "&gt;")
replacemeliteral("[lessthansymbol]", "&lt;")
--Kicking it Eurostyle, like MC Dan Frakes tells us to
replacemeliteral("[euro]", "€")
replacemeliteral("[poundsymbol]", "£")
-- replaces stray ampersands with the ampersand entity to satisfy TULIPS and its cruel masters
-- but doesn't swallow ampersands that are part of the < > entities
-- replaceme("&(?!(lt|gt);)", "&amp;")
--Expand [[4.0 mice]] style into proper IMG tag
replaceme("\\[\\[([0-5])\\.([05]) (mice)\\]\\]", "<img src=\"http://images.macworld.com/images/layout/bluemouse\\1\\2.gif\" border=\"0\" alt=\"2.5-mouse rating\"/>")
--TULIPS-ify images. default is medium, align right.
--replaceme("<img src=\"([^\"]+)\" alt=\"([^\"]+)\" title=\"([^\"]+)\" />", "<img src=\"http://images.macworld.com/images/reviews/graphics/AID-\\1\" align=\"right\" alt=\"\\2\" title=\"\\3\" class=\"medium\" />")
--replace jump tag with actual jump tag
replaceme("<p>\\[\\[jump\\]\\]</p>", "<a href=\"/jump\">Read more...</a>")
--close up emdashes
replacemeliteral(" — ", "—")
--Properly orient curly tags for iLife ’09 and its ilk
replaceme("‘(?=[0-9]{2}[^0-9])", "’")
--and we're done.
(*
Below here are the subroutines that do the search-and-replace action.
*)
--replace a string in BBEdit using grep
on replaceme(theFind, theReplace)
tell application "BBEdit"
replace theFind using theReplace searching in text of text window 1 options {search mode:grep, starting at top:true, wrap around:true, backwards:false, case sensitive:false, match words:false, extend selection:false}
end tell
end replaceme
--replace a string in BBEdit without using grep
on replacemeliteral(theFind, theReplace)
tell application "BBEdit"
replace theFind using theReplace searching in text of text window 1 options {search mode:literal, starting at top:true, wrap around:true, backwards:false, case sensitive:false, match words:false, extend selection:false}
end tell
end replacemeliteral
@jasonsnell
Copy link
Author

Apologies to real programmers. This is ugly but it's helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment