Bump version (incompatilibity: dropping old Pythons).
[texml.git] / scripts / texml.py
1 #!python
2 # $Id: texml.py,v 1.20 2006-07-20 03:56:49 olpa Exp $
3
4 VERSION = "3.0"; # GREPVERSION # Format of this string is important
5 usage = """Convert TeXML markup to [La]TeX markup. v.%s. Usage:
6 python texml.py [-e encoding] [-w auto_width] [-c|--context] [-a|--ascii] in_file out_file""" % VERSION
7
8 #
9 # Check command line, print help
10 #
11 import sys
12 if len(sys.argv) < 3:
13   print >>sys.stderr, usage
14   sys.exit(1)
15
16 #
17 # Parse command line options
18 #
19 encoding      = 'ascii'
20 always_ascii  = 0
21 width         = 62
22 use_context   = 0
23 use_namespace = 1
24 import getopt
25 try:
26   opts, args = getopt.getopt(sys.argv[1:], 'hcaw:e:', ['help', 'context', 'ascii', 'width=', 'encoding=', ])
27 except getopt.GetoptError as e:
28   print >>sys.stderr, 'texml: Can\'t parse command line: %s' % e
29   print >>sys.stderr, usage
30   sys.exit(2)
31 for o, a in opts:
32   if o in ('-h', '--help'):
33     print >>sys.stderr, usage;
34     sys.exit(1)
35   if o in ('-c', '--context'):
36     use_context = 1
37   if o in ('-a', '--ascii'):
38     always_ascii = 1
39   if o in ('-w', '--width'):
40     try:
41       width = int(a)
42     except:
43       print >>sys.stderr, "texml: Width is not an integer: '%s'" % a
44       sys.exit(1)
45     if width < 3: # just a random value
46       print >>sys.stderr, "texml: Width should be greater 3 but get '%s'" % a
47       sys.exit(1)
48   if o in ('-e', '--encoding'):
49     encoding = a
50
51 #
52 # Get input and output file
53 #
54 if len(args) != 2:
55   print >>sys.stderr, 'texml: Expected two command line arguments, but got %d' % len(args)
56   sys.exit(3)
57 (infile, outfile) = args
58
59 #
60 # Prepare transformation-1: input file, XML parser
61 #
62 import xml.sax 
63 from xml.sax.handler import feature_namespaces
64
65 if '-' == infile:
66   infile = sys.stdin
67
68 #
69 # Prepare transformation-2: output
70 #
71 if '-' == outfile:
72   f = sys.stdout
73 else:
74   f = open(outfile, 'wb')
75
76 #
77 # An error handler
78 #
79 def quit(msg):
80     sys.stderr.write(msg)
81     f.close()
82     sys.exit(1)
83
84 #
85 # import main class and parse
86 #
87 import Texml.processor
88 try:
89   Texml.processor.process(infile, f, encoding, width, always_ascii, use_context)
90     
91 except Exception as msg:
92   msg = 'texml: %s\n' % (str(msg))
93   quit(msg)
94
95 f.close()
96 sys.exit(0)