From: <mi...@us...> - 2009-06-17 21:16:50
|
Author: milde Date: 2009-06-17 23:16:27 +0200 (Wed, 17 Jun 2009) New Revision: 5989 Modified: trunk/docutils/docs/dev/todo.txt trunk/docutils/docutils/transforms/parts.py trunk/docutils/docutils/writers/latex2e/__init__.py Log: Support "depth" argument of "contents" directive also with "use-latex-toc" The docutils.transforms.parts.Contents transform is made aware of use_latex_toc, and does the right thing for LaTeX. Modified: trunk/docutils/docs/dev/todo.txt =================================================================== --- trunk/docutils/docs/dev/todo.txt 2009-06-16 11:59:20 UTC (rev 5988) +++ trunk/docutils/docs/dev/todo.txt 2009-06-17 21:16:27 UTC (rev 5989) @@ -2048,22 +2048,10 @@ table of contents ^^^^^^^^^^^^^^^^^ -Problem: - when I specify a :depth: to a contents directive, this value is used but - not preserved in the doctree. +* support non-numbered sections with use-latex-toc + (now easy because of the \addtocontents lines) +* use-latex-doc by default? -David wrote: - - There is a setting, use_latex_toc. Unfortunately its implementation is - suboptimal. When use_latex_toc is True, the Docutils-generated ToC - shouldn't be built at all. It's a waste of time. - - Instead, the docutils.transforms.parts.Contents transform should be - made aware of use_latex_toc, and do the right thing for LaTeX. Or the - LaTeX writer should override that transform and substitute a custom - version. - - Default layout -------------- Modified: trunk/docutils/docutils/transforms/parts.py =================================================================== --- trunk/docutils/docutils/transforms/parts.py 2009-06-16 11:59:20 UTC (rev 5988) +++ trunk/docutils/docutils/transforms/parts.py 2009-06-17 21:16:27 UTC (rev 5989) @@ -79,6 +79,14 @@ default_priority = 720 def apply(self): + # build a contents list from the section headings? + # if False, the writer (or output software) builds the contents list + # Example: the LaTeX writer with option "use-latex-toc". + try: + build = not(self.document.settings.use_latex_toc) + except AttributeError: + build = True + details = self.startnode.details if 'local' in details: startnode = self.startnode.parent.parent @@ -93,11 +101,16 @@ self.backlinks = details['backlinks'] else: self.backlinks = self.document.settings.toc_backlinks - contents = self.build_contents(startnode) - if len(contents): - self.startnode.replace_self(contents) + if build: + contents = self.build_contents(startnode) + if len(contents): + self.startnode.replace_self(contents) + else: + self.startnode.parent.parent.remove(self.startnode.parent) else: - self.startnode.parent.parent.remove(self.startnode.parent) + # pass customization settings to the parent node + self.startnode.parent.attributes.update(details) + self.startnode.parent.remove(self.startnode) def build_contents(self, node, level=0): level += 1 Modified: trunk/docutils/docutils/writers/latex2e/__init__.py =================================================================== --- trunk/docutils/docutils/writers/latex2e/__init__.py 2009-06-16 11:59:20 UTC (rev 5988) +++ trunk/docutils/docutils/writers/latex2e/__init__.py 2009-06-17 21:16:27 UTC (rev 5989) @@ -186,6 +186,13 @@ writers.Writer.__init__(self) self.translator_class = LaTeXTranslator + # TODO: footnote collection transform + # Override parent method to add latex-specific transforms + ## def get_transforms(self): + ## # call the parent class' method + ## # return writers.Writer.get_transforms(self) + [footnotes.collect] + ## return writers.Writer.get_transforms(self) + def translate(self): visitor = self.translator_class(self.document) self.document.walkabout(visitor) @@ -1440,6 +1447,7 @@ if node['ids']: self.title += self.labels(node) self.body_prefix.append('\\maketitle\n\n') + def depart_document(self, node): # Complete header with information gained from walkabout # a) conditional requirements (before style sheet) @@ -2067,7 +2075,7 @@ def visit_paragraph(self, node): # no newline if the paragraph is first in a list item if ((isinstance(node.parent, nodes.list_item) or - isinstance(node.parent, nodes.description)) and + isinstance(node.parent, nodes.description)) and node is node.parent[0]): return index = node.parent.index(node) @@ -2359,7 +2367,7 @@ if self.settings.use_titlepage_env: self.body.append('\\end{titlepage}\n') if self.use_latex_toc: - self.body.append('\\renewcommand{\\contentsname}{') + self.body.append('\n\\renewcommand{\\contentsname}{') self.context.append('}\n\\tableofcontents\n\n\\bigskip\n') self.has_latex_toc = True else: @@ -2368,7 +2376,7 @@ self.body.append('\\subsubsection*{~\\hfill ') self.context.append('\\hfill ~%s}\n' % self.bookmark(node)) else: # other topic titles - # TODO: user DUtopictitle: + # TODO: use DUtopictitle: # self.body.append('\n\\DUtopictitle{') # self.context.append('}\n') self.body.append('\n\\subsubsection*{~\\hfill ') @@ -2433,12 +2441,12 @@ # name-prefix for current section level section_name = self.d_class.section(self.section_level) if section_name == 'part': - level = 'part' + minitoc_name = 'part' elif section_name == 'chapter': - level = 'mini' + minitoc_name = 'mini' elif (section_name == 'section' and 'chapter' not in self.d_class.sections): - level = 'sect' + minitoc_name = 'sect' else: # minitoc only supports local toc in part- or top-level warn = self.document.reporter.warning warn('Skipping local ToC at %s level.\n' % section_name + @@ -2446,19 +2454,28 @@ return # Requirements/Setup self.requirements['minitoc'] = PreambleCmds.minitoc - self.requirements['minitoc-%s' % level] = r'\do%stoc' % level + self.requirements['minitoc-%s' % + minitoc_name] = r'\do%stoc' % minitoc_name # depth: (Docutils defaults to unlimited depth) - depth = len(self.d_class.sections) - self.requirements['minitoc-%s-depth' % level] = ( - r'\mtcsetdepth{%stoc}{%d}' % (level, depth)) + max_depth = len(self.d_class.sections) + self.requirements['minitoc-%s-depth' % minitoc_name] = ( + r'\mtcsetdepth{%stoc}{%d}' % (minitoc_name, max_depth)) # TODO: set the depth according to the :depth: argument # Attention: Docutils stores a relative depth while minitoc # expects an absolute depth! - ## self.body.append('\\setcounter{%stocdepth}{%d}' % (level, depth)) + offset = {'sect': 1, 'mini': 0, 'part': 0} + if 'chapter' in self.d_class.sections: + offset['part'] = -1 + depth = node.get('depth', 0) + if depth: + # depth += self.section_level + self.body.append('\\setcounter{%stocdepth}{%d}' % + (minitoc_name, depth + offset[minitoc_name])) # title: - self.body.append('\\mtcsettitle{%stoc}{%s}\n' % (level, toctitle)) + self.body.append('\\mtcsettitle{%stoc}{%s}\n' % + (minitoc_name, toctitle)) # the toc-generating command: - self.body.append('\\%stoc\n' % level) + self.body.append('\\%stoc\n' % minitoc_name) def visit_topic(self, node): self.topic_classes = node['classes'] |