Skip to content

Instantly share code, notes, and snippets.

Created July 31, 2012 02:05
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save kroger/3212745 to your computer and use it in GitHub Desktop.
Custom Epub builder for Sphinx
import os
import shutil
from import EpubBuilder
# We subclass EpubBuilder and re-define build_epub to call
# clean_html_file_for_ibooks. This function replaces all instances of
# the SPAN tag to SAMP. It's uglish but it works. A better way would
# be to change sphinx.writers.html to emmit the SAMP tag in the first
# place, but it seems even more difficult to do.
def clean_html_file_for_ibooks(filename):
bakname = filename + ".bak"
shutil.move(filename, bakname)
destination = open(filename, 'w')
original = open(bakname, 'r')
for line in original:
txt = line.replace("<span", "<samp").replace("</span>", "</samp>")
class MyEpubBuilder(EpubBuilder):
name = "epub2"
def add_visible_links(self, doctree):
def build_epub(self, outdir, outname):'cleaning html files...')
for item in self.files:
if item.endswith("html"):
clean_html_file_for_ibooks(os.path.join(outdir, item))
super(MyEpubBuilder, self).build_epub(outdir, outname)
def setup(app):
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment