#! /usr/bin/env python3
# -*- coding: iso-8859-2

import re 
import xml.dom.minidom
from xml.dom.minidom import Node

import sys
#import codecs
#sys.stdout = codecs.getwriter('utf8')(sys.stdout)

def main():
    
  def get_removable_styles(automatic_styles):
    removable = []
    for style in automatic_styles:
        res = re.search("(P\d*)", style.getAttribute("style:name"))
        if res: 
          removable.extend(res.groups())
        res = re.search("(T\d*)", style.getAttribute("style:name"))
        if res: 
          removable.extend(res.groups())
    return removable

  def remove_bad_styles_xml(doc, removable_styles):

    for row in doc.getElementsByTagName("text:p"):
      log = 0
      if log:
        print(*["******************** Processing row node:", row])

      if row.getElementsByTagName("text:span"):
        r = row.getElementsByTagName("text:span")
        r.reverse()
        if log:
          print(r)
        for node in r:
          if node.getAttribute("text:style-name") in removable_styles:
            if node.hasChildNodes():
              if log:
                print(*["******************** Processing span node:", node])
                print(*["with parent:", node.parentNode])
                print(*["starting node structure:", node, node.childNodes])

              for grandchild in node.childNodes:
                # add all chlidnodes as textnodes instead of their
                # parentnode (that is node) to their grandparent node
                # (that is anything that is above node)
                if log: 
                  print(*["Nodevalue:", grandchild.nodeValue])
                  print(*["Node:", grandchild])
                  print(*["Put under:", node.parentNode])
                movednode=grandchild.cloneNode(True)
                node.parentNode.insertBefore(movednode, node)
                print(*["Childnodes after move:", node])
                print(*["deleting node:", node])

              if log:
                print(*["deleting node:", node])
              node.parentNode.removeChild(node)
              if log:
                print(*["final node structure:", grandchild.parentNode, grandchild.parentNode.childNodes])

    return doc

  doc = xml.dom.minidom.parse("content.xml")

  automatic_styles = doc.getElementsByTagName("style:style")

  removable = get_removable_styles(automatic_styles)

  #for a in removable:
   # print(a)

  doc = remove_bad_styles_xml(doc, removable)

  file = open("newcont.xml", "w")
  doc.writexml(file)


if __name__ == "__main__":
      main()
