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.php
10 Full author contact details are available in the file CREDITS
11 or at http://www.lyx.org/about/credits.php
14 python clean_dvi.py infile.dvi outfile.dvi
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
22 1 translating the machine readable .dvi file to human
24 2 manipulating any references to external files
25 3 translating the .dtl file back to .dvi format.
27 It requires dv2dt and dt2dv from the DTL dviware package
28 http://www.ctan.org/tex-archive/dviware/dtl/
34 return 'Usage: %s in.dvi out.dvi\n' \
35 % os.path.basename(prog_name)
39 sys.stderr.write(message + '\n')
43 sys.stderr.write(message + '\n')
47 def manipulated_dtl(data):
48 psfile_re = re.compile(r'(special1 +)([0-9]+)( +\'PSfile=")(.*)(" llx=.*)')
50 lines = data.split('\n')
51 for i in range(len(lines)):
53 match = psfile_re.match(line)
57 file = file.replace('"', '')
58 # Don't forget to update the length of the string too...
59 strlen = int(match.group(2)) - (filelen - len(file))
61 lines[i] = '%s%d%s%s%s' \
62 % ( match.group(1), strlen, match.group(3),
63 file, match.group(5) )
65 return '\n'.join(lines)
69 # First establish that the expected information has
70 # been input on the command line and whether the
71 # required executables exist.
78 if not os.path.exists(infile):
79 error('Unable to read "%s"\n' % infile)
81 # Convert the input .dvi file to .dtl format.
82 dv2dt_call = 'dv2dt "%s"' % infile
83 dv2dt_stdin, dv2dt_stdout, dv2dt_stderr = \
84 os.popen3(dv2dt_call, 't')
87 dv2dt_data = dv2dt_stdout.read()
88 dv2dt_status = dv2dt_stdout.close()
90 if dv2dt_status != None or len(dv2dt_data) == 0:
91 dv2dt_err = dv2dt_stderr.read()
92 error("Failed: %s\n%s\n" % ( dv2dt_call, dv2dt_err) )
94 # Manipulate the .dtl file.
95 dtl_data = manipulated_dtl(dv2dt_data)
97 error("Failed to manipulate the dtl file")
99 # Convert this .dtl file back to .dvi format.
100 dt2dv_call = 'dt2dv -si "%s"' % outfile
101 dt2dv_stdin = os.popen(dt2dv_call, 'w')
102 dt2dv_stdin.write(dtl_data)
105 if __name__ == "__main__":