2024-04-16 09:41:10 -04:00
|
|
|
// Copyright (c) 2024 yellows111
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the "Software"),
|
|
|
|
// to deal in the Software without restriction, including without limitation
|
|
|
|
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
// and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
// Software is furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
// DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2024-04-18 09:14:07 -04:00
|
|
|
// local
|
|
|
|
const Package = require("./package.json");
|
|
|
|
|
2024-04-16 09:41:10 -04:00
|
|
|
// npm
|
|
|
|
const Remarkable = require("remarkable").Remarkable; // remove .Remarkable if you're using Remarkable ^1.0.0!
|
|
|
|
const TOCGenerator = require("markdown-toc");
|
|
|
|
const RemarkableHeaderIDs = require("remarkable-header-ids");
|
2024-04-17 07:39:36 -04:00
|
|
|
const HTMLFormatter = require("@liquify/prettify").formatSync;
|
2024-04-16 09:41:10 -04:00
|
|
|
|
|
|
|
// natives
|
|
|
|
const Process = require("process");
|
|
|
|
const Filesystem = require("fs");
|
|
|
|
const Path = require("path");
|
|
|
|
|
2024-05-09 07:14:50 -04:00
|
|
|
if(Process.argv.length < 4 && require.main === module) {
|
2024-04-16 09:41:10 -04:00
|
|
|
console.error("You need a source.md and target.htm path!");
|
|
|
|
Process.exit(1);
|
|
|
|
}
|
|
|
|
|
2024-05-09 07:14:50 -04:00
|
|
|
function markup(input, options) {
|
2024-10-26 06:21:44 -04:00
|
|
|
var usedSlugs = {};
|
2024-04-16 09:41:10 -04:00
|
|
|
let tableOfContents = new Remarkable().use(TOCGenerator.plugin(options)).render(input);
|
2024-10-26 06:21:44 -04:00
|
|
|
let output = new Remarkable().use(
|
|
|
|
RemarkableHeaderIDs({
|
|
|
|
levels: [1,2,3,4,5,6],
|
|
|
|
anchorClassName: "header-anchor",
|
|
|
|
anchorText: "#",
|
|
|
|
headerId: function(slug) {
|
|
|
|
if(usedSlugs[slug] > 0) {
|
|
|
|
const out_ = `${slug}-${usedSlugs[slug]}`;
|
|
|
|
usedSlugs[slug] = usedSlugs[slug] + 1;
|
|
|
|
return out_;
|
|
|
|
} else {
|
|
|
|
if(!usedSlugs.hasOwnProperty(slug)) {
|
|
|
|
usedSlugs[slug] = 0;
|
|
|
|
}
|
|
|
|
usedSlugs[slug] = usedSlugs[slug] + 1;
|
|
|
|
return slug;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
).render("# Table of contents:\n"+tableOfContents.content+"\n\n"+input);
|
2024-04-16 09:41:10 -04:00
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
function format(data, name) {
|
2024-04-16 17:50:07 -04:00
|
|
|
var wikiName = "A new yiki!";
|
2024-04-17 07:39:36 -04:00
|
|
|
var rootPrefix = "/";
|
2024-04-16 17:50:07 -04:00
|
|
|
if(process.env["WIKINAME"]) {
|
|
|
|
wikiName = process.env["WIKINAME"];
|
|
|
|
}
|
2024-04-17 07:39:36 -04:00
|
|
|
if(process.env["VPREFIX"]) {
|
|
|
|
rootPrefix = process.env["VPREFIX"];
|
|
|
|
}
|
|
|
|
// forceIndent screws with text nodes pretty bad but the alternative is to not allow the full document to be formatted
|
|
|
|
const formatterRules = {"indentChar": "\t", "indentSize": 1, "markup": {"forceAttribute": false, "forceIndent": true}};
|
2024-04-18 06:02:01 -04:00
|
|
|
function render() {
|
|
|
|
return HTMLFormatter(data, formatterRules)
|
|
|
|
.split("\n") /* split by every newline */
|
|
|
|
.join("\n\t\t\t") /* then add padding and convert back into string */
|
2024-04-18 09:14:07 -04:00
|
|
|
.replace( /* multitag text node fix (should really just encase all text with <span>, but whatever) */
|
2024-10-13 09:53:18 -04:00
|
|
|
/(\t*)(<(?:code|a href=".+?")>)\n\t*(.*?)\n\t*(<\/(?:code|a)>)/g,
|
2024-04-18 09:14:07 -04:00
|
|
|
"$1$2$3$4"
|
|
|
|
)
|
2024-10-13 09:53:18 -04:00
|
|
|
.replace( /* multitag prefix fix for (, [ and - */
|
|
|
|
/([(\[\-])\n\t*(<(?:code|a href=".+?")>.*<\/(?:code|a)>)/g,
|
2024-04-18 09:14:07 -04:00
|
|
|
"$1$2"
|
|
|
|
)
|
|
|
|
.replace( /* multitag suffix fix for special characters. */
|
2024-10-26 06:21:44 -04:00
|
|
|
/(<\/(?:a|code)>)\n\t*([.)\];:,?!+\-'])/g,
|
2024-04-18 09:14:07 -04:00
|
|
|
"$1$2"
|
2024-05-13 06:40:42 -04:00
|
|
|
)
|
|
|
|
.replace( /* fix tag attached to word and not whitespace */
|
2024-04-22 08:26:06 -04:00
|
|
|
/(\w+)(<[a-zA-Z]\w*>)/g,
|
|
|
|
"$1 $2"
|
2024-10-10 15:25:34 -04:00
|
|
|
)
|
|
|
|
.replace( /* replace .md links to html ones */
|
2024-10-13 09:53:18 -04:00
|
|
|
/href="(.+?)\.md"/g,
|
2024-10-10 15:25:34 -04:00
|
|
|
"href=\"$1.html\""
|
|
|
|
)
|
|
|
|
.replace( /* evil codeblock fix */
|
|
|
|
/<pre><code>((.|\n)*?)<\/code><\/pre>/g,
|
|
|
|
function(match, inline) {
|
|
|
|
return "<pre><code>" + inline.replace(/\t/g, "") + "</code></pre>";
|
|
|
|
}
|
2024-10-11 12:56:31 -04:00
|
|
|
)
|
|
|
|
.replace( /* fix monospace'd links */
|
|
|
|
/<a href="(.+?)">\n\t+<code>(.*?)<\/code>\n\t+<\/a>/g,
|
|
|
|
"<a href=\"$1\"><code>$2</code></a>"
|
2024-04-18 06:02:01 -04:00
|
|
|
);
|
|
|
|
}
|
2024-04-16 09:41:10 -04:00
|
|
|
// probably should bring in a better templating engine but whatever
|
2024-04-17 07:39:36 -04:00
|
|
|
return ( /* eslint-disable indent */
|
|
|
|
`<!doctype html>
|
|
|
|
<html lang="en">
|
|
|
|
<head>
|
|
|
|
<meta charset="UTF-8">
|
|
|
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
2024-04-22 08:26:06 -04:00
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=2.0">
|
|
|
|
<meta name="description" content="${name} on ${wikiName}">
|
2024-04-17 07:39:36 -04:00
|
|
|
<title>${name} - Documentation</title>
|
|
|
|
<link rel="stylesheet" type="text/css" href="${rootPrefix}yiki.css">
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<nav>
|
|
|
|
<span>${wikiName}</span>
|
|
|
|
<span> | </span>
|
|
|
|
<a href="${rootPrefix}index.html">home</a>
|
|
|
|
</nav>
|
|
|
|
<hr>
|
|
|
|
<main>
|
2024-04-18 06:02:01 -04:00
|
|
|
${render()}
|
2024-04-17 07:39:36 -04:00
|
|
|
</main>
|
2024-04-18 09:14:07 -04:00
|
|
|
<hr>
|
|
|
|
<footer>
|
|
|
|
<span>page rendered by ${Package.name} ${Package.version} on ${new Date().toGMTString()}</span>
|
|
|
|
</footer>
|
2024-04-17 07:39:36 -04:00
|
|
|
</body>
|
|
|
|
</html>`
|
|
|
|
); /* eslint-enable indent */
|
2024-04-16 09:41:10 -04:00
|
|
|
}
|
|
|
|
|
2024-05-09 07:14:50 -04:00
|
|
|
module.exports = {"markup": markup, "format": format};
|
|
|
|
|
|
|
|
if(require.main === module) {
|
|
|
|
let content = markup(Filesystem.readFileSync(Process.argv[2]).toString());
|
|
|
|
Filesystem.writeFileSync(Process.argv[3], format(content, Path.basename(Process.argv[3], ".html").replace(/_/g, " ")));
|
2024-05-13 06:40:42 -04:00
|
|
|
}
|