Skip to content
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ __pycache__/
.eggs/
.pytest_cache/
.DS_Store
.venv/
20 changes: 20 additions & 0 deletions codeinclude/languages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from pygments.lexers import get_lexer_for_filename
from pygments.util import ClassNotFound


def get_lang_class(filename: str) -> str:
"""Returns the Pygments _language alias_ for the filename.

Pygments is used by codehilite, a widely used extension for code highlighting:
https://squidfunk.github.io/mkdocs-material/extensions/codehilite/

The Pygments language aliases are expected to be compatible with highlight.js language classes,
which are used by some MkDocs themes: https://www.mkdocs.org/user-guide/styling-your-docs/#built-in-themes
For a table of 'Language -> Language Classes' in _highlight.js_,
see https://github.com/highlightjs/highlight.js#supported-languages
"""
try:
lexer = get_lexer_for_filename(filename)
return lexer.aliases[0]
except ClassNotFound:
return "none"
17 changes: 15 additions & 2 deletions codeinclude/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from mkdocs.plugins import BasePlugin
from codeinclude.resolver import select
from codeinclude.languages import get_lang_class

RE_START = r"""(?x)
^
Expand Down Expand Up @@ -33,7 +34,14 @@


def get_substitute(page, title, filename, lines, block, inside_block):

# Compute the fence header
lang_code = get_lang_class(filename)
header = lang_code
title = title.strip()
if len(title) > 0:
header += f' tab="{title}"'

# Select the code content
page_parent_dir = os.path.dirname(page.file.abs_src_path)
import_path = os.path.join(page_parent_dir, filename)
with open(import_path) as f:
Expand All @@ -45,7 +53,12 @@ def get_substitute(page, title, filename, lines, block, inside_block):

dedented = textwrap.dedent(selected_content)

return '\n```java tab="' + title + '"\n' + dedented + "\n```\n\n"
return f'''
```{header}
{dedented}
```

'''


class CodeIncludePlugin(BasePlugin):
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-e .
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def read_file(fname):
python_requires='>=3.6',
install_requires=[
'mkdocs>=0.17',
'mkdocs'
'pygments'
],
classifiers=[
'Development Status :: 5 - Production/Stable',
Expand Down
18 changes: 18 additions & 0 deletions tests/codeinclude/test_languages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import unittest
from codeinclude.languages import get_lang_class


class MyTestCase(unittest.TestCase):
def test_get_lang_class(self):
self.assertEquals('java', get_lang_class('HelloWorld.java'))
self.assertEquals('python', get_lang_class('HelloWorld.py'))
self.assertEquals('csharp', get_lang_class('HelloWorld.cs'))
self.assertEquals('rust', get_lang_class('HelloWorld.rs'))
self.assertEquals('docker', get_lang_class('Dockerfile'))
self.assertEquals('xml', get_lang_class('HelloWorld.xml'))
self.assertEquals('toml', get_lang_class('HelloWorld.toml'))
self.assertEquals('json', get_lang_class('HelloWorld.json'))


if __name__ == '__main__':
unittest.main()
33 changes: 32 additions & 1 deletion tests/codeinclude/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,21 @@

"""

EMPTY_TITLE_MARKDOWN_EXAMPLE = """
# hello world

some text before
<!--codeinclude-->
[](Foo.java)
<!--/codeinclude-->
and some text after

"""

c = Config(schema=DEFAULT_SCHEMA)
c["site_url"] = "http://example.org/"

PAGE_EXAMPLE = Page("", File(os.path.abspath("./fixture/text.md"), "/src", "/dest", False), c)
PAGE_EXAMPLE = Page("", File(os.path.abspath("./tests/codeinclude/fixture/text.md"), "/src", "/dest", False), c)


class PluginTextCase(unittest.TestCase):
Expand Down Expand Up @@ -85,3 +96,23 @@ def test_multi_tab_case(self):
and some text after
""").strip(),
result.strip())

def test_empty_title_case(self):
plugin = CodeIncludePlugin()
result = plugin.on_page_markdown(EMPTY_TITLE_MARKDOWN_EXAMPLE, PAGE_EXAMPLE, dict())

print(result)
self.assertEqual(textwrap.dedent("""
# hello world

some text before

```java
public class Foo {

}
```

and some text after
""").strip(),
result.strip())