#! /usr/bin/python # vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab # For use with Topaz Scripts Version 2.6 import csv import sys import os import getopt import re from struct import pack from struct import unpack debug = False class DocParser(object): def __init__(self, flatxml, fontsize, ph, pw): self.flatdoc = flatxml.split(b'\n') self.fontsize = int(fontsize) self.ph = int(ph) * 1.0 self.pw = int(pw) * 1.0 stags = { b'paragraph' : 'p', b'graphic' : '.graphic' } attr_val_map = { b'hang' : 'text-indent: ', b'indent' : 'text-indent: ', b'line-space' : 'line-height: ', b'margin-bottom' : 'margin-bottom: ', b'margin-left' : 'margin-left: ', b'margin-right' : 'margin-right: ', b'margin-top' : 'margin-top: ', b'space-after' : 'padding-bottom: ', } attr_str_map = { b'align-center' : 'text-align: center; margin-left: auto; margin-right: auto;', b'align-left' : 'text-align: left;', b'align-right' : 'text-align: right;', b'align-justify' : 'text-align: justify;', b'display-inline' : 'display: inline;', b'pos-left' : 'text-align: left;', b'pos-right' : 'text-align: right;', b'pos-center' : 'text-align: center; margin-left: auto; margin-right: auto;', } # find tag if within pos to end inclusive def findinDoc(self, tagpath, pos, end) : result = None docList = self.flatdoc cnt = len(docList) if end == -1 : end = cnt else: end = min(cnt,end) foundat = -1 for j in range(pos, end): item = docList[j] if item.find(b'=') >= 0: (name, argres) = item.split(b'=',1) else : name = item argres = b'' if (isinstance(tagpath,str)): tagpath = tagpath.encode('utf-8') if name.endswith(tagpath) : result = argres foundat = j break return foundat, result # return list of start positions for the tagpath def posinDoc(self, tagpath): startpos = [] pos = 0 res = b"" while res != None : (foundpos, res) = self.findinDoc(tagpath, pos, -1) if res != None : startpos.append(foundpos) pos = foundpos + 1 return startpos # returns a vector of integers for the tagpath def getData(self, tagpath, pos, end, clean=False): if clean: digits_only = re.compile(rb'''([0-9]+)''') argres=[] (foundat, argt) = self.findinDoc(tagpath, pos, end) if (argt != None) and (len(argt) > 0) : argList = argt.split(b'|') for strval in argList: if clean: m = re.search(digits_only, strval) if m != None: strval = m.group() argres.append(int(strval)) return argres def process(self): classlst = '' csspage = '.cl-center { text-align: center; margin-left: auto; margin-right: auto; }\n' csspage += '.cl-right { text-align: right; }\n' csspage += '.cl-left { text-align: left; }\n' csspage += '.cl-justify { text-align: justify; }\n' # generate a list of each