Skip to content

Add --docformat argument for reStructuredText or plaintext input #137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ Prominent features include:
[namedtuple](http://docs.python.org/2.7/library/collections.html#namedtuple-factory-function-for-tuples-with-named-fields)),
the special variable `__pdoc__` can be used in your module to
document any identifier in your public interface.
* Usage is simple. Just write your documentation as Markdown. There are no
added special syntax rules.
* Usage is simple. Just write all your docstring documentation as Markdown
(default), or reStructuredText. There are no added special syntax rules.
* `pdoc` respects your `__all__` variable when present.
* `pdoc` will automatically link identifiers in your docstrings to its
corresponding documentation.
Expand Down
16 changes: 13 additions & 3 deletions pdoc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,8 @@ def __init__(self):


def html(module_name, docfilter=None, allsubmodules=False,
external_links=False, link_prefix='', source=True):
external_links=False, link_prefix='', source=True,
docformat="markdown"):
"""
Returns the documentation for the module `module_name` in HTML
format. The module must be importable.
Expand All @@ -262,12 +263,17 @@ def html(module_name, docfilter=None, allsubmodules=False,
If `source` is `True`, then source code will be retrieved for
every Python object whenever possible. This can dramatically
decrease performance when documenting large modules.

Argument `docformat` should be a string like `'markdown'`, or
`'restructuredtext'` or `'plaintext'` which specifies how the
docstring text should be processed.
"""
mod = Module(import_module(module_name),
docfilter=docfilter,
allsubmodules=allsubmodules)
return mod.html(external_links=external_links,
link_prefix=link_prefix, source=source)
link_prefix=link_prefix, source=source,
docformat=docformat)


def text(module_name, docfilter=None, allsubmodules=False):
Expand Down Expand Up @@ -653,7 +659,7 @@ def text(self):
return text

def html(self, external_links=False, link_prefix='',
source=True, **kwargs):
source=True, docformat="markdown", **kwargs):
"""
Returns the documentation for this module as
self-contained HTML.
Expand All @@ -668,13 +674,17 @@ def html(self, external_links=False, link_prefix='',
every Python object whenever possible. This can dramatically
decrease performance when documenting large modules.

`docformat` should be `'markdown'` or `'restructuredtext'`
and specified how the docstring text should be processed.

`kwargs` is passed to the `mako` render function.
"""
t = _get_tpl('/html.mako')
t = t.render(module=self,
external_links=external_links,
link_prefix=link_prefix,
show_source_code=source,
docformat=docformat,
**kwargs)
return t.strip()

Expand Down
20 changes: 15 additions & 5 deletions pdoc/templates/html.mako
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import re
import sys

import docutils
import docutils.core
import markdown
try:
import pygments
Expand Down Expand Up @@ -65,11 +67,19 @@
if not module_list:
s, _ = re.subn('`[^`]+`', linkify, s)

extensions = []
if use_pygments:
extensions = ['markdown.extensions.codehilite(linenums=False)']
s = markdown.markdown(s.strip(), extensions=extensions)
return s
if docformat == "markdown":
extensions = []
if use_pygments:
extensions = ['markdown.extensions.codehilite(linenums=False)']
m = markdown.markdown(s.strip(), extensions=extensions)
elif docformat == "restructuredtext":
m = docutils.core.publish_parts(s, writer_name='html')['html_body']
else:
# e.g. plaintext
m = "<pre>%s</pre>" % s
# For debugging:
# m = "<p>Given:</p><pre>%s</pre>\n<p>Treated as %s, giving:</p>\n%s" % (s, docformat, m)
return m

def glimpse(s, length=100):
if len(s) < length:
Expand Down
6 changes: 5 additions & 1 deletion scripts/pdoc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ aa('ident_name', type=str, nargs='?',
'Has no effect when --http is set.')
aa('--version', action='store_true',
help='Print the version of pdoc and exit.')
aa('--docformat', type=str, default='markdown',
help='The markup language used for the docstring content.',
choices=['markdown', 'restructuredtext', 'plaintext'])
aa('--html', action='store_true',
help='When set, the output will be HTML formatted.')
aa('--html-dir', type=str, default='.',
Expand Down Expand Up @@ -339,7 +342,8 @@ def html_out(m, html=True):
out = m.html(external_links=args.external_links,
link_prefix=args.link_prefix,
http_server=args.http_html,
source=not args.html_no_source)
source=not args.html_no_source,
docformat=args.docformat)
print(out, file=w)
except Exception:
try:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
],
scripts=['scripts/pdoc'],
provides=['pdoc'],
requires=['argparse', 'mako', 'markdown'],
requires=['argparse', 'docutils', 'mako', 'markdown'],
install_requires=install_requires,
extras_require={'syntax_highlighting': ['pygments']},
)