Permalink
Browse files

adding newid action, changed string interpolation operator to str.for…

…mat() call
  • Loading branch information...
1 parent ef67fe7 commit 9c00f524ab1ecb891608b864335e9998c48b3f1d @bkidwell committed Dec 4, 2011
Showing with 47 additions and 12 deletions.
  1. +9 −9 mdepub/actions/epub.py
  2. +1 −1 mdepub/actions/html.py
  3. +35 −0 mdepub/actions/newid.py
  4. +2 −2 mdepub/filename.py
View
@@ -50,7 +50,7 @@ def run():
"ebook-convert",
'"' + getFN("html") + '"',
'"' + getFN("epub") + '"',
- "--title=\"%s\"" % quote(options['title'])
+ "--title=\"{}\"".format(quote(options['title']))
]
for a, b in (
('author-sort', 'author sort'),
@@ -65,36 +65,36 @@ def run():
('series-index', 'series index'),
('title-sort', 'title sort'),
):
- if options.get(b): args.append("--%s=\"%s\"" % (a, options[b]))
+ if options.get(b): args.append("--{}=\"{}\"".format(a, quote(options[b])))
if options.get("tags"):
- args.append( "--tags=\"%s\"" % quote(','.join(options['tags'])) )
+ args.append( "--tags=\"{}\"".format(quote(','.join(options['tags']))) )
- args.append("--chapter=\"//h:h%s\"" % (options.get('chapter head level') or 2))
+ args.append("--chapter=\"//h:h{}\"".format(options.get('chapter head level') or 2))
tmp = []
for i in range(options.get('chapter head level') or 2):
if len(tmp) > 0: tmp.append("or")
- tmp.append("name()='h%s'" % str(i + 1))
+ tmp.append("name()='h{}'".format(str(i + 1)))
args.append(
- "--level1-toc=\"//*[%s]\"" % " ".join(tmp)
+ "--level1-toc=\"//*[{}]\"".format(" ".join(tmp))
)
for ext in ["png", "jpg", "svg"]:
- cover_filename = "cover.%s" % ext
+ cover_filename = "cover.{}".format(ext)
if os.path.exists(cover_filename):
break
else:
cover_filename = None
if cover_filename:
- args.append("--cover=%s" % cover_filename)
+ args.append("--cover=\"{}\"".format(cover_filename))
if not options.get('stretch cover image'):
args.append("--preserve-cover-aspect-ratio")
if options.get('margin'):
for i in ['top', 'left', 'right', 'bottom']:
if options['margin'].get(i) is not None:
- args.append("--margin-%s" % i)
+ args.append("--margin-{}".format(i))
args.append(str(options['margin'][i]))
# Run ebook-convert
View
@@ -19,7 +19,7 @@ def checkForBadLinks(html):
href = tag['href']
if href[:1] == u'#':
if href[1:] not in ids:
- log.warn("Internal link points to non-existent target '%s'" % href)
+ log.warn("Internal link points to non-existent target '{}'".format(href))
def run():
log.debug("run()")
View
@@ -0,0 +1,35 @@
+import logging
+import mdepub
+from mdepub import new_id
+from mdepub import project_path
+from mdepub import options
+import re
+import os
+
+# TODO: implement 'clean' action
+
+log = logging.getLogger('newid')
+
+r_uuid = re.compile(
+ r'^([ \t]*)uuid:([ \t]*)([\S]*)([ \t]*)$',
+ re.MULTILINE
+)
+
+def run():
+ log.debug("run()")
+ os.chdir(project_path)
+
+ print mdepub.options
+ if 'uuid' in options:
+ # replace existing uuid
+ with open("options.yaml", "r") as f: txt = f.read()
+ txt = r_uuid.sub(
+ r'\g<1>uuid:\g<2>' + str(new_id()) + r'\g<4>',
+ txt
+ )
+ print txt
+ with open("options.yaml", "w") as f: f.write(txt)
+ else:
+ # add new uuid field to end of file
+ with open("options.yaml", "a") as f:
+ f.write("\nuuid: {0}\n".format(new_id()))
View
@@ -2,12 +2,12 @@
import string
import mdepub
-validFilenameChars = "'-_.() %s%s" % (string.ascii_letters, string.digits)
+validFilenameChars = "'-_.() {}{}".format(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)
+ return "{}.{}".format(mdepub.options['filename'], ext)

0 comments on commit 9c00f52

Please sign in to comment.