forked from standardebooks/tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-draft
executable file
·572 lines (437 loc) · 23.6 KB
/
create-draft
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
#!/usr/bin/env python3
import argparse
import os
import shutil
from subprocess import call
import requests
import git
import regex
import unicodedata
from ftfy import fix_text
import se
import se.formatting
import se.epub
from bs4 import BeautifulSoup
def get_word_widths(str, target_height):
words = []
for word in reversed(str.split()):
width = 0
for char in word:
# Convert accented characters to unaccented characters
char = regex.sub(r"\p{M}", "", unicodedata.normalize("NFKD", char))
width += (se.LEAGUE_SPARTAN_100_WIDTHS[char] * target_height / 100) + se.LEAGUE_SPARTAN_KERNING + se.LEAGUE_SPARTAN_AVERAGE_SPACING
width = width - se.LEAGUE_SPARTAN_KERNING - se.LEAGUE_SPARTAN_AVERAGE_SPACING
words.append({"word": word, "width": width})
return words
def calculate_image_lines(str, target_height, canvas_width):
words = get_word_widths(str, target_height)
lines = []
current_line = ""
current_width = 0
for word in words:
if current_width == 0:
current_width = word["width"]
else:
current_width = current_width + (se.LEAGUE_SPARTAN_100_WIDTHS[" "] * target_height / 100) + word["width"]
if current_width < canvas_width:
current_line = word["word"] + " " + current_line
else:
lines.append(current_line.strip())
current_line = word["word"]
current_width = word["width"]
lines.append(current_line.strip())
lines.reverse()
return lines
def generate_titlepage_svg(title, authors, contributors, title_string, templates_path):
"""
Generate a draft of the titlepage SVG.
The function tries to build the title with the widest line at the bottom, moving up.
We approximate a few values, like the width of a space, which are variable in the font.
title: The title
authors: an author, or an array of authors
contributors: a dict in the form of: {"contributor descriptor": "contributor name(s)" ... }
"""
svg = ""
canvas_width = se.TITLEPAGE_WIDTH - (se.TITLEPAGE_HORIZONTAL_PADDING * 2)
if not isinstance(authors, list):
authors = [authors]
# Read our template SVG to get some values before we begin
with open(os.path.join(templates_path, "titlepage.svg" ), "r", encoding="utf-8") as file:
svg = file.read()
# Remove the template text elements from the SVG source, we'll write out to it later
svg = regex.sub(r"\s*<text.+</svg>", "</svg>", svg, flags=regex.DOTALL).strip()
# Calculate the title lines
title_lines = calculate_image_lines(title.upper(), se.TITLEPAGE_TITLE_HEIGHT, canvas_width)
# Calculate the author lines
authors_lines = []
for author in authors:
authors_lines.append(calculate_image_lines(author.upper(), se.TITLEPAGE_AUTHOR_HEIGHT, canvas_width))
# Calculate the contributor lines
contributor_lines = []
for descriptor, contributor in contributors.items():
contributor_lines.append([descriptor, calculate_image_lines(contributor.upper(), se.TITLEPAGE_CONTRIBUTOR_HEIGHT, canvas_width)])
# Construct the output
text_elements = ""
y = se.TITLEPAGE_VERTICAL_PADDING
# Add the title
for line in title_lines:
y += se.TITLEPAGE_TITLE_HEIGHT
text_elements += "\t<text class=\"title\" x=\"700\" y=\"{:.0f}\">{}</text>\n".format(y, line)
y += se.TITLEPAGE_TITLE_MARGIN
y -= se.TITLEPAGE_TITLE_MARGIN
# Add the author(s)
y += se.TITLEPAGE_AUTHOR_SPACING
for author_lines in authors_lines:
for line in author_lines:
y += se.TITLEPAGE_AUTHOR_HEIGHT
text_elements += "\t<text class=\"author\" x=\"700\" y=\"{:.0f}\">{}</text>\n".format(y, line)
y += se.TITLEPAGE_AUTHOR_MARGIN
y -= se.TITLEPAGE_AUTHOR_MARGIN
# Add the contributor(s)
if contributor_lines:
y += se.TITLEPAGE_CONTRIBUTORS_SPACING
for contributor in contributor_lines:
y += se.TITLEPAGE_CONTRIBUTOR_DESCRIPTOR_HEIGHT
text_elements += "\t<text class=\"contributor-descriptor\" x=\"700\" y=\"{:.0f}\">{}</text>\n".format(y, contributor[0])
y += se.TITLEPAGE_CONTRIBUTOR_MARGIN
for person in contributor[1]:
y += se.TITLEPAGE_CONTRIBUTOR_HEIGHT
text_elements += "\t<text class=\"contributor\" x=\"700\" y=\"{:.0f}\">{}</text>\n".format(y, person)
y += se.TITLEPAGE_CONTRIBUTOR_MARGIN
y -= se.TITLEPAGE_CONTRIBUTOR_MARGIN
y += se.TITLEPAGE_CONTRIBUTOR_DESCRIPTOR_MARGIN
y -= se.TITLEPAGE_CONTRIBUTOR_DESCRIPTOR_MARGIN
else:
# Remove unused CSS
svg = regex.sub(r"\n\t\t\.contributor-descriptor{.+?}\n", "", svg, flags=regex.DOTALL)
svg = regex.sub(r"\n\t\t\.contributor{.+?}\n", "", svg, flags=regex.DOTALL)
y += se.TITLEPAGE_VERTICAL_PADDING
svg = svg.replace("</svg>", "\n" + text_elements + "</svg>\n").replace("TITLESTRING", title_string)
svg = regex.sub(r"viewBox=\".+?\"", "viewBox=\"0 0 1400 {:.0f}\"".format(y), svg)
return svg
def generate_cover_svg(title, authors, title_string, templates_path):
svg = ""
canvas_width = se.COVER_TITLE_BOX_WIDTH - (se.COVER_TITLE_BOX_PADDING * 2)
if not isinstance(authors, list):
authors = [authors]
# Read our template SVG to get some values before we begin
with open(os.path.join(templates_path, "cover.svg" ), "r", encoding="utf-8") as file:
svg = file.read()
# Remove the template text elements from the SVG source, we'll write out to it later
svg = regex.sub(r"\s*<text.+</svg>", "</svg>", svg, flags=regex.DOTALL).strip()
# Calculate the title lines
title_height = se.COVER_TITLE_HEIGHT
title_class = "title"
title_lines = calculate_image_lines(title.upper(), title_height, canvas_width)
if len(title_lines) > 2:
title_height = se.COVER_TITLE_SMALL_HEIGHT
title_class = "title-small"
title_lines = calculate_image_lines(title.upper(), title_height, canvas_width)
if len(title_lines) > 2:
title_height = se.COVER_TITLE_XSMALL_HEIGHT
title_class = "title-xsmall"
title_lines = calculate_image_lines(title.upper(), title_height, canvas_width)
# Calculate the author lines
authors_lines = []
for author in authors:
authors_lines.append(calculate_image_lines(author.upper(), se.COVER_AUTHOR_HEIGHT, canvas_width))
# Construct the output
text_elements = ""
y = se.COVER_TITLE_BOX_Y + \
+ ((se.COVER_TITLE_BOX_HEIGHT \
- ( (len(title_lines) * title_height) \
+ ( (len(title_lines) - 1) * se.COVER_TITLE_MARGIN ) \
+ se.COVER_AUTHOR_SPACING \
+ (len(authors_lines) * se.COVER_AUTHOR_HEIGHT) \
)) / 2)
# Add the title
for line in title_lines:
y += title_height
text_elements += "\t<text class=\"{}\" x=\"700\" y=\"{:.0f}\">{}</text>\n".format(title_class, y, line)
y += se.COVER_TITLE_MARGIN
y -= se.COVER_TITLE_MARGIN
# Add the author(s)
y += se.COVER_AUTHOR_SPACING
for author_lines in authors_lines:
for line in author_lines:
y += se.COVER_AUTHOR_HEIGHT
text_elements += "\t<text class=\"author\" x=\"700\" y=\"{:.0f}\">{}</text>\n".format(y, line)
y += se.COVER_AUTHOR_MARGIN
y -= se.COVER_AUTHOR_MARGIN
# Remove unused CSS
if title_class != "title":
svg = regex.sub(r"\n\n\t\t\.title\{.+?\}", "", svg, flags=regex.DOTALL)
if title_class != "title-small":
svg = regex.sub(r"\n\n\t\t\.title-small\{.+?\}", "", svg, flags=regex.DOTALL)
if title_class != "title-xsmall":
svg = regex.sub(r"\n\n\t\t\.title-xsmall\{.+?\}", "", svg, flags=regex.DOTALL)
svg = svg.replace("</svg>", "\n" + text_elements + "</svg>\n").replace("TITLESTRING", title_string)
return svg
def get_wikipedia_url(string, get_nacoaf_url=False):
# We try to get the Wikipedia URL by the subject by taking advantage of the fact that Wikipedia's special search will redirect you immediately
# if there's an article match. So if the search page tries to redirect us, we use that redirect link as the Wiki URL. If the search page
# returns HTTP 200, then we didn't find a direct match and return nothing.
try:
response = requests.get("https://en.wikipedia.org/wiki/Special:Search", params={"search": string, "go": "Go"}, allow_redirects=False)
except Exception as ex:
se.print_error("Couldn't contact Wikipedia. Error: {}".format(ex))
if response.status_code == 302:
nacoaf_url = None
wiki_url = response.headers["Location"]
if get_nacoaf_url:
try:
response = requests.get(wiki_url)
except Exception as ex:
se.print_error("Couldn't contact Wikipedia. Error: {}".format(ex))
for match in regex.findall(r"http://id\.loc\.gov/authorities/names/n[0-9]+", response.text):
nacoaf_url = match
return wiki_url, nacoaf_url
return None, None
def main():
parser = argparse.ArgumentParser(description="Create a skeleton of a new Standard Ebook in the current directory.")
parser.add_argument("-a", "--author", dest="author", required=True, help="the author of the ebook")
parser.add_argument("-t", "--title", dest="title", required=True, help="the title of the ebook")
parser.add_argument("-i", "--illustrator", dest="illustrator", help="the illustrator of the ebook")
parser.add_argument("-r", "--translator", dest="translator", help="the translator of the ebook")
parser.add_argument("-p", "--gutenberg-ebook-url", dest="pg_url", help="the URL of the Project Gutenberg ebook to download")
parser.add_argument("-s", "--create-se-repo", dest="create_se_repo", action="store_true", help="initialize a new repository on the Standard Ebook server; Standard Ebooks admin powers required")
parser.add_argument("-g", "--create-github-repo", dest="create_github_repo", action="store_true", help="initialize a new repository at the Standard Ebooks GitHub account; Standard Ebooks admin powers required; can only be used when --create-se-repo is specified")
parser.add_argument("-e", "--email", dest="email", help="use this email address as the main committer for the local Git repository")
args = parser.parse_args()
if args.create_github_repo and not args.create_se_repo:
se.print_error("--create-github-repo option specified, but --create-se-repo option not specified.")
exit(1)
if args.pg_url and not regex.match("^https?://www.gutenberg.org/ebooks/[0-9]+$", args.pg_url):
se.print_error("Project Gutenberg URL must look like: https://www.gutenberg.org/ebooks/<EBOOK-ID>")
exit(1)
# Put together some variables for later use
templates_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "templates")
identifier = se.formatting.make_url_safe(args.author) + "/" + se.formatting.make_url_safe(args.title)
title_string = args.title.replace("'", "’") + ", by " + args.author.replace("'", "’")
sorted_title = regex.sub(r"^(A|An|The) (.+)$", "\\2, \\1", args.title)
pg_producers = []
if args.translator:
identifier = identifier + "/" + se.formatting.make_url_safe(args.translator)
title_string = title_string + ". Translated by " + args.translator
if args.illustrator:
identifier = identifier + "/" + se.formatting.make_url_safe(args.illustrator)
title_string = title_string + ". Illustrated by " + args.illustrator
repo_name = identifier.replace("/", "_")
if os.path.isdir(repo_name):
se.print_error("./{}/ already exists.".format(repo_name))
exit(1)
# Download PG HTML and do some fixups
if args.pg_url:
args.pg_url = args.pg_url.replace("http://", "https://")
# Get the ebook metadata
try:
response = requests.get(args.pg_url)
pg_metadata_html = response.text
except Exception as ex:
se.print_error("Couldn't download Project Gutenberg ebook metadata page. Error: {}".format(ex))
exit(1)
soup = BeautifulSoup(pg_metadata_html, "lxml")
# Get the ebook HTML URL from the metadata
pg_ebook_url = None
for element in soup.select("a[type^=\"text/html\"]"):
pg_ebook_url = regex.sub("^//", "https://", element["href"])
if not pg_ebook_url:
se.print_error("Could download ebook metadata, but couldn't find URL for the ebook HTML.")
exit(1)
# Get the ebook LCSH categories
pg_subjects = []
for element in soup.select("td[property=\"dcterms:subject\"]"):
if element["datatype"] == "dcterms:LCSH":
for subject_link in element.find("a"):
pg_subjects.append(subject_link.strip())
# Get the PG publication date
pg_publication_year = None
for element in soup.select("td[itemprop=\"datePublished\"]"):
pg_publication_year = regex.sub(r".+?([0-9]{4})", "\\1", element.text)
# Get the actual ebook URL
try:
response = requests.get(pg_ebook_url)
pg_ebook_html = response.text
except Exception as ex:
se.print_error("Couldn't download Project Gutenberg ebook HTML. Error: {}".format(ex))
exit(1)
try:
fixed_pg_ebook_html = fix_text(pg_ebook_html)
pg_ebook_html = se.epub.strip_bom(fixed_pg_ebook_html)
except Exception as ex:
se.print_error("Couldn't determine text encoding of Project Gutenberg HTML file. Error: {}".format(ex))
exit(1)
# Try to guess the ebook language
pg_language = "en-US"
if "colour" in pg_ebook_html or "favour" in pg_ebook_html or "honour" in pg_ebook_html:
pg_language = "en-GB"
# Create necessary directories
os.makedirs(os.path.join(repo_name, "images"))
os.makedirs(os.path.join(repo_name, "src", "epub", "css"))
os.makedirs(os.path.join(repo_name, "src", "epub", "images"))
os.makedirs(os.path.join(repo_name, "src", "epub", "text"))
os.makedirs(os.path.join(repo_name, "src", "META-INF"))
# Write PG data if we have it
if args.pg_url and pg_ebook_html:
soup = BeautifulSoup(pg_ebook_html, "lxml")
# Try to get the PG producers. We only try this if there's a <pre> block with the header info (which is not always the case)
for element in soup(text=regex.compile(r"\*\*\*\s*Produced by.+$", flags=regex.DOTALL)):
if element.parent.name == "pre":
pg_producers = regex.sub(r".+?Produced by (.+?)\s*$", "\\1", element, flags=regex.DOTALL)
pg_producers = regex.sub(r"\(.+?\)", "", pg_producers, flags=regex.DOTALL)
pg_producers = regex.sub(r"(at )?https?://www\.pgdp\.net", "", pg_producers, flags=regex.DOTALL)
pg_producers = regex.sub(r"[\r\n]+", " ", pg_producers, flags=regex.DOTALL)
pg_producers = regex.sub(r",? and ", ", and ", pg_producers)
pg_producers = pg_producers.replace(" and the Online", " and The Online")
pg_producers = pg_producers.replace(", and ", ", ").strip().split(", ")
# Try to strip out the PG header
for element in soup(text=regex.compile(r"\*\*\*\s*START OF THIS")):
for sibling in element.parent.find_previous_siblings():
sibling.decompose()
element.parent.decompose()
# Try to strip out the PG license footer
for element in soup(text=regex.compile(r"End of (the )?Project Gutenberg")):
for sibling in element.parent.find_next_siblings():
sibling.decompose()
element.parent.decompose()
with open(os.path.join(repo_name, "src", "epub", "text", "body.xhtml"), "w") as file:
file.write(str(soup))
# Copy over templates
shutil.copy(os.path.normpath(templates_path + "/gitignore"), os.path.normpath(repo_name + "/.gitignore"))
shutil.copy(os.path.normpath(templates_path + "/LICENSE.md"), os.path.normpath(repo_name + "/"))
shutil.copy(os.path.normpath(templates_path + "/META-INF/container.xml"), os.path.normpath(repo_name + "/src/META-INF/"))
shutil.copy(os.path.normpath(templates_path + "/mimetype"), os.path.normpath(repo_name + "/src/"))
shutil.copy(os.path.normpath(templates_path + "/content.opf"), os.path.normpath(repo_name + "/src/epub/"))
shutil.copy(os.path.normpath(templates_path + "/onix.xml"), os.path.normpath(repo_name + "/src/epub/"))
shutil.copy(os.path.normpath(templates_path + "/toc.xhtml"), os.path.normpath(repo_name + "/src/epub/"))
shutil.copy(os.path.normpath(templates_path + "/core.css"), os.path.normpath(repo_name + "/src/epub/css/"))
shutil.copy(os.path.normpath(templates_path + "/local.css"), os.path.normpath(repo_name + "/src/epub/css/"))
shutil.copy(os.path.normpath(templates_path + "/logo.svg"), os.path.normpath(repo_name + "/src/epub/images/"))
shutil.copy(os.path.normpath(templates_path + "/colophon.xhtml"), os.path.normpath(repo_name + "/src/epub/text/"))
shutil.copy(os.path.normpath(templates_path + "/imprint.xhtml"), os.path.normpath(repo_name + "/src/epub/text/"))
shutil.copy(os.path.normpath(templates_path + "/titlepage.xhtml"), os.path.normpath(repo_name + "/src/epub/text/"))
shutil.copy(os.path.normpath(templates_path + "/uncopyright.xhtml"), os.path.normpath(repo_name + "/src/epub/text/"))
shutil.copy(os.path.normpath(templates_path + "/titlepage.svg"), os.path.normpath(repo_name + "/images/"))
shutil.copy(os.path.normpath(templates_path + "/cover.jpg"), os.path.normpath(repo_name + "/images/cover.jpg"))
shutil.copy(os.path.normpath(templates_path + "/cover.svg"), os.path.normpath(repo_name + "/images/cover.svg"))
# Try to find Wikipedia links if possible
author_wiki_url, author_nacoaf_url = get_wikipedia_url(args.author, True)
ebook_wiki_url, _ = get_wikipedia_url(args.title)
translator_wiki_url = None
if args.translator:
translator_wiki_url, translator_nacoaf_url = get_wikipedia_url(args.translator, True)
# Pre-fill a few templates
se.replace_in_file(os.path.normpath(repo_name + "/src/epub/text/titlepage.xhtml"), "TITLESTRING", title_string)
se.replace_in_file(os.path.normpath(repo_name + "/images/titlepage.svg"), "TITLESTRING", title_string)
se.replace_in_file(os.path.normpath(repo_name + "/images/cover.svg"), "TITLESTRING", title_string)
# Create the titlepage SVG
contributors = {}
if args.translator:
contributors["translated by"] = args.translator
if args.illustrator:
contributors["illustrated by"] = args.illustrator
with open(os.path.join(repo_name, "images", "titlepage.svg"), "w") as file:
file.write(generate_titlepage_svg(args.title, args.author, contributors, title_string, templates_path))
# Create the cover SVG
with open(os.path.join(repo_name, "images", "cover.svg"), "w") as file:
file.write(generate_cover_svg(args.title, args.author, title_string, templates_path))
if args.pg_url:
se.replace_in_file(os.path.normpath(repo_name + "/src/epub/text/imprint.xhtml"), "PGLINK", args.pg_url)
with open(os.path.join(repo_name, "src", "epub", "text", "colophon.xhtml"), "r+", encoding="utf-8") as file:
colophon_xhtml = file.read()
colophon_xhtml = colophon_xhtml.replace("SEIDENTIFIER", identifier)
colophon_xhtml = colophon_xhtml.replace(">AUTHOR<", ">{}<".format(args.author))
colophon_xhtml = colophon_xhtml.replace("TITLE", args.title)
if author_wiki_url:
colophon_xhtml = colophon_xhtml.replace("AUTHORWIKILINK", author_wiki_url)
if args.pg_url:
colophon_xhtml = colophon_xhtml.replace("PGLINK", args.pg_url)
if pg_publication_year:
colophon_xhtml = colophon_xhtml.replace("PG_YEAR", pg_publication_year)
if pg_producers:
producers_xhtml = ""
for i, producer in enumerate(pg_producers):
if "Distributed Proofreading" in producer:
producers_xhtml = producers_xhtml + "<a href=\"https://www.pgdp.net\">The Online Distributed Proofreading Team</a>"
else:
producers_xhtml = producers_xhtml + "<span class=\"name\">{}</span>".format(producer)
if i < len(pg_producers) - 1:
producers_xhtml = producers_xhtml + ", "
if i == len(pg_producers) - 2:
producers_xhtml = producers_xhtml + "and "
producers_xhtml = producers_xhtml + "<br/>"
colophon_xhtml = colophon_xhtml.replace("<span class=\"name\">TRANSCRIBER1</span>, <span class=\"name\">TRANSCRIBER2</span>, and <a href=\"https://www.pgdp.net\">The Online Distributed Proofreading Team</a><br/>", producers_xhtml)
file.seek(0)
file.write(colophon_xhtml)
file.truncate()
with open(os.path.join(repo_name, "src", "epub", "content.opf"), "r+", encoding="utf-8") as file:
metadata_xhtml = file.read()
metadata_xhtml = metadata_xhtml.replace("SEIDENTIFIER", identifier)
metadata_xhtml = metadata_xhtml.replace(">AUTHOR<", ">{}<".format(args.author))
metadata_xhtml = metadata_xhtml.replace(">TITLESORT<", ">{}<".format(sorted_title))
metadata_xhtml = metadata_xhtml.replace(">TITLE<", ">{}<".format(args.title))
metadata_xhtml = metadata_xhtml.replace("VCSIDENTIFIER", repo_name)
if pg_producers:
producers_xhtml = ""
i = 1
for producer in pg_producers:
producers_xhtml = producers_xhtml + "\t\t<dc:contributor id=\"transcriber-{}\">{}</dc:contributor>\n".format(i, producer)
if "Distributed Proofreading" in producer:
producers_xhtml = producers_xhtml + "\t\t<meta property=\"file-as\" refines=\"#transcriber-{}\">Online Distributed Proofreading Team, The</meta>\n\t\t<meta property=\"se:url.homepage\" refines=\"#transcriber-{}\">https://pgdp.net</meta>\n".format(i, i)
else:
producers_xhtml = producers_xhtml + "\t\t<meta property=\"file-as\" refines=\"#transcriber-{}\">TRANSCRIBERSORT</meta>\n".format(i)
producers_xhtml = producers_xhtml + "\t\t<meta property=\"role\" refines=\"#transcriber-{}\" scheme=\"marc:relators\">trc</meta>\n".format(i)
i = i + 1
metadata_xhtml = regex.sub(r"\t\t<dc:contributor id=\"transcriber-1\">TRANSCRIBER</dc:contributor>\s*<meta property=\"file-as\" refines=\"#transcriber-1\">TRANSCRIBERSORT</meta>\s*<meta property=\"se:url.homepage\" refines=\"#transcriber-1\">LINK</meta>\s*<meta property=\"role\" refines=\"#transcriber-1\" scheme=\"marc:relators\">trc</meta>", "\t\t" + producers_xhtml.strip(), metadata_xhtml, flags=regex.DOTALL)
if author_wiki_url:
metadata_xhtml = metadata_xhtml.replace(">AUTHORWIKILINK<", ">{}<".format(author_wiki_url))
if author_nacoaf_url:
metadata_xhtml = metadata_xhtml.replace(">AUTHORNACOAFLINK<", ">{}<".format(author_nacoaf_url))
if ebook_wiki_url:
metadata_xhtml = metadata_xhtml.replace(">EBOOKWIKILINK<", ">{}<".format(ebook_wiki_url))
if args.translator:
metadata_xhtml = metadata_xhtml.replace(">TRANSLATOR<", ">{}<".format(args.translator))
if translator_wiki_url:
metadata_xhtml = metadata_xhtml.replace(">TRANSLATORWIKILINK<", ">{}<".format(translator_wiki_url))
if translator_nacoaf_url:
metadata_xhtml = metadata_xhtml.replace(">TRANSLATORNACOAFLINK<", ">{}<".format(translator_nacoaf_url))
else:
metadata_xhtml = regex.sub(r"<dc:contributor id=\"translator\">.+?<dc:contributor id=\"artist\">", "<dc:contributor id=\"artist\">", metadata_xhtml, flags=regex.DOTALL)
if args.pg_url:
if pg_subjects:
subject_xhtml = ""
i = 1
for subject in pg_subjects:
subject_xhtml = subject_xhtml + "\t\t<dc:subject id=\"subject-{}\">{}</dc:subject>\n".format(i, subject)
i = i + 1
i = 1
for subject in pg_subjects:
subject_xhtml = subject_xhtml + "\t\t<meta property=\"meta-auth\" refines=\"#subject-{}\">{}</meta>\n".format(i, args.pg_url)
i = i + 1
metadata_xhtml = regex.sub(r"\t\t<dc:subject id=\"subject-1\">SUBJECT1</dc:subject>\s*<dc:subject id=\"subject-2\">SUBJECT2</dc:subject>\s*<meta property=\"meta-auth\" refines=\"#subject-1\">LOCLINK1</meta>\s*<meta property=\"meta-auth\" refines=\"#subject-2\">LOCLINK2</meta>", "\t\t" + subject_xhtml.strip(), metadata_xhtml)
metadata_xhtml = metadata_xhtml.replace("<dc:language>LANG</dc:language>", "<dc:language>{}</dc:language>".format(pg_language))
metadata_xhtml = metadata_xhtml.replace("<dc:source>LINK</dc:source>", "<dc:source>{}</dc:source>".format(args.pg_url))
file.seek(0)
file.write(metadata_xhtml)
file.truncate()
# Set up local git repo
repo = git.Repo.init(repo_name)
if args.email:
with repo.config_writer() as config:
config.set_value("user", "email", args.email)
# Set up remote git repos
if args.create_se_repo:
git_command = git.cmd.Git(repo_name)
git_command.remote("add", "origin", "standardebooks.org:/standardebooks.org/ebooks/{}.git".format(repo_name))
# Set git to automatically push to SE
git_command.config("branch.master.remote", "origin")
git_command.config("branch.master.merge", "refs/heads/master")
github_option = ""
if args.create_github_repo:
github_option = "--github"
return_code = call(["ssh", "standardebooks.org", "/standardebooks.org/scripts/init-se-repo --repo-name={} --title-string=\"{}\" {}".format(repo_name, title_string, github_option)])
if return_code != 0:
se.print_error("Failed to create repository on Standard Ebooks server: ssh returned code {}.".format(return_code))
exit(1)
if __name__ == "__main__":
main()