-
Notifications
You must be signed in to change notification settings - Fork 7
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
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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?