Skip to content

Commit

Permalink
Add style for php 'blade' templates
Browse files Browse the repository at this point in the history
  • Loading branch information
raboof committed Feb 3, 2025
1 parent 920b644 commit 4e04dae
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/reuse/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,14 @@ class BibTexCommentStyle(CommentStyle):
SHEBANGS = ["% !BIB", "%!BIB"]


class BladeCommentStyle(CommentStyle):
"""Laravel Blade Template comment style."""

_shorthand = "blade"

MULTI_LINE = MultiLineSegments("{{--", "", "--}}")


class CCommentStyle(CommentStyle):
"""C comment style."""

Expand Down Expand Up @@ -709,7 +717,6 @@ class XQueryCommentStyle(CommentStyle):
".mk": PythonCommentStyle,
".ml": MlCommentStyle,
".mli": MlCommentStyle,
".nim.cfg": PythonCommentStyle, # Nim-lang build config parameters/settings
".nim": PythonCommentStyle,
".nimble": PythonCommentStyle, # Nim-lang build config
".nimrod": PythonCommentStyle,
Expand Down Expand Up @@ -837,6 +844,16 @@ class XQueryCommentStyle(CommentStyle):
key.lower(): value for key, value in EXTENSION_COMMENT_STYLE_MAP.items()
}

#: A map of (common) double file extensions against comment types.
MULTIPLE_EXTENSION_COMMENT_STYLE_MAP = {
".blade.php": BladeCommentStyle,
".nim.cfg": PythonCommentStyle, # Nim-lang build config parameters/settings
}

MULTIPLE_EXTENSION_COMMENT_STYLE_MAP_LOWERCASE = {
k.lower(): v for k, v in MULTIPLE_EXTENSION_COMMENT_STYLE_MAP.items()
}

FILENAME_COMMENT_STYLE_MAP = {
".bashrc": PythonCommentStyle,
".bazelignore": PythonCommentStyle,
Expand Down Expand Up @@ -930,6 +947,10 @@ def get_comment_style(path: StrPath) -> Optional[Type[CommentStyle]]:
"""Return value of CommentStyle detected for *path* or None."""
path = Path(path)
style = FILENAME_COMMENT_STYLE_MAP_LOWERCASE.get(path.name.lower())
if style is None:
style = MULTIPLE_EXTENSION_COMMENT_STYLE_MAP_LOWERCASE.get(
"".join(path.suffixes).lower()
)
if style is None:
style = cast(
Optional[Type[CommentStyle]],
Expand Down
10 changes: 10 additions & 0 deletions tests/test_comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
import pytest

from reuse.comment import (
BladeCommentStyle,
CommentStyle,
CppCommentStyle,
HtmlCommentStyle,
LispCommentStyle,
PythonCommentStyle,
_all_style_classes,
get_comment_style,
)
from reuse.exceptions import CommentCreateError, CommentParseError

Expand Down Expand Up @@ -688,3 +690,11 @@ def test_parse_comment_lisp():
)

assert LispCommentStyle.parse_comment(text) == expected


def test_get_comment_style():
"""Select the right style based on the filename"""
assert get_comment_style("foo.php") == CppCommentStyle
assert get_comment_style("foo.blade.php") == BladeCommentStyle
assert get_comment_style("foo.bar.blade.php") == CppCommentStyle
assert get_comment_style("foo.php.blade") is None

0 comments on commit 4e04dae

Please sign in to comment.