~ubuntu-branches/ubuntu/hardy/python-docutils/hardy

« back to all changes in this revision

Viewing changes to docutils/transforms/parts.py

  • Committer: Bazaar Package Importer
  • Author(s): martin f. krafft
  • Date: 2006-07-10 11:45:05 UTC
  • mfrom: (2.1.4 edgy)
  • Revision ID: james.westby@ubuntu.com-20060710114505-otkhqcslevewxmz5
Tags: 0.4-3
Added build dependency on python-central (closes: #377580).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Authors: David Goodger, Ueli Schlaepfer, Dmitry Jemerov
2
2
# Contact: goodger@users.sourceforge.net
3
 
# Revision: $Revision: 1.17 $
4
 
# Date: $Date: 2004/04/02 02:28:23 $
 
3
# Revision: $Revision: 3909 $
 
4
# Date: $Date: 2005-09-26 20:17:31 +0200 (Mon, 26 Sep 2005) $
5
5
# Copyright: This module has been placed in the public domain.
6
6
 
7
7
"""
33
33
 
34
34
    def apply(self):
35
35
        self.maxdepth = self.startnode.details.get('depth', sys.maxint)
 
36
        self.startvalue = self.startnode.details.get('start', 1)
 
37
        self.prefix = self.startnode.details.get('prefix', '')
 
38
        self.suffix = self.startnode.details.get('suffix', '')
36
39
        self.startnode.parent.remove(self.startnode)
37
40
        if self.document.settings.sectnum_xform:
38
41
            self.update_section_numbers(self.document)
39
42
 
40
43
    def update_section_numbers(self, node, prefix=(), depth=0):
41
44
        depth += 1
42
 
        sectnum = 1
 
45
        if prefix:
 
46
            sectnum = 1
 
47
        else:
 
48
            sectnum = self.startvalue
43
49
        for child in node:
44
50
            if isinstance(child, nodes.section):
45
51
                numbers = prefix + (str(sectnum),)
46
52
                title = child[0]
47
53
                # Use   for spacing:
48
54
                generated = nodes.generated(
49
 
                    '', '.'.join(numbers) + u'\u00a0' * 3, CLASS='sectnum')
 
55
                    '', (self.prefix + '.'.join(numbers) + self.suffix
 
56
                         +  u'\u00a0' * 3),
 
57
                    classes=['sectnum'])
50
58
                title.insert(0, generated)
51
59
                title['auto'] = 1
52
60
                if depth < self.maxdepth:
76
84
        details = self.startnode.details
77
85
        if details.has_key('local'):
78
86
            startnode = self.startnode.parent.parent
79
 
            # @@@ generate an error if the startnode (directive) not at
80
 
            # section/document top-level? Drag it up until it is?
81
 
            while not isinstance(startnode, nodes.Structural):
 
87
            while not (isinstance(startnode, nodes.section)
 
88
                       or isinstance(startnode, nodes.document)):
 
89
                # find the ToC root: a direct ancestor of startnode
82
90
                startnode = startnode.parent
83
91
        else:
84
92
            startnode = self.document
85
 
 
86
 
        self.toc_id = self.startnode.parent['id']
 
93
        self.toc_id = self.startnode.parent['ids'][0]
87
94
        if details.has_key('backlinks'):
88
95
            self.backlinks = details['backlinks']
89
96
        else:
90
97
            self.backlinks = self.document.settings.toc_backlinks
91
98
        contents = self.build_contents(startnode)
92
99
        if len(contents):
93
 
            self.startnode.parent.replace(self.startnode, contents)
 
100
            self.startnode.replace_self(contents)
94
101
        else:
95
102
            self.startnode.parent.parent.remove(self.startnode.parent)
96
103
 
109
116
            title = section[0]
110
117
            auto = title.get('auto')    # May be set by SectNum.
111
118
            entrytext = self.copy_and_filter(title)
112
 
            reference = nodes.reference('', '', refid=section['id'],
 
119
            reference = nodes.reference('', '', refid=section['ids'][0],
113
120
                                        *entrytext)
114
121
            ref_id = self.document.set_id(reference)
115
122
            entry = nodes.paragraph('', '', reference)
116
123
            item = nodes.list_item('', entry)
117
 
            if self.backlinks == 'entry':
118
 
                title['refid'] = ref_id
119
 
            elif self.backlinks == 'top':
120
 
                title['refid'] = self.toc_id
 
124
            if ( self.backlinks in ('entry', 'top')
 
125
                 and title.next_node(nodes.reference) is None):
 
126
                if self.backlinks == 'entry':
 
127
                    title['refid'] = ref_id
 
128
                elif self.backlinks == 'top':
 
129
                    title['refid'] = self.toc_id
121
130
            if level < depth:
122
131
                subsects = self.build_contents(section, level)
123
132
                item += subsects
125
134
        if entries:
126
135
            contents = nodes.bullet_list('', *entries)
127
136
            if auto:
128
 
                contents.set_class('auto-toc')
 
137
                contents['classes'].append('auto-toc')
129
138
            return contents
130
139
        else:
131
140
            return []
140
149
class ContentsFilter(nodes.TreeCopyVisitor):
141
150
 
142
151
    def get_entry_text(self):
143
 
        return self.get_tree_copy().get_children()
 
152
        return self.get_tree_copy().children
144
153
 
145
154
    def visit_citation_reference(self, node):
146
155
        raise nodes.SkipNode