Skip to content
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

Correctly display inline code blocks #96

Closed
wants to merge 3 commits 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
22 changes: 22 additions & 0 deletions mkdoxy/filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import re


PLAIN_CODE_BLOCK = re.compile(r'```(\n.*?\n)```', re.DOTALL)


def use_code_language(value, code_language: str|None):
"""! Jinja2 filter to apply a code language to all plain code blocks
@details
@param value: the value to apply the filter to.
@param code_language (str|None): the code language to apply.
@return: The filtered value.
"""
if not code_language:
return value

return re.sub(
PLAIN_CODE_BLOCK,
lambda m: f'```{code_language}{m[1]}```',
str(value)
)

7 changes: 5 additions & 2 deletions mkdoxy/generatorBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import string
from typing import Dict

from jinja2 import Template
from jinja2 import BaseLoader, Environment, Template
from jinja2.exceptions import TemplateError
from mkdocs import exceptions

import mkdoxy
from mkdoxy.constants import Kind
from mkdoxy.filters import use_code_language
from mkdoxy.node import DummyNode, Node
from mkdoxy.utils import (
merge_two_dicts,
Expand Down Expand Up @@ -38,6 +39,8 @@ def __init__(self, templateDir: str = "", ignore_errors: bool = False, debug: bo
self.templates: Dict[str, Template] = {}
self.metaData: Dict[str, list[str]] = {}

environment = Environment(loader=BaseLoader())
environment.filters['use_code_language'] = use_code_language
# code from https://github.com/daizutabi/mkapi/blob/master/mkapi/core/renderer.py#L29-L38
path = os.path.join(os.path.dirname(mkdoxy.__file__), "templates")
for fileName in os.listdir(path):
Expand All @@ -46,7 +49,7 @@ def __init__(self, templateDir: str = "", ignore_errors: bool = False, debug: bo
with open(filePath, "r") as file:
name = os.path.splitext(fileName)[0]
fileTemplate, metaData = parseTemplateFile(file.read())
self.templates[name] = Template(fileTemplate)
self.templates[name] = environment.from_string(fileTemplate)
self.metaData[name] = metaData
else:
log.error(f"Trying to load unsupported file '{filePath}'. Supported file ends with '.jinja2'.")
Expand Down
2 changes: 2 additions & 0 deletions mkdoxy/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,11 @@ def append(self, line: str):
self.lines.append(line)

def render(self, f: MdRenderer, indent: str):
f.write('```\n')
for line in self.lines:
f.write(line)
f.write("\n")
f.write('```\n')


class MdBlockQuote(Md):
Expand Down
2 changes: 1 addition & 1 deletion mkdoxy/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ def codeblock(self) -> str:

else:
code.append(self._definition.plain())
return "\n".join(code)
return "\n".join(['```', *code, '```'])

@property
def has_base_classes(self) -> bool:
Expand Down
7 changes: 3 additions & 4 deletions mkdoxy/templates/example.jinja2
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{% filter indent(config.get('indent_level', 0), True) %}
# {{node.title}}

{{node.details}}
{{node.details | use_code_language(node.code_language)}}

{{node.programlisting | use_code_language(node.code_language)}}

```{{ node.code_language }}
{{node.programlisting}}
```
{% endfilter %}
6 changes: 2 additions & 4 deletions mkdoxy/templates/memDef.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@ url_source: True
{%- endif -%}
{%- endif -%}

```{{node.code_language}}
{{node.codeblock}}
```
{{node.codeblock | use_code_language(node.code_language)}}


{% if configMemDef.get('details') -%}
{% if node.has_details -%}
{{node.details}}
{{node.details | use_code_language(node.code_language)}}
{%- endif -%}
{%- endif -%}

Expand Down
2 changes: 1 addition & 1 deletion mkdoxy/templates/member.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Inherited by the following classes:
{%- if node.has_details %}
# Detailed Description

{{node.details}}
{{node.details | use_code_language(node.code_language)}}
{%- endif %}

{%- for visibility in ['public', 'protected'] -%}
Expand Down
2 changes: 1 addition & 1 deletion mkdoxy/templates/page.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{% if node.title %}{{node.title}}{% else %}# Related Pages{% endif %}

{% if node.details %}
{{node.details}}
{{node.details | use_code_language(node.code_language)}}
{% else %}
No related pages found.
{% endif %}
Expand Down
4 changes: 1 addition & 3 deletions mkdoxy/templates/programlisting.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ breadcrumbs: False

[Go to the documentation of this file]({{node.url}})

```{{ node.code_language }}
{{node.programlisting}}
```
{{node.programlisting | use_code_language(node.code_language)}}

{% endfilter %}
Loading