anolis
author Geoffrey Sneddon <geoffers@gmail.com>
Sat Aug 30 11:43:23 2008 +0100 (2008-08-30)
changeset 254 cf4770338aa0
parent 252 ec226b16dbf1
child 255 bf926a2e75ab
permissions -rw-r--r--
Add fromFile, toFile, and fromToFile functions in generator. This simplifies a lot of other code.
     1 #!/usr/bin/env python
     2 """usage: anolis [options] input output
     3 
     4 Post-process a document, adding cross-references, table of contents, etc.
     5 """
     6 
     7 from optparse import OptionParser, SUPPRESS_HELP
     8 import sys
     9 
    10 from anolislib import generator, utils
    11 
    12 def main():
    13 	# Create the options parser
    14 	optParser = getOptParser()
    15 	opts, args = optParser.parse_args()
    16 	
    17 	# Check we have enough arguments
    18 	if len(args) >= 2:
    19 		try:
    20 			# Get options
    21 			kwargs = vars(opts)
    22 			
    23 			# Get input and generate
    24 			input = open(args[0], "rb")
    25 			tree = generator.fromFile(input, **kwargs)
    26 			input.close()
    27 			
    28 			# Write output
    29 			output = open(args[1], "wb")
    30 			generator.toFile(tree, output, **kwargs)
    31 			output.close()
    32 		except (utils.AnolisException, IOError, etree.XMLSyntaxError), e:
    33 			sys.stderr.write(unicode(e) + u"\n")
    34 			sys.exit(1)
    35 	else:
    36 		sys.stderr.write(u"anolis expects two arguments. Use -h for help\n")
    37 		sys.exit(2)
    38 
    39 def getOptParser():
    40 	parser = OptionParser(usage = __doc__, version="%prog 1.0")
    41 	
    42 	parser.add_option("", "--enable", action="callback", callback=enable,
    43 		type="string", dest="processes", help="Enable the process given as the option value")
    44 	
    45 	parser.add_option("", "--disable", action="callback", callback=disable,
    46 		type="string", help="Disable the process given as the option value")
    47 	
    48 	#parser.add_option("", "", action="store_true",
    49 	#	dest="xml", help="Use an XML parser/serializer.")
    50 	
    51 	parser.add_option("", "--lxml.html", action="store_true",
    52 		dest="lxml_html", help="Use lxml's HTML parser/serializer.")
    53 	
    54 	parser.add_option("", "--newline-char", action="store", type="string",
    55 		dest="newline_char", help="Set the newline character/string used when creating new newlines. This should match the rest of the newlines in the document.")
    56 	
    57 	parser.add_option("", "--indent-char", action="store", type="string",
    58 		dest="indent_char", help="Set the character/string used when creating indenting new blocks of (X)HTML. This should match the rest of the indentation in the document.")
    59 	
    60 	parser.add_option("", "--force-html4-id", action="store_true",
    61 		dest="force_html4_id", help="Force the ID generation algorithm to create HTML 4 compliant IDs regardless of the DOCTYPE.")
    62 	
    63 	parser.add_option("", "--min-depth", action="store", type="int",
    64 		default=2, dest="min_depth", help="Highest ranking header to number/insert into TOC.")
    65 	
    66 	parser.add_option("", "--max-depth", action="store", type="int",
    67 		default=6, dest="max_depth", help="Lowest ranking header to number/insert into TOC.")
    68 	
    69 	parser.add_option("", "--allow-duplicate-dfns", action="store_true",
    70 		dest="allow_duplicate_dfns", help="Allow multiple definitions of terms when cross-referencing (the last instance of the term is used when referencing it).")
    71 	
    72 	parser.add_option("", "--w3c-compat", action="store_true",
    73 		dest="w3c_compat", help="Behave in a (mostly) compatible way to the W3C CSS WG's Postprocessor (this implies all of the other --w3c-compat options with the exception of --w3c-compat-crazy-substitution, as that is too crazy).")
    74 	
    75 	parser.add_option("", "--w3c-compat-xref-elements", action="store_true",
    76 		dest="w3c_compat_xref_elements", help="Uses the same list of elements to look for cross-references in as the W3C CSS WG's Postprocessor, even when the elements shouldn't semantically be used for cross-reference terms.")
    77 	
    78 	parser.add_option("", "--w3c-compat-xref-a-placement", action="store_true",
    79 		dest="w3c_compat_xref_a_placement", help="When cross-referencing elements apart from span, put the a element inside the element instead of outside the element.")
    80 	
    81 	parser.add_option("", "--w3c-compat-xref-normalization", action="store_true",
    82 		dest="w3c_compat_xref_normalization", help="Only use ASCII letters, numbers, and spaces in comparison of cross-reference terms.")
    83 	
    84 	parser.add_option("", "--w3c-compat-class-toc", action="store_true",
    85 		dest="w3c_compat_class_toc", help="Add @class='toc' on every ol element in the table of contents (instead of only the root ol element).")
    86 	
    87 	parser.add_option("", "--w3c-compat-substitutions", action="store_true",
    88 		dest="w3c_compat_substitutions", help="Do W3C specific substitutions.")
    89 	
    90 	parser.add_option("", "--w3c-compat-crazy-substitutions", action="store_true",
    91 		dest="w3c_compat_crazy_substitutions", help="Do crazy W3C specific substitutions, which may cause unexpected behaviour (i.e., replacing random strings within the document with no special marker).")
    92 	
    93 	parser.add_option("", "--profile", action="store_true",
    94 		dest="profile", help=SUPPRESS_HELP)
    95 	
    96 	parser.add_option("", "--inject-meta-charset", action="store_true",
    97 		dest="inject_meta_charset", help=SUPPRESS_HELP)
    98 	
    99 	parser.add_option("", "--strip-whitespace", action="store_true",
   100 		dest="strip_whitespace", help=SUPPRESS_HELP)
   101 
   102 	parser.add_option("", "--omit-optional-tags", action="store_true",
   103 		dest="omit_optional_tags", help=SUPPRESS_HELP)
   104 
   105 	parser.add_option("", "--quote-attr-values", action="store_true",
   106 		dest="quote_attr_values", help=SUPPRESS_HELP)
   107 
   108 	parser.add_option("", "--use-best-quote-char", action="store_true",
   109 		dest="use_best_quote_char",	help=SUPPRESS_HELP)
   110 
   111 	parser.add_option("", "--no-minimize-boolean-attributes",
   112 		action="store_false", default=True,
   113 		dest="minimize_boolean_attributes", help=SUPPRESS_HELP)
   114 
   115 	parser.add_option("", "--use-trailing-solidus", action="store_true",
   116 		dest="use_trailing_solidus", help=SUPPRESS_HELP)
   117 
   118 	parser.add_option("", "--space-before-trailing-solidus",
   119 		action="store_true", default=False,
   120 		dest="space_before_trailing_solidus", help=SUPPRESS_HELP)
   121 
   122 	parser.add_option("", "--escape-lt-in-attrs", action="store_true",
   123 		dest="escape_lt_in_attrs", help=SUPPRESS_HELP)
   124 
   125 	parser.add_option("", "--escape-rcdata", action="store_true",
   126 		dest="escape_rcdata", help=SUPPRESS_HELP)
   127 	
   128 	parser.set_defaults(
   129 		processes=set(["sub", "xref", "toc"]),
   130 		xml=False,
   131 		lxml_html=False,
   132 		newline_char=u"\n",
   133 		indent_char=u"\t",
   134 		force_html4_id=False,
   135 		min_depth=2,
   136 		max_depth=6,
   137 		allow_duplicate_dfns=False,
   138 		w3c_compat=False,
   139 		w3c_compat_xref_elements=False,
   140 		w3c_compat_xref_a_placement=False,
   141 		w3c_compat_xref_normalization=False,
   142 		w3c_compat_class_toc=False,
   143 		w3c_compat_substitutions=False,
   144 		w3c_compat_crazy_substitutions=False,
   145 		profile=False,
   146 		inject_meta_charset=False,
   147 		omit_optional_tags=False,
   148 		quote_attr_values=False,
   149 		use_best_quote_char=False,
   150 		minimize_boolean_attributes=False,
   151 		use_trailing_solidus=False,
   152 		space_before_trailing_solidus=False,
   153 		escape_lt_in_attrs=False,
   154 		escape_rcdata=False
   155 	)
   156 
   157 	return parser
   158 
   159 def enable(option, opt_str, value, parser, *args, **kwargs):
   160 	parser.values.processes.add(opt_str)
   161 
   162 def disable(option, opt_str, value, parser, *args, **kwargs):
   163 	parser.values.processes.discard(opt_str)
   164 
   165 if __name__ == "__main__":
   166 	main()