3 # Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
4 # file at the top-level directory of this distribution and at
5 # http://rust-lang.org/COPYRIGHT.
7 # Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
8 # http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
9 # <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
10 # option. This file may not be copied, modified, or distributed
11 # except according to those terms.
14 # this script attempts to turn doc comment attributes (#[doc = "..."])
15 # into sugared-doc-comments (/** ... */ and /// ...)
17 # it sugarises all .rs/.rc files underneath the working directory
20 import sys, os, fnmatch, re
23 DOC_PATTERN = '^(?P<indent>[\\t ]*)#\\[(\\s*)doc(\\s*)=' + \
24 '(\\s*)"(?P<text>(\\"|[^"])*?)"(\\s*)\\]' + \
27 ESCAPES = [("\\'", "'"),
35 for (find, repl) in ESCAPES:
36 s = s.replace(find, repl)
43 # remove leading/trailing whitespace-lines
44 while lns and not lns[0].strip():
46 while lns and not lns[-1].strip():
49 # remove leading horizontal whitespace
53 n = min(n, len(re.search('^\s*', ln).group()))
55 lns = [ln[n:] for ln in lns]
57 # strip trailing whitespace
58 lns = [ln.rstrip() for ln in lns]
64 indent = m.group('indent')
65 text = block_trim(unescape(m.group('text')))
68 inner = '!' if m.group('semi') else '*'
69 starify = lambda s: indent + ' *' + (' ' + s if s else '')
70 text = '\n'.join(map(starify, text))
71 repl = indent + '/*' + inner + '\n' + text + '\n' + indent + ' */'
73 inner = '!' if m.group('semi') else '/'
74 repl = indent + '//' + inner + ' ' + text[0]
79 def sugarise_file(path):
82 r = re.compile(DOC_PATTERN, re.MULTILINE | re.DOTALL)
83 ns = re.sub(r, replace_doc, s)
86 open(path, 'w').write(ns)
89 for (dirpath, dirnames, filenames) in os.walk('.'):
90 for name in fnmatch.filter(filenames, '*.r[sc]'):
91 sugarise_file(os.path.join(dirpath, name))