Yuxuan 'fishy' Wang | e6cbae9 | 2013-09-03 18:26:54 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # Copyright (C) 2013 The Android Open Source Project |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | from optparse import OptionParser |
Yuxuan 'fishy' Wang | e6cbae9 | 2013-09-03 18:26:54 -0700 | [diff] [blame] | 17 | import re |
| 18 | import sys |
| 19 | |
David Pursehouse | 6c25ea8 | 2013-09-26 16:48:27 +0900 | [diff] [blame^] | 20 | PAT_GERRIT = re.compile(r'^GERRIT') |
| 21 | PAT_INCLUDE = re.compile(r'^(include::.*)(\[\])$') |
| 22 | PAT_GET = re.compile(r'^get::([^ \t\n]*)') |
Yuxuan 'fishy' Wang | e6cbae9 | 2013-09-03 18:26:54 -0700 | [diff] [blame] | 23 | |
| 24 | GERRIT_UPLINK = """ |
| 25 | |
| 26 | ++++ |
| 27 | <hr style=\" |
| 28 | height: 2px; |
| 29 | color: silver; |
| 30 | margin-top: 1.2em; |
| 31 | margin-bottom: 0.5em; |
| 32 | \"> |
| 33 | ++++ |
| 34 | """ |
| 35 | |
| 36 | GET_MACRO = """ |
| 37 | |
| 38 | ++++ |
| 39 | <a id=\"{0}\" onmousedown="javascript: |
| 40 | var i = document.URL.lastIndexOf(\'/Documentation/\'); |
| 41 | var url = document.URL.substring(0, i) + \'{0}\'; |
| 42 | document.getElementById(\'{0}\').href = url;"> |
| 43 | GET {0} HTTP/1.0 |
| 44 | </a> |
| 45 | ++++ |
| 46 | """ |
| 47 | |
| 48 | opts = OptionParser() |
| 49 | opts.add_option('-o', '--out', help='output file') |
| 50 | opts.add_option('-s', '--src', help='source file') |
| 51 | opts.add_option('-x', '--suffix', help='suffix for included filenames') |
| 52 | options, _ = opts.parse_args() |
| 53 | |
| 54 | try: |
| 55 | out_file = open(options.out, 'w') |
| 56 | src_file = open(options.src, 'r') |
| 57 | last_line = '' |
| 58 | ignore_next_line = False |
| 59 | for line in src_file.xreadlines(): |
| 60 | if PAT_GERRIT.match(last_line): |
| 61 | # Case of "GERRIT\n------" at the footer |
| 62 | out_file.write(GERRIT_UPLINK) |
| 63 | last_line = '' |
| 64 | elif PAT_INCLUDE.match(line): |
| 65 | # Case of 'include::<filename>' |
| 66 | match = PAT_INCLUDE.match(line) |
| 67 | out_file.write(last_line) |
| 68 | last_line = match.group(1) + options.suffix + match.group(2) + '\n' |
| 69 | elif PAT_GET.match(line): |
| 70 | # Case of '****\nget::<url>\n****' in rest api |
| 71 | url = PAT_GET.match(line).group(1) |
| 72 | out_file.write(GET_MACRO.format(url)) |
| 73 | ignore_next_line = True |
| 74 | elif ignore_next_line: |
| 75 | # Handle the trailing '****' of the 'get::' case |
| 76 | last_line = '' |
| 77 | ignore_next_line = False |
| 78 | else: |
| 79 | out_file.write(last_line) |
| 80 | last_line = line |
| 81 | out_file.write(last_line) |
| 82 | out_file.close() |
| 83 | except IOError as err: |
| 84 | sys.stderr.write( |
| 85 | "error while expanding %s to %s: %s" % (options.src, options.out, err)) |
| 86 | exit(1) |