Created
February 11, 2010 18:50
-
-
Save pmuellr/301803 to your computer and use it in GitHub Desktop.
convert the progit source to HTML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# convert the pro git book into HTML, | |
# suitable for reading in Dropbox on the iPhone | |
import os | |
import re | |
import sys | |
import base64 | |
import subprocess | |
iDir = None | |
htmlHeader = """ | |
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> | |
<title>%(title)s</title> | |
<style> | |
h1,h2 { | |
padding: 0.3em 0.3em 0.3em 1em; | |
border: 1 solid #000; | |
background: #EEE; | |
z-box-shadow: 0.3em 0.3em 0.3em 0em #DDD; | |
z-moz-box-shadow: 0.3em 0.3em 0.3em 0em #DDD; | |
z-webkit-box-shadow: 0.3em 0.3em 0.3em 0em #DDD; | |
border-radius: 0.5em; | |
-moz-border-radius: 0.5em; | |
-webkit-border-radius: 0.5em; | |
} | |
.page-break { | |
page-break-before: always; | |
} | |
li.toc { | |
font-size: 150%%; | |
margin-top: 0.5em; | |
margin-bottom: 0.5em; | |
} | |
pre, xmp { | |
border-color: #CBCBCB; | |
background: #FFF8ED; | |
color: #000000; | |
overflow: visible; | |
border-width: 1px; | |
border-style: dotted; | |
padding-top: 0.4em; | |
padding-bottom: 0.4em; | |
padding-left: 1.4em; | |
padding-right: 1.4em; | |
margin-left: 1.4em; | |
margin-right: 1.4em; | |
z-font-size: 80%%; | |
} | |
.caption { | |
margin-left: 2em; | |
font-style: italic; | |
} | |
@media only screen and (max-device-width: 480px) { | |
body { | |
font-size: 200%%; | |
} | |
} | |
@media only screen and (min-device-width: 481px) { | |
body { | |
font-size: 125%%; | |
} | |
} | |
</style> | |
</head> | |
<body> | |
<h1>%(title)s</h1> | |
""" | |
htmlTrailer = """ | |
</body> | |
</html> | |
""" | |
#---------------------------------------------------------------------------------------- | |
class File: | |
def __init__(self, fileName, chapName): | |
self.fileName = fileName | |
self.chapName = chapName.replace("-", " ") | |
self.chapId = chapName | |
self.content = "" | |
#---------------------------------------------------------------------------------------- | |
def gather_input_files(iDir): | |
result = [] | |
entries = os.listdir(iDir) | |
for entry in entries: | |
entryDir = os.path.join(iDir, entry) | |
srcFiles = os.listdir(entryDir) | |
if len(srcFiles) != 1: | |
print "The directory %s does not appear to be a progit source directory" % entryDir | |
fileName = os.path.join(entryDir, srcFiles[0]) | |
chapName = entry | |
file = File(fileName, chapName) | |
result.append(file) | |
return result | |
# <p>Insert 18333fig0101.png | |
#---------------------------------------------------------------------------------------- | |
def filter_images(content): | |
lines = content.split("\n") | |
pattern = re.compile(r'^<p>Insert (.*)\.png\s*$') | |
for lineNo, line in enumerate(lines): | |
match = pattern.match(line) | |
if not match: continue | |
imageName = "%s-tn.png" % match.group(1) | |
imageName = os.path.join(iDir, "../figures", imageName) | |
# print " found an image: %s" % imageName | |
imageFile = open(imageName, "r") | |
imageContent = imageFile.read() | |
imageFile.close() | |
imageBase64 = base64.b64encode(imageContent) | |
lines[lineNo] = "<p><img src='data:image/png;base64,%s'></p><p class='caption'>" % imageBase64 | |
return "\n".join(lines) | |
#---------------------------------------------------------------------------------------- | |
def generate_output_file(iFile): | |
iFileName = iFile.fileName | |
chapName = iFile.chapName | |
oFileName = chapName + ".html" | |
print "reading %s, writing %s" % (iFileName, oFileName) | |
content = subprocess.Popen(["Markdown.pl", iFileName], stdout=subprocess.PIPE).communicate()[0] | |
content = filter_images(content) | |
iFile.content = content | |
oFile = open(oFileName, "w") | |
title = "Pro Git - Chapter %s" % chapName | |
oFile.write(htmlHeader % {"title": title}) | |
oFile.write(content) | |
oFile.write(htmlTrailer) | |
oFile.close() | |
return content | |
#---------------------------------------------------------------------------------------- | |
if len(sys.argv) != 2: | |
print "Expecting directory name of progit files" | |
sys.exit() | |
iDir = sys.argv[1] | |
iFiles = gather_input_files(iDir) | |
for iFile in iFiles: | |
generate_output_file(iFile) | |
oFileName = "pro-git.html" | |
oFile = open(oFileName, "w") | |
oFile.write(htmlHeader % {"title": "Pro Git"}) | |
oFile.write("<h1>Contents</h1>") | |
oFile.write("<ul>") | |
for iFile in iFiles: | |
oFile.write("<li class='toc'><a href='#%s'>%s</a>" % (iFile.chapId, iFile.chapName)) | |
oFile.write("</ul>") | |
for iFile in iFiles: | |
oFile.write("<h1 class='page-break' id='%s'>%s</h1>\n%s" % (iFile.chapId, iFile.chapName, iFile.content)) | |
oFile.write(htmlTrailer) | |
oFile.close() | |
print "wrote %s" % oFileName |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment