37 from cubicweb import _ |
37 from cubicweb import _ |
38 |
38 |
39 # table of content management ################################################# |
39 # table of content management ################################################# |
40 |
40 |
41 |
41 |
42 def build_toc_index(node, index): |
42 def build_toc_index(node, parent, index): |
43 try: |
43 try: |
44 nodeidx = node.attrib['resource'] |
44 nodeidx = node.attrib['resource'] |
45 assert nodeidx not in index, nodeidx |
45 assert nodeidx not in index, nodeidx |
46 index[nodeidx] = node |
46 index[nodeidx] = node, parent |
47 except KeyError: |
47 except KeyError: |
48 pass |
48 pass |
49 for child in node: |
49 for child in node: |
50 build_toc_index(child, index) |
50 build_toc_index(child, node, index) |
51 child.parent = node |
|
52 |
51 |
53 |
52 |
54 def get_insertion_point(section, index): |
53 def get_insertion_point(section, index): |
55 if section.attrib.get('insertafter'): |
54 if section.attrib.get('insertafter'): |
56 snode = index[section.attrib['insertafter']] |
55 snode, node = index[section.attrib['insertafter']] |
57 node = snode.parent |
|
58 idx = list(node).index(snode) + 1 |
56 idx = list(node).index(snode) + 1 |
59 elif section.attrib.get('insertbefore'): |
57 elif section.attrib.get('insertbefore'): |
60 snode = index[section.attrib['insertbefore']] |
58 snode, node = index[section.attrib['insertbefore']] |
61 node = snode.parent |
|
62 idx = node.getchildren().index(snode) |
59 idx = node.getchildren().index(snode) |
63 elif 'appendto' in section.attrib: |
60 elif 'appendto' in section.attrib: |
64 node = index[section.attrib['appendto']] |
61 node, _ = index[section.attrib['appendto']] |
65 idx = None |
62 idx = None |
66 else: |
63 else: |
67 node, idx = None, None |
64 node, idx = None, None |
68 return node, idx |
65 return node, idx |
69 |
66 |
70 |
67 |
71 def build_toc(config): |
68 def build_toc(config): |
72 alltocfiles = reversed(tuple(config.locate_all_files('toc.xml'))) |
69 alltocfiles = reversed(tuple(config.locate_all_files('toc.xml'))) |
73 maintoc = parse(next(alltocfiles)).getroot() |
70 maintoc = parse(next(alltocfiles)).getroot() |
74 maintoc.parent = None |
|
75 index = {} |
71 index = {} |
76 build_toc_index(maintoc, index) |
72 build_toc_index(maintoc, None, index) |
77 # insert component documentation into the tree according to their toc.xml |
73 # insert component documentation into the tree according to their toc.xml |
78 # file |
74 # file |
79 for fpath in alltocfiles: |
75 for fpath in alltocfiles: |
80 toc = parse(fpath).getroot() |
76 toc = parse(fpath).getroot() |
81 for section in toc: |
77 for section in toc: |
124 break |
119 break |
125 else: |
120 else: |
126 raise NotFound |
121 raise NotFound |
127 self.tocindex = build_toc(vreg.config) |
122 self.tocindex = build_toc(vreg.config) |
128 try: |
123 try: |
129 node = self.tocindex[fid] |
124 node, parent = self.tocindex[fid] |
130 except KeyError: |
125 except KeyError: |
131 node = None |
126 node, parent = None, None |
132 else: |
127 else: |
133 self.navigation_links(node) |
128 self.navigation_links(node, parent) |
134 self.w(u'<div class="hr"></div>') |
129 self.w(u'<div class="hr"></div>') |
135 self.w(u'<h1>%s</h1>' % (title_for_lang(node, self._cw.lang))) |
130 self.w(u'<h1>%s</h1>' % (title_for_lang(node, self._cw.lang))) |
136 with io.open(join(resourcedir, rid)) as f: |
131 with io.open(join(resourcedir, rid)) as f: |
137 self.w(rest_publish(self, f.read())) |
132 self.w(rest_publish(self, f.read())) |
138 if node is not None: |
133 if node is not None: |
139 self.subsections_links(node) |
134 self.subsections_links(node) |
140 self.w(u'<div class="hr"></div>') |
135 self.w(u'<div class="hr"></div>') |
141 self.navigation_links(node) |
136 self.navigation_links(node, parent) |
142 |
137 |
143 def navigation_links(self, node): |
138 def navigation_links(self, node, parent): |
144 parent = node.parent |
|
145 if parent is None: |
139 if parent is None: |
146 return |
140 return |
147 brothers = subsections(parent) |
141 brothers = subsections(parent) |
148 self.w(u'<div class="docnav">\n') |
142 self.w(u'<div class="docnav">\n') |
149 previousidx = brothers.index(node) - 1 |
143 previousidx = brothers.index(node) - 1 |