MACRO Template: Copy Quiver Note to Clipboard as JavaScript
Copy Quiver Note to Clipboard as JavaScript.kmmacros (18.7 KB)
###NOTES:
This is a first draft. It works as it is, but I expect it to go through many changes.
I'm posting this so other people can take the idea, and go wherever they want with it. It's just a working template - use it as is, or let your imagination run wild (or both).
##PURPOSE:
This is a macro which takes a Quiver note formatted like this:

and translate it into JavaScript with comments, like this:
// Does word-wrapping on a string.
// PARAMETERS:
// str: The input string.
// width: The width to wrap at, or 0 for no wrapping.
// cut: Cut in the middle of a long word, if needed.
// RESULTS:
// Returns an array of strings.
// UPDATED: 2016/06/16 09:23 PDT
function wordWrap(str, width, cut) {
if (!str || width <= 0) {
return [str];
}
cut = cut || false;
var regex = '.{1,' + width + '}(\\s|$)' +
(cut ? '|.{' + width + '}|.+$' : '|\\S+?(\\s|$)');
return str.match(RegExp(regex, 'g'));
}
The main item of interest is the JS code that does the translation. Currently, this is the logic:
- It expects there to be Markdown code, followed by the JavaScript code block.
- It has Regexes for markdown lines that should be skipped. In this example, the "##" (h2) line is removed, as is the "## PURPOSE" line. Use as you see fit.
- It removes the leading "#" characters.
- It optionally word-wraps the comments, using a VERY COOL word wrapping function I found.
- It adds leading "//" characters
- It removes blank lines between the last comment and the JavaScript code.
###Things it does not handle:
- There is a function called "removeSpecialMarkings", but it isn't coded yet. It's intended to be code that removes Markdown code like bold, etc.
###Here's the source for JavaScript routine:
(function() {
'use strict';
function readFile(strPath) {
var error = $(),
str = ObjC.unwrap(
$.NSString.stringWithContentsOfFileEncodingError(
$(strPath)
.stringByStandardizingPath,
$.NSUTF8StringEncoding,
error
)
);
if (error && error.code) {
throw Error('Error ' + error.code + ' reading file "' + strPath + '"');
}
return str;
}
function shouldSkip(line) {
return [
/^## /,
/^#* PURPOSE/,
/^### AUTOMATION/
].some(function(regexp) {
return regexp.test(line);
});
}
function wordWrap(str, width, cut) {
if (!str || width <= 0) {
return [str];
}
cut = cut || false;
var regex = '.{1,' + width + '}(\\s|$)' +
(cut ? '|.{' + width + '}|.+$' : '|\\S+?(\\s|$)');
return str.match(RegExp(regex, 'g'));
}
function commentAndWordWrap(str, width) {
var lines = wordWrap(str, width, false);
var result = "";
lines.forEach(function(line) {
result = (result + (result === "" ? "" : "\n") + "// " + line)
.trimRight();
});
return result;
}
// tbd
function removeSpecialMarkings(str) {
return str;
}
var _kme = Application("Keyboard Maestro Engine");
var _filePath = _kme.getvariable("cnjsFileName");
if (_filePath == "") {
throw "No file name";
}
var _inputLines = readFile(_filePath).split("\n");
var _wrapWidth = 75;
var _result = [];
var _inJS = false;
for (var i = 0; i < _inputLines.length; i++) {
var line = _inputLines[i].trimRight();
if (_inJS) {
if (line.match(/^```/)) {
break;
}
_result.push(line);
} else if (line.match(/^```/)) {
_inJS = true;
if (_result.length > 0 && _result[_result.length - 1] == "//") {
_result.pop();
}
} else if (!shouldSkip(line)) {
_result.push(
commentAndWordWrap(
line.replace(/(#* ?)/, ""), _wrapWidth));
}
}
return _result.join("\n");
})()