|
1 # vim: set filetype=python : |
|
2 # arch-tag: general-purpose SCons build file for sgml-common |
|
3 |
|
4 from glob import glob |
|
5 import os, re |
|
6 |
|
7 ############################################################ |
|
8 # Setup |
|
9 ############################################################ |
|
10 |
|
11 SConsignFile('.sconsign-master') |
|
12 #Import('env') |
|
13 d = env.Dictionary() |
|
14 if not 'JADE' in d: |
|
15 d['JADE'] = 'jade' |
|
16 if not 'INDEXNODE' in d: |
|
17 d['INDEXNODE'] = 'ch.index' |
|
18 if not 'GTKIMGPATH' in d: |
|
19 d['GTKIMGPATH'] = '/usr/share/gtk-doc/data' |
|
20 if not 'PS2EPSI' in d: |
|
21 d['PS2EPSI'] = '../sgml-common/ps2epsi' |
|
22 |
|
23 def removeindex(l): |
|
24 while 'index/index.sgml' in l: |
|
25 l.remove('index/index.sgml') |
|
26 |
|
27 master = d['MASTERBASE'] |
|
28 mastersgml = master + '.sgml' |
|
29 sources = [mastersgml] + glob('*/*.sgml') + glob('*/*/*.sgml') |
|
30 removeindex(sources) |
|
31 db2htmlcmd = 'docbook-2-html -D $JADE ${HTMLARGS} ${SOURCE}' |
|
32 db2htmlindexcmd = 'docbook-2-html -D $JADE -O -V -O html-index ${HTMLARGS} ${SOURCE}' |
|
33 |
|
34 ################################################## |
|
35 # SCANNERS |
|
36 ################################################## |
|
37 def recursescan(scanner, node, env): |
|
38 result = scanner(node, env) |
|
39 retval = [] |
|
40 for item in result: |
|
41 retval.append(item) |
|
42 retval.extend(recursescan(scanner, item, env)) |
|
43 return retval |
|
44 |
|
45 SGML_includes_re = re.compile(r'<!ENTITY[^>]+SYSTEM[^>]+"(.+)"', re.M) |
|
46 def SGML_includes_scan(node, env, path): |
|
47 ret = SGML_includes_re.findall(node.get_contents()) |
|
48 removeindex(ret) |
|
49 return ret |
|
50 |
|
51 SGML_includes_scanner = Scanner(name = 'SGML_includes', |
|
52 function = SGML_includes_scan, recursive = 1, skeys = ['.sgml', '.ent']) |
|
53 |
|
54 SGML_image_pdf_re = re.compile(r'<(graphic|imagedata).+?fileref="([^"]+\.pdf)"', re.S) |
|
55 SGML_image_png_re = re.compile(r'<(graphic|imagedata).+?fileref="([^"]+\.png)"', re.S) |
|
56 def SGML_image_scanner(node, env, path, arg): |
|
57 root, ext = os.path.splitext(str(node)) |
|
58 contents = node.get_contents() |
|
59 return SGML_includes_scan(node, env, path) + \ |
|
60 [os.getcwd() + '/' + x[1] for x in arg.findall(contents)] |
|
61 |
|
62 SGML_pdf_scanner = Scanner(name = 'SGML_pdf', |
|
63 function = SGML_image_scanner, argument = SGML_image_pdf_re, |
|
64 recursive = 1) |
|
65 SGML_png_scanner = Scanner(name = 'SGML_png', |
|
66 function = SGML_image_scanner, argument = SGML_image_png_re, |
|
67 recursive = 1) |
|
68 |
|
69 ################################################## |
|
70 # BUILDERS |
|
71 ################################################## |
|
72 |
|
73 #### PLAIN TEXT |
|
74 Btxt = Builder(action="docbook2txt $SOURCE", src_suffix='.sgml', suffix='.txt') |
|
75 |
|
76 #### PDF / POSTSCRIPT |
|
77 Bpdf = Builder(action="docbook-2-pdf -D ${JADE} -q -O -V -O paper-size=Letter ${PDFARGS} ${SOURCE}", |
|
78 src_suffix='.sgml', suffix='.pdf') |
|
79 Bpdf2ps = Builder(action="pdftops ${SOURCE}", src_suffix='.pdf', suffix='.ps') |
|
80 |
|
81 #### MAN PAGES |
|
82 # FIXME: test this |
|
83 Bman = Builder(action="docbook2man $SOURCE", src_suffix='.sgml', suffix='.1') |
|
84 |
|
85 #### HTML |
|
86 Bhtml = Builder(action = [ \ |
|
87 'if test -d ${TARGET.dir} ; then rm -r ${TARGET.dir} ; fi', |
|
88 'mkdir ${TARGET.dir}', |
|
89 db2htmlcmd, |
|
90 'mv ${MASTERBASE}-html/* ${TARGET.dir}/', |
|
91 'rmdir ${MASTERBASE}-html', |
|
92 'ln -s ${TOPNODE}.html ${TARGET.dir}/index.html', |
|
93 'cp ${GTKIMGPATH}/*.png ${TARGET.dir}/']) |
|
94 |
|
95 #### PNG |
|
96 Bepip2png = Builder(action = 'gs -q -dTextAlphaBits=4 -dGraphicsAlphaBits=4 ' +\ |
|
97 '-r90 -dBATCH -dNOPAUSE -dSAFER -sOutputFile=$TARGET ' + \ |
|
98 '-sDEVICE=png16m $SOURCE -c showpage', suffix='.png', src_suffix='.pngepi') |
|
99 |
|
100 #### EPI from PS |
|
101 def getpagenumfromname(target, source, env, for_signature): |
|
102 return re.search('^.*_(\d+)\.(png){0,1}epi$', str(target[0])).group(1) |
|
103 d['GETPAGE'] = getpagenumfromname |
|
104 |
|
105 Aps2epi = Action(['psselect -q ${GETPAGE} $SOURCE temp.ps', |
|
106 'psresize -w ${WIDTH} -h ${HEIGHT} temp.ps temp2.ps', |
|
107 '$PS2EPSI temp2.ps $TARGET', |
|
108 'rm temp.ps temp2.ps']) |
|
109 Bps2epi = Builder(action=Aps2epi, src_suffix='.ps', suffix='.epi') |
|
110 Bps2epip = Builder(action=Aps2epi, src_suffix='.ps', suffix='.pngepi') |
|
111 Bepi2pdf = Builder(action="epstopdf -o=${TARGET} ${SOURCE}", suffix='.pdf', |
|
112 src_suffix='.epi') |
|
113 |
|
114 #### PLUCKER |
|
115 Bplucker = Builder(action = 'plucker-build --bpp=4 --compression=zlib ' + \ |
|
116 '--doc-name="${MASTERBASE}" -H file:${SOURCE.abspath} -M 5 ' + \ |
|
117 '--maxheight=320 --maxwidth=310 --staybelow=file:`pwd`/${SOURCE.dir} ' + \ |
|
118 '--title="${MASTERBASE}" -p . -f ${MASTERBASE}') |
|
119 |
|
120 ################################################## |
|
121 # General setup |
|
122 ################################################## |
|
123 |
|
124 env.Append(BUILDERS = {'Text': Btxt, 'PDF2PS': Bpdf2ps, 'PDF': Bpdf, 'HTML': Bhtml, |
|
125 'Plucker': Bplucker, 'PS2EPI': Bps2epi, 'PS2EPIP': Bps2epip, |
|
126 'EPI2PDF': Bepi2pdf, 'EPIP2PNG': Bepip2png, 'MAN': Bman}) |
|
127 |
|
128 #### INDEX GENERATION |
|
129 if 'DOINDEX' in d: |
|
130 Bindex = Builder(action = ['if test -d ${TARGET.dir} ; then rm -r ${TARGET.dir} ; fi', |
|
131 "mkdir ${TARGET.dir}", |
|
132 "collateindex.pl -i $INDEXNODE -N -o $TARGET", |
|
133 db2htmlindexcmd, |
|
134 "mv ${MASTERBASE}-html/HTML.index ${TARGET.dir}/", |
|
135 "rm -r ${MASTERBASE}-html", |
|
136 "collateindex.pl -i $INDEXNODE -g -o $TARGET ${TARGET.dir}/HTML.index"]) |
|
137 env['BUILDERS']['Index'] = Bindex |
|
138 index = env.Index('index/index.sgml', mastersgml) |
|
139 env.Depends(index, sources) |
|
140 env.Clean(index, 'index') |
|
141 deps = sources + [index] |
|
142 else: |
|
143 deps = sources |
|
144 |
|
145 ################################################## |
|
146 # BUILD RULES |
|
147 ################################################### |
|
148 # Text |
|
149 text = env.Text(mastersgml) |
|
150 env.Depends(text, deps) |
|
151 env.Alias('text', text) |
|
152 |
|
153 # PDF |
|
154 pdfsgml = File(mastersgml) |
|
155 pdf = env.PDF(pdfsgml) |
|
156 figsindoc = [x for x in recursescan(SGML_pdf_scanner, pdfsgml, env) if str(x).endswith('.pdf')] |
|
157 epipdf = [] |
|
158 for file in figsindoc: |
|
159 pdfname = re.sub('_\d+\.pdf$', '.pdf', str(file)) |
|
160 if pdfname == str(file): |
|
161 # This is not a filename that fits our pattern; add unmodified. |
|
162 epipdf.append(file) |
|
163 continue |
|
164 psfile = env.PDF2PS(source = pdfname) |
|
165 epifile = env.PS2EPI(str(file).replace(".pdf", ".epi"), psfile, |
|
166 WIDTH='6.375in', HEIGHT='8.25in') |
|
167 epipdf.append(env.EPI2PDF(source = epifile)) |
|
168 |
|
169 env.Depends(pdf, deps) |
|
170 env.Depends(pdf, epipdf) |
|
171 env.Alias('pdf', pdf) |
|
172 env.Clean(pdf, ['jadetex.cfg', '${MASTERBASE}.aux', '${MASTERBASE}.dvi', |
|
173 '${MASTERBASE}.jtex', '${MASTERBASE}.log', '${MASTERBASE}.out', |
|
174 'jade-out.fot']) |
|
175 |
|
176 # PS |
|
177 ps = env.PDF2PS(source = pdf) |
|
178 env.Alias('ps', ps) |
|
179 |
|
180 # HTML |
|
181 htmlsgml = File(mastersgml) |
|
182 buildhtml = env.HTML('html/index.html', htmlsgml) |
|
183 figsindoc = [x for x in recursescan(SGML_png_scanner, htmlsgml, env) if str(x).endswith('.png')] |
|
184 epipng = [] |
|
185 for file in figsindoc: |
|
186 pdfname = re.sub('_\d+\.png$', '.pdf', str(file)) |
|
187 if pdfname == str(file): |
|
188 # This is not a filename that fits our pattern; add unmodified. |
|
189 epipng.append(file) |
|
190 continue |
|
191 psfile = env.PDF2PS(source = pdfname) |
|
192 epifile = env.PS2EPIP(str(file).replace(".png", ".pngepi"), psfile, |
|
193 WIDTH='8.5in', HEIGHT='11in') |
|
194 epipng.append(env.EPIP2PNG(source = epifile)) |
|
195 |
|
196 env.Depends(buildhtml, epipng) |
|
197 env.Depends(buildhtml, deps) |
|
198 pnginstalls = env.InstallAs(['html/' + str(x) for x in epipng], epipng) |
|
199 env.Depends(pnginstalls, buildhtml) |
|
200 html = env.Alias('html', buildhtml) |
|
201 html = env.Alias('html', pnginstalls) |
|
202 env.Clean(buildhtml, 'html') |
|
203 |
|
204 # Plucker |
|
205 plucker = env.Plucker(master + '.pdb', 'html/index.html') |
|
206 env.Alias('plucker', plucker) |
|
207 |
|
208 env.Default(html) |