2013-01-18 01:28:42 -06:00
|
|
|
# xfail-license
|
|
|
|
|
2012-01-20 19:32:36 -06:00
|
|
|
# Script for extracting compilable fragments from markdown
|
|
|
|
# documentation. See prep.js for a description of the format
|
2013-01-21 00:52:42 -06:00
|
|
|
# recognized by this tool. Expects a directory fragments/ to exist
|
2012-01-20 19:32:36 -06:00
|
|
|
# under the current directory, and writes the fragments in there as
|
|
|
|
# individual .rs files.
|
|
|
|
|
2013-01-21 00:52:42 -06:00
|
|
|
import sys, re
|
2012-01-20 19:32:36 -06:00
|
|
|
|
2012-01-20 20:05:07 -06:00
|
|
|
if len(sys.argv) < 3:
|
2012-01-20 19:32:36 -06:00
|
|
|
print("Please provide an input filename")
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
filename = sys.argv[1]
|
2012-01-20 20:05:07 -06:00
|
|
|
dest = sys.argv[2]
|
2012-01-20 19:32:36 -06:00
|
|
|
f = open(filename)
|
|
|
|
lines = f.readlines()
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
cur = 0
|
|
|
|
line = ""
|
|
|
|
chapter = ""
|
|
|
|
chapter_n = 0
|
|
|
|
|
|
|
|
while cur < len(lines):
|
|
|
|
line = lines[cur]
|
|
|
|
cur += 1
|
2013-01-21 00:52:42 -06:00
|
|
|
chap = re.match("# (.*)", line)
|
2012-01-20 19:32:36 -06:00
|
|
|
if chap:
|
|
|
|
chapter = re.sub(r"\W", "_", chap.group(1)).lower()
|
|
|
|
chapter_n = 1
|
|
|
|
elif re.match("~~~", line):
|
2012-03-20 18:01:32 -05:00
|
|
|
# Parse the tags that open a code block in the pandoc format:
|
|
|
|
# ~~~ {.tag1 .tag2}
|
|
|
|
tags = re.findall("\.([\w-]*)", line)
|
2012-01-20 19:32:36 -06:00
|
|
|
block = ""
|
2012-03-20 18:01:32 -05:00
|
|
|
ignore = "notrust" in tags or "ignore" in tags
|
2012-03-20 18:49:12 -05:00
|
|
|
# Some tags used by the language ref that indicate not rust
|
|
|
|
ignore |= "ebnf" in tags
|
|
|
|
ignore |= "abnf" in tags
|
|
|
|
ignore |= "keyword" in tags
|
|
|
|
ignore |= "field" in tags
|
|
|
|
ignore |= "precedence" in tags
|
2012-03-20 18:01:32 -05:00
|
|
|
xfail = "xfail-test" in tags
|
2012-01-20 19:32:36 -06:00
|
|
|
while cur < len(lines):
|
|
|
|
line = lines[cur]
|
|
|
|
cur += 1
|
2012-03-20 18:01:32 -05:00
|
|
|
if re.match("~~~", line):
|
2012-01-20 19:32:36 -06:00
|
|
|
break
|
|
|
|
else:
|
2012-09-15 20:06:20 -05:00
|
|
|
# Lines beginning with '# ' are turned into valid code
|
|
|
|
line = re.sub("^# ", "", line)
|
2013-01-21 00:52:42 -06:00
|
|
|
# Allow ellipses in code snippets
|
2012-09-18 13:54:22 -05:00
|
|
|
line = re.sub("\.\.\.", "", line)
|
2012-09-15 20:06:20 -05:00
|
|
|
block += line
|
2012-01-20 19:32:36 -06:00
|
|
|
if not ignore:
|
|
|
|
if not re.search(r"\bfn main\b", block):
|
2012-07-02 18:27:53 -05:00
|
|
|
block = "fn main() {\n" + block + "\n}\n"
|
2013-05-23 15:06:29 -05:00
|
|
|
if not re.search(r"\bextern mod extra\b", block):
|
|
|
|
block = "extern mod extra;\n" + block
|
2013-07-26 20:04:20 -05:00
|
|
|
block = """#[ deny(warnings) ];
|
|
|
|
#[ allow(unused_variable) ];\n
|
|
|
|
#[ allow(dead_assignment) ];\n
|
|
|
|
#[ allow(unused_mut) ];\n
|
2013-01-21 00:52:42 -06:00
|
|
|
""" + block
|
2012-01-20 20:05:07 -06:00
|
|
|
if xfail:
|
|
|
|
block = "// xfail-test\n" + block
|
|
|
|
filename = (dest + "/" + str(chapter)
|
|
|
|
+ "_" + str(chapter_n) + ".rs")
|
2012-01-20 19:32:36 -06:00
|
|
|
chapter_n += 1
|
|
|
|
f = open(filename, 'w')
|
|
|
|
f.write(block)
|
|
|
|
f.close()
|