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

Add support for any language: #5

Closed
wants to merge 2 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
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"
8 changes: 7 additions & 1 deletion 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 @@ -44,8 +45,13 @@ def get_substitute(page, title, filename, lines, block, inside_block):
)

dedented = textwrap.dedent(selected_content)
lang_code = get_lang_class(filename)
return f'''
```{lang_code} tab="{title}"
{dedented}
```

return '\n```java tab="' + title + '"\n' + dedented + "\n```\n\n"
'''


class CodeIncludePlugin(BasePlugin):
Expand Down
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'))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, the test seems to fail on this line - I wonder if there's varying support for the toml extension in different versions of pygments.

>       self.assertEquals('toml', get_lang_class('HelloWorld.toml'))
E       AssertionError: 'toml' != 'none'
E       - toml
E       + none

Copy link
Contributor Author

@dmitry-timofeev dmitry-timofeev Jun 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems I used Pygments~=2.5 or newer, which version fails for you? Would it make sense to require a certain version of it, or just remove this test?

self.assertEquals('json', get_lang_class('HelloWorld.json'))


if __name__ == '__main__':
unittest.main()