about summary refs log tree commit diff
path: root/doc/tutorial/extract.js
blob: e84619675455e396cfbe5b77d26ca4076165c01f (plain)
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
var fs = require("fs"), md = require("./lib/markdown");

// Runs markdown.js over the tutorial, to find the code blocks in it.
// Uses the #-markers in those code blocks, along with some vague
// heuristics, to turn them into compilable files. Outputs these files
// to fragments/.
//
// '##ignore' means don't test this block
// '##notrust' means the block isn't rust code
//     (used by build.js to not highlight it)
// '# code' means insert the given code to complete the fragment
//     (build.js strips out such lines)

var curFile, curFrag;
md.Markdown.dialects.Maruku.block.code = function code(block, next) {
  if (block.match(/^    /)) {
    var ignore, text = String(block);
    while (next.length && next[0].match(/^    /)) text += "\n" + String(next.shift());
    text = text.split("\n").map(function(line) {
      line = line.slice(4);
      if (line == "## ignore" || line == "## notrust") { ignore = true; line = ""; }
      if (/^# /.test(line)) line = line.slice(2);
      return line;
    }).join("\n");
    if (ignore) return;
    if (!/\bfn main\b/.test(text)) {
      if (/(^|\n) *(native|use|mod|import|export)\b/.test(text))
        text += "\nfn main() {}\n";
      else text = "fn main() {\n" + text + "\n}\n";
    }
    if (!/\buse std\b/.test(text)) text = "use std;\n" + text;
    fs.writeFileSync("fragments/" + curFile + "_" + (++curFrag) + ".rs", text);
  }
};

fs.readFileSync("order", "utf8").split("\n").filter(id).forEach(handle);

function id(x) { return x; }
function handle(file) {
  curFile = file; curFrag = 0;
  md.parse(fs.readFileSync(file + ".md", "utf8"), "Maruku");
}