summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStuart Rackham <srackham@methods.co.nz>2010-08-08 20:47:20 +1200
committerStuart Rackham <srackham@methods.co.nz>2010-08-08 20:47:20 +1200
commit172acffe9f545d9204c152082bb8e20ccbd64640 (patch)
tree0def2fc0fadfafe3de4128fec2ae02358818cda1
parent12142885c64fb66303ac8255d7e4a467c98e9eb5 (diff)
downloadasciidoc-172acffe9f545d9204c152082bb8e20ccbd64640.tar.gz
Options can also be set in the AsciiDoc source file. If the source file
contains a line beginning with '// a2x:' then the remainder of the line will be treated as a2x command-line options.
-rwxr-xr-xa2x.py43
-rw-r--r--doc/a2x.1.txt8
2 files changed, 50 insertions, 1 deletions
diff --git a/a2x.py b/a2x.py
index 5cc6ab3..ec6d7ae 100755
--- a/a2x.py
+++ b/a2x.py
@@ -281,6 +281,44 @@ def exec_xsltproc(xsl_file, xml_file, dst_dir, opts = ''):
finally:
shell_cd(cwd)
+def get_source_options(asciidoc_file):
+ '''
+ Look for a2x command options in AsciiDoc source file.
+ Limitation: options cannot contain double-quote characters.
+ '''
+ PREFIX = '// a2x:'
+
+ def parse_line():
+ # Parse options in line to result sequence.
+ inquotes = False
+ opt = ''
+ for c in line:
+ if c == '"':
+ if inquotes:
+ result.append(opt)
+ opt = ''
+ inquotes = False
+ else:
+ inquotes = True
+ elif c == ' ':
+ if inquotes:
+ opt += c
+ elif opt:
+ result.append(opt)
+ opt = ''
+ else:
+ opt += c
+ if opt:
+ result.append(opt)
+
+ result = []
+ if os.path.isfile(asciidoc_file):
+ for line in open(asciidoc_file):
+ if line.startswith(PREFIX):
+ line = line[len(PREFIX):].strip()
+ parse_line()
+ return result
+
#####################################################################
# Application class
@@ -730,12 +768,15 @@ if __name__ == '__main__':
help='increase verbosity')
if len(sys.argv) == 1:
parser.parse_args(['--help'])
- opts, args = parser.parse_args()
+ source_options = get_source_options(sys.argv[-1])
+ argv = source_options + sys.argv[1:]
+ opts, args = parser.parse_args(argv)
if len(args) != 1:
parser.error('incorrect number of arguments')
opts = eval(str(opts)) # Convert optparse.Values to dict.
a2x = A2X(opts)
OPTIONS = a2x # verbose and dry_run used by utility functions.
+ verbose('args: %r' % argv)
a2x.asciidoc_file = args[0]
try:
a2x.load_conf()
diff --git a/doc/a2x.1.txt b/doc/a2x.1.txt
index 1fab2ba..c9e7af2 100644
--- a/doc/a2x.1.txt
+++ b/doc/a2x.1.txt
@@ -104,6 +104,14 @@ OPTIONS
*--dblatex-opts*='DBLATEX_OPTS'::
Additional 'dblatex(1)' options.
+Options can also be set in the AsciiDoc source file. If 'FILE'
+contains a line beginning with *// a2x:* then the remainder of the
+line will be treated as 'a2x' command-line options. Actual
+command-line options take precedence over options set in the source
+file. Example:
+
+ // a2x: -dbook --dblatex-opts "-P latex.output.revhistory=0"
+
OUTPUT FILES
------------