diff options
| author | Kang Seonghoon <public+git@mearie.org> | 2015-01-18 03:23:52 +0900 |
|---|---|---|
| committer | Kang Seonghoon <public+git@mearie.org> | 2015-01-18 03:23:52 +0900 |
| commit | 39e19ccdb6c89703a9c88eb88a26f61ec10d31f3 (patch) | |
| tree | a63f74b0a2b07262c538db4dceefc72f797540ad /src/etc/htmldocck.py | |
| parent | de6f520192d76151bf99f2250afac75da3f040d5 (diff) | |
| download | rust-39e19ccdb6c89703a9c88eb88a26f61ec10d31f3.tar.gz rust-39e19ccdb6c89703a9c88eb88a26f61ec10d31f3.zip | |
tests: Tidy and allows multi-line htmldocck commands.
Diffstat (limited to 'src/etc/htmldocck.py')
| -rw-r--r-- | src/etc/htmldocck.py | 38 |
1 files changed, 35 insertions, 3 deletions
diff --git a/src/etc/htmldocck.py b/src/etc/htmldocck.py index 9693129db3b..25734217589 100644 --- a/src/etc/htmldocck.py +++ b/src/etc/htmldocck.py @@ -148,11 +148,43 @@ class CustomHTMLParser(HTMLParser): Command = namedtuple('Command', 'negated cmd args lineno') -LINE_PATTERN = re.compile(r'(?<=(?<!\S)@)(?P<negated>!?)(?P<cmd>[A-Za-z]+(?:-[A-Za-z]+)*)(?P<args>.*)$') +# returns a generator out of the file object, which +# - removes `\\` then `\n` then a shared prefix with the previous line then optional whitespace; +# - keeps a line number (starting from 0) of the first line being concatenated. +def concat_multi_lines(f): + lastline = None # set to the last line when the last line has a backslash + firstlineno = None + catenated = '' + for lineno, line in enumerate(f): + line = line.rstrip('\r\n') + + # strip the common prefix from the current line if needed + if lastline is not None: + maxprefix = 0 + for i in xrange(min(len(line), len(lastline))): + if line[i] != lastline[i]: break + maxprefix += 1 + line = line[maxprefix:].lstrip() + + firstlineno = firstlineno or lineno + if line.endswith('\\'): + lastline = line[:-1] + catenated += line[:-1] + else: + yield firstlineno, catenated + line + lastline = None + firstlineno = None + catenated = '' + +LINE_PATTERN = re.compile(r''' + (?<=(?<!\S)@)(?P<negated>!?) + (?P<cmd>[A-Za-z]+(?:-[A-Za-z]+)*) + (?P<args>.*)$ +''', re.X) def get_commands(template): with open(template, 'rUb') as f: - for lineno, line in enumerate(f): - m = LINE_PATTERN.search(line.rstrip('\r\n')) + for lineno, line in concat_multi_lines(f): + m = LINE_PATTERN.search(line) if not m: continue negated = (m.group('negated') == '!') |
