Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Start refactoring generic "build a book" stuff out

  • Loading branch information...
commit f35f7b767347f9f0743355103b49058147eec4b5 1 parent f014ed5
Tom Parker authored September 30, 2012

Showing 2 changed files with 67 additions and 42 deletions. Show diff stats Hide diff stats

  1. 52  books.py
  2. 57  common.py
52  books.py
@@ -3,13 +3,12 @@
3 3
 from google.protobuf import text_format
4 4
 from blog_pb2 import All
5 5
 from re import compile, DOTALL, MULTILINE
6  
-from os import mkdir, system
7  
-from os.path import exists, join
8  
-from hashlib import md5
  6
+from os import  system
  7
+from os.path import exists
9 8
 from codecs import open
10  
-from sys import argv
11 9
 from urlparse import urljoin
12 10
 from optparse import OptionParser
  11
+from common import generatePage, tocStart, tocEnd
13 12
 
14 13
 c = Cache()
15 14
 
@@ -51,16 +50,7 @@
51 50
 	index = 1
52 51
 	while page!=None:
53 52
 		folder = "%s #%02d"%(s.description, index)
54  
-		if not exists(folder):
55  
-			mkdir(folder)
56  
-		toc = open(join(folder, "toc.html"), "wb", "utf-8")
57  
-		toc.write("""<html xmlns="http://www.w3.org/1999/xhtml">
58  
-	<head>
59  
-		<title>%s</title>
60  
-	</head>
61  
-	<body class="vcenter">
62  
-		<div style="display:none">
63  
-""" % folder)
  53
+		toc = tocStart()
64 54
 		titlePattern = compile(s.titlePattern, DOTALL | MULTILINE)
65 55
 		contentPattern = compile(s.contentPattern, DOTALL | MULTILINE)
66 56
 		nextPattern = compile(s.nextPattern, DOTALL | MULTILINE)
@@ -82,43 +72,21 @@
82 72
 					age = 3600
83 73
 				else:
84 74
 					break
85  
-			fname = md5(page).hexdigest() + ".html"
86  
-			fpath = join(folder, fname)
87  
-
88 75
 			title = titlePattern.search(data)
89 76
 			assert title != None, page
90 77
 			title = title.groups()[0]
91  
-
92  
-			toc.write("\t\t\t<a title=\"%s\" href=\"%s\" />\n"%(title, fname))
93  
-			if not exists(fpath):
94  
-				newitems = True
95  
-				content = contentPattern.search(data)
96  
-				assert content != None, page
97  
-				content = content.groups()[0]
98  
-
99  
-				open (fpath, "wb", "utf-8").write(u"""<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
100  
-		<head>
101  
-			<style type="text/css" title="override_css">
102  
-				@page {padding: 0pt; margin:0pt}
103  
-			</style>
104  
-			<title>%s</title>
105  
-		</head>
106  
-		<body>
107  
-			<h1>%s</h1>
108  
-			%s
109  
-		</body>
110  
-	</html>"""%(title, title, content))
111  
-			if link != None:
  78
+			content = contentPattern.search(data)
  79
+			assert content != None, page
  80
+			content = content.groups()[0]
  81
+			newitems = generatePage(title, content, folder, toc) or newitems
  82
+			if link is not None:
112 83
 				link = link.groups()[0]
113 84
 			newpage = urljoin(page, link)
114 85
 			if page == None or newpage == page:
115 86
 				page = None
116 87
 				break
117 88
 			page = newpage
118  
-		toc.write("""\t\t</div>
119  
-	</body>
120  
-</html>""")
121  
-		toc.close()
  89
+		tocEnd(toc)
122 90
 		if newitems or not exists(folder + ".mobi"):
123 91
 			cmd = "rm -f book.zip && zip -j book.zip %s/* && ebook-convert book.zip \"%s.mobi\" --output-profile kindle --margin-top 0 --margin-bottom 0 --margin-left 0 --authors=\"%s\" --input-encoding=utf-8" %(folder.replace(" ", "\\ "), folder, s.author)
124 92
 			print cmd
57  common.py
... ...
@@ -0,0 +1,57 @@
  1
+from codecs import open
  2
+from os import mkdir
  3
+from os.path import join, exists
  4
+
  5
+try:
  6
+	import hashlib
  7
+except ImportError: # python < 2.5
  8
+	import md5
  9
+	hashlib = None
  10
+
  11
+def hexdigest_md5(data):
  12
+	if hashlib:
  13
+		return hashlib.md5(data).hexdigest()
  14
+	else:
  15
+		return md5.new(data).hexdigest()
  16
+
  17
+def generatePage(page, title, content, folder, toc):
  18
+	fname = hexdigest_md5(page) + ".html"
  19
+	fpath = join(folder, fname)
  20
+	toc.write("\t\t\t<a title=\"%s\" href=\"%s\" />\n" % (title, fname))
  21
+	if not exists(fpath):
  22
+		open(fpath, "wb", "utf-8").write(u"""<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  23
+		<head>
  24
+			<style type="text/css" title="override_css">
  25
+				@page {padding: 0pt; margin:0pt}
  26
+			</style>
  27
+			<title>%s</title>
  28
+		</head>
  29
+		<body>
  30
+			<h1>%s</h1>
  31
+			%s
  32
+		</body>
  33
+	</html>""" % (title, title, content))
  34
+		return True
  35
+	else:
  36
+		return False
  37
+
  38
+
  39
+def tocStart(folder):
  40
+	if not exists(folder):
  41
+		mkdir(folder)
  42
+	toc = open(join(folder, "toc.html"), "wb", "utf-8")
  43
+	toc.write("""<html xmlns="http://www.w3.org/1999/xhtml">
  44
+	<head>
  45
+		<title>%s</title>
  46
+	</head>
  47
+	<body class="vcenter">
  48
+		<div style="display:none">
  49
+""" % folder)
  50
+	return toc
  51
+
  52
+
  53
+def tocEnd(toc):
  54
+	toc.write("""\t\t</div>
  55
+	</body>
  56
+</html>""")
  57
+	toc.close()

0 notes on commit f35f7b7

Please sign in to comment.
Something went wrong with that request. Please try again.