Skip to content

Commit a4b7775

Browse files
committed
Move tutorial over to a format similar to the reference doc
And adjust highlighting/testing scripts to deal with this.
1 parent d699db6 commit a4b7775

28 files changed

+2719
-4015
lines changed

doc/extract-tests.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/local/bin/node
2+
3+
/***
4+
* Script for extracting compilable fragments from markdown
5+
* documentation. See prep.js for a description of the format
6+
* recognized by this tool. Expects a directory fragements/ to exist
7+
* under the current directory, and writes the fragments in there as
8+
* individual .rs files.
9+
*/
10+
11+
var fs = require("fs");
12+
13+
if (!process.argv[2]) {
14+
console.log("Please provide an input file name.");
15+
process.exit(1);
16+
}
17+
18+
var lines = fs.readFileSync(process.argv[2]).toString().split(/\n\r?/g);
19+
var cur = 0, line, chapter, chapter_n;
20+
21+
while ((line = lines[cur++]) != null) {
22+
var chap = line.match(/^# (.*)/);
23+
if (chap) {
24+
chapter = chap[1].toLowerCase().replace(/\W/g, "_");
25+
chapter_n = 1;
26+
} else if (/^~~~/.test(line)) {
27+
var block = "", ignore = false;
28+
while ((line = lines[cur++]) != null) {
29+
if (/^\s*## (?:notrust|ignore)/.test(line)) ignore = true;
30+
else if (/^~~~/.test(line)) break;
31+
else block += line.replace(/^# /, "") + "\n";
32+
}
33+
if (!ignore) {
34+
if (!/\bfn main\b/.test(block)) {
35+
if (/(^|\n) *(native|use|mod|import|export)\b/.test(block))
36+
block += "\nfn main() {}\n";
37+
else block = "fn main() {\n" + block + "\n}\n";
38+
}
39+
if (!/\buse std\b/.test(block)) block = "use std;\n" + block;
40+
var filename = "fragments/" + chapter + "_" + (chapter_n++) + ".rs";
41+
fs.writeFileSync(filename, block);
42+
}
43+
}
44+
}
File renamed without changes.
File renamed without changes.

doc/prep.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/local/bin/node
2+
3+
/***
4+
* Pandoc-style markdown preprocessor that drops extra directives
5+
* included for running doc code, and that optionally, when
6+
* --highlight is provided, replaces code blocks that are Rust code
7+
* with highlighted HTML blocks. The directives recognized are:
8+
*
9+
* '## ignore' tells the test extractor (extract-tests.js) to ignore
10+
* the block completely.
11+
* '## notrust' makes the test extractor ignore the block, makes
12+
* this script not highlight the block.
13+
* '# [any text]' is a line that is stripped out by this script, and
14+
* converted to a normal line of code (without the leading #) by
15+
* the test extractor.
16+
*/
17+
18+
var fs = require("fs");
19+
CodeMirror = require("./lib/codemirror-node");
20+
require("./lib/codemirror-rust");
21+
22+
function help() {
23+
console.log("usage: " + process.argv[0] + " [--highlight] [-o outfile] [infile]");
24+
process.exit(1);
25+
}
26+
27+
var highlight = false, infile, outfile;
28+
29+
for (var i = 2; i < process.argv.length; ++i) {
30+
var arg = process.argv[i];
31+
if (arg == "--highlight") highlight = true;
32+
else if (arg == "-o" && outfile == null && ++i < process.argv.length) outfile = process.argv[i];
33+
else if (arg[0] != "-") infile = arg;
34+
else help();
35+
}
36+
37+
var lines = fs.readFileSync(infile || "/dev/stdin").toString().split(/\n\r?/g), cur = 0, line;
38+
var out = outfile ? fs.createWriteStream(outfile) : process.stdout;
39+
40+
while ((line = lines[cur++]) != null) {
41+
if (/^~~~/.test(line)) {
42+
var block = "", bline, isRust = true;
43+
while ((bline = lines[cur++]) != null) {
44+
if (/^\s*## notrust/.test(bline)) isRust = false;
45+
else if (/^~~~/.test(bline)) break;
46+
if (!/^\s*##? /.test(bline)) block += bline + "\n";
47+
}
48+
if (!highlight || !isRust)
49+
out.write(line + "\n" + block + bline + "\n");
50+
else {
51+
var html = '<pre class="cm-s-default">', curstr = "", curstyle = null;
52+
function add(str, style) {
53+
if (style != curstyle) {
54+
if (curstyle) html += '<span class="cm-' + curstyle + '">' + curstr
55+
+ "</span>";
56+
else if (curstr) html += curstr;
57+
curstr = str; curstyle = style;
58+
} else curstr += str;
59+
}
60+
CodeMirror.runMode(block, "rust", add);
61+
add("", "bogus"); // Flush pending string.
62+
out.write(html + "</pre>\n");
63+
}
64+
} else {
65+
out.write(line + "\n");
66+
}
67+
}

doc/tutorial/test.sh renamed to doc/run-tests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash
22
rm -f fragments/*.rs
33
mkdir -p fragments
4-
node extract.js
4+
node extract-tests.js $1
55
for F in `ls fragments/*.rs`; do
66
$RUSTC $F > /dev/null
77
if [[ $? != 0 ]] ; then echo $F; fi

doc/rust.css

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@ body {
66

77
body {
88
padding: 1em 6em;
9-
max-width: 50em;
9+
max-width: 60em;
1010
}
1111

12-
h1 { font-size: 22pt; }
12+
h1 {
13+
font-size: 22pt;
14+
margin-top: 2em;
15+
border-bottom: 2px solid silver;
16+
}
1317
h2 { font-size: 17pt; }
1418
h3 { font-size: 14pt; }
1519

@@ -23,3 +27,27 @@ a, a:visited, a:link {
2327
text-decoration: none;
2428
color: #00438a;
2529
}
30+
31+
h1 a:link, h1 a:visited, h2 a:link, h2 a:visited,
32+
h3 a:link, h3 a:visited { color: black; }
33+
34+
/* Code highlighting */
35+
.cm-s-default span.cm-keyword {color: #708;}
36+
.cm-s-default span.cm-atom {color: #219;}
37+
.cm-s-default span.cm-number {color: #164;}
38+
.cm-s-default span.cm-def {color: #00f;}
39+
.cm-s-default span.cm-variable {color: black;}
40+
.cm-s-default span.cm-variable-2 {color: #05a;}
41+
.cm-s-default span.cm-variable-3 {color: #085;}
42+
.cm-s-default span.cm-property {color: black;}
43+
.cm-s-default span.cm-operator {color: black;}
44+
.cm-s-default span.cm-comment {color: #a50;}
45+
.cm-s-default span.cm-string {color: #a11;}
46+
.cm-s-default span.cm-string-2 {color: #f50;}
47+
.cm-s-default span.cm-meta {color: #555;}
48+
.cm-s-default span.cm-error {color: #f00;}
49+
.cm-s-default span.cm-qualifier {color: #555;}
50+
.cm-s-default span.cm-builtin {color: #30a;}
51+
.cm-s-default span.cm-bracket {color: #cc7;}
52+
.cm-s-default span.cm-tag {color: #170;}
53+
.cm-s-default span.cm-attribute {color: #00c;}

0 commit comments

Comments
 (0)