Remove asciidoc.conf

Asciidoctor doesn't support the conf file, so we remove it, do the macro
replacing ourselves, and put all the attributes into the command line
parameters.

Change-Id: I21b49e5a2d75ff0f9b52f26fccaae42fcbaa0837
This commit is contained in:
Yuxuan 'fishy' Wang
2013-09-03 18:26:54 -07:00
parent cd4be95d6e
commit e6cbae9ef6
6 changed files with 145 additions and 61 deletions

View File

@@ -33,10 +33,18 @@ genasciidoc(
name = 'generate_html',
srcs = SRCS + [genfile('licenses.txt')],
outs = HTML + ['licenses.html'],
deps = [':config', ':licenses.txt'],
attributes = ['toc', 'newline="\\n"'],
deps = [':licenses.txt'],
attributes = [
'toc',
'newline="\\n"',
'asterisk="*"',
'plus="+"',
'caret="^"',
'startsb="["',
'endsb="]"',
'tilde="~"',
],
backend = 'xhtml11',
conf_file = genfile('asciidoc.conf'),
)
genrule(
@@ -46,16 +54,12 @@ genrule(
out = 'licenses.txt',
)
genrule(
name = 'config',
cmd = 'cp $SRCS $OUT &&' +
'echo "[attributes]" >>$OUT &&' +
'echo "revision=`git describe HEAD`" >>$OUT',
srcs = ['asciidoc.conf'],
out = 'asciidoc.conf',
)
python_binary(
name = 'gen_licenses',
main = 'gen_licenses.py',
)
python_binary(
name = 'replace_macros',
main = 'replace_macros.py',
)

View File

@@ -1,29 +0,0 @@
[attributes]
asterisk=*
plus=+
caret=^
startsb=[
endsb=]
tilde=~
[specialsections]
GERRIT=gerrituplink
[gerrituplink]
<hr style="
height: 2px;
color: silver;
margin-top: 1.2em;
margin-bottom: 0.5em;
">
[macros]
(?u)^(?P<name>get)::(?P<target>\S*?)$=#
[get-blockmacro]
<a id="{target}" onmousedown="javascript:
var i = document.URL.lastIndexOf('/Documentation/');
var url = document.URL.substring(0, i) + '{target}';
document.getElementById('{target}').href = url;">
GET {target} HTTP/1.0
</a>

View File

@@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
include_defs('//tools/git.defs')
def genasciidoc(
name,
srcs = [],
@@ -19,28 +21,34 @@ def genasciidoc(
deps = [],
attributes = [],
backend = None,
conf_file = None,
visibility = []):
MACRO_SUFFIX = '.expanded'
cmd = ['asciidoc', '-o', '$OUT']
if backend:
cmd.extend(['-b', backend])
for attribute in attributes:
cmd.extend(['-a', attribute])
if conf_file:
cmd.append('-f')
cmd.extend(['-a', 'revision="%s"' % git_describe()])
cmd.append('$SRCS')
for p in zip(srcs, outs):
s, o = p
if conf_file:
src_list = [conf_file, s]
else:
src_list = [s]
filename = s
if filename.startswith('BUCKGEN:') :
filename = s[8:]
genrule(
name = filename + MACRO_SUFFIX,
cmd = '$(exe :replace_macros) -s $SRCS -o $OUT --suffix=' + MACRO_SUFFIX,
srcs = [s],
deps = deps + [':replace_macros'],
out = filename + MACRO_SUFFIX,
)
genrule(
name = o,
cmd = ' '.join(cmd),
srcs = src_list,
deps = deps,
srcs = [genfile(filename + MACRO_SUFFIX)],
deps = deps + [':' + filename + MACRO_SUFFIX],
out = o,
visibility = visibility,
)

87
Documentation/replace_macros.py Executable file
View File

@@ -0,0 +1,87 @@
#!/usr/bin/python
# 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.
from optparse import OptionParser
import os
import re
import sys
PAT_GERRIT = re.compile('^GERRIT')
PAT_INCLUDE = re.compile('^(include::.*)(\[\])$')
PAT_GET = re.compile('^get::([^ \t\n]*)')
GERRIT_UPLINK = """
++++
<hr style=\"
height: 2px;
color: silver;
margin-top: 1.2em;
margin-bottom: 0.5em;
\">
++++
"""
GET_MACRO = """
++++
<a id=\"{0}\" onmousedown="javascript:
var i = document.URL.lastIndexOf(\'/Documentation/\');
var url = document.URL.substring(0, i) + \'{0}\';
document.getElementById(\'{0}\').href = url;">
GET {0} HTTP/1.0
</a>
++++
"""
opts = OptionParser()
opts.add_option('-o', '--out', help='output file')
opts.add_option('-s', '--src', help='source file')
opts.add_option('-x', '--suffix', help='suffix for included filenames')
options, _ = opts.parse_args()
try:
out_file = open(options.out, 'w')
src_file = open(options.src, 'r')
last_line = ''
ignore_next_line = False
for line in src_file.xreadlines():
if PAT_GERRIT.match(last_line):
# Case of "GERRIT\n------" at the footer
out_file.write(GERRIT_UPLINK)
last_line = ''
elif PAT_INCLUDE.match(line):
# Case of 'include::<filename>'
match = PAT_INCLUDE.match(line)
out_file.write(last_line)
last_line = match.group(1) + options.suffix + match.group(2) + '\n'
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))
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.close()
except IOError as err:
sys.stderr.write(
"error while expanding %s to %s: %s" % (options.src, options.out, err))
exit(1)

View File

@@ -1,3 +1,5 @@
include_defs('//tools/git.defs')
java_library2(
name = 'init',
srcs = glob(['src/main/java/**/*.java']),
@@ -59,17 +61,6 @@ prebuilt_jar(
visibility = ['//:'],
)
# TODO(sop): Move git describe into an uncacheable genrule()
def git_describe():
import subprocess
cmd = ['git', 'describe', '--match', 'v[0-9].*', '--dirty']
p = subprocess.Popen(cmd, stdout = subprocess.PIPE)
v = p.communicate()[0].strip()
r = p.returncode
if r != 0:
raise subprocess.CalledProcessError(r, ' '.join(cmd))
return v
genrule(
name = 'gen_version',
cmd = ';'.join([

23
tools/git.defs Normal file
View File

@@ -0,0 +1,23 @@
# 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.
def git_describe():
import subprocess
cmd = ['git', 'describe', '--match', 'v[0-9].*', '--dirty']
p = subprocess.Popen(cmd, stdout = subprocess.PIPE)
v = p.communicate()[0].strip()
r = p.returncode
if r != 0:
raise subprocess.CalledProcessError(r, ' '.join(cmd))
return v