1 # Script for extracting compilable fragments from markdown
2 # documentation. See prep.js for a description of the format
3 # recognized by this tool. Expects a directory fragements/ to exist
4 # under the current directory, and writes the fragments in there as
5 # individual .rs files.
10 print("Please provide an input filename")
13 filename = sys.argv[1]
24 while cur < len(lines):
27 chap = re.match("# (.*)", line);
29 chapter = re.sub(r"\W", "_", chap.group(1)).lower()
31 elif re.match("~~~", line):
32 # Parse the tags that open a code block in the pandoc format:
34 tags = re.findall("\.([\w-]*)", line)
36 ignore = "notrust" in tags or "ignore" in tags
37 # Some tags used by the language ref that indicate not rust
38 ignore |= "ebnf" in tags
39 ignore |= "abnf" in tags
40 ignore |= "keyword" in tags
41 ignore |= "field" in tags
42 ignore |= "precedence" in tags
43 xfail = "xfail-test" in tags
44 while cur < len(lines):
47 if re.match("~~~", line):
50 block += re.sub("^# ", "", line)
52 if not re.search(r"\bfn main\b", block):
53 block = "fn main() {\n" + block + "\n}\n"
54 if not re.search(r"\bextern mod std\b", block):
55 block = "extern mod std;\n" + block;
57 block = "// xfail-test\n" + block
58 filename = (dest + "/" + str(chapter)
59 + "_" + str(chapter_n) + ".rs")
61 f = open(filename, 'w')