]> git.lyx.org Git - lyx.git/blob - lib/scripts/html2latexwrapper.py
update layout files to format 101
[lyx.git] / lib / scripts / html2latexwrapper.py
1 # -*- coding: utf-8 -*-
2
3 # file html2latexwrapper.py
4 # This file is part of LyX, the document processor.
5 # Licence details can be found in the file COPYING.
6
7 # author Georg Baum
8
9 # Full author contact details are available in file CREDITS
10
11 # Usage:
12 # html2latexwrapper.py <converter> <from file> <to file>
13
14 # This script will call <converter> -s <from file> > <to file>
15 # and add a \usepackage{inputenc} line if needed.
16
17
18 import os, string, sys, re
19
20 from lyxpreview_tools import error, run_command
21
22
23 def usage(prog_name):
24     return "Usage: %s <converter> <from file> <to file>" % prog_name
25
26
27 def get_encoding(from_file_name):
28     '''Read the encoding from a HTML or XHTML file'''
29     try:
30         from_file = open(from_file_name, 'rt')
31         regexpxml = re.compile(r'^\s?<\?xml\s+.*?encoding\s*=\s*"([^"]+)"', re.IGNORECASE)
32         regexptype = re.compile(r'^\s?<meta\s+.*?charset\s*=\s*"([^"]+)"', re.IGNORECASE)
33         for line in from_file.readlines():
34             m = regexpxml.match(line)
35             if not m:
36                 m = regexptype.match(line)
37             if m:
38                 from_file.close()
39                 return m.group(1).lower()
40         from_file.close()
41     except:
42         pass
43     return ''
44
45
46 def main(argv):
47     # Parse and manipulate the command line arguments.
48     if len(argv) != 4:
49         error(usage(argv[0]))
50
51     converter = argv[1]
52     from_file_name = argv[2]
53     to_file_name = argv[3]
54
55     # Run gnuhtml2latex
56     cmd = '%s -s %s' % (converter, from_file_name)
57     (ret, output) = run_command(cmd, False)
58
59     # Determine encoding of HTML file
60     enc = get_encoding(from_file_name).replace('iso_8859', 'iso-8859')
61     # The HTML encodings were taken from http://www.iana.org/assignments/character-sets/character-sets.xml.
62     # Only those with inputenc support were added, and only thge most important aliases.
63     # List of encodings that have the same name in HTML (may be as an alias) and inputenc
64     same_enc = ['cp437', 'cp850', 'cp852', 'cp855', 'cp858', 'cp862', 'cp865', 'cp866', \
65                 'cp1250', 'cp1251', 'cp1252', 'cp1255', 'cp1256', 'cp1257', \
66                 'koi8-r', 'koi8-u', 'pt154', 'pt254', \
67                 'latin1', 'latin2', 'latin3', 'latin4', 'latin5', 'latin9', 'latin10']
68     # Translation table from HTML encoding names to inputenc encoding names
69     encodings = {'utf-8' : 'utf8', 'csutf8' : 'utf8', \
70                  'iso-8859-1' : 'latin1', 'cp819' : 'latin1', \
71                  'iso-8859-2' : 'latin2', \
72                  'iso-8859-3' : 'latin3', \
73                  'iso-8859-4' : 'latin4', \
74                  'iso-8859-5' : 'iso88595', 'cyrillic' : 'iso88595', \
75                  'iso-8859-6' : '8859-6', 'arabic' : '8859-6', \
76                  'iso-8859-7' : 'iso-8859-7', 'greek' : 'iso-8859-7', \
77                  'iso-8859-8' : '8859-8', 'hebrew' : '8859-8', \
78                  'iso-8859-9' : 'latin5', \
79                  'iso-8859-13' : 'l7xenc', \
80                  'iso-8859-15' : 'latin9', \
81                  'iso-8859-16' : 'latin10', \
82                  'ibm437' : 'cp437', \
83                  'ibm850' : 'cp850', \
84                  'ibm852' : 'cp852', \
85                  'ibm855' : 'cp855', \
86                  'ibm858' : 'cp858', \
87                  'ibm862' : 'cp862', \
88                  'ibm865' : 'cp865', \
89                  'ibm866' : 'cp866', \
90                  'ibm1250' : 'cp1250', \
91                  'ibm1251' : 'cp1251', \
92                  'ibm1255' : 'cp1255', \
93                  'ibm1256' : 'cp1256', \
94                  'ibm1257' : 'cp1257', \
95                  'macintosh' : 'applemac', 'mac' : 'applemac', 'csmacintosh' : 'applemac'}
96     if enc != '':
97         if enc in encodings.keys():
98             enc = encodings[enc]
99         elif enc not in same_enc:
100             enc = ''
101
102     # Read conversion result
103     lines = output.split('\n')
104
105     # Do not add the inputenc call if inputenc or CJK is already loaded
106     add_inputenc = (enc != '')
107     if add_inputenc:
108         regexp = re.compile(r'^\s?\\usepackage\s?(\[[^]+]\])?\s?{(inputenc)|(CJK)|(CJKutf8)}')
109         for line in lines:
110             if regexp.match(line):
111                 add_inputenc = False
112                 break
113
114     # Write output file and insert inputenc call if needed
115     to_file = open(to_file_name, 'wt')
116     for line in lines:
117         to_file.write(line + '\n')
118         if add_inputenc and line.find('\\documentclass') == 0:
119             to_file.write('\\usepackage[%s]{inputenc}\n' % enc)
120     to_file.close()
121
122     return ret
123
124
125 if __name__ == "__main__":
126     main(sys.argv)