Last active
August 29, 2015 14:01
-
-
Save walterdavis/403146ef74431fc4459e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<folder-action name="com.wdstudio.inlay_folder" title="Inlay"> | |
<action-version version="0.1"> | |
Inlay Folder Action | |
Collects the pages from your document for Inlay.io. | |
(c)2014 Walter Davis Studio | |
</action-version> | |
<action-file var name="manifest"/> | |
<action-javascript> | |
if(!'test'.strip) String.prototype.strip = function() { | |
return this.replace(/^\s+/, '').replace(/\s+$/, ''); | |
}; | |
function parseJSON(data){ | |
if(!data) return; | |
return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(data.replace(/"(\\.|[^"\\])*"/g, ""))) && eval("(" + data + ")"); //" | |
} | |
function toJSON(arr) { | |
var parts = []; | |
var is_list = (Object.prototype.toString.apply(arr) === '[object Array]'); | |
for(var key in arr) { | |
var value = arr[key]; | |
if(typeof value == "object") { //Custom handling for arrays | |
if(is_list) parts.push(toJSON(value)); /* :RECURSION: */ | |
else parts.push('"' + key + '":' + toJSON(value)); /* :RECURSION: */ | |
} else { | |
var str = ""; | |
if(!is_list) str = '"' + key + '":'; | |
//Custom handling for multiple data types | |
if(typeof value == "number") str += value; //Numbers | |
else if(value === false) str += 'false'; //The booleans | |
else if(value === true) str += 'true'; | |
else str += '"' + value + '"'; //All other things | |
// :TODO: Is there any more datatype we should be in the lookout for? (Functions?) | |
parts.push(str); | |
} | |
} | |
var json = parts.join(","); | |
if(is_list) return '[' + json + ']';//Return numerical JSON | |
return '{' + json + '}';//Return associative JSON | |
} | |
function get_inlay_hash(){ | |
var address = complete_url(); | |
if(address.length < 1) return; | |
var loc = fwShellCommand('curl -d -H "Accept: application/json" "http://localhost:3000/pages/get_inlay_hash.json?url=' + escape(address) + '&path=' + escape(fwPage.fwWebAddress().replace(fwDocument.fwWebAddress(), '')) + '&base=' + escape(fwDocument.fwWebAddress()) + '"').toString(); | |
return parseJSON(loc); | |
} | |
function complete_url(){ | |
var site = fwDocument.fwWebAddress(); | |
if(site.length < 1) | |
fwAbort('Please set the Web Address in Document Setup before publishing'); | |
return fwPage.fwWebAddress(); | |
} | |
function fwBeforeEndBody(){ | |
tag = fwDocument.fwTags.fwFind('body'); | |
var inlay_hash = get_inlay_hash(); | |
var myFile = new FWFile(); | |
var rootFolder = fwDocument.fwPages.fwItems[0].fwFolder; | |
if(!rootFolder) fwAbort('Could not locate the root folder. Make sure there is at least one page in it.') | |
var manifest_path = (rootFolder.fwHttpPath(null, true) + '/manifest.txt').replace(/\//g, ':'); | |
if(myFile.fwOpenRead(manifest_path, false)){ | |
var content = parseJSON(myFile.fwReadString(myFile.fwAvailable).strip()); | |
}else{ | |
content = {}; | |
} | |
key = '_' + inlay_hash.slug; | |
content[key] = inlay_hash; | |
myFile.fwOpenWrite(manifest_path, true); | |
myFile.fwSetEncoding('UTF-8'); | |
myFile.fwWrite(toJSON(content)) | |
myFile.fwClose(); | |
} | |
</action-javascript> | |
</folder-action> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment