3 This file is part of LyX, the document processor.
4 Licence details can be found in the file COPYING
5 or at http://www.lyx.org/about/licence.php
8 Full author contact details are available in the file CREDITS
9 or at http://www.lyx.org/about/credits.php
12 python clean_dvi.py infile.dvi outfile.dvi
14 clean_dvi modifies the input .dvi file so that
15 dvips and yap (a dvi viewer on Windows) can find
16 any embedded PostScript files whose names are protected
20 1 translating the machine readable .dvi file to human
22 2 manipulating any references to external files
23 3 translating the .dtl file back to .dvi format.
25 It requires dv2dt and dt2dv from the DTL dviware package
26 http://www.ctan.org/tex-archive/dviware/dtl/
29 import os, re, subprocess, sys
32 return 'Usage: %s in.dvi out.dvi\n' \
33 % os.path.basename(prog_name)
37 sys.stderr.write(message + '\n')
41 sys.stderr.write(message + '\n')
45 def manipulated_dtl(data):
46 psfile_re = re.compile(r'(special1 +)([0-9]+)( +\'PSfile=")(.*)(" llx=.*)')
48 lines = data.split('\n')
49 for i in range(len(lines)):
51 match = psfile_re.match(line)
55 file = file.replace('"', '')
56 # Don't forget to update the length of the string too...
57 strlen = int(match.group(2)) - (filelen - len(file))
59 lines[i] = '%s%d%s%s%s' \
60 % ( match.group(1), strlen, match.group(3),
61 file, match.group(5) )
63 return '\n'.join(lines)
67 # First establish that the expected information has
68 # been input on the command line and whether the
69 # required executables exist.
76 if not os.path.exists(infile):
77 error('Unable to read "%s"\n' % infile)
79 # Convert the input .dvi file to .dtl format.
84 dv2dt_call = 'dv2dt "%s"' % infile
85 dv2dt_pipe = subprocess.Popen(dv2dt_call, universal_newlines=True, \
86 stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, \
87 shell=unix, close_fds=unix)
88 (dv2dt_stdin, dv2dt_stdout, dv2dt_stderr) = \
89 (dv2dt_pipe.stdin, dv2dt_pipe.stdout, dv2dt_pipe.stderr)
92 dv2dt_data = dv2dt_stdout.read()
93 dv2dt_status = dv2dt_stdout.close()
95 if dv2dt_status != None or len(dv2dt_data) == 0:
96 dv2dt_err = dv2dt_stderr.read()
97 error("Failed: %s\n%s\n" % ( dv2dt_call, dv2dt_err) )
99 # Manipulate the .dtl file.
100 dtl_data = manipulated_dtl(dv2dt_data)
102 error("Failed to manipulate the dtl file")
104 # Convert this .dtl file back to .dvi format.
105 dt2dv_call = 'dt2dv -si "%s"' % outfile
106 dt2dv_stdin = os.popen(dt2dv_call, 'w')
107 dt2dv_stdin.write(dtl_data)
110 if __name__ == "__main__":