#! /usr/bin/python # vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab # For use with Topaz Scripts Version 2.6 from __future__ import print_function 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('\n') self.fontsize = int(fontsize) self.ph = int(ph) * 1.0 self.pw = int(pw) * 1.0 stags = { 'paragraph' : 'p', 'graphic' : '.graphic' } attr_val_map = { 'hang' : 'text-indent: ', 'indent' : 'text-indent: ', 'line-space' : 'line-height: ', 'margin-bottom' : 'margin-bottom: ', 'margin-left' : 'margin-left: ', 'margin-right' : 'margin-right: ', 'margin-top' : 'margin-top: ', 'space-after' : 'padding-bottom: ', } attr_str_map = { 'align-center' : 'text-align: center; margin-left: auto; margin-right: auto;', 'align-left' : 'text-align: left;', 'align-right' : 'text-align: right;', 'align-justify' : 'text-align: justify;', 'display-inline' : 'display: inline;', 'pos-left' : 'text-align: left;', 'pos-right' : 'text-align: right;', '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 xrange(pos, end): item = docList[j] if item.find('=') >= 0: (name, argres) = item.split('=',1) else : name = item argres = '' 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 = "" 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(r'''([0-9]+)''') argres=[] (foundat, argt) = self.findinDoc(tagpath, pos, end) if (argt != None) and (len(argt) > 0) : argList = argt.split('|') 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