Ejemplo n.º 1
0
 def _expandFragments(self, source, target):
     fields = [f for f in tags(source, psins, fld)]
     fragments = [f for f in tags(target, psins, fmnt)]
     for fragment in fragments:
         fclass = getclass(fragment)
         for field in fields:
             if fclass == getname(field):
                 for child in fragment.childNodes:
                     clone = child.cloneNode(1)
                     field.parentNode.insertBefore(clone, field)
     for field in fields: field.parentNode.removeChild(field)
Ejemplo n.º 2
0
 def tostatic(self, file=None):        
     self._exports = {}
     if file: self._doc = nr.parseUri(file)
     stripws(self._doc)
     names = [getname(i) for i in tags(self._doc, psins, rsrc)]
     classes = [getclass(i) for i in tags(self._doc, psins, rsrc)]
     self._unexpanded = [i for i in tags(self._doc, psins, rsrc)
                         if getclass(i) in names]
     self._templates = [i for i in tags(self._doc, psins, rsrc)
                        if getname(i) in classes]
     self._expandTemplates()
     self._expandResources()
     self._exportResources()
Ejemplo n.º 3
0
 def _expandTemplatesDW(self):
     tbe = 'TemplateBeginEditable name="%s"'
     tee = 'TemplateEndEditable'
     dwtemplates = [d.cloneNode(1) for d in self._templates]
     for i in dwtemplates:
         for master in self._templates:
             if getclass(i) == getname(master):
                 child = master.cloneNode(1)
                 attset(child, nm, getname(i))
                 attdel(child, cls)
                 self._expandFragmentsDW(child, i)                
                 dwtemplates.remove(i)
                 dwtemplates.append(child)
                 i = child
             fields = [f for f in tags(i, psins, fld)]           
             for field in fields:
                 pn = field.parentNode
                 ppn = pn.parentNode.insertBefore
                 ttbe = self._doc.createComment(tbe % getname(field))
                 ttee = self._doc.createComment(tee)
                 ppn(ttbe, pn)
                 ppn(ttee, pn.nextSibling)
                 ph = self._doc.createTextNode(getname(i))
                 field.parentNode.replaceChild(ph, field)
             self._exports['/Templates/%s.dwt' % getname(i)] = i.firstChild
     self._expand(self._templates, self._expandFragments, self._cleanTemplates)
     self._cleanUnexpanded()
Ejemplo n.º 4
0
def fixspans(doc):
    tables = list(tags(doc, htmlns, 'table'))
    for table in tables:
        trcount, tdcount = 0, 0
        for tr in table.childNodes:
            if tr.localName == 'tr': trcount += 1
        for tr in table.childNodes:
            count = 0
            for td in tr.childNodes:
                if td.localName == 'td': count += 1
            if count > tdcount: tdcount = count
        for tr in table.childNodes:
            for td in tr.childNodes:
                if iselement(td):
                    if hasatt(td, 'rowspan'):
                        if int(attget(td, 'rowspan')) > trcount:
                            attset(td, 'rowspan', str(trcount))
                    elif hasatt(td, 'colspan'):
                        if int(attget(td, 'colspan')) > tdcount:
                            attset(td, 'colspan', str(tdcount))
        grid, temp = [], []
        for tr in table.childNodes:
            if iselement(tr) and isname(tr, 'tr'):
                if len(temp):
                    grid.append(temp)
                    temp = []
                for td in tr.childNodes:
                    if iselement(td) and isname(td, 'td'): temp.append(td)
        if len(temp): grid.append(temp)
        rowspans = list(tagsbyAttr(table, 'rowspan'))
        if len(rowspans):
            for td in rowspans:
                trs, sibs = [], ['td.parentNode']
                for x in range(int(attget(td, 'rowspan'))-1):
                    sibs.append('.nextSibling')
                    if eval(''.join(sibs)): trs.append(eval(''.join(sibs)))
                for nodelist in grid:
                    for i in nodelist:
                        if td in nodelist: loc = nodelist.index(td)
                    for tr in trs:
                        if tr.firstChild in nodelist: nodelist.insert(loc, td)
        for nodelist in grid:
            for td in nodelist:
                if hasatt(td, 'colspan'):
                    index = nodelist.index(td)
                    colspan = int(attget(td, 'colspan'))
                    offset, largest = len(nodelist[:index]), 0
                    for nlist in grid:
                        actual = len(nlist[index:-offset])
                        if colspan > actual:
                            if actual > largest:
                                count = 0
                                for i in nlist[index:-offset]:
                                    if hasatt(i, 'colspan'):
                                        count += int(attget(i, 'colspan'))
                                if count != colspan: largest = actual
                    if largest: attset(td, 'colspan', str(largest))
                    if int(attget(td, 'colspan')) <= 1: attdel(td, 'colspan')
Ejemplo n.º 5
0
 def _expandFragmentsDW(self, source, target):
     tbe = 'InstanceBeginEditable name="%s"'
     tee = 'InstanceEndEditable'
     fields = [f for f in tags(source, psins, fld)]
     fragments = [f for f in tags(target, psins, fmnt)]
     for fragment in fragments:
         fclass = getclass(fragment)
         for field in fields:
             if fclass == getname(field):
                 pn = field.parentNode
                 ppn = pn.parentNode.insertBefore
                 ttbe = self._doc.createComment(tbe % getname(field))
                 ttee = self._doc.createComment(tee)
                 ppn(ttbe, field.parentNode)
                 for child in fragment.childNodes:
                     clone = child.cloneNode(1)
                     pn.insertBefore(clone, field)
                 ppn(ttee, pn.nextSibling)
     for field in fields: field.parentNode.removeChild(field)
Ejemplo n.º 6
0
def cssifier(doc, **kwargs):
        
    def insertcss(attdict):
        for id in attdict:
             if id[1] in map:
                mapping = map.get(id[1])
                if mapping:
                    if type(mapping) == types.TupleType:
                        if len(mapping) > 1:
                            for item in mapping:
                                if item.find('%s') != -1:
                                    value = attlist.get(id).value
                                    try:
                                        if int(value):
                                            value = ''.join([value, 'px'])
                                    except ValueError: pass
                                    delete.append(id)
                                    yield item % value
                                else:
                                    delete.append(id)
                                    yield item
                        elif len(mapping) == 1:
                            if mapping not in css:
                                value = tag.attributes.get(id).value
                                css[mapping % value] = 0
                                delete.append(id)
                    elif type(mapping) == types.DictType:
                        value = mapping.get(tag.attributes.get(id).value)
                        if value:
                            delete.append(id)
                            yield value
                    elif mapping.find('%s') != -1:
                        value = tag.attributes.get(id).value
                        try:
                            if int(value): value = ''.join([value, 'px'])
                        except ValueError: pass
                        delete.append(id)
                        yield mapping % value

    def recss(cdict):
        return '; '.join(['%s: %s' % (i, j) for i, j in cdict.iteritems()])

    from htmldefs import html2css
    import types
    if 'embed' in kwargs: embed = 1
    else: embed = None
    if 'file' in kwargs: file = kwargs['file']
    else: file = None
    css, tags, count = getcss(doc), list(tagsbyNS(htmlns, doc)), 1
    for tag in tags:
        if tag.localName in html2css:
            delete, map = list(), html2css.get(tag.localName)
            tcss, scss = list(insertcss(tag.attributes)), dict()
            if len(delete):
                for i in delete: del tag.attributes[i]
            if len(tcss):
                for cmd, value in cssplit2('; '.join(tcss)): scss[cmd] = value
                if hasatt(tag, 'class'):
                    scss.update(css.get(''.join(['.', attget(tag, 'class')])))
                    attdel(tag, 'class')
                if hasatt(tag, 'style'):
                    style = css.get(getattr(tag, 'style'))
                    for cmd, value in cssplit2(style): scss[cmd] = value
                    attdel(tag, 'style')
                if scss not in css.values():
                    classvalue = ''.join(['css', str(count)])
                    css[''.join(['.', classvalue])] = scss
                    count += 1
                elif scss in css.values():
                    for i, j in css.iteritems():
                        if j == scss: classvalue = i.strip('.')
                if embed or file: attset(tag, 'class', classvalue)
                else: attset(tag, 'style', recss(scss))
    if embed or file:
        cssgather = [('%s {%s}' % (i, recss(css.get(i)))) for i in css]
        cssgather.sort()
        cssmaster = '\n'.join(cssgather)
        for i in tagsinList(doc, ['style', 'link']):
            if isname(i, 'link'):
                if hasatt(i, 'rel'):
                    if attget(i, 'rel') == 'stylesheet':
                        i.parentNode.removeChild(i)
            elif isname(i, 'style'): i.parentNode.removeChild(i)
        if kwargs:
            if embed:
                style = doc.createElementNS(htmlns, 'style')
                style.appendChild(doc.createComment(cssmaster))
                list(tags(doc, htmlns, 'head'))[0].appendChild(style)
            elif file:
                import urllib
                open(file, 'wb').write(cssmaster)
                style = doc.createElementNS(htmlns, 'link')
                attset(style, 'rel', 'stylesheet')
                attset(style, 'href', urllib.pathname2url(file))
            attset(style, 'type', 'text/css')
            list(tags(doc, htmlns, 'head'))[0].appendChild(style)
Ejemplo n.º 7
0
def notrwidth(doc):
    trs = list(tags(doc, htmlns, 'tr'))
    for tr in trs: tr.removeAttributeNS(None, 'width')