[scribus] [scribus-dev] Add-In Development Export Styles as CSS

José Antonio Rocha joseantoniorocha at gmail.com
Sun Jan 29 13:55:14 UTC 2012


2012/1/28 Rob Oakes <LyX-Devel at oak-tree.us>

> Do you know of any methods to look at the properties of styles?


I do a script that read the SLA as XML and get styles  from it:

import xml.parsers.expat

################################################
# Code to handle elements of XML Scribus doc
# Convert default points values to current units
################################################
# Dictionaries to hold style sheets
styles = {}
charstyles = {}
document = {}
pages = {}

def start_element(name, attrs):
    '''Build dictonaries from SLA doc file.'''
    # print 'Start element:', name, attrs  #debug
    global styles, charstyles, document, pages
    if name == 'DOCUMENT':
        document = attrs
    if name == 'STYLE':
        #print '=====\n',name,attrs,attrs[u'NAME']
        # A bug in style editor:
        # sometimes create a anomale style definition <STYLE
LANGUAGE="Portuguese"/>
        # So we need to catch errors and ignore it
        try: styles[attrs[u'NAME']] = attrs
        except: pass
    if name == 'CHARSTYLE':
        try: charstyles[attrs[u'CNAME']] = attrs
        except: pass
    if name == 'PAGE':
        try: pages[attrs[u'NUM']] = attrs
        except: pass

def getParagraphStyle(styleName):
    '''Return a dictionary with paragraph style parameters.
    If style don't exist, create it with default paragraph parameters.'''
    try:
        res = styles[styleName]
    except:
        print styleName,'not in style sheet. Creating it.'
        scribus.createParagraphStyle(styleName)
        styles[styleName] = styles[defaultParagraphStyle]
        res = styles[defaultParagraphStyle]
    return res

def getParagraphAttribute(styleName,attrName):
    '''Return value of given attribute of given style
    ***in current mesurement units.*** '''
    res = 0
    # Find parameter in parent style, if not in current style, up to 10
levels.
    for n in range(10):
        dic = getParagraphStyle(styleName)
        if attrName in dic:
            res = dic[attrName]
            # Convert points to current units
            unit = scribus.getUnit()
            res = float(res)*unitDict[unit]
            print "Find",attrName,"=",res,'in',styleName,"."
            break
        else:
            print "\nI don't find",attrName,"in",styleName,"."
            if 'PARENT' in dic:
                styleName = dic['PARENT']
                print "I will search in his father,",styleName,"."
            else:
                print "I don't find any value. Setting to zero..."
                break
    else:
        res = 0
    return res

def getStyleLineSpacing(styleName):
    '''Return line spacing of given style.'''
    # Need test line spacing mode
    # If line space mode is automatic,
    # line space is equal font size times line space percent
    res = float(getParagraphAttribute(styleName,'LINESP'))
    return res

def getStyleLineSpaceMode(styleName):
    '''0=fixed, 1=proportional.'''
    res = int(getParagraphAttribute(styleName,'LINESPMode'))
    return res

def getStyleSpaceAbove(styleName):
    '''Return space above of given paragraph style.'''
    res = float(getParagraphAttribute(styleName,'VOR'))
    return res

def getStyleSpaceBelow(styleName):
    '''Return space below of given paragraph style.'''
    res = float(getParagraphAttribute(styleName,'NACH'))
    return res

def getBaselineGrid():
    '''Get document baseline grid.'''
    global document
    if document == {}:
        res = 0
    else:
        res = float(document['BASEGRID'])
        unit = scribus.getUnit()
        res = res*unitDict[unit]
    return res

def getBaselineOffset():
    '''Get document baseline offset.'''
    global document
    if document == {}:
        res = 0
    else:
        res = float(document['BASEO'])
        unit = scribus.getUnit()
        res = res*unitDict[unit]
    return res

def getAutoLineSpace():
    '''Get automatic line space percentage of current document.'''
    global document
    if document == {}:
        res = 0
    else:
        res = float(document['AUTOL'])
        unit = scribus.getUnit()
        res = res*unitDict[unit]
    return res

def getPageAttributes(page):
    page = page - 1
    try: res = pages[str(unicode(page))]
    except:
        print 'Page',page,'not in document.'
        res = {}
    return res

def getVAutoGap():
    page = scribus.currentPage()
    pdic = getPageAttributes(page)
    if pdic == {}:
        res = 0
    else:
        try:
            res = float(pdic['AGverticalAutoGap'])
            unit = scribus.getUnit()
            res = res*unitDict[unit]
        except:
            print 'AGverticalAutoGap','not in page',page
    return res

def parseDoc():
    ''' Get attributes from current Scribus doc.'''
    debug()
    global styleList
    parser = xml.parsers.expat.ParserCreate('UTF-8')
    parser.StartElementHandler = start_element
    try:
        docName = scribus.saveDoc()
        # Undocumented Scribus 1.3.9 function:
        docName = unicode(scribus.getDocName())
        file = open(docName,'r')
        parser.ParseFile(file)
        file.close()
    except:
        print "Error analising doc."
        earlyExit()
    styleList = scribus.getAllStyles()
    styleList.sort()


-- 
[image: Meira]
------------------------------
nome: "José Antonio Meira da Rocha"
googletalk: email: MSN: joseantoniorocha at gmail.com
veículo: [ http://meiradarocha.jor.br ]
------------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.scribus.net/pipermail/scribus/attachments/20120129/b8a33c03/attachment.html>


More information about the scribus mailing list