Blame view

md2html 3.65 KB
73f2ae9d   Geoffrey PREUD'HOMME   Outils de rédaction
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
  #!/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 = '<!DOCTYPE html> <html lang="fr"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta charset="UTF-8"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.11.0/styles/xcode.min.css" integrity="sha256-OED7Gmqde0cMVVeo1zVd+3fBD4EST32D4h9YT7KY0aY=" crossorigin="anonymous" /> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.7.1/katex.min.css" integrity="sha384-wITovz90syo1dJWVh32uuETPVEtGigN07tkttEqPv+uR2SE/mbQcG7ATL28aI9H0" crossorigin="anonymous"> <style type="text/css"> image { max-width: 100 % } </style> <title>%TITLE%</title> </head> <body> <main class="page-content" aria-label="Content"> %BODY% </main> </body> </html>'
  
  
  // 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");
  }
  
  
  // 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.built_in += ' vrai faux';
          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 (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
  markdownString = markdownString.replace(/\\\$/g, '&dollar;')
  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});
  fullHtmlString = template.replace('%BODY%', htmlString);
  
  // Saving
  if (argv.o == '/dev/stdout') {
      console.log(fullHtmlString);
  } else {
      fs.writeFileSync(argv.o, fullHtmlString);
  }