0001"""Restructured Text Support Module
0002
0003This module is used internally to transform docstrings and Restructured
0004Text documents to HTML.
0005
0006"""
0007
0008import sys
0009from docutils.core import publish_parts
0010
0011def parts(file):
0012 """
0013 Parse ``file`` and return a dictionary containing transformed parts.
0014
0015 This uses the ``docutils.core.publish_parts`` function internally.
0016 Interesting parts include the following:
0017
0018 fragment
0019 The body without the header, title, or footer.
0020
0021 title
0022 The document title.
0023
0024 """
0025 fo = open(file, 'r')
0026 source = fo.read()
0027 fo.close()
0028 parts = publish_parts(source, source_path=file, writer_name='html')
0029 for k, v in parts.items():
0030 parts[k] = _scrub_html(v)
0031 return parts
0032
0033def to_html(text):
0034 """
0035 Convert ``text`` to HTML and return result as a unicode string.
0036 """
0037 source = text
0038 parts = publish_parts(source, writer_name='html')
0039 return _scrub_html(parts['body'])
0040
0041def _scrub_html(html):
0042 return html.replace(' ', ' ')
0043
0044def trim(docstring):
0045 """
0046 Trim a docstring.
0047
0048 Taken from the example given in :pep:`257`.
0049 """
0050
0051 if not docstring:
0052 return ''
0053
0054
0055 lines = docstring.expandtabs().splitlines()
0056
0057 indent = sys.maxint
0058 for line in lines[1:]:
0059 stripped = line.lstrip()
0060 if stripped:
0061 indent = min(indent, len(line) - len(stripped))
0062
0063 trimmed = [lines[0].strip()]
0064 if indent < sys.maxint:
0065 for line in lines[1:]:
0066 trimmed.append(line[indent:].rstrip())
0067
0068 while trimmed and not trimmed[-1]:
0069 trimmed.pop()
0070 while trimmed and not trimmed[0]:
0071 trimmed.pop(0)
0072
0073 return '\n'.join(trimmed)
0074
0075
0076__all__ = ['parts', 'to_html', 'trim']
0077
0078
0079__author__ = "Ryan Tomayko <rtomayko@gmail.com>"
0080__date__ = "$Date: 2005-06-12 00:05:07 -0400 (Sun, 12 Jun 2005) $"
0081__revision__ = "$Revision: 22 $"
0082__url__ = "$URL: svn://lesscode.org/pudge/trunk/pudge/rst.py $"
0083__copyright__ = "Copyright 2005, Ryan Tomayko"
0084__license__ = "MIT <http://www.opensource.org/licenses/mit-license.php>"