Skip to content

Instantly share code, notes, and snippets.

@sergejx
Created October 30, 2010 16:07
Show Gist options
  • Save sergejx/655458 to your computer and use it in GitHub Desktop.
Save sergejx/655458 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import zipfile
from BeautifulSoup import BeautifulSoup, Tag # Better for HTML
from lxml import etree # Better for XML
CSS = """
@font-face {
font-family: 'Droid Serif', serif, sans-serif;
font-style: normal; font-weight: normal;
src: url(DroidSerif-Regular.ttf)
}
@font-face {
font-family: 'Droid Serif', serif, sans-serif;
font-style: normal; font-weight: bold;
src: url(DroidSerif-Bold.ttf)
}
@font-face {
font-family: 'Droid Serif', serif, sans-serif;
font-style: italic; font-weight: normal;
src: url(DroidSerif-Italic.ttf)
}
@font-face {
font-family: 'Droid Serif', serif, sans-serif;
font-style: italic; font-weight: bold;
src: url(DroidSerif-BoldItalic.ttf)
}
@font-face {
font-family: "DroidSansMono", monospace;
font-style: normal; font-weight: normal;
src: url(DroidSansMono.ttf)
}
@font-face {
font-family: "DroidSansMono", monospace;
font-style: italic; font-weight: normal;
src: url(DroidSansMono.ttf)
}
@font-face {
font-family: "DroidSansMono", monospace;
font-style: normal; font-weight: bold;
src: url(DroidSansMono.ttf)
}
@font-face {
font-family: "DroidSansMono", monospace;
font-style: italic; font-weight: bold;
src: url(DroidSansMono.ttf)
}
body { font-family: 'Droid Serif'; word-wrap: break-word; }
pre, code { font-family: 'DroidSansMono'; font-size: 85%; }
pre { white-space: pre-wrap; }
"""
FONTS = [
'DroidSerif-Regular.ttf',
'DroidSerif-Bold.ttf',
'DroidSerif-Italic.ttf',
'DroidSerif-BoldItalic.ttf',
'DroidSansMono.ttf',
]
FONTS_PATH = '/usr/share/fonts/google-droid/'
OPF = '{http://www.idpf.org/2007/opf}'
if len(sys.argv) != 2:
exit(1)
filename = sys.argv[1]
os.rename(filename, filename+'.bak')
old = zipfile.ZipFile(filename+'.bak')
new = zipfile.ZipFile(filename, 'w', zipfile.ZIP_DEFLATED)
for name in old.namelist():
if name == 'content.opf': ### Add items to manifest
doc = etree.parse(old.open(name))
manifest = doc.find(OPF+'manifest')
manifest.append(etree.Element(OPF+'item',
{'id': 'fonts', 'href': 'fonts.css', 'media-type': 'text/css'}))
for font in FONTS:
manifest.append(etree.Element(OPF+'item',
{'id': font[0:-4], 'href': font,
'media-type': 'application/octet-stream'}))
new.writestr(name,
etree.tostring(doc, xml_declaration=True, encoding='UTF-8'))
elif name.endswith('.html'): ### Add links to CSS
print name
html = BeautifulSoup(old.open(name))
head = html.find('head')
link = Tag(html, 'link')
link['rel'] = 'stylesheet'
link['type'] = 'text/css'
link['href'] = 'fonts.css'
head.insert(0, link)
# Fix common problems
for tr in html.findAll('tr'):
if tr.parent.name != 'table':
tr.name = 'div'
for td in html.findAll('td'):
if td.parent.name != 'tr':
td.name = 'div'
# Special fixes for Slovo
if str(html.head.title.string).find(' Slovo |') != -1:
# Clean up title
html.find('h1').extract()
h = html.find('h4')
h.string = h.string[0:-142]
# Remove sidebar
html.body.div.contents[3].extract()
new.writestr(name, str(html))
else: ### Copy everything else
info = old.getinfo(name)
new.writestr(info, old.open(info).read())
# Add files
new.writestr('fonts.css', CSS)
for font in FONTS:
new.write(FONTS_PATH + font, font)
old.close()
new.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment