From ad7c0d8c3dff4a8a8a7e19d5e81699a46ba9a06c Mon Sep 17 00:00:00 2001 From: Arnout Engelen Date: Sat, 20 Aug 2022 10:40:04 +0200 Subject: [PATCH] Add style for php 'blade' templates --- src/reuse/comment.py | 22 +++++++++++++++++++++- tests/test_comment.py | 8 ++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/reuse/comment.py b/src/reuse/comment.py index d48e3bc45..5d2eab7c2 100644 --- a/src/reuse/comment.py +++ b/src/reuse/comment.py @@ -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.""" @@ -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, @@ -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 = { + key.lower(): value for key, value in MULTIPLE_EXTENSION_COMMENT_STYLE_MAP.items() +} + FILENAME_COMMENT_STYLE_MAP = { ".bashrc": PythonCommentStyle, ".bazelignore": PythonCommentStyle, @@ -930,6 +947,9 @@ 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: + suffixes_lower = "".join(path.suffixes).lower() + style = MULTIPLE_EXTENSION_COMMENT_STYLE_MAP_LOWERCASE.get(suffixes_lower) if style is None: style = cast( Optional[Type[CommentStyle]], diff --git a/tests/test_comment.py b/tests/test_comment.py index 51c1e1247..aa897152d 100644 --- a/tests/test_comment.py +++ b/tests/test_comment.py @@ -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 @@ -688,3 +690,9 @@ def test_parse_comment_lisp(): ) assert LispCommentStyle.parse_comment(text) == expected + +def test_get_comment_style(): + 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") == None