
Murasaki is a generic robot writer for J++ and Newsworthy that consumes either Pug templates or Javascript functions. It's named after Murasaki Shikibu.
Note that Murasaki is build for pregenerating texts, not to be called in frontend code!
The Murasaki API can compile Pug templates or Javascript template strings (so called template literals), with a number of helper functions and mixins.
For further documentation see /docs
[POST] /html?lang=sv-SE&timezone=Europe/Stockholmhtml endpointThe request body should follow this general pattern:
{
data,
template|templates,
templateEngine,
translations
}
template contains the actual template. templates is an array of `{id, text}` objects. One of the two must be provided.
data will be exposed to the template in the same namespace as helper functions and mixins, meaning that you need to be careful to avoid naming conflicts with built-in functions.
templateEngine is the template engine to use.
Supported engines are pug, pugz (Pug but gzipped, base 64),
function (JS functions code,), javascript (JS string literal), and mustache.
JS code, string literals and pug code is sandboxed and only a subset of JS is allowed through.
translations is a translation dictionary.
{
"data": {"a": "world"},
"engine": "pug",
"template": "p Hello #{a}!"
}will produce:<p>Hello world!</p>
{
"data": {"a": "World"},
"template": "({a, upper}) => `Hello ${upper(a)}!`",
"engine": "function"
}will produce:Hello WORLD!
{
"template": "eJwrUMjJL0rNVcgsKC7NVUjJB/IUijNLFBJzU0sAlrcKdQ==",
"engine": "pugz"
}will produce:<p>lorem ipsum dolor sit amet</p>
The template string used in the last (pugz) example can be produced like this:
const zlib = require("zlib")
zlib.deflateSync("p lorem ipsum dolor sit amet").toString("base64")
echo 'p lorem psum dolor sit amet' | gzip | base64 -w0
import base64
import zlib
base64.b64encode(zlib.compress(b"p lorem ipsum dolor sit amet"))
Set Cache-Control `frontend` to add aggressive caching directive, in case Murasaki is called from a browser. Note that this is _not_ the recommended way to use Murasaki.
Using requests and Python 3
import requests
from requests.auth import HTTPBasicAuth
endpoint = "https://jplusplus-murasaki.herokuapp.com/html?language=en"
template = """
h1 #{upper(book_title)}
p #{title(book_title)} was written by #{author}, and published in #{monthYear(published_date)}.
"""
context = {
'book_title': "A Tale of Two Cities",
'author': "Charles Dickens",
'published_date': "1859-04-30",
}
r = requests.post(
endpoint,
json={
'template': template,
'data': context,
'engine': "pug",
},
auth=HTTPBasicAuth(os.environ["MURASAKI_USER"], os.environ["MURASAKI_PWD"])
)
print(r.text)
An example using Axios in a NodeJS environment.
const axios = require("axios")
const endpoint = "https://jplusplus-murasaki.herokuapp.com/html?language=en"
const template = {
book_title: "A Tale of Two Cities",
author: "Charles Dickens",
published_date: "1859-04-30",
}
const data = `
h1=upper(book_title)
p #{title(book_title)} was written by #{author}, and published in #{monthYear(published_date)}.
`
axios
.post(url, {
data,
template,
engine: "pug",
}, {
auth: {
username: process.env.MURASAKI_USER,
password: process.env.MURASAKI_PWD,
}
})
.then(res => console.log(res))
.catch(err => console.log(err))