diff --git a/.gitignore b/.gitignore index f5f65ee..1170462 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ # Ignore Credentials client_secret_*.json credentials.json +.~lock* # Byte-compiled / optimized / DLL files __pycache__/ @@ -106,4 +107,4 @@ venv.bak/ /site # mypy -.mypy_cache/ \ No newline at end of file +.mypy_cache/ diff --git a/CNAME b/CNAME new file mode 100644 index 0000000..36bfa08 --- /dev/null +++ b/CNAME @@ -0,0 +1 @@ +book.status.im diff --git a/book-of-status.odt b/book-of-status.odt index 4282897..b18002a 100644 Binary files a/book-of-status.odt and b/book-of-status.odt differ diff --git a/index.html b/index.html index 548d09e..93467e2 100644 --- a/index.html +++ b/index.html @@ -22,17 +22,11 @@ function onEditorCreated(err, e) { editor = e; - // editor.openDocumentFromUrl("book-of-status.odt"); - editor.openDocumentFromUrl("texts/Cover.odt"); + editor.openDocumentFromUrl("book-of-status.odt"); } editorOptions = { - modus: Wodo.MODUS_REVIEW, - // undoRedoEnabled: false, - // userData: { - // fullName: "Otto \"WebODF\" Annotator", - // color: "blue" - // } + modus: Wodo.MODUS_REVIEW }; Wodo.createTextEditor('container', editorOptions, onEditorCreated); diff --git a/main.py b/main.py index 7c1d584..20e1372 100644 --- a/main.py +++ b/main.py @@ -2,6 +2,7 @@ from __future__ import print_function import os, re, shutil, stat, io import git +from datetime import datetime, timezone # OAuth import httplib2 @@ -14,8 +15,10 @@ from apiclient.http import MediaIoBaseDownload # ODF import zipfile, xml.dom.minidom from odf.opendocument import OpenDocumentText, load +from odf import text, teletype from odf.element import Text from odf.text import P +from odf.style import Style, TextProperties, ParagraphProperties try: import argparse @@ -60,7 +63,8 @@ def remove_readonly(func, path, _): func(path) def merge(inputfile, textdoc): - # TODO replace + + inputtextdoc = load(inputfile) # Need to make a copy of the list because addElement unlinks from the original for meta in inputtextdoc.meta.childNodes[:]: @@ -86,16 +90,30 @@ def merge(inputfile, textdoc): for body in inputtextdoc.body.childNodes[:]: textdoc.body.addElement(body) + textdoc.Pictures = {**textdoc.Pictures, **inputtextdoc.Pictures} + return textdoc - textdoc.Pictures = inputtextdoc.Pictures +def replace_tokens(textdoc): + repo = git.Repo('.') + assert not repo.bare + LAST_COMMIT = repo.head.object.hexsha + GENERATED_TIME = datetime.now(timezone.utc).strftime("%Y.%m.%d %H:%M") + + texts = textdoc.getElementsByType(text.P) + s = len(texts) + for i in range(s): + tmp_text = teletype.extractText(texts[i]) + if '%DATETIME%' in tmp_text or '%LAST_GIT_COMMIT%' in tmp_text: + tmp_text = tmp_text.replace('%DATETIME%',GENERATED_TIME) + tmp_text = tmp_text.replace('%LAST_GIT_COMMIT%', LAST_COMMIT) + new_S = text.P() + new_S.setAttribute("stylename",texts[i].getAttribute("stylename")) + new_S.addText(tmp_text) + texts[i].parentNode.insertBefore(new_S,texts[i]) + texts[i].parentNode.removeChild(texts[i]) return textdoc def main(): - repo = git.Repo('.') - assert not repo.bare - sha = repo.head.object.hexsha - exit(sha) - credentials = get_credentials() http = credentials.authorize(httplib2.Http()) service = discovery.build('drive', 'v3', http=http) @@ -116,16 +134,34 @@ def main(): shutil.rmtree(TEXTS_DIR, onerror=remove_readonly) os.makedirs(TEXTS_DIR) - book_of_status = OpenDocumentText() - for item in items: + # Hacky but to prevent step iterator issue when mergning documents + doc_stream = get_document(service, items[0]['id']) + doc_file = os.path.join(TEXTS_DIR, "{0}.odt".format(items[0]['name'])) + with open(doc_file, 'wb') as out: + out.write(doc_stream.getvalue()) + book_of_status = load(doc_stream) + + # withbreak = Style(name="WithBreak", parentstylename="Standard", family="paragraph") + # withbreak.addElement(ParagraphProperties(breakbefore="page")) + # book_of_status.automaticstyles.addElement(withbreak) + # page_seperator = P(stylename=withbreak,text=u'') + + + for item in items[1:]: print('{0} ({1})'.format(item['name'], item['id'])) doc_stream = get_document(service, item['id']) doc_file = os.path.join(TEXTS_DIR, "{0}.odt".format(item['name'])) - with open(doc_file, 'wb') as out: out.write(doc_stream.getvalue()) + # TODO unclear if this is the best approach + # book_of_status.text.addElement(page_seperator) + book_of_status.text.addElement(text.SoftPageBreak()) book_of_status = merge(doc_stream, book_of_status) + + + # Doesn't work well + book_of_status = replace_tokens(book_of_status) book_of_status.save("book-of-status.odt") diff --git a/texts/Cover.odt b/texts/Cover.odt index d5ec7bc..d3166bf 100644 Binary files a/texts/Cover.odt and b/texts/Cover.odt differ diff --git a/texts/Front Matter.odt b/texts/Front Matter.odt new file mode 100644 index 0000000..042add3 Binary files /dev/null and b/texts/Front Matter.odt differ