]> git.lyx.org Git - lyx.git/blob - development/Win32/packaging/clean_dvi.py
Check if we are on column 0 for special case
[lyx.git] / development / Win32 / packaging / clean_dvi.py
1 #! /usr/bin/env python
2
3 '''
4 file clean_dvi.py
5 This file is part of LyX, the document processor.
6 Licence details can be found in the file COPYING
7 or at http://www.lyx.org/about/licence.php3
8
9 author Angus Leeming
10 Full author contact details are available in the file CREDITS
11 or at http://www.lyx.org/about/credits.php
12
13 Usage:
14     python clean_dvi.py infile.dvi outfile.dvi
15
16 clean_dvi modifies the input .dvi file so that
17 dvips and yap (a dvi viewer on Windows) can find
18 any embedded PostScript files whose names are protected
19 with "-quotes.
20
21 It works by:
22 1 translating the machine readable .dvi file to human
23   readable .dtl form,
24 2 manipulating any references to external files
25 3 translating the .dtl file back to .dvi format.
26
27 It requires dv2dt and dt2dv from the DTL dviware package
28 http://www.ctan.org/tex-archive/dviware/dtl/
29 '''
30
31 import os, re, sys
32
33 def usage(prog_name):
34     return 'Usage: %s in.dvi out.dvi\n' \
35            % os.path.basename(prog_name)
36
37
38 def warning(message):
39     sys.stderr.write(message + '\n')
40
41
42 def error(message):
43     sys.stderr.write(message + '\n')
44     sys.exit(1)
45
46
47 def manipulated_dtl(data):
48     psfile_re = re.compile(r'(.*PSfile=")(.*)(" llx=.*)')
49
50     lines = data.split('\n')
51     for i in range(len(lines)):
52         line = lines[i]
53         match = psfile_re.search(line)
54         if match != None:
55             file = match.group(2).replace('"', '')
56             lines[i] = '%s%s%s' \
57                        % ( match.group(1), file, match.group(3) )
58
59     return '\n'.join(lines)
60
61
62 def main(argv):
63     # First establish that the expected information has
64     # been input on the command line and whether the
65     # required executables exist.
66     if len(argv) != 3:
67         error(usage(argv[0]))
68
69     infile  = argv[1]
70     outfile = argv[2]
71
72     if not os.path.exists(infile):
73         error('Unable to read "%s"\n' % infile)
74
75     # Convert the input .dvi file to .dtl format.
76     dv2dt_call = 'dv2dt "%s"' % infile
77     dv2dt_stdin, dv2dt_stdout, dv2dt_stderr = \
78         os.popen3(dv2dt_call, 't')
79
80     dv2dt_stdin.close()
81     dv2dt_data   = dv2dt_stdout.read()
82     dv2dt_status = dv2dt_stdout.close()
83
84     if dv2dt_status != None or len(dv2dt_data) == 0:
85         dv2dt_err = dv2dt_stderr.read()
86         error("Failed: %s\n%s\n" % ( dv2dt_call, dv2dt_err) )
87
88     # Manipulate the .dtl file.
89     dtl_data = manipulated_dtl(dv2dt_data)
90     if dtl_data == None:
91         error("Failed to manipulate the dtl file")
92
93     # Convert this .dtl file back to .dvi format.
94     dt2dv_call = 'dt2dv -si "%s"' % outfile
95     dt2dv_stdin = os.popen(dt2dv_call, 'w')
96     dt2dv_stdin.write(dtl_data)
97
98
99 if __name__ == "__main__":
100     main(sys.argv)