Skip to content
Merged
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
27 changes: 17 additions & 10 deletions src/textual/highlight.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class HighlightTheme:
}


def guess_language(code: str, path: str) -> str:
def guess_language(code: str, path: str | None) -> str:
"""Guess the language based on the code and path.
The result may be used in the [highlight][textual.highlight.highlight] function.

Expand All @@ -64,19 +64,28 @@ def guess_language(code: str, path: str) -> str:
The language, suitable for use with Pygments.
"""

if path is not None and os.path.splitext(path)[-1] == ".tcss":
if path and os.path.splitext(path)[-1] == ".tcss":
# A special case for TCSS files which aren't known outside of Textual
return "scss"

lexer: Lexer | None = None
lexer_name = "default"
if code:
try:
lexer = guess_lexer_for_filename(path, code)
except ClassNotFound:
pass
if path:
try:
lexer = guess_lexer_for_filename(path, code)
except ClassNotFound:
pass

if lexer is None:
from pygments.lexers import guess_lexer

try:
lexer = guess_lexer(code)
except Exception:
pass

if not lexer:
if not lexer and path:
try:
_, ext = os.path.splitext(path)
if ext:
Expand Down Expand Up @@ -113,9 +122,7 @@ def highlight(
Returns:
A Content instance which may be used in a widget.
"""
if language is None:
if path is None:
raise RuntimeError("One of 'language' or 'path' must be supplied.")
if not language:
language = guess_language(code, path)

assert language is not None
Expand Down
3 changes: 2 additions & 1 deletion src/textual/widgets/_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -870,12 +870,13 @@ def __init__(self, markdown: Markdown, token: Token, code: str) -> None:
self.lexer = token.info
self._highlighted_code = self.highlight(self.code, self.lexer)

@property
def allow_horizontal_scroll(self) -> bool:
return True

@classmethod
def highlight(cls, code: str, language: str) -> Content:
return highlight(code, language=language)
return highlight(code, language=language or None)

def _copy_context(self, block: MarkdownBlock) -> None:
if isinstance(block, MarkdownFence):
Expand Down
4 changes: 4 additions & 0 deletions tests/test_highlight.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ def test_highlight() -> None:
@pytest.mark.parametrize(
"code,path,language",
[
("", "", "default"),
("# Don't matter", "foo.tcss", "scss"),
("import this", "foo.py", "python"),
("<xml>", "foo.xml", "xml"),
("{}", "data.json", "json"),
("#! python", "", "python"),
("", "foo.py", "python"),
],
)
def test_guess_language(code: str, path: str, language: str) -> None:
Expand Down
Loading