CGI scripts chdir into objavi root so as to access all those other parts
[objavi2.git] / htdocs / font-list.cgi.pdf
blob93a046d37b74ad4b672afaf067ccd3fc87dddb7e
1 #!/usr/bin/python
3 # Part of Objavi2, which turns html manuals into books.
4 # This python script generates lists of fonts in pdf and html form.
6 # Copyright (C) 2009 Douglas Bagnall
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License along
19 # with this program; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 import os, sys
23 os.chdir('..')
25 from objavi import config
26 from objavi.book_utils import log
27 import tempfile, re
28 import cgi
29 import hashlib
30 from subprocess import Popen, PIPE
32 def get_font_list():
33     p = Popen(['fc-list'], stdout=PIPE, stderr=PIPE)
34     out, err = p.communicate()
35     fonts = set(re.findall(r"^([^:,]+)", out.strip().replace('\-', '-'), re.M))
36     return sorted(fonts, key=str.lower)
39 def font_html(fonts, example_template, dir="LTR"):
40     html = ['<html dir="%s"><meta http-equiv="content-type" content="text/html; charset=utf-8"> <style>'
41             '.font-name {background: #ffc; padding: 0.25em; font-family: "Dejavu Sans", sans-serif}'
42             ' div{padding:0.9em 0}'
43             '</style><body>' % dir]
44     for f in fonts:
45         html.append(example_template % {'font':f})
46     html.append('</body></html>')
47     return '\n'.join(html)
49 def font_pdf(html, pdfname):
50     fh, htmlname = tempfile.mkstemp(suffix='.html', dir=config.TMPDIR)
51     os.write(fh, html)
52     os.close(fh)
53     cmd = ['xvfb-run', config.WKHTMLTOPDF, '-q', '-s', 'A4',
54            htmlname, pdfname]
56     p = Popen(cmd, stdout=PIPE, stderr=PIPE)
57     r = p.communicate()
58     log(r)
60 def html_font_list(fonts, name):
61     f = open(name, 'w')
62     for x in fonts:
63         f.write(x + '\n')
64     f.close()
66 #Instead of regenerating the pdf every time, which is expensive, keep
67 #a cached version indexed by the font list and script version
68 fonts = get_font_list()
69 h = hashlib.sha1(str(fonts))
71 #Any change to this file will effectively clear the cache.
72 f = open(__file__)
73 h.update(f.read())
74 f.close()
76 #XXX should also include the script example include in the hash
78 form = cgi.FieldStorage()
79 script = form.getfirst('script')
80 if script not in (x for x in os.listdir(config.FONT_EXAMPLE_SCRIPT_DIR) if x.isalnum()):
81     script = 'latin'
83 pdfname = os.path.join(config.BOOK_LIST_CACHE_DIR, 'font-list-%s-%s.pdf' %(script, h.hexdigest()))
85 if not os.path.exists(pdfname):
86     #So this particular font list has not been made before
87     f = open(os.path.join(config.FONT_EXAMPLE_SCRIPT_DIR, script))
88     example_template = f.read()
89     f.close()
90     dir = 'LTR'
91     if script in config.RTL_SCRIPTS:
92         dir = 'RTL'
93     html = font_html(fonts, example_template, dir)
94     font_pdf(html, pdfname)
96     include_name = os.path.join(config.BOOK_LIST_CACHE_DIR, 'font-list.inc')
97     html_font_list(fonts, include_name)
101 print "Content-type: application/pdf\n"
102 f = open(pdfname)
103 print f.read()
104 f.close()
105 sys.exit()