Org-mode support for MarkLogic

Volume 19, Issue 10; 08 May 2016

Hacking MarkLogic support into Emacs org-mode code blocks.

Regular readers will know that Emacs is my tool of choice for almost every kind of editing. In the course of some recent explorations of various sorts of non-XML markup formats (AsciiDoc, Common Mark, and Org-modeThis posting is not my rant about editing formats, their deficiencies, or my reluctant acceptance of their utility. That one’s coming later.) I’ve (re)discovered some of the exceptional features of Org-mode.

In particular, Org-mode allows you to identify code blocks, like so:

#+begin_src python
def fib(x):
  if x==1 or x==2:
    return 1
    return fib(x - 1) + fib(x - 2)

return fib(10)

What’s special about these blocks is that typing C-c ' in one will open the code in a new window in the relevant editing mode. At least as interesting, typing C-c C-c will run the code block in the correct environment and return the answer, inserting it into your document. Typing C-c C-c in the above block adds this result:

: 55

In my day job, I write a lot of XQuery and some JavaScript and a little SPARQL. Most of which is expected to run in MarkLogic. [You can see where this is going, can’t you? —ed]

A couple of late evenings of hacking later and ob-ml-marklogic was born. Set it up and give this a try:

#+begin_src ml-xquery :var startDate="2017-04-19T12:34:57"
    xquery version "1.0-ml";

    declare default function namespace "";

    declare option xdmp:mapping "false";

    declare variable $startDate external;

    let $date   := $startDate cast as xs:dateTime
    let $diff   := current-dateTime() - $date
      current-dateTime() - $date

It’ll package up the query, fire it off to MarkLogic’s “eval” endpoint, parse the result, tidy it up if it’s JSON or XML, and drop it into your document.

Very cool. The result is

: -P345DT19H17M43.755462S

at the moment, FWIW.

Also cool, and also not the subject of this posting, I have a halfway finished hack that takes the Org-mode AST and turns it into XML!