diff options
| author | Jon Waltman <jonathan.waltman@gmail.com> | 2012-09-12 15:43:27 -0500 |
|---|---|---|
| committer | Jon Waltman <jonathan.waltman@gmail.com> | 2012-09-12 15:43:27 -0500 |
| commit | 309f0615824f4b0bf347037668a63892cad4ee7a (patch) | |
| tree | 79a5c001c1c6dc6cccd883d0c40cb95e7ba0e8f8 | |
| parent | 7a507c4a7f381bfd03f84621d3e70bf0915e3b84 (diff) | |
| download | sphinx-309f0615824f4b0bf347037668a63892cad4ee7a.tar.gz | |
Closes #886: Fix handling of section headings in "only" directives.
| -rw-r--r-- | sphinx/directives/other.py | 43 |
1 files changed, 40 insertions, 3 deletions
diff --git a/sphinx/directives/other.py b/sphinx/directives/other.py index aa4142d6..506d4be9 100644 --- a/sphinx/directives/other.py +++ b/sphinx/directives/other.py @@ -338,9 +338,46 @@ class Only(Directive): node.document = self.state.document set_source_info(self, node) node['expr'] = self.arguments[0] - self.state.nested_parse(self.content, self.content_offset, node, - match_titles=1) - return [node] + + # Same as util.nested_parse_with_titles but try to handle nested + # sections which should be raised higher up the doctree. + surrounding_title_styles = self.state.memo.title_styles + surrounding_section_level = self.state.memo.section_level + self.state.memo.title_styles = [] + self.state.memo.section_level = 0 + try: + result = self.state.nested_parse(self.content, self.content_offset, + node, match_titles=1) + title_styles = self.state.memo.title_styles + if (not surrounding_title_styles + or not title_styles + or title_styles[0] not in surrounding_title_styles + or not self.state.parent): + # No nested sections so no special handling needed. + return [node] + # Calculate the depths of the current and nested sections. + current_depth = 0 + parent = self.state.parent + while parent: + current_depth += 1 + parent = parent.parent + current_depth -= 2 + title_style = title_styles[0] + nested_depth = len(surrounding_title_styles) + if title_style in surrounding_title_styles: + nested_depth = surrounding_title_styles.index(title_style) + # Use these depths to determine where the nested sections should + # be placed in the doctree. + n_sects_to_raise = current_depth - nested_depth + 1 + parent = self.state.parent + for i in xrange(n_sects_to_raise): + if parent.parent: + parent = parent.parent + parent.append(node) + return [] + finally: + self.state.memo.title_styles = surrounding_title_styles + self.state.memo.section_level = surrounding_section_level class Include(BaseInclude): |
