From: <mi...@us...> - 2009-08-22 22:28:38
|
Author: milde Date: 2009-08-23 00:01:26 +0200 (Sun, 23 Aug 2009) New Revision: 6095 Modified: trunk/docutils/docutils/writers/latex2e/__init__.py Log: cosmetic cleanup (indentation, function names, boolean values) without effect on the output Modified: trunk/docutils/docutils/writers/latex2e/__init__.py =================================================================== --- trunk/docutils/docutils/writers/latex2e/__init__.py 2009-08-22 21:50:35 UTC (rev 6094) +++ trunk/docutils/docutils/writers/latex2e/__init__.py 2009-08-22 22:01:26 UTC (rev 6095) @@ -175,7 +175,7 @@ config_section_dependencies = ('writers',) visitor_attributes = ('head_prefix', 'stylesheet', 'head', - 'body_prefix', 'body', 'body_suffix') + 'body_prefix', 'body', 'body_suffix') output = None """Final translated form of `document`.""" @@ -312,6 +312,9 @@ """Return list of values sorted by keys""" return [self[key] for key in self.sortedkeys()] + +# PreambleCmds +# ````````````` # A container for LaTeX code snippets that can be # inserted into the preamble if required in the document. # @@ -765,8 +768,8 @@ # --------- latex_head = r'\documentclass[%s]{%s}' - # conditionally if no hyperref is used dont include - linking = PreambleCmds.linking + linking = PreambleCmds.linking # if false, hyperref is not loaded + # TODO: use a config value? generator = '% generated by Docutils <http://docutils.sourceforge.net/>' @@ -775,7 +778,7 @@ # TODO: use mixins for different implementations. # list environment for docinfo. else tabularx - use_optionlist_for_docinfo = 0 # NOT YET IN USE + ## use_optionlist_for_docinfo = False # TODO: NOT YET IN USE # Use compound enumerations (1.A.1.) compound_enumerators = 0 @@ -790,9 +793,23 @@ # default link color hyperlink_color = 'blue' - # Bookkeeping: - has_latex_toc = False # is there a toc in the doc (needed by minitoc) + # Auxiliary variables + # ------------------- + has_latex_toc = False # is there a toc in the doc? (needed by minitoc) + is_toc_list = False # is the current bullet_list a ToC? + section_level = 0 + + # Flags to encode(): + # inside citation reference labels underscores dont need to be escaped + inside_citation_reference_label = False + verbatim = False # do not encode + insert_non_breaking_blanks = False # replace blanks by "~" + insert_newline = False # add latex newline commands + literal = False # teletype: replace underscores + literal_block = False # inside literal block: no quote mangling + + def __init__(self, document): nodes.NodeVisitor.__init__(self, document) self.settings = settings = document.settings @@ -875,7 +892,7 @@ # (or use documentclass-default (as currently))? ## if self.use_latex_toc: ## self.requirements['tocdepth'] = (r'\setcounter{tocdepth}{%d}' % - ## len(self.d_class.sections)) + ## len(self.d_class.sections)) if not self.settings.sectnum_xform: # section numbering by LaTeX: sectnum_cmds = [] @@ -883,7 +900,7 @@ # 0 no "sectnum" directive -> no section numbers # None "sectnum" directive without depth arg -> LaTeX default # else value of the "depth" argument -> limit to supported - # section levels + # section levels if settings.sectnum_depth is not None: sectnum_depth = min(settings.sectnum_depth, len(self.d_class.sections)) @@ -929,9 +946,6 @@ else: self.stylesheet.append(r'\input{%s}' % sheet) - ## if len(self.stylesheet) == 1: # if there are no styles, - ## self.stylesheet = [] # remove comment line - # Part of LaTeX preamble before style sheet(s) self.head_prefix = [ self.generator, @@ -967,23 +981,8 @@ self.body_prefix = ['\n%%% Body\n\\begin{document}\n'] self.body = [] self.body_suffix = ['\n\\end{document}\n'] - self.section_level = 0 self.context = [] - self.topic_classes = [] - # column specification for tables - self.table_caption = None - # Flags to encode - # --------------- - # verbatim: to tell encode not to encode. - self.verbatim = 0 - # insert_non_breaking_blanks: to tell encode to replace blanks by "~". - self.insert_non_breaking_blanks = 0 - # insert_newline: to tell encode to add latex newline. - self.insert_newline = 0 - # inside citation reference labels underscores dont need to be escaped. - self.inside_citation_reference_label = 0 - # Stack of section counters so that we don't have to use_latex_toc. # This will grow and shrink as processing occurs. # Initialized for potential first-level sections. @@ -999,14 +998,13 @@ self._max_enumeration_counters = 0 self._bibitems = [] - - # docinfo. - self.docinfo = None - # inside literal block: no quote mangling. - self.literal_block = 0 + self.docinfo = [] self.literal_block_stack = [] - self.literal = 0 + + # Auxiliary Methods + # ----------------- + def to_latex_encoding(self,docutils_encoding): """Translate docutils encoding name into LaTeX's. @@ -1198,13 +1196,28 @@ body = ''.join(self.body_prefix + self.body + self.body_suffix) return head + '\n' + body - # Tests - # ----- - def is_inline(self, node): """Check whether a node represents an inline element""" return isinstance(node.parent, nodes.TextElement) + def append_hypertargets(self, node): + """Append hypertargets for all ids of `node`""" + # hypertarget places the anchor at the target's baseline, + # so we raise it explicitely + self.body.append('%\n'.join(['\\raisebox{1em}{\\hypertarget{%s}{}}' % + id for id in node['ids']])) + + def ids_to_labels(self, node, set_anchor=True): + """Return list of label definitions for all ids of `node` + + If `set_anchor` is True, an anchor is set with \phantomsection. + """ + labels = ['\\label{%s}' % id for id in node.get('ids', [])] + if set_anchor and labels: + labels.insert(0, '\\phantomsection') + return labels + + # Visitor methods # --------------- @@ -1226,7 +1239,6 @@ classes = ','.join(node['classes']) title = '' else: # specific admonitions - self.fallbacks['title'] = PreambleCmds.title classes = node.tagname.replace('_', '-') title = '\\DUtitle[%s]{%s}\n' % ( classes, self.language.labels.get(classes, classes)) @@ -1366,7 +1378,7 @@ self.body.append(',') else: self.body.append('}') - self.inside_citation_reference_label = 0 + self.inside_citation_reference_label = False else: self.body.append('}]') @@ -1486,7 +1498,7 @@ if name == 'address': # newlines are meaningful self.insert_newline = 1 text = self.encode(node.astext()) - self.insert_newline = 0 + self.insert_newline = False else: text = self.attval(node.astext()) self.author_stack[-1].append(text) @@ -1512,7 +1524,7 @@ del self.body[size:] dest.extend(tail) # for address we did set insert_newline - self.insert_newline = 0 + self.insert_newline = False def visit_doctest_block(self, node): self.body.append( '\\begin{verbatim}' ) @@ -1520,7 +1532,7 @@ def depart_doctest_block(self, node): self.body.append( '\\end{verbatim}\n' ) - self.verbatim = 0 + self.verbatim = False def visit_document(self, node): if self.settings.use_titlepage_env: @@ -1529,7 +1541,7 @@ if (self.use_latex_docinfo or len(node) and isinstance(node[0], nodes.title)): if node['ids']: - self.title += self.labels(node) + self.title += self.ids_to_labels(node) self.body_prefix.append('\\maketitle\n\n') def depart_document(self, node): @@ -1822,7 +1834,7 @@ self.body.append('\\begin{figure}[b]') self.append_hypertargets(node) if node.get('id') == node.get('name'): # explicite label - self.body += self.labels(node) + self.body += self.ids_to_labels(node) def depart_footnote(self, node): if self.use_latex_footnotes: @@ -2023,16 +2035,16 @@ pass def visit_literal(self, node): - self.literal = 1 + self.literal = True self.body.append('\\texttt{') if node['classes']: self.visit_inline(node) def depart_literal(self, node): + self.literal = False if node['classes']: self.depart_inline(node) self.body.append('}') - self.literal = 0 # Literal blocks are used for '::'-prefixed literal-indented # blocks of text, where the inline markup is not recognized, @@ -2085,11 +2097,11 @@ def depart_literal_block(self, node): if self.verbatim: self.body.append('\n\\end{%s}\n' % self.literal_block_env) - self.verbatim = 0 + self.verbatim = False else: self.body.append('\n}') - self.insert_non_breaking_blanks = 0 - self.literal_block = 0 + self.insert_non_breaking_blanks = False + self.literal_block = False self.body.append(self.context.pop()) ## def visit_meta(self, node): @@ -2181,13 +2193,13 @@ return self.body.append('\n') if node.get('ids'): - self.body += self.labels(node) + ['\n'] + self.body += self.ids_to_labels(node) + ['\n'] def depart_paragraph(self, node): self.body.append('\n') def visit_problematic(self, node): - self.requirements['color'] = r'\usepackage{color}' + self.requirements['color'] = PreambleCmds.color self.body.append('%\n') self.append_hypertargets(node) self.body.append(r'\hyperlink{%s}{\textbf{\color{red}' % node['refid']) @@ -2297,7 +2309,7 @@ if isinstance(node.parent, nodes.document): self.title.append(r'\\ % subtitle') self.title.append(r'\large{%s}' % self.encode(node.astext())) - self.title += self.labels(node, set_anchor=False) + self.title += self.ids_to_labels(node, set_anchor=False) raise nodes.SkipNode # Section subtitle -> always "starred": no number, not in ToC elif isinstance(node.parent, nodes.section): @@ -2355,23 +2367,6 @@ else: self.active_table.set_table_style(self.settings.table_style) - def append_hypertargets(self, node): - """Append hypertargets for all ids of `node`""" - # Some viewers place the target's baseline at the top of the window - # so we raise the hypertarget(s) 1 line: - self.body.append('%\n'.join(['\\raisebox{1em}{\\hypertarget{%s}{}}' % - id for id in node['ids']])) - - def labels(self, node, set_anchor=True): - """Return list of label definitions for all ids of `node` - - If `set_anchor` is True, an anchor is set with \phantomsection. - """ - labels = ['\\label{%s}' % id for id in node.get('ids', [])] - if set_anchor and labels: - labels.insert(0, '\\phantomsection') - return labels - def visit_target(self, node): # Skip indirect targets: if ('refuri' in node # external hyperlink @@ -2380,7 +2375,7 @@ ## self.body.append('%% %s\n' % node) # for debugging return self.body.append('%\n') - self.body += self.labels(node) + self.body += self.ids_to_labels(node) def depart_target(self, node): pass @@ -2449,7 +2444,7 @@ result.append(r'\phantomsection') result.append(r'\addcontentsline{toc}{%s}{%s}' % (section_name, section_title)) - result += self.labels(node.parent, set_anchor=False) + result += self.ids_to_labels(node.parent, set_anchor=False) return '%\n '.join(result) + '%\n' def visit_title(self, node): @@ -2542,7 +2537,7 @@ # table of contents: if 'contents' in node['classes']: self.body.append('\n') - self.body += self.labels(node) + self.body += self.ids_to_labels(node) if self.settings.use_titlepage_env: # TODO: move this to a save place # (what if the contents are at the end of the document?) |