VPWebExportScript
is a special JavaScript page that is executed along with the web export process. You can alter the behavior of what happens by implementing a couple of JavaScript functions. Simply create a page named "VPWebExportScript" in your document, and VP will autofill the required methods for you in this page.
The methods in this page are webExportWillBegin
, webExportWillMarkupAttributedStringForItem
, webExportWillWriteHTMLForItem
, webExportDidEnd
. When they are called, it gives you the opportunity to alter how pages are exported, by altering the HTML before it is written to disk, or perform certain actions based on the page being exported.
Note: The VPWebExportScript
script may be executed in a background thread.
The contextDictionary
object that is passed to the various events functions is a mutable dictionary (specifically, an NSMutableDictionary
). You can put data in there to pass to various functions later on. It also includes the following keys and values:
outputDirectoryURL
, which is an NSURL
object pointing to the location where the web export is writing to.pagesToExport
, which is an NSArray of the page keys being exported.webExportPresetName
, is present when there is a web export that has been selected. If there isn't a preset being selected, then the key will not be present.Besides the context dictionary, there is also a global variable named document
which represents your VoodooPad document.
Using the web export event method, you can make an RSS feed at the same time your document is exported. You can download a working example from here: RSSWebExportSample.vpdoc, and the source to the VPWebExportScript
page is available for you to look at below. It uses a combination of JavaScript and Cocoa to get the job done.
var rss = "";
var timeFormat = "%Y.%m.%d %I:%m %p"
var baseURL = "http://plausible.coop/voodoopad/extras/rsswebexport/";
function webExportWillBegin(contextDictionary) {
rss = rss + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
"<rss version=\"2.0\"\n" +
" xmlns:content=\"http://purl.org/rss/1.0/modules/content/\"\n" +
" xmlns:itunes=\"http://www.itunes.com/dtds/podcast-1.0.dtd\"\n" +
" xmlns:dc=\"http://purl.org/dc/elements/1.1/\"\n" +
" xmlns:atom=\"http://www.w3.org/2005/Atom\">\n" +
" <channel>\n" +
" <title>Sample Title</title>\n" +
" <link>" + baseURL + "</link>\n" +
" <atom:link href=\"" + baseURL + "rss.xml\" rel=\"self\" type=\"application/rss+xml\" />\n" +
" <pubDate>" + formatDateForRSS(NSDate.date()) + "</pubDate>\n" +
" <description>Sample Description.</description>\n";
}
function webExportWillMarkupAttributedStringForItem(contextDictionary, item, attributedString) {
return attributedString;
}
function webExportWillWriteHTMLForItem(contextDictionary, item, fileName, mutableHTMLString) {
rss = rss + " <item>\n" +
" <title>" + escapeForXML(item.displayName()) + "</title>\n" +
" <link>" + baseURL + escape(escapeForXML(fileName)) + "</link>\n" +
" <description>" + escapeForXML(item.stringData()) + "</description>\n" +
" <guid>" + baseURL + escape(escapeForXML(fileName)) + "</guid>\n" +
" <pubDate>" + formatDateForRSS(item.createdDate()) + "</pubDate>\n" +
" </item>\n";
if (item.key().isEqualToString("index")) {
// let's add the rss link to our index page.
var searchRange = NSMakeRange(0, mutableHTMLString.length());
var headAddition = "<link rel=\"alternate\" type=\"application/rss+xml\" title=\"RSS\" href=\"rss.xml\" />";
mutableHTMLString.replaceOccurrencesOfString_withString_options_range_("</head>", headAddition + "\n</head>", 0, searchRange);
}
return mutableHTMLString;
}
function webExportDidEnd(contextDictionary) {
rss = rss + " </channel>\n</rss>";
var s = NSString.stringWithString(rss);
var outputDirectoryURL = contextDictionary.outputDirectoryURL;
var urlToSaveTo = outputDirectoryURL.URLByAppendingPathComponent_("rss.xml");
s.writeToURL_atomically_encoding_error_(urlToSaveTo, true, NSUTF8StringEncoding, null);
}
function escapeForXML(s) {
s = s.stringByReplacingOccurrencesOfString_withString_("&", "&");
s = s.stringByReplacingOccurrencesOfString_withString_(">", ">");
s = s.stringByReplacingOccurrencesOfString_withString_("<", "<");
return s;
}
function formatDateForRSS(d) {
var df = NSDateFormatter.alloc().init().autorelease();
df.setDateFormat("EE, dd MMM yyyy HH:mm:ss Z");
return df.stringFromDate(NSDate.date());
}