OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 # This script converts old-style <a> links to API docs to the new $ref links. | 6 # This script converts old-style <a> links to API docs to the new $ref links. |
7 # See reference_resolver.py for more info on the format of $ref links. | 7 # See reference_resolver.py for more info on the format of $ref links. |
8 | 8 |
9 import optparse | 9 import optparse |
10 import os | 10 import os |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 return '%s$ref:%s%s' % (padding, page, link) | 53 return '%s$ref:%s%s' % (padding, page, link) |
54 else: | 54 else: |
55 return '%s$ref:[%s%s %s]' % (padding, page, link, title) | 55 return '%s$ref:[%s%s %s]' % (padding, page, link, title) |
56 | 56 |
57 def _ConvertFile(filename, use_stdout): | 57 def _ConvertFile(filename, use_stdout): |
58 regex = re.compile(r'<a(.*?)href=(.*?)>(.*?)</a>', flags=re.DOTALL) | 58 regex = re.compile(r'<a(.*?)href=(.*?)>(.*?)</a>', flags=re.DOTALL) |
59 contents = _ReadFile(filename) | 59 contents = _ReadFile(filename) |
60 contents = re.sub(regex, | 60 contents = re.sub(regex, |
61 lambda m: _Replace(m, filename), | 61 lambda m: _Replace(m, filename), |
62 contents) | 62 contents) |
63 contents = contents.replace('$ref:extension.lastError', | 63 contents = contents.replace('$ref:runtime.lastError', |
64 '$ref:runtime.lastError') | 64 '$ref:runtime.lastError') |
65 if use_stdout: | 65 if use_stdout: |
66 print contents | 66 print contents |
67 else: | 67 else: |
68 _WriteFile(filename, contents) | 68 _WriteFile(filename, contents) |
69 | 69 |
70 if __name__ == '__main__': | 70 if __name__ == '__main__': |
71 parser = optparse.OptionParser( | 71 parser = optparse.OptionParser( |
72 description='Converts <a> links to $ref links.', | 72 description='Converts <a> links to $ref links.', |
73 usage='usage: %prog [option] <directory>') | 73 usage='usage: %prog [option] <directory>') |
74 parser.add_option('-f', '--file', default='', | 74 parser.add_option('-f', '--file', default='', |
75 help='Convert links in single file.') | 75 help='Convert links in single file.') |
76 parser.add_option('-o', '--out', action='store_true', default=False, | 76 parser.add_option('-o', '--out', action='store_true', default=False, |
77 help='Write to stdout.') | 77 help='Write to stdout.') |
78 regex = re.compile(r'<a(.*?)href=(.*?)>(.*?)</a>', flags=re.DOTALL) | 78 regex = re.compile(r'<a(.*?)href=(.*?)>(.*?)</a>', flags=re.DOTALL) |
79 | 79 |
80 opts, argv = parser.parse_args() | 80 opts, argv = parser.parse_args() |
81 if opts.file: | 81 if opts.file: |
82 _ConvertFile(opts.file, opts.out) | 82 _ConvertFile(opts.file, opts.out) |
83 else: | 83 else: |
84 if len(argv) != 1: | 84 if len(argv) != 1: |
85 parser.print_usage() | 85 parser.print_usage() |
86 exit(0) | 86 exit(0) |
87 for root, dirs, files in os.walk(argv[0]): | 87 for root, dirs, files in os.walk(argv[0]): |
88 for name in files: | 88 for name in files: |
89 _ConvertFile(os.path.join(root, name), opts.out) | 89 _ConvertFile(os.path.join(root, name), opts.out) |
OLD | NEW |