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 style for php 'blade' templates #573

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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