mirror of
https://github.com/status-im/book.status.im.git
synced 2025-02-21 19:18:44 +00:00
Properly rename stylename and parentstylename on both automaticstyles and styles.
This commit is contained in:
parent
bc312f496a
commit
f0c4bd6694
Binary file not shown.
106
main.py
106
main.py
@ -15,7 +15,7 @@ from apiclient.http import MediaIoBaseDownload
|
|||||||
# ODF
|
# ODF
|
||||||
import zipfile, xml.dom.minidom
|
import zipfile, xml.dom.minidom
|
||||||
from odf.opendocument import OpenDocumentText, load
|
from odf.opendocument import OpenDocumentText, load
|
||||||
from odf import text, teletype
|
from odf import office, text, teletype
|
||||||
from odf.element import Text
|
from odf.element import Text
|
||||||
from odf.text import P
|
from odf.text import P
|
||||||
from odf.style import Style, TextProperties, ParagraphProperties
|
from odf.style import Style, TextProperties, ParagraphProperties
|
||||||
@ -27,7 +27,7 @@ except ImportError:
|
|||||||
flags = None
|
flags = None
|
||||||
|
|
||||||
SCOPES = 'https://www.googleapis.com/auth/drive.readonly'
|
SCOPES = 'https://www.googleapis.com/auth/drive.readonly'
|
||||||
CREDENTIALS='credentials.json'
|
CREDENTIALS = 'credentials.json'
|
||||||
APPLICATION_NAME = 'Book of Status'
|
APPLICATION_NAME = 'Book of Status'
|
||||||
TEXTS_DIR = './texts'
|
TEXTS_DIR = './texts'
|
||||||
DRIVE_ID = '0AEhafKkWf9UkUk9PVA'
|
DRIVE_ID = '0AEhafKkWf9UkUk9PVA'
|
||||||
@ -76,32 +76,56 @@ def print_e(el, indent=0):
|
|||||||
print_e(el, indent)
|
print_e(el, indent)
|
||||||
|
|
||||||
|
|
||||||
|
def rename_style(style, style_renaming, document_id):
|
||||||
|
if style.tagName == 'style:style':
|
||||||
|
# Rename style, and parent style.
|
||||||
|
for style_type in ['name', 'parentstylename']:
|
||||||
|
attr_name = style.getAttribute(style_type)
|
||||||
|
if attr_name:
|
||||||
|
new_attr_name = "%s_doc%s" % (attr_name, document_id)
|
||||||
|
style.setAttribute(style_type, new_attr_name)
|
||||||
|
style_renaming[attr_name] = new_attr_name
|
||||||
|
return style
|
||||||
|
|
||||||
|
|
||||||
|
def replace_style(el, style_renaming, document_id):
|
||||||
|
|
||||||
|
if el.attributes:
|
||||||
|
|
||||||
|
stylename = el.attributes.get(('urn:oasis:names:tc:opendocument:xmlns:text:1.0', 'style-name'))
|
||||||
|
if stylename and style_renaming.get(stylename):
|
||||||
|
print('Replacing style-name: %s with %s' % (stylename, style_renaming.get(stylename)))
|
||||||
|
el.attributes[('urn:oasis:names:tc:opendocument:xmlns:text:1.0', 'style-name')] = \
|
||||||
|
style_renaming[stylename]
|
||||||
|
|
||||||
|
parent_stylename = el.attributes.get(('urn:oasis:names:tc:opendocument:xmlns:text:1.0', 'parent-style-name'))
|
||||||
|
if parent_stylename and style_renaming.get(parent_stylename):
|
||||||
|
print('Replacing parent-style-name: %s with: %s' % (parent_stylename, style_renaming.get(parent_stylename)))
|
||||||
|
el.attributes[('urn:oasis:names:tc:opendocument:xmlns:text:1.0', 'parent-style-name')] = \
|
||||||
|
style_renaming[parent_stylename]
|
||||||
|
|
||||||
|
el.childNodes = [
|
||||||
|
replace_style(x, style_renaming, document_id) for x in el.childNodes[:]
|
||||||
|
]
|
||||||
|
|
||||||
|
return el
|
||||||
|
|
||||||
|
|
||||||
def merge(inputfile, textdoc, document_id):
|
def merge(inputfile, textdoc, document_id):
|
||||||
style_renaming = {}
|
style_renaming = {}
|
||||||
inputtextdoc = load(inputfile)
|
inputtextdoc = load(inputfile)
|
||||||
# print_e(inputtextdoc.automaticstyles)
|
|
||||||
# print('&&&&&&&&&&&')
|
|
||||||
# print_e(inputtextdoc.body)
|
|
||||||
# return
|
|
||||||
# import ipdb; ipdb.set_trace()
|
|
||||||
|
|
||||||
# Need to make a copy of the list because addElement unlinks from the original
|
# Need to make a copy of the list because addElement unlinks from the original
|
||||||
for meta in inputtextdoc.meta.childNodes[:]:
|
for meta in inputtextdoc.meta.childNodes[:]:
|
||||||
textdoc.meta.addElement(meta)
|
textdoc.meta.addElement(meta)
|
||||||
|
|
||||||
def rename_style(style):
|
|
||||||
attr_name = style.getAttribute('name')
|
|
||||||
new_attr_name = "%s_doc%s" % (attr_name, document_id)
|
|
||||||
print(attr_name, new_attr_name)
|
|
||||||
style.setAttribute('name', new_attr_name)
|
|
||||||
style_renaming[attr_name] = new_attr_name
|
|
||||||
return style
|
|
||||||
|
|
||||||
for style in inputtextdoc.styles.childNodes[:]:
|
|
||||||
textdoc.styles.addElement(style)
|
|
||||||
|
|
||||||
for autostyle in inputtextdoc.automaticstyles.childNodes[:]:
|
for autostyle in inputtextdoc.automaticstyles.childNodes[:]:
|
||||||
textdoc.automaticstyles.addElement(rename_style(autostyle))
|
s = rename_style(autostyle, style_renaming, document_id)
|
||||||
|
textdoc.automaticstyles.addElement(s)
|
||||||
|
|
||||||
|
for style in inputtextdoc.styles.childNodes[:]:
|
||||||
|
s = rename_style(style, style_renaming, document_id)
|
||||||
|
textdoc.styles.addElement(style)
|
||||||
|
|
||||||
for masterstyles in inputtextdoc.masterstyles.childNodes[:]:
|
for masterstyles in inputtextdoc.masterstyles.childNodes[:]:
|
||||||
textdoc.masterstyles.addElement(masterstyles)
|
textdoc.masterstyles.addElement(masterstyles)
|
||||||
@ -115,34 +139,12 @@ def merge(inputfile, textdoc, document_id):
|
|||||||
for settings in inputtextdoc.settings.childNodes[:]:
|
for settings in inputtextdoc.settings.childNodes[:]:
|
||||||
textdoc.settings.addElement(settings)
|
textdoc.settings.addElement(settings)
|
||||||
|
|
||||||
def replace_style(el):
|
|
||||||
|
|
||||||
if not el.attributes:
|
|
||||||
return el
|
|
||||||
# print(el.attributes)
|
|
||||||
stylename = el.attributes.get(('urn:oasis:names:tc:opendocument:xmlns:text:1.0', 'style-name'))
|
|
||||||
if stylename and style_renaming.get(stylename):
|
|
||||||
print('Replacing style-name: %s with %s' % (stylename, style_renaming.get(stylename)))
|
|
||||||
el.attributes[('urn:oasis:names:tc:opendocument:xmlns:text:1.0', 'style-name')] = \
|
|
||||||
style_renaming[stylename]
|
|
||||||
|
|
||||||
parent_stylename = el.attributes.get(('urn:oasis:names:tc:opendocument:xmlns:text:1.0', 'parent-style-name'))
|
|
||||||
if parent_stylename and style_renaming.get(parent_stylename):
|
|
||||||
print('Replacing parent-style-name: %s with: %s' % (parent_stylename, style_renaming.get(parent_stylename)))
|
|
||||||
el.attributes[('urn:oasis:names:tc:opendocument:xmlns:text:1.0', 'parent-style-name')] = \
|
|
||||||
style_renaming[parent_stylename]
|
|
||||||
|
|
||||||
el.childNodes = [
|
|
||||||
replace_style(x) for x in el.childNodes[:]
|
|
||||||
]
|
|
||||||
|
|
||||||
return el
|
|
||||||
|
|
||||||
for body in inputtextdoc.body.childNodes[:]:
|
for body in inputtextdoc.body.childNodes[:]:
|
||||||
b = replace_style(body)
|
b = replace_style(body, style_renaming, document_id)
|
||||||
textdoc.body.addElement(body)
|
textdoc.body.addElement(b)
|
||||||
|
|
||||||
|
textdoc.Pictures.update(inputtextdoc.Pictures)
|
||||||
|
|
||||||
textdoc.Pictures = {**textdoc.Pictures, **inputtextdoc.Pictures}
|
|
||||||
return textdoc
|
return textdoc
|
||||||
|
|
||||||
|
|
||||||
@ -157,12 +159,12 @@ def replace_tokens(textdoc):
|
|||||||
for i in range(s):
|
for i in range(s):
|
||||||
tmp_text = teletype.extractText(texts[i])
|
tmp_text = teletype.extractText(texts[i])
|
||||||
if '%DATETIME%' in tmp_text or '%LAST_GIT_COMMIT%' in tmp_text:
|
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('%DATETIME%', GENERATED_TIME)
|
||||||
tmp_text = tmp_text.replace('%LAST_GIT_COMMIT%', LAST_COMMIT)
|
tmp_text = tmp_text.replace('%LAST_GIT_COMMIT%', LAST_COMMIT)
|
||||||
new_S = text.P()
|
new_S = text.P()
|
||||||
new_S.setAttribute("stylename",texts[i].getAttribute("stylename"))
|
new_S.setAttribute("stylename", texts[i].getAttribute("stylename"))
|
||||||
new_S.addText(tmp_text)
|
new_S.addText(tmp_text)
|
||||||
texts[i].parentNode.insertBefore(new_S,texts[i])
|
texts[i].parentNode.insertBefore(new_S, texts[i])
|
||||||
texts[i].parentNode.removeChild(texts[i])
|
texts[i].parentNode.removeChild(texts[i])
|
||||||
return textdoc
|
return textdoc
|
||||||
|
|
||||||
@ -192,15 +194,12 @@ def main():
|
|||||||
doc_stream = get_document(service, items[0]['id'])
|
doc_stream = get_document(service, items[0]['id'])
|
||||||
doc_file = os.path.join(TEXTS_DIR, "{0}.odt".format(items[0]['name']))
|
doc_file = os.path.join(TEXTS_DIR, "{0}.odt".format(items[0]['name']))
|
||||||
with open(doc_file, 'wb') as out:
|
with open(doc_file, 'wb') as out:
|
||||||
out.write(doc_stream.getvalue())
|
out.write(doc_stream.getvalue())
|
||||||
book_of_status = OpenDocumentText()
|
book_of_status = OpenDocumentText()
|
||||||
|
|
||||||
# book_of_status = load(doc_stream)
|
|
||||||
|
|
||||||
# withbreak = Style(name="WithBreak", parentstylename="Standard", family="paragraph")
|
# withbreak = Style(name="WithBreak", parentstylename="Standard", family="paragraph")
|
||||||
# withbreak.addElement(ParagraphProperties(breakbefore="page"))
|
# withbreak.addElement(ParagraphProperties(breakbefore="page"))
|
||||||
# book_of_status.automaticstyles.addElement(withbreak)
|
# book_of_status.automaticstyles.addElement(withbreak)
|
||||||
# page_seperator = P(stylename=withbreak,text=u'')
|
|
||||||
|
|
||||||
document_id = 0
|
document_id = 0
|
||||||
for item in items[:]:
|
for item in items[:]:
|
||||||
@ -210,9 +209,6 @@ def main():
|
|||||||
with open(doc_file, 'wb') as out:
|
with open(doc_file, 'wb') as out:
|
||||||
out.write(doc_stream.getvalue())
|
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, str(document_id))
|
book_of_status = merge(doc_stream, book_of_status, str(document_id))
|
||||||
document_id += 1
|
document_id += 1
|
||||||
|
|
||||||
|
BIN
texts/Cover.odt
BIN
texts/Cover.odt
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user