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
26 DOC_PATTERN = '^(?P<indent>[\\t ]*)#\\[(\\s*)doc(\\s*)=' + \
27 '(\\s*)"(?P<text>(\\"|[^"])*?)"(\\s*)\\]' + \
30 ESCAPES = [("\\'", "'"),
38 for (find, repl) in ESCAPES:
39 s = s.replace(find, repl)
46 # remove leading/trailing whitespace-lines
47 while lns and not lns[0].strip():
49 while lns and not lns[-1].strip():
52 # remove leading horizontal whitespace
56 n = min(n, len(re.search('^\s*', ln).group()))
58 lns = [ln[n:] for ln in lns]
60 # strip trailing whitespace
61 lns = [ln.rstrip() for ln in lns]
67 indent = m.group('indent')
68 text = block_trim(unescape(m.group('text')))
71 inner = '!' if m.group('semi') else '*'
72 starify = lambda s: indent + ' *' + (' ' + s if s else '')
73 text = '\n'.join(map(starify, text))
74 repl = indent + '/*' + inner + '\n' + text + '\n' + indent + ' */'
76 inner = '!' if m.group('semi') else '/'
77 repl = indent + '//' + inner + ' ' + text[0]
82 def sugarise_file(path):
85 r = re.compile(DOC_PATTERN, re.MULTILINE | re.DOTALL)
86 ns = re.sub(r, replace_doc, s)
89 open(path, 'w').write(ns)
91 for (dirpath, dirnames, filenames) in os.walk('.'):
92 for name in fnmatch.filter(filenames, '*.r[sc]'):
93 sugarise_file(os.path.join(dirpath, name))