This commit is contained in:
Talon Poole 2021-01-28 23:13:02 +00:00
parent e587d1a933
commit 06a8bab54a
2 changed files with 22 additions and 20 deletions

2
cli.js
View file

@ -24,7 +24,7 @@ require("yargs")
(argv) => { (argv) => {
fs.src(argv.files) fs.src(argv.files)
.pipe(map(tokenize)) .pipe(map(tokenize))
.pipe(map(toHTML({css: argv["css"]}))) .pipe(map(toHTML({ css: argv["css"] })))
.pipe(fs.dest((file) => path.dirname(file.path))); .pipe(fs.dest((file) => path.dirname(file.path)));
} }
) )

View file

@ -1,13 +1,15 @@
const TOKENS_EXT = /\.tokens\.json$/; const TOKENS_EXT = /\.tokens\.json$/;
module.exports = ({css} = {css: true}) => (file, cb) => { module.exports = ({ css } = { css: true }) => (file, cb) => {
if (!TOKENS_EXT.test(file.path)) return cb(null, file); if (!TOKENS_EXT.test(file.path)) return cb(null, file);
file.path = file.path.replace(TOKENS_EXT, ".html"); file.path = file.path.replace(TOKENS_EXT, ".html");
file.contents = Buffer.from(`<!DOCTYPE html> file.contents = Buffer.from(`<!DOCTYPE html>
<html> <html>
<meta charset="utf-8"> <meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">${css ? <meta name="viewport" content="width=device-width,initial-scale=1">${
'\n<meta name="color-scheme" content="dark light">\n<link rel="stylesheet" href="/gmi.min.css">' css
: ''} ? '\n<meta name="color-scheme" content="dark light">\n<link rel="stylesheet" href="/gmi.min.css">'
: ""
}
<body> <body>
${toHTML(JSON.parse(file.contents.toString("utf8")))} ${toHTML(JSON.parse(file.contents.toString("utf8")))}
</body> </body>
@ -17,29 +19,29 @@ ${toHTML(JSON.parse(file.contents.toString("utf8")))}
}; };
function toHTML(tokens) { function toHTML(tokens) {
let body = [] let body = [];
let cursor = tokens.shift() let cursor = tokens.shift();
while (tokens.length) { while (tokens.length) {
if (cursor.pre) { if (cursor.pre) {
body.push(`<pre${cursor.alt ? `title="${cursor.alt}"` : ""}>`) body.push(`<pre${cursor.alt ? `title="${cursor.alt}"` : ""}>`);
const closing = tokens.findIndex(token => token.pre) const closing = tokens.findIndex((token) => token.pre);
body = body.concat(tokens.slice(0, closing).map(({text}) => text)) body = body.concat(tokens.slice(0, closing).map(({ text }) => text));
body.push("</pre>") body.push("</pre>");
tokens = tokens.slice(closing + 1) tokens = tokens.slice(closing + 1);
} }
if (cursor.li) { if (cursor.li) {
body.push(`<ul>`) body.push(`<ul>`);
const closing = tokens.findIndex(token => !token.li) const closing = tokens.findIndex((token) => !token.li);
body = body.concat(tokens.slice(0, closing).map(line)) body = body.concat(tokens.slice(0, closing).map(line));
body.push("</ul>") body.push("</ul>");
tokens = tokens.slice(closing + 1) tokens = tokens.slice(closing + 1);
} }
body.push(line(cursor)) body.push(line(cursor));
cursor = tokens.shift() cursor = tokens.shift();
} }
return body.join("\n") return body.join("\n");
} }
function line({ text, href, title, pre, alt, h1, h2, h3, li, quote }) { function line({ text, href, title, pre, alt, h1, h2, h3, li, quote }) {