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