Last active
January 12, 2016 21:37
-
-
Save jobez/02980b18a90a3c8a7a23 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| #!/usr/bin/env node | |
| #!/usr/bin/env node | |
| var cljs = require("clojurescript"); | |
| var orgModeParser = require("org-mode-parser"); | |
| var _ = require("underscore"); | |
| var fs = require("fs"); | |
| String.prototype.attribute = function(attr, match) { | |
| return this.replace(match, "\x1b[" + attr + "m$1\x1b[0m"); | |
| } | |
| process.stdin.setEncoding("utf8"); | |
| function logToRelief(msg) { | |
| fs.appendFile("relief.text", msg); | |
| } | |
| var cljsCodeChunks = ["(fn [logger]", | |
| " (let [cb-registry (atom {\"org-pipe-in\" []", | |
| " \"org-parsed-out\" []})]", | |
| " (js-obj \"whenbang\" (fn [event cb]"," (logger event)", | |
| " (swap! cb-registry update event conj cb))"," \"preporg\" (fn [preporg]", | |
| " (let [prepped (js-obj)]"," (doseq [k (range (get preporg \"count\"))]", | |
| " (aset prepped k (get preporg k)))"," prepped))", | |
| " \"thenbang\" (fn [event]"," (fn [result]"," (doseq [cb (get @cb-registry event)]"," (logger event)"," (logger result)", | |
| " (cb result)))))))",""] | |
| var toEdn = cljs.eval("#(do (js->clj (js/JSON.parse (js/JSON.stringify %)) :keywordize-keys true))"); | |
| var ppr = cljs.eval("(fn [message] (cljs.pprint/pprint message))"); | |
| var bangbangCtr = cljs.eval(cljsCodeChunks.join("")); | |
| function logToFile(msg) { | |
| fs.appendFile("log.text", JSON.stringify(msg)); | |
| } | |
| var whenThen = bangbangCtr(logToFile); | |
| function orgParser(results){ | |
| orgModeParser.makelistFromStringWithPerformance(results, whenThen.thenbang("org-parsed-out"), true) | |
| } | |
| function prePrep(parsePrep) { | |
| var prepped = _.reduce(_.range(parsePrep.count), function(truth, idx) { | |
| truth.push(parsePrep[idx]); | |
| return truth; | |
| }, []) | |
| return prepped.join(""); | |
| } | |
| whenThen.whenbang("org-parsed-out", _.compose(ppr, toEdn)) | |
| whenThen.whenbang("org-pipe-in", _.compose(orgParser, prePrep)); | |
| // org file is piped in -> | |
| // parsed by node library -> | |
| // handed to cljs to ednify -> | |
| // sent to stdout -> | |
| // ~* world peace *~ | |
| function nestRest(nest) { | |
| debugger; | |
| var self = this; | |
| self.count++ | |
| var next = {nest: nest, | |
| rest: null}; | |
| self[self.count] = nest; | |
| if(nest === "end") { | |
| self.then(self) | |
| } else { | |
| return nestRest.bind(self); | |
| } | |
| } | |
| var next = nestRest.bind({count: -1, then: whenThen.thenbang("org-pipe-in")}); | |
| process.stdin.on("data", function(chunk) { | |
| debugger; | |
| if(next){ | |
| next = next(chunk) | |
| }; | |
| }); | |
| process.stdin.on("end", function() { | |
| debugger; | |
| next("end"); | |
| next = null; | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment