blob: 6f906970dd4bfdda3928cc7d8b79f2436481f437 [file] [log] [blame]
Philipp Wollermanncb4b1e32018-03-23 10:24:37 +01001#!/usr/bin/env python
Dave Borowitzbface272016-05-19 16:20:00 -04002# coding=utf-8
Yuxuan 'fishy' Wange6cbae92013-09-03 18:26:54 -07003# Copyright (C) 2013 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17from optparse import OptionParser
Yuxuan 'fishy' Wange6cbae92013-09-03 18:26:54 -070018import re
19import sys
20
David Pursehouse6c25ea82013-09-26 16:48:27 +090021PAT_GERRIT = re.compile(r'^GERRIT')
22PAT_INCLUDE = re.compile(r'^(include::.*)(\[\])$')
23PAT_GET = re.compile(r'^get::([^ \t\n]*)')
Yuxuan 'fishy' Wang71acd112013-10-02 12:37:53 -070024PAT_TITLE = re.compile(r'^\.(.*)')
25PAT_STARS = re.compile(r'^\*\*\*\*')
Yuxuan 'fishy' Wang99cb68d2013-10-31 17:26:00 -070026PAT_SEARCHBOX = re.compile(r'^SEARCHBOX')
Yuxuan 'fishy' Wange6cbae92013-09-03 18:26:54 -070027
28GERRIT_UPLINK = """
29
30++++
31<hr style=\"
32 height: 2px;
33 color: silver;
34 margin-top: 1.2em;
35 margin-bottom: 0.5em;
36\">
37++++
Yuxuan 'fishy' Wang71acd112013-10-02 12:37:53 -070038
Yuxuan 'fishy' Wange6cbae92013-09-03 18:26:54 -070039"""
40
Yuxuan 'fishy' Wang71acd112013-10-02 12:37:53 -070041GET_TITLE = '<div class="title">%s</div>'
42
Yuxuan 'fishy' Wange6cbae92013-09-03 18:26:54 -070043GET_MACRO = """
44
45++++
Yuxuan 'fishy' Wang0fed98e2013-10-04 10:08:55 -070046<div class="listingblock">
Yuxuan 'fishy' Wang71acd112013-10-02 12:37:53 -070047%s
Yuxuan 'fishy' Wang0fed98e2013-10-04 10:08:55 -070048<div class="content">
Yuxuan 'fishy' Wange6cbae92013-09-03 18:26:54 -070049<a id=\"{0}\" onmousedown="javascript:
50 var i = document.URL.lastIndexOf(\'/Documentation/\');
51 var url = document.URL.substring(0, i) + \'{0}\';
52 document.getElementById(\'{0}\').href = url;">
53 GET {0} HTTP/1.0
54</a>
Yuxuan 'fishy' Wang71acd112013-10-02 12:37:53 -070055</div>
Yuxuan 'fishy' Wang0fed98e2013-10-04 10:08:55 -070056</div>
Yuxuan 'fishy' Wange6cbae92013-09-03 18:26:54 -070057++++
Yuxuan 'fishy' Wang71acd112013-10-02 12:37:53 -070058
Yuxuan 'fishy' Wange6cbae92013-09-03 18:26:54 -070059"""
60
Yuxuan 'fishy' Wang99cb68d2013-10-31 17:26:00 -070061SEARCH_BOX = """
62
63++++
Saša Živkov0b6f8fe2016-04-05 10:45:50 +020064<div style="
65 position:fixed;
66 top:0px;
67 right:0px;
68 text-align:
69 right;
70 padding-top:2px;
71 padding-right:0.5em;
72 padding-bottom:2px;">
73<input size="40"
74 style="line-height: 0.75em;font-size: 0.75em;"
75 id="docSearch"
76 type="text">
77<button style="
78 background:none!important;
79 border:none;
80 padding:0!important;
81 vertical-align:bottom;
82 font-family:'Open Sans','DejaVu Sans',sans-serif;
83 font-size:0.8em;
84 color:#1d4b8f;
85 text-decoration:none;"
86 type="button"
87 id="searchBox">
88 Search
89</button>
Yuxuan 'fishy' Wang99cb68d2013-10-31 17:26:00 -070090<script type="text/javascript">
91var f = function() {
92 window.location = '../#/Documentation/' +
93 encodeURIComponent(document.getElementById("docSearch").value);
94}
95document.getElementById("searchBox").onclick = f;
96document.getElementById("docSearch").onkeypress = function(e) {
97 if (13 == (e.keyCode ? e.keyCode : e.which)) {
98 f();
99 }
100}
101</script>
102</div>
103++++
104
105"""
106
Edwin Kempina92861f2014-11-04 17:48:10 +0100107LINK_SCRIPT = """
108
109++++
110<script type="text/javascript">
111 decorate(document.getElementsByTagName('h1'));
112 decorate(document.getElementsByTagName('h2'));
113 decorate(document.getElementsByTagName('h3'));
Edwin Kempine55cf282014-12-11 17:09:45 +0100114 decorate(document.getElementsByTagName('h4'));
Edwin Kempina92861f2014-11-04 17:48:10 +0100115
116 var divs = document.getElementsByTagName('div');
117 var arr = new Array();
118 var excluded = getExcludedIds();
119 for(var i = 0; i < divs.length; i++) {
120 var d = divs[i];
121 var id = d.getAttribute('id');
122 if (id != null && !(id in excluded)) {
123 arr[arr.length] = d;
124 }
125 }
126 decorate(arr);
127
128 var anchors = document.getElementsByTagName('a');
129 arr = new Array();
130 for(var i = 0; i < anchors.length; i++) {
131 var a = anchors[i];
132 // if the anchor has no id there is no target to
133 // which we can link
134 if (a.getAttribute('id') != null) {
135 // if the anchor is empty there is no content which
136 // can receive the mouseover event, an empty anchor
137 // applies to the element that follows, move the
138 // element that follows into the anchor so that there
139 // is content which can receive the mouseover event
140 if (a.firstChild == null) {
141 var next = a.nextSibling;
142 if (next != null) {
143 next.parentNode.removeChild(next);
144 a.appendChild(next);
145 }
146 }
147 arr[arr.length] = a;
148 }
149 }
150 decorate(arr);
151
152 function decorate(e) {
153 for(var i = 0; i < e.length; i++) {
154 e[i].onmouseover = function (evt) {
155 var element = this;
156 // do nothing if the link icon is currently showing
157 var a = element.firstChild;
158 if (a != null && a instanceof Element
159 && a.getAttribute('id') == 'LINK') {
160 return;
161 }
162
163 // if there is no id there is no target to link to
164 var id = element.getAttribute('id');
165 if (id == null) {
166 return;
167 }
168
169 // create and show a link icon that links to this element
170 a = document.createElement('a');
171 a.setAttribute('id', 'LINK');
172 a.setAttribute('href', '#' + id);
173 a.setAttribute('style', 'position: absolute;'
174 + ' left: ' + (element.offsetLeft - 16 - 2 * 4) + 'px;'
Yuxuan 'fishy' Wang4f5ad9d2016-05-03 16:18:58 -0700175 + ' padding-left: 4px; padding-right: 4px;');
176 var span = document.createElement('span');
177 span.setAttribute('style', 'height: ' + element.offsetHeight + 'px;'
178 + ' display: inline-block; vertical-align: baseline;'
179 + ' font-size: 16px; text-decoration: none; color: grey;');
180 a.appendChild(span);
181 var link = document.createTextNode('🔗');
182 span.appendChild(link);
Edwin Kempina92861f2014-11-04 17:48:10 +0100183 element.insertBefore(a, element.firstChild);
184
185 // remove the link icon when the mouse is moved away,
Chad Horohoe69142322018-05-17 10:19:22 -0700186 // but keep it shown if the mouse is over the element, the link or
187 // the icon
Edwin Kempina92861f2014-11-04 17:48:10 +0100188 hide = function(evt) {
189 if (document.elementFromPoint(evt.clientX, evt.clientY) != element
190 && document.elementFromPoint(evt.clientX, evt.clientY) != a
Yuxuan 'fishy' Wang4f5ad9d2016-05-03 16:18:58 -0700191 && document.elementFromPoint(evt.clientX, evt.clientY) != span
192 && document.elementFromPoint(evt.clientX, evt.clientY) != link
Edwin Kempina92861f2014-11-04 17:48:10 +0100193 && element.contains(a)) {
194 element.removeChild(a);
195 }
196 }
197 element.onmouseout = hide;
198 a.onmouseout = hide;
Yuxuan 'fishy' Wang4f5ad9d2016-05-03 16:18:58 -0700199 span.onmouseout = hide;
200 link.onmouseout = hide;
Edwin Kempina92861f2014-11-04 17:48:10 +0100201 }
202 }
203 }
204
205 function getExcludedIds() {
206 var excluded = {};
207 excluded['header'] = true;
208 excluded['toc'] = true;
209 excluded['toctitle'] = true;
210 excluded['content'] = true;
211 excluded['preamble'] = true;
212 excluded['footer'] = true;
213 excluded['footer-text'] = true;
214 return excluded;
215 }
216</script>
217
218++++
219
220"""
221
Yuxuan 'fishy' Wange6cbae92013-09-03 18:26:54 -0700222opts = OptionParser()
223opts.add_option('-o', '--out', help='output file')
224opts.add_option('-s', '--src', help='source file')
225opts.add_option('-x', '--suffix', help='suffix for included filenames')
Yuxuan 'fishy' Wang537664a2014-09-03 10:06:16 -0700226opts.add_option('-b', '--searchbox', action="store_true", default=True,
227 help="generate the search boxes")
228opts.add_option('--no-searchbox', action="store_false", dest='searchbox',
229 help="don't generate the search boxes")
Yuxuan 'fishy' Wange6cbae92013-09-03 18:26:54 -0700230options, _ = opts.parse_args()
231
232try:
Chad Horohoedd224702018-05-16 22:33:06 -0400233 try:
234 out_file = open(options.out, 'w', errors='ignore')
235 src_file = open(options.src, 'r', errors='ignore')
236 except TypeError:
237 out_file = open(options.out, 'w')
238 src_file = open(options.src, 'r')
239 last_line = ''
240 ignore_next_line = False
241 last_title = ''
242 for line in src_file:
243 if PAT_GERRIT.match(last_line):
244 # Case of "GERRIT\n------" at the footer
245 out_file.write(GERRIT_UPLINK)
246 last_line = ''
247 elif PAT_SEARCHBOX.match(last_line):
248 # Case of 'SEARCHBOX\n---------'
249 if options.searchbox:
250 out_file.write(SEARCH_BOX)
251 last_line = ''
252 elif PAT_INCLUDE.match(line):
253 # Case of 'include::<filename>'
254 match = PAT_INCLUDE.match(line)
255 out_file.write(last_line)
256 last_line = match.group(1) + options.suffix + match.group(2) + '\n'
257 elif PAT_STARS.match(line):
258 if PAT_TITLE.match(last_line):
259 # Case of the title in '.<title>\n****\nget::<url>\n****'
260 match = PAT_TITLE.match(last_line)
261 last_title = GET_TITLE % match.group(1)
262 else:
263 out_file.write(last_line)
264 last_title = ''
265 elif PAT_GET.match(line):
266 # Case of '****\nget::<url>\n****' in rest api
267 url = PAT_GET.match(line).group(1)
268 out_file.write(GET_MACRO.format(url) % last_title)
269 ignore_next_line = True
270 elif ignore_next_line:
271 # Handle the trailing '****' of the 'get::' case
272 last_line = ''
273 ignore_next_line = False
274 else:
275 out_file.write(last_line)
276 last_line = line
277 out_file.write(last_line)
278 out_file.write(LINK_SCRIPT)
279 out_file.close()
Yuxuan 'fishy' Wange6cbae92013-09-03 18:26:54 -0700280except IOError as err:
Chad Horohoedd224702018-05-16 22:33:06 -0400281 sys.stderr.write(
282 "error while expanding %s to %s: %s" % (options.src, options.out, err))
283 exit(1)