Skip to content

Commit

Permalink
display line numbers when viewing source code, fix #328 (#385)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhils authored Apr 24, 2022
1 parent b523c58 commit 163ec0d
Show file tree
Hide file tree
Showing 23 changed files with 3,732 additions and 3,706 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

# Unreleased: pdoc next

- Display line numbers when viewing source code.
([#328](https://github.com/mitmproxy/pdoc/issues/328), [@mhils](https://github.com/mhils))
- Fix catastrophic backtracking in a markdown2 regex that processes pyshell examples.
([#376](https://github.com/mitmproxy/pdoc/issues/376), [@Andrew-Sheridan](https://github.com/Andrew-Sheridan) and [@mhils](https://github.com/mhils))
- pdoc now uses the submodule name in the rendered sidebar, rather than the full import path.
([#374](https://github.com/mitmproxy/pdoc/issues/374), [@jacksund](https://github.com/jacksund))
- Fix a bug where explicit links were rendered incorrectly.
([#382](https://github.com/mitmproxy/pdoc/issues/382), [@mhils](https://github.com/mhils))
- Fix compatibility with pygments 2.12.
- Fix compatibility with Pygments 2.12.
([#384](https://github.com/mitmproxy/pdoc/issues/384), [@mhils](https://github.com/mhils))

# 2022-04-06: pdoc 11.0.0
Expand Down
24 changes: 19 additions & 5 deletions pdoc/render_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import os
import re
import warnings
from contextlib import contextmanager
from collections.abc import Collection, Iterable, Mapping
from contextlib import contextmanager
from unittest.mock import patch

import pygments.formatters.html
Expand All @@ -30,7 +30,11 @@
The pygments lexer used for pdoc.render_helpers.highlight.
Overwrite this to configure pygments lexing.
"""
formatter = pygments.formatters.html.HtmlFormatter(cssclass="pdoc-code codehilite")
formatter = pygments.formatters.html.HtmlFormatter(
cssclass="pdoc-code codehilite",
linenos="inline",
anchorlinenos=True,
)
"""
The pygments formatter used for pdoc.render_helpers.highlight.
Overwrite this to configure pygments highlighting.
Expand Down Expand Up @@ -58,9 +62,19 @@


@cache
def highlight(code: str) -> str:
"""Highlight a piece of Python code using pygments."""
return Markup(pygments.highlight(code, lexer, formatter))
def highlight(doc: pdoc.doc.Doc) -> str:
"""Highlight the source code of a documentation object using pygments."""
if isinstance(doc, str): # pragma: no cover
warnings.warn(
"Passing a string to the `highlight` render helper is deprecated, pass a pdoc.doc.Doc object instead.",
DeprecationWarning,
)
return Markup(pygments.highlight(doc, lexer, formatter))

# set up correct line numbers and anchors
formatter.linespans = doc.qualname or "L"
formatter.linenostart = doc.source_lines[0] if doc.source_lines else 1
return Markup(pygments.highlight(doc.source, lexer, formatter))


@cache
Expand Down
12 changes: 11 additions & 1 deletion pdoc/templates/content.css
Original file line number Diff line number Diff line change
Expand Up @@ -200,17 +200,27 @@ This makes sure that the pdoc styling doesn't leak to the rest of the page when
.pdoc h3:target,
.pdoc h4:target,
.pdoc h5:target,
.pdoc h6:target {
.pdoc h6:target,
.pdoc .pdoc-code > pre > span:target {
background-color: var(--active);
box-shadow: -1rem 0 0 0 var(--active);
}

.pdoc .pdoc-code > pre > span:target {
/* make the highlighted line full width so that the background extends */
display: block;
}

.pdoc div:target > .attr,
.pdoc section:target > .attr,
.pdoc dd:target > a {
background-color: var(--active);
}

.pdoc * {
scroll-margin: 2rem;
}

.pdoc .attr:hover {
filter: contrast(0.95);
}
Expand Down
2 changes: 1 addition & 1 deletion pdoc/templates/default/module.html.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ See https://pdoc.dev/docs/pdoc/render_helpers.html#DefaultMacroExtension for an
{% if show_source and doc.source %}
<details>
<summary>View Source</summary>
{{ doc.source | highlight }}
{{ doc | highlight }}
</details>
{% endif %}
{% enddefaultmacro %}
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
python_requires=">=3.7",
install_requires=[
"Jinja2 >= 2.11.0",
"pygments",
"pygments >= 2.12.0",
"MarkupSafe",
"astunparse; python_version<'3.9'",
],
Expand Down
86 changes: 43 additions & 43 deletions test/testdata/demo.html

Large diffs are not rendered by default.

36 changes: 18 additions & 18 deletions test/testdata/demo_eager.html

Large diffs are not rendered by default.

874 changes: 437 additions & 437 deletions test/testdata/demo_long.html

Large diffs are not rendered by default.

106 changes: 53 additions & 53 deletions test/testdata/demopackage.html

Large diffs are not rendered by default.

214 changes: 107 additions & 107 deletions test/testdata/demopackage_dir.html

Large diffs are not rendered by default.

86 changes: 43 additions & 43 deletions test/testdata/example_customtemplate.html

Large diffs are not rendered by default.

86 changes: 43 additions & 43 deletions test/testdata/example_darkmode.html

Large diffs are not rendered by default.

86 changes: 43 additions & 43 deletions test/testdata/example_mkdocs.html

Large diffs are not rendered by default.

1,690 changes: 845 additions & 845 deletions test/testdata/flavors_google.html

Large diffs are not rendered by default.

1,866 changes: 933 additions & 933 deletions test/testdata/flavors_numpy.html

Large diffs are not rendered by default.

660 changes: 330 additions & 330 deletions test/testdata/flavors_rst.html

Large diffs are not rendered by default.

106 changes: 53 additions & 53 deletions test/testdata/math_demo.html

Large diffs are not rendered by default.

1,248 changes: 624 additions & 624 deletions test/testdata/misc.html

Large diffs are not rendered by default.

60 changes: 30 additions & 30 deletions test/testdata/misc_py310.html

Large diffs are not rendered by default.

88 changes: 44 additions & 44 deletions test/testdata/misc_py39.html

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions test/testdata/render_options.html

Large diffs are not rendered by default.

52 changes: 26 additions & 26 deletions test/testdata/top_level_reimports.html

Large diffs are not rendered by default.

44 changes: 22 additions & 22 deletions test/testdata/type_checking_imports.html

Large diffs are not rendered by default.

0 comments on commit 163ec0d

Please sign in to comment.