Changeset 91:f8cc09945b2b

Show
Ignore:
Timestamp:
01/16/09 03:11:00 (14 months ago)
Author:
Ga?tan de Menten <ged@…>
Branch:
default
Message:

preliminary work on column repetition

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • relatorio/templates/opendocument.py

    r90 r91  
    2626import zipfile 
    2727from cStringIO import StringIO 
     28from copy import deepcopy 
     29 
    2830 
    2931import warnings 
     
    9496        return {'{http://www.w3.org/1999/xlink}href': path} 
    9597 
     98class TableColumnDef: 
     99    """A class used to add the correct number of column definitions to a 
     100    table containing an horizontal repetition" 
     101    """ 
     102    def __init__(self, zfile, context): 
     103        self.zip = zfile 
     104        self.context = context.copy() 
     105 
     106    def __call__(self, expr, name): 
     107        #FIXME: name argument is unused 
     108        return 
    96109 
    97110class Template(MarkupTemplate): 
     
    207220                else: 
    208221                    closing_tags[id(opened_tags.pop())] = statement 
    209             # - we operate only on opening statements 
     222            # - we only need to return opening statements 
    210223            if is_opening: 
    211224                r_statements.append((statement, 
     
    213226                                   ) 
    214227        assert not opened_tags 
    215  
    216228        return r_statements, closing_tags 
    217229 
     
    221233        tags 
    222234        """ 
    223         # Some tag name constants 
     235        # Some tag/attribute name constants 
    224236        table_cell_tag = '{%s}table-cell' % self.namespaces['table'] 
     237        table_row_tag = '{%s}table-row' % self.namespaces['table'] 
     238        num_col_attr = '{%s}number-columns-repeated' % self.namespaces['table'] 
    225239        attrib_name = '{%s}attrs' % self.namespaces['py'] 
    226240        office_name = '{%s}value' % self.namespaces['office'] 
    227241        office_valuetype = '{%s}value-type' % self.namespaces['office'] 
    228         genshi_replace = '{%s}replace' % self.namespaces['py'] 
     242        py_namespace = self.namespaces['py'] 
     243        genshi_replace = '{%s}replace' % py_namespace 
    229244 
    230245        r_statements, closing_tags = self._relatorio_statements(tree) 
     
    235250            # If the node is a genshi directive statement: 
    236251            if directive is not None: 
    237  
    238252                opening = r_node 
    239253                closing = closing_tags[id(r_node)] 
     
    248262                        idx = c_ancestors.index(node) 
    249263                        assert c_ancestors[idx] == node 
     264                        # we only need ancestors up to the common one 
    250265                        del c_ancestors[idx:] 
    251266                        ancestor = node 
     
    257272                       "No common ancestor found for opening and closing tag" 
    258273 
     274                # handle horizontal repetition (over columns) 
     275                if False: 
     276                #if directive == "for" and ancestor.tag == table_row_tag: 
     277                    print "horizontal repetition", a_val 
     278                    # find position of current cell in row 
     279                    position_xpath_expr = \ 
     280                    'count(ancestor::table:table-cell/preceding-sibling::*)' 
     281                    opening_pos = opening.xpath(position_xpath_expr, 
     282                                                namespaces=self.namespaces) 
     283                    closing_pos = closing.xpath(position_xpath_expr, 
     284                                                namespaces=self.namespaces) 
     285                    print "opening_pos", opening_pos 
     286                    print "closing_pos", closing_pos 
     287 
     288                    idx = 0 
     289                    table_node = opening.xpath('ancestor::table:table[1]', 
     290                                               namespaces=self.namespaces)[0] 
     291                    #XXX: use getiterator('table:table-column') instead of xpath? 
     292                    to_split = [] 
     293                    to_move = [] 
     294                    for tag in table_node.xpath('table:table-column', 
     295                                                namespaces=self.namespaces): 
     296                        if num_col_attr in tag.attrib: 
     297                            oldidx = idx 
     298                            idx += int(tag.attrib[num_col_attr]) 
     299                            print "oldidx", oldidx, "idx", idx 
     300                            if oldidx < opening_pos < idx or \ 
     301                               oldidx < closing_pos < idx: 
     302                                to_split.append(tag) 
     303                                print "to_split", to_split 
     304                        else: 
     305                            idx += 1 
     306 
     307                    # split tags 
     308                    for tag in to_split: 
     309                        tag_pos = table_node.index(tag) 
     310                        print "tag_pos", tag_pos 
     311                        num = int(tag.attrib[num_col_attr]) 
     312                        tag.attrib.pop(num_col_attr) 
     313                        new_tags = [deepcopy(tag) for _ in range(num)] 
     314                        table_node[tag_pos:tag_pos] = new_tags 
     315 
     316                    # compute moves 
     317                    if False: 
     318                        if idx < opening_pos: 
     319                            pass 
     320                        elif opening_pos < idx < closing_pos: 
     321                            to_move.append(tag) 
     322                        else: 
     323                            break 
     324                        print idx 
     325                    # move tags 
     326                    # 
     327                    # add a <py:for each="%s"> % a_val 
     328#                    for_node = EtreeElement('{%s}%s' % (py_namespace, 'for'), 
     329#                                            attrib={attr: a_val}, 
     330#                                            nsmap=self.namespaces) 
     331 
    259332                # - we create a <py:xxx> node 
    260                 genshi_node = EtreeElement('{%s}%s' % (self.namespaces['py'], 
     333                genshi_node = EtreeElement('{%s}%s' % (py_namespace, 
    261334                                                       directive), 
    262335                                           attrib={attr: a_val},