Add fromFile, toFile, and fromToFile functions in generator. This simplifies a lot of other code.
2 """usage: anolis [options] input output
4 Post-process a document, adding cross-references, table of contents, etc.
7 from optparse import OptionParser, SUPPRESS_HELP
10 from anolislib import generator, utils
13 # Create the options parser
14 optParser = getOptParser()
15 opts, args = optParser.parse_args()
17 # Check we have enough arguments
23 # Get input and generate
24 input = open(args[0], "rb")
25 tree = generator.fromFile(input, **kwargs)
29 output = open(args[1], "wb")
30 generator.toFile(tree, output, **kwargs)
32 except (utils.AnolisException, IOError, etree.XMLSyntaxError), e:
33 sys.stderr.write(unicode(e) + u"\n")
36 sys.stderr.write(u"anolis expects two arguments. Use -h for help\n")
40 parser = OptionParser(usage = __doc__, version="%prog 1.0")
42 parser.add_option("", "--enable", action="callback", callback=enable,
43 type="string", dest="processes", help="Enable the process given as the option value")
45 parser.add_option("", "--disable", action="callback", callback=disable,
46 type="string", help="Disable the process given as the option value")
48 #parser.add_option("", "", action="store_true",
49 # dest="xml", help="Use an XML parser/serializer.")
51 parser.add_option("", "--lxml.html", action="store_true",
52 dest="lxml_html", help="Use lxml's HTML parser/serializer.")
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.")
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.")
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.")
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.")
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.")
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).")
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).")
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.")
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.")
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.")
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).")
87 parser.add_option("", "--w3c-compat-substitutions", action="store_true",
88 dest="w3c_compat_substitutions", help="Do W3C specific substitutions.")
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).")
93 parser.add_option("", "--profile", action="store_true",
94 dest="profile", help=SUPPRESS_HELP)
96 parser.add_option("", "--inject-meta-charset", action="store_true",
97 dest="inject_meta_charset", help=SUPPRESS_HELP)
99 parser.add_option("", "--strip-whitespace", action="store_true",
100 dest="strip_whitespace", help=SUPPRESS_HELP)
102 parser.add_option("", "--omit-optional-tags", action="store_true",
103 dest="omit_optional_tags", help=SUPPRESS_HELP)
105 parser.add_option("", "--quote-attr-values", action="store_true",
106 dest="quote_attr_values", help=SUPPRESS_HELP)
108 parser.add_option("", "--use-best-quote-char", action="store_true",
109 dest="use_best_quote_char", help=SUPPRESS_HELP)
111 parser.add_option("", "--no-minimize-boolean-attributes",
112 action="store_false", default=True,
113 dest="minimize_boolean_attributes", help=SUPPRESS_HELP)
115 parser.add_option("", "--use-trailing-solidus", action="store_true",
116 dest="use_trailing_solidus", help=SUPPRESS_HELP)
118 parser.add_option("", "--space-before-trailing-solidus",
119 action="store_true", default=False,
120 dest="space_before_trailing_solidus", help=SUPPRESS_HELP)
122 parser.add_option("", "--escape-lt-in-attrs", action="store_true",
123 dest="escape_lt_in_attrs", help=SUPPRESS_HELP)
125 parser.add_option("", "--escape-rcdata", action="store_true",
126 dest="escape_rcdata", help=SUPPRESS_HELP)
129 processes=set(["sub", "xref", "toc"]),
134 force_html4_id=False,
137 allow_duplicate_dfns=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,
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,
159 def enable(option, opt_str, value, parser, *args, **kwargs):
160 parser.values.processes.add(opt_str)
162 def disable(option, opt_str, value, parser, *args, **kwargs):
163 parser.values.processes.discard(opt_str)
165 if __name__ == "__main__":