#!/usr/bin/env python # coding=utf-8 # Copyright (C) 2013 The Android Open Source Project # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import argparse import re import sys PAT_GERRIT = re.compile(r'^GERRIT') PAT_INCLUDE = re.compile(r'^(include::.*)(\[\])$') PAT_GET = re.compile(r'^get::([^ \t\n]*)') PAT_TITLE = re.compile(r'^\.(.*)') PAT_STARS = re.compile(r'^\*\*\*\*') PAT_SEARCHBOX = re.compile(r'^SEARCHBOX') GERRIT_UPLINK = """ ++++
++++ """ GET_TITLE = '
%s
' GET_MACRO = """ ++++
%s
GET {0} HTTP/1.0
++++ """ SEARCH_BOX = """ ++++
%s
++++ """ BUILTIN_SEARCH = """ """ GOOGLE_SITE_SEARCH = """ """ LINK_SCRIPT = """ ++++ ++++ """ parser = argparse.ArgumentParser() parser.add_argument('-o', '--out', help='output file') parser.add_argument('-s', '--src', help='source file') parser.add_argument('-x', '--suffix', help='suffix for included filenames') parser.add_argument('-b', '--searchbox', action="store_true", default=True, help="generate the search boxes") parser.add_argument('--no-searchbox', action="store_false", dest='searchbox', help="don't generate the search boxes") parser.add_argument('--site-search', action="store", metavar="SITE", help=("generate the search box using google. SITE should " + "point to the domain/path of the site, eg. " + "gerrit-review.googlesource.com/Documentation")) args = parser.parse_args() if args.site_search: SEARCH_BOX = (SEARCH_BOX % GOOGLE_SITE_SEARCH.replace("@SITE@", args.site_search)) else: SEARCH_BOX = SEARCH_BOX % BUILTIN_SEARCH try: try: out_file = open(args.out, 'w', errors='ignore') src_file = open(args.src, 'r', errors='ignore') except TypeError: out_file = open(args.out, 'w') src_file = open(args.src, 'r') last_line = '' ignore_next_line = False last_title = '' for line in src_file: if PAT_GERRIT.match(last_line): # Case of "GERRIT\n------" at the footer out_file.write(GERRIT_UPLINK) last_line = '' elif PAT_SEARCHBOX.match(last_line): # Case of 'SEARCHBOX\n---------' if args.searchbox: out_file.write(SEARCH_BOX) last_line = '' elif PAT_INCLUDE.match(line): # Case of 'include::' match = PAT_INCLUDE.match(line) out_file.write(last_line) last_line = match.group(1) + args.suffix + match.group(2) + '\n' elif PAT_STARS.match(line): if PAT_TITLE.match(last_line): # Case of the title in '.\n****\nget::<url>\n****' match = PAT_TITLE.match(last_line) last_title = GET_TITLE % match.group(1) else: out_file.write(last_line) last_title = '' elif PAT_GET.match(line): # Case of '****\nget::<url>\n****' in rest api url = PAT_GET.match(line).group(1) out_file.write(GET_MACRO.format(url) % last_title) ignore_next_line = True elif ignore_next_line: # Handle the trailing '****' of the 'get::' case last_line = '' ignore_next_line = False else: out_file.write(last_line) last_line = line out_file.write(last_line) out_file.write(LINK_SCRIPT) out_file.close() except IOError as err: sys.stderr.write( "error while expanding %s to %s: %s" % (args.src, args.out, err)) exit(1)