Skip to content

Instantly share code, notes, and snippets.

@davedelong
Last active June 28, 2019 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davedelong/90e5c98c6c3ee3703d0c314c2f077586 to your computer and use it in GitHub Desktop.
Save davedelong/90e5c98c6c3ee3703d0c314c2f077586 to your computer and use it in GitHub Desktop.
Export a blog from MarsEdit
on pad(thisNumber)
set thisText to thisNumber as text
if length of thisText = 1 then
return "0" & thisText
else
return thisText
end if
end pad
on formatTimestamp(thisDate)
set {hours:h, minutes:m, seconds:s} to thisDate
set t to my pad(h as integer) & ":" & my pad(m as integer) & ":" & my pad(s as integer)
return my formatDate(thisDate) & "T" & t & "-0700"
end formatTimestamp
on formatDate(thisDate)
set {year:y, month:m, day:d} to thisDate as date
return (y as text) & "-" & my pad(m as integer) & "-" & my pad(d as integer)
end formatDate
on join(pieces)
set final to ""
set max to length of pieces
repeat with a from 1 to max
set final to final & "\"" & (item a of pieces) & "\""
if a is less than max then
set final to final & ", "
end if
end repeat
return final
end join
on writeFile(thisText, thisFile)
-- display dialog "writing to " & thisFile as text
try
do shell script "touch " & quoted form of thisFile
set pFile to POSIX file thisFile
set openFile to (open for access file pFile with write permission)
set eof of openFile to 0
write thisText to openFile starting at eof
close access openFile
on error errMsg
try
close access file pFile
end try
display dialog errMsg
end try
end writeFile
tell application "MarsEdit"
(* pick a blog to export *)
set nameList to name of every blog
if length of nameList is greater than 1 then
set blogName to (choose from list nameList with title "Choose a blog to export:")
set b to first blog whose name is blogName
else
set b to first blog
end if
set postList to posts of b
(* pick a folder to receive the posts *)
set outputFolder to POSIX path of (choose folder with prompt "Select a folder to contain the exported posts")
repeat with p in postList
set s to slug of p as text
set pubDate to published date of p
set fileName to my formatDate(pubDate) & "-" & s & ".md"
set frontMatter to "---
layout: post
id: " & s & "
title: \"" & (title of p as text) & "\"
published: " & my formatTimestamp(pubDate) & "
tags: [" & my join(category names of p) & "]
---
"
set content to frontMatter & (body of p as text)
set fullPath to outputFolder & fileName
my writeFile(content, fullPath)
end repeat
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment