Permalink
Browse files

cleaned up repeated code to render a project file name

  • Loading branch information...
1 parent f97ce0c commit b55fb93cc767761a4a03c1377e3562748b727525 @bkidwell committed Nov 27, 2011
Showing with 24 additions and 19 deletions.
  1. +0 −1 TODO.md
  2. +1 −1 mdepub/__init__.py
  3. +5 −4 mdepub/actions/archive.py
  4. +9 −9 mdepub/actions/epub.py
  5. +4 −4 mdepub/actions/html.py
  6. +5 −0 mdepub/filename.py
View
1 TODO.md
@@ -1,7 +1,6 @@
# Todo
* mdepub.actions.epub: more metadata attributes
-* refactor a lot of stuff, especially filename handling (options['filename']...)
* add checks for error conditions (missing source file, etc.)
* add action to insert/update epub file in Calibre
* add action to extract epub file from Calibre
View
2 mdepub/__init__.py
@@ -2,6 +2,7 @@
import os
import yaml
from mdepub.argumentparser import ArgumentParser
+options = None
import filename
# actions:
@@ -14,7 +15,6 @@
log.debug("Working path: %s", project_path)
options_file = os.path.join(project_path, "options.yaml")
-options = None
if os.path.exists(options_file):
log.debug("Loading options.yaml")
options = yaml.load(open("options.yaml"))
View
9 mdepub/actions/archive.py
@@ -5,6 +5,7 @@
from mdepub import options
from mdepub import project_path
from zipfile import ZipFile
+from mdepub.filename import getFN
log = logging.getLogger('archive')
@@ -14,12 +15,12 @@ def run():
os.chdir(project_path)
skip = [
- "./%s.html" % options['filename'],
- "./%s.epub" % options['filename'],
- "./%s.zip" % options['filename']
+ "./" + getFN("html"),
+ "./" + getFN("epub"),
+ "./" + getFN("zip")
]
- with ZipFile("%s.zip" % options['filename'], 'w') as zip:
+ with ZipFile(getFN("zip"), 'w') as zip:
for root, dirs, files in os.walk("."):
for file in files:
fpath = os.path.join(root, file)
View
18 mdepub/actions/epub.py
@@ -7,6 +7,7 @@
from mdepub import arguments
from zipfile import ZipFile
from BeautifulSoup import BeautifulSoup
+from mdepub.filename import getFN
log = logging.getLogger('epub')
@@ -22,15 +23,15 @@ def run():
time = {}
for ext in ['md', 'css', 'html', 'zip']:
- f = "%s.%s" % (options['filename'], ext)
+ f = getFN(ext)
if os.path.exists(f):
time[ext] = os.path.getmtime(f)
else:
time[ext] = None
if time['html'] < time['md'] or time['html'] < time['css']:
mdepub.actions.html.run()
- time['html'] = os.path.getmtime("%s.html" % options['filename'])
+ time['html'] = os.path.getmtime(getFN("html"))
if time['zip'] < time['html']:
mdepub.actions.archive.run()
@@ -42,8 +43,8 @@ def run():
args = [
"ebook-convert",
- "\"%s.html\"" % options['filename'],
- "\"%s.epub\"" % options['filename'],
+ getFN("html"),
+ getFN("epub"),
"--authors=\"%s\"" % quote(options['authors']),
"--author-sort=\"%s\"" % quote(options['author sort']),
"--pubdate=\"%s\"" % options['publication date'],
@@ -80,17 +81,16 @@ def run():
# Run ebook-convert
shell.run(" ".join(args), shell=True)
- #shell.save_output(args, "%s.html" % options['filename'])
# Add source zip file to epub and update content.opf with correct uuid
metadata = None
- with ZipFile("%s.epub" % options['filename'], 'a') as zip:
+ with ZipFile(getFN("epub"), 'a') as zip:
if arguments.source:
- zip.write("%s.zip" % options['filename'], "META-INF/source.mdepub.zip")
+ zip.write(getFN("zip"), "META-INF/source.mdepub.zip")
metadata = zip.read("content.opf")
- shell.run(["zip", "-d", "%s.epub" % options['filename'], "content.opf"])
- with ZipFile("%s.epub" % options['filename'], 'a') as zip:
+ shell.run(["zip", "-d", getFN("epub"), "content.opf"])
+ with ZipFile(getFN("epub"), 'a') as zip:
#metadata = zip.read("content.opf")
soup = BeautifulSoup(metadata)
id = soup.find(id="uuid_id")
View
8 mdepub/actions/html.py
@@ -5,6 +5,7 @@
from mdepub import options
from mdepub import project_path
from BeautifulSoup import BeautifulSoup
+from mdepub.filename import getFN
log = logging.getLogger('html')
@@ -25,20 +26,19 @@ def run():
os.chdir(project_path)
- css_file = "%s.css" % options['filename']
+ css_file = getFN("css")
args = ["pandoc", "--standalone", "--email-obfuscation=none"]
if os.path.exists(css_file):
args.extend(["-c", css_file])
if options['smart quotes']:
args.append("--smart")
- args.append("%s.md" % options['filename'])
+ args.append(getFN("md"))
- #shell.save_output(args, "%s.html" % options['filename'])
html = shell.pipe(args, None)
checkForBadLinks(html)
- with file("%s.html" % options['filename'], 'w') as f:
+ with file(getFN("html"), 'w') as f:
f.write(html)
View
5 mdepub/filename.py
@@ -1,8 +1,13 @@
import unicodedata
import string
+import mdepub
validFilenameChars = "'-_.() %s%s" % (string.ascii_letters, string.digits)
def clean(filename):
cleanedFilename = unicodedata.normalize('NFKD', unicode(filename)).encode('ASCII', 'ignore')
return ''.join(c for c in cleanedFilename if c in validFilenameChars)
+
+def getFN(ext):
+ """Get the project filename with the given extension (.md, .htm, etc.)"""
+ return "%s.%s" % (mdepub.options['filename'], ext)

0 comments on commit b55fb93

Please sign in to comment.