forked from standardebooks/tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepare-release
executable file
·105 lines (80 loc) · 5.04 KB
/
prepare-release
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env python3
import argparse
import os
import sys
import subprocess
import datetime
import regex
import se
def main():
parser = argparse.ArgumentParser(description="Calculate work word count, insert release date if not yet set, and update modified date and revision number.")
parser.add_argument("-v", "--verbose", action="store_true", help="increase output verbosity")
parser.add_argument("-w", "--no-word-count", dest="word_count", action="store_false", help="don't calculate word count")
parser.add_argument("-r", "--no-revision", dest="revision", action="store_false", help="don't increment the revision number")
parser.add_argument("directories", metavar="DIRECTORY", nargs="+", help="a Standard Ebooks source directory")
args = parser.parse_args()
script_path = os.path.realpath(__file__)
word_count_path = os.path.join(os.path.dirname(script_path), "word-count")
reading_ease_path = os.path.join(os.path.dirname(script_path), "reading-ease")
ordinal_path = os.path.join(os.path.dirname(script_path), "ordinal")
timestamp = datetime.datetime.utcnow()
iso_timestamp = regex.sub(r"\.[0-9]+$", "", timestamp.isoformat()) + "Z"
# Construct the friendly timestamp
friendly_timestamp = "{0:%B %e, %Y, %l:%M <abbr class=\"time eoc\">%p</abbr>}".format(timestamp)
friendly_timestamp = regex.sub(r"\s+", " ", friendly_timestamp).replace("AM", "a.m.").replace("PM", "p.m.")
for directory in args.directories:
directory = os.path.abspath(directory)
if not os.path.isdir(directory):
se.print_error("Not a directory: {}".format(directory))
exit(1)
if args.verbose:
print("Processing {} ...".format(directory))
with open(os.path.join(directory, "src", "epub", "content.opf"), "r+", encoding="utf-8") as file:
xhtml = file.read()
processed_xhtml = xhtml
if args.word_count:
if args.verbose:
print("\tUpdating word count and reading ease ...", end="", flush=True)
word_count = subprocess.run([word_count_path, "-x", directory], stdout=subprocess.PIPE).stdout.decode().strip()
reading_ease = subprocess.run([reading_ease_path, directory], stdout=subprocess.PIPE).stdout.decode().strip()
processed_xhtml = regex.sub(r"<meta property=\"se:word-count\">[^<]*</meta>", "<meta property=\"se:word-count\">{}</meta>".format(word_count), processed_xhtml)
processed_xhtml = regex.sub(r"<meta property=\"se:reading-ease\.flesch\">[^<]*</meta>", "<meta property=\"se:reading-ease.flesch\">{}</meta>".format(reading_ease), processed_xhtml)
if args.verbose:
print(" OK")
if args.revision:
if args.verbose:
print("\tUpdating revision number ...", end="", flush=True)
# Calculate the new revision number
revision = int(regex.search(r"<meta property=\"se:revision-number\">([0-9]+)</meta>", processed_xhtml).group(1))
revision = revision + 1
# If this is an initial release, set the release date in content.opf
if revision == 1:
processed_xhtml = regex.sub(r"<dc:date>[^<]+?</dc:date>", "<dc:date>{}</dc:date>".format(iso_timestamp), processed_xhtml)
# Set modified date and revision number in content.opf
processed_xhtml = regex.sub(r"<meta property=\"dcterms:modified\">[^<]+?</meta>", "<meta property=\"dcterms:modified\">{}</meta>".format(iso_timestamp), processed_xhtml)
processed_xhtml = regex.sub(r"<meta property=\"se:revision-number\">[^<]+?</meta>", "<meta property=\"se:revision-number\">{}</meta>".format(revision), processed_xhtml)
# Update the colophon with release info
with open(os.path.join(directory, "src", "epub", "text", "colophon.xhtml"), "r+", encoding="utf-8") as colophon:
colophon_xhtml = colophon.read()
# Are we moving from the first edition to the nth edition?
if revision == 1:
colophon_xhtml = regex.sub(r"<span class=\"release-date\">.+?</span>", "<span class=\"release-date\">{}</span>".format(friendly_timestamp), colophon_xhtml)
else:
ordinal = subprocess.run([ordinal_path, str(revision)], stdout=subprocess.PIPE).stdout.decode().strip()
if "<p>This is the first edition of this ebook.<br/>" in colophon_xhtml:
colophon_xhtml = colophon_xhtml.replace("This edition was released on<br/>", "The first edition was released on<br/>")
colophon_xhtml = colophon_xhtml.replace("<p>This is the first edition of this ebook.<br/>", "<p>This is the <span class=\"revision-number\">{}</span> edition of this ebook.<br/>\n\t\t\tThis edition was released on<br/>\n\t\t\t<span class=\"revision-date\">{}</span><br/>".format(ordinal, friendly_timestamp))
else:
colophon_xhtml = regex.sub(r"<span class=\"revision-date\">.+?</span>", "<span class=\"revision-date\">{}</span>".format(friendly_timestamp), colophon_xhtml)
colophon_xhtml = regex.sub(r"<span class=\"revision-number\">[^<]+?</span>", "<span class=\"revision-number\">{}</span>".format(ordinal), colophon_xhtml)
colophon.seek(0)
colophon.write(colophon_xhtml)
colophon.truncate()
if args.verbose:
print(" OK")
if processed_xhtml != xhtml:
file.seek(0)
file.write(processed_xhtml)
file.truncate()
if __name__ == "__main__":
main()