-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathepub.py
executable file
·85 lines (82 loc) · 6.11 KB
/
epub.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env python
from bs4 import BeautifulSoup
import sys, os, fnmatch
from pygments import highlight
from pygments.lexers import *
from pygments.formatters import HtmlFormatter
from string import *
def findReplace(directory, find, replace, filePattern):
for path, dirs, files in os.walk(os.path.abspath(directory)):
for filename in fnmatch.filter(files, filePattern):
filepath = os.path.join(path, filename)
print filepath
with open(filepath) as f:
l = filepath.split('/')
name = ''
if(l[len(l) -2]) == 'build':
name = l[len(l) - 1]
s = f.read()
#s = s.replace(find, replace)
s = s.replace("index.html", "")
soup = BeautifulSoup(s, "lxml")
#
# for i in soup.find_all("table", attrs={"summary": "Navigation header"}):
# i.contents[0].contents[0].clear()
# if name == "index.html":
# link = BeautifulSoup("<a href=\"ix01.html\">Index</a>", "lxml")
# elif name =="ix01.html":
# link = BeautifulSoup("", "lxml")
# else:
# link = BeautifulSoup("<a href=\"../ix01.html\">Index</a>", "lxml")
# i.contents[0].contents[0].insert(0, link)
# if name == "index.html":
# link = BeautifulSoup("", "lxml")
# elif name == "ix01.html":
# link = BeautifulSoup("<a href=\"index.html\">Table of Contents</a>", "lxml")
# else:
# link = BeautifulSoup("<a href=\"../\">Table of Contents</a>", "lxml")
# i.contents[1].contents[1].insert(0, link)
# soup = BeautifulSoup(soup.renderContents(), "lxml")
# for j in soup.findAll("table", attrs={"summary": "Navigation footer"}):
# if name == "index.html":
# link = BeautifulSoup("<a href=\"ix01.html\">Index</a>", "lxml")
# elif name == "ix01.html":
# link = BeautifulSoup("", "lxml")
# else:
# link = BeautifulSoup("<a href=\"../ix01.html\">Index</a>", "lxml")
# j.contents[0].contents[1].insert(0, link)
# if name == "ix01.html":
# link = BeautifulSoup("<a href=\"index.html\">Table of Contents</a>", "lxml")
# if name == "index.html":
# link = BeautifulSoup("", "lxml")
# elif name == "ix01.html":
# link = BeautifulSoup("<a href=\"index.html\">Table of Contents</a>", "lxml")
# else:
# link = BeautifulSoup("<a href=\"../\">Table of Contents</a>", "lxml")
# j.contents[0].contents[1].insert(0, link)
# j.contents[1].contents[1].clear()
# j.contents[1].contents[1].insert(0, link)
# Now mathjax is part of html
# p = BeautifulSoup("<h3><a href='/'>Site Home</a></h3>", "lxml")#<p class='alert alert-info'>Please see <a href=\"http://caniuse.com/#feat=mathml\">http://caniuse.com/#feat=mathml</a> if your browser supports MathML because certain sections of this book rely on MathML. If your browser does not support MathML please install Firefox from <a href=\"https://www.mozilla.org\">Mozilla</a> because AFAIK Firefox supports MathML.</p>", "lxml")
# soup.body.insert(0, p)
# soup = BeautifulSoup(soup.renderContents(), "lxml")
# for i in soup.find_all("pre", "CommonLispLexer"):
# code = BeautifulSoup(highlight(i.string, CommonLispLexer(), HtmlFormatter()))
# i.string.replace_with(code)
# soup = BeautifulSoup(soup.renderContents(), "lxml")
for i in soup.find_all("pre", "CLexer"):
#print i.string
code = BeautifulSoup(highlight(i.string, CLexer(), HtmlFormatter()), "lxml")
i.string.replace_with(code)
soup = BeautifulSoup(soup.renderContents(), "lxml")
for i in soup.find_all("pre", "ALexer"):
code = BeautifulSoup(highlight(i.string, CObjdumpLexer(), HtmlFormatter()), "lxml")
i.string.replace_with(code)
soup = BeautifulSoup(soup.renderContents(), "lxml")
for i in soup.find_all("pre", "MakefileLexer"):
code = BeautifulSoup(highlight(i.string, MakefileLexer(), HtmlFormatter()), "lxml")
i.string.replace_with(code)
with open(filepath, "w") as f:
f.write(soup.encode(formatter='html'))
findReplace("OEBPS/", "mml:", "", "index.html")
findReplace("OEBPS/", "mml:", "", "ix01.html")