From a3db873c47372dc82c69cafb14c18d3ae81f2056 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Thu, 10 Jul 2008 13:59:59 +0000 Subject: [PATCH] Fixed docstring dedenting and made the html writer more configurable in the sense that it doesn't change behavior based on the builder name any longer. --- CHANGES | 4 ++++ sphinx/builder.py | 6 ++++++ sphinx/ext/autodoc.py | 35 ++++++++++++++++++++++------------- sphinx/htmlwriter.py | 4 ++-- 4 files changed, 34 insertions(+), 15 deletions(-) diff --git a/CHANGES b/CHANGES index 6409ae2cb..ef3e9ff00 100644 --- a/CHANGES +++ b/CHANGES @@ -15,6 +15,10 @@ New features added * The `automodule` directive now supports the ``synopsis``, ``deprecated`` and ``platform`` options. +* The HTML builders have two additional attributes now that can be used + to disable the anchor-link creation after headlines and definition + links. EXPERIMENTAL + Release 0.4.1 (Jul 5, 2008) =========================== diff --git a/sphinx/builder.py b/sphinx/builder.py index f9bd9f101..988f1df30 100644 --- a/sphinx/builder.py +++ b/sphinx/builder.py @@ -301,6 +301,8 @@ class StandaloneHTMLBuilder(Builder): supported_image_types = ['image/svg+xml', 'image/png', 'image/gif', 'image/jpeg'] searchindex_filename = 'searchindex.json' + add_header_links = True + add_definition_links = True def init(self): """Load templates.""" @@ -810,6 +812,10 @@ class HTMLHelpBuilder(StandaloneHTMLBuilder): copysource = False supported_image_types = ['image/png', 'image/gif', 'image/jpeg'] + # don't add links + add_header_links = False + add_definition_links = False + def init(self): StandaloneHTMLBuilder.init(self) # the output files for HTML help must be .html only diff --git a/sphinx/ext/autodoc.py b/sphinx/ext/autodoc.py index 9af6195de..b821eb064 100644 --- a/sphinx/ext/autodoc.py +++ b/sphinx/ext/autodoc.py @@ -12,6 +12,7 @@ """ import re +import sys import types import inspect import textwrap @@ -158,17 +159,25 @@ def prepare_docstring(s): of nested_parse().) An empty line is added to act as a separator between this docstring and following content. """ - if not s or s.isspace(): - return [''] - s = s.expandtabs() - nl = s.rstrip().find('\n') - if nl == -1: - # Only one line... - return [s.strip(), ''] - # The first line may be indented differently... - firstline = s[:nl].strip() - otherlines = textwrap.dedent(s[nl+1:]) - return [firstline] + otherlines.splitlines() + [''] + lines = s.expandtabs().splitlines() + # Find minimum indentation of any non-blank lines after first line. + margin = sys.maxint + for line in lines[1:]: + content = len(line.lstrip()) + if content: + indent = len(line) - content + margin = min(margin, indent) + # Remove indentation. + if lines: + lines[0] = lines[0].lstrip() + if margin < sys.maxint: + for i in range(1, len(lines)): lines[i] = lines[i][margin:] + # Remove any trailing or leading blank lines. + while lines and not lines[-1]: + lines.pop() + while lines and not lines[0]: + lines.pop(0) + return lines def get_module_charset(module): @@ -441,8 +450,8 @@ def generate_rst(what, name, members, options, add_content, document, lineno, # unqualified :members: given if what == 'module': # for implicit module members, check __module__ to avoid documenting - # imported objects - members_check_module = True + # imported objects if __all__ is not defined + members_check_module = not hasattr(todoc, '__all__') all_members = inspect.getmembers(todoc) else: if options.inherited_members: diff --git a/sphinx/htmlwriter.py b/sphinx/htmlwriter.py index 37fd574af..8596bd246 100644 --- a/sphinx/htmlwriter.py +++ b/sphinx/htmlwriter.py @@ -71,7 +71,7 @@ class HTMLTranslator(BaseTranslator): if node.parent['desctype'] in ('class', 'exception'): self.body.append('%s ' % node.parent['desctype']) def depart_desc_signature(self, node): - if node['ids'] and self.builder.name != 'htmlhelp': + if node['ids'] and self.builder.add_definition_links: self.body.append(u'\u00B6') self.body.append('\n') @@ -342,7 +342,7 @@ class HTMLTranslator(BaseTranslator): def depart_title(self, node): close_tag = self.context[-1] - if self.builder.name != 'htmlhelp' and \ + if self.builder.add_header_links and \ (close_tag.startswith('