From d4de627de25b381813094896d378db67755b0a34 Mon Sep 17 00:00:00 2001 From: Jason Blevins Date: Fri, 10 Nov 2017 15:34:41 -0500 Subject: [PATCH] Font lock for HTML entities --- CHANGES.md | 2 ++ markdown-mode.el | 9 +++++++++ tests/markdown-test.el | 30 ++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 72a95d7..e02b28f 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -14,6 +14,8 @@ Alexis Gallagher for a patch. ([GH-272][], [GH-274][]) - Added pipe table editing features. Thanks to Dmitry Safronov for a patch. ([GH-171][], [GH-266][]) + - Font lock for HTML entities, with a new face + `markdown-html-entity-face`. * Improvements: diff --git a/markdown-mode.el b/markdown-mode.el index 8e8415e..251883e 100644 --- a/markdown-mode.el +++ b/markdown-mode.el @@ -1886,6 +1886,10 @@ Group 2 matches the opening square bracket. Group 3 matches the footnote text, without the surrounding markup. Group 4 matches the closing square bracket.") +(defconst markdown-regex-html-entity + "\\(&#?[[:alnum:]]+;\\)" + "Regular expression for matching HTML entities.") + ;;; Syntax ==================================================================== @@ -2763,6 +2767,11 @@ For example, this applies to plain angle bracket URLs: "Face for horizontal rules." :group 'markdown-faces) +(defface markdown-html-entity-face + '((t (:inherit font-lock-variable-name-face))) + "Face for HTML entities." + :group 'markdown-faces) + (defcustom markdown-header-scaling nil "Whether to use variable-height faces for headers. When non-nil, `markdown-header-face' will inherit from diff --git a/tests/markdown-test.el b/tests/markdown-test.el index bfb9cd3..036e587 100644 --- a/tests/markdown-test.el +++ b/tests/markdown-test.el @@ -3107,6 +3107,36 @@ note.] And then you can close it and continue writing." across blocks]" (markdown-test-range-has-face (point-min) (point-max) nil))) +(ert-deftest test-markdown-font-lock/html-entity-named () + "Test basic font-lock support for named HTML entities." + (markdown-test-string " " + (markdown-test-range-has-face 1 6 'markdown-html-entity-face))) + +(ert-deftest test-markdown-font-lock/html-entity-hex () + "Test basic font-lock support for hexadecimal HTML entities." + (markdown-test-string "✪" + (markdown-test-range-has-face 1 8 'markdown-html-entity-face))) + +(ert-deftest test-markdown-font-lock/html-entity-decimal () + "Test basic font-lock support for decimal HTML entities." + (markdown-test-string " " + (markdown-test-range-has-face 1 4 'markdown-html-entity-face))) + +(ert-deftest test-markdown-font-lock/html-entity-in-inline-code () + "Test that HTML entities are not matched inside inline code." + (markdown-test-string "` `" + (markdown-test-range-has-face 1 1 'markdown-markup-face) + (markdown-test-range-has-face 2 5 'markdown-inline-code-face) + (markdown-test-range-has-face 6 6 'markdown-markup-face) + (should-not (markdown-range-property-any 1 6 'face '(markdown-html-entity-face))))) + +(ert-deftest test-markdown-font-lock/html-entity-in-gfm-code-block () + "Test that HTML entities are not matched inside GFM code blocks." + (markdown-test-string "```\n \n✪\n \n```" + (should-not + (markdown-range-property-any + (point-min) (point-max) 'face '(markdown-html-entity-face))))) + ;;; Markdown Parsing Functions: (ert-deftest test-markdown-parsing/extend-region-function () -- 2.11.4.GIT