#!/usr/bin/env node // Imports var fs = require('fs'); var marked = require('marked'); var highlight = require('highlight.js'); var katex = require('katex'); var yargs = require('yargs'); var extend = require('util')._extend; // Constants var template = ' %TITLE%
%BODY%
' // Understanding var argv = yargs .usage("Usage: $0 [options]") .example('$0 -i doc.md -o doc.html', 'Convert doc.md to HTML using the default template') .help('h') .alias('h', 'help') .describe('i', 'Input file') .alias('i', 'input') .default('i', '/dev/stdin') .describe('o', 'Output file') .alias('o', 'output') .default('o', '/dev/stdout') .describe('t', 'Template file (%BODY% is replaced by the text)') .alias('t', 'template') .argv; if (argv.t) { template = fs.readFileSync(argv.t, "utf8"); } var latex = true; // TODO Arg // Settings var extraLangages = { avrpseudo: function(hljs) { lang = extend({}, highlight.getLanguage('avrasm')); lang.keywords.keyword += ' Si Alors Sinon FinSi TantQue FinTantQue Pour FinPour allant de à ←'; lang.keywords.keyword += ' Lire Sortir sur Appeler Retourner'; lang.keywords.keyword += ' DecalerDroite DecalerGauche'; lang.keywords.keyword += ' Incrementer Decrementer'; lang.keywords.built_in += ' vrai faux'; lang.contains.push({ className: 'meta', begin: /Configurer.+/, end: /\n/, }); return lang; }, avrasmplus: function(hljs) { lang = extend({}, highlight.getLanguage('avrasm')); lang.keywords.keyword += ' si saut alors et ou if then goto && || <-'; lang.contains.push({ className: 'meta', begin: /@\w+/, }); return lang; }, }; for (lang in extraLangages) { // This must be done before any call to highlight.highlight :/ highlight.registerLanguage(lang, extraLangages[lang]); } var renderer = new marked.Renderer(); marked.setOptions({ highlight: function(code, lang) { if (lang == 'raw') { return code; } else if (highlight.getLanguage(lang)) { return highlight.highlight(lang, code).value; } else { // if (extraLangages[lang]) { // highlight.registerLanguage(lang, extraLangages[lang]); // return highlight.highlight(lang, code).value; // } else { // } console.warn("Unknown language: " + lang); return highlight.highlightAuto(code).value; } } }); // Processing markdownString = fs.readFileSync(argv.i, "utf8"); // TeX if (latex) { markdownString = markdownString.replace(/\\\$/g, '$') markdownString = markdownString.replace(/\$\$([\s\S]+)\$\$/gm, function(glob, formula) { return katex.renderToString(formula, { displayMode: true }); }); markdownString = markdownString.replace(/\$([^$]+)\$/g, function(glob, formula) { return katex.renderToString(formula, { displayMode: false }); }); } // Conversion htmlString = marked(markdownString, { renderer: renderer, breaks: false }); // fullHtmlString = htmlString; fullHtmlString = template.replace('%BODY%', () => { return htmlString }); // Saving if (argv.o == '/dev/stdout') { console.log(fullHtmlString); } else { fs.writeFileSync(argv.o, fullHtmlString); }