|
| 1 | +;;; flycheck-css-colorguard.el --- Detect similar colors in CSS |
| 2 | + |
| 3 | +;; Copyright (C) 2015 Saša Jovanić |
| 4 | + |
| 5 | +;; Author: Saša Jovanić <[email protected]> |
| 6 | +;; URL: https://github.com/simplify/flycheck-css-colorguard/ |
| 7 | +;; Version: 0.1.0 |
| 8 | +;; Keywords: flycheck, CSS |
| 9 | +;; Package-Requires: ((flycheck "0.22") (emacs "24.4")) |
| 10 | + |
| 11 | +;; This file is not part of GNU Emacs. |
| 12 | + |
| 13 | +;; This program is free software: you can redistribute it and/or modify |
| 14 | +;; it under the terms of the GNU General Public License as published by |
| 15 | +;; the Free Software Foundation, either version 3 of the License, or |
| 16 | +;; (at your option) any later version. |
| 17 | + |
| 18 | +;; This program is distributed in the hope that it will be useful, |
| 19 | +;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | +;; GNU General Public License for more details. |
| 22 | + |
| 23 | +;; You should have received a copy of the GNU General Public License |
| 24 | +;; along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 25 | + |
| 26 | + |
| 27 | +;;; Commentary: |
| 28 | + |
| 29 | +;; This is extension for Flycheck. |
| 30 | + |
| 31 | +;; From https://github.com/SlexAxton/css-colorguard: |
| 32 | +;; CSS Colorguard helps you maintain the color set that you want, and warns |
| 33 | +;; you when colors you've added are too similar to ones that already exist. |
| 34 | + |
| 35 | +;; For more infomations about CSS Colorguard, please check the GitHub |
| 36 | +;; https://github.com/SlexAxton/css-colorguard |
| 37 | + |
| 38 | +;; For more information about Flycheck: |
| 39 | +;; http://www.flycheck.org/ |
| 40 | +;; https://github.com/flycheck/flycheck |
| 41 | + |
| 42 | +;; For more information about this Flycheck extension: |
| 43 | +;; https://github.com/simplify/flycheck-css-colorguard |
| 44 | + |
| 45 | + |
| 46 | +;;;; Setup |
| 47 | + |
| 48 | +;; (eval-after-load 'flycheck |
| 49 | +;; '(progn |
| 50 | +;; (require 'flycheck-css-colorguard) |
| 51 | +;; (flycheck-add-next-checker 'css-csslint |
| 52 | +;; 'css-colorguard 'append))) |
| 53 | + |
| 54 | + |
| 55 | +;;;; CSS Colorguard output 1.0.0 or higher: |
| 56 | +;; line 2 col 3 #020202 collides with rgba(0,0,0,1) (2:44) |
| 57 | +;; line 3 col 3 #000000 collides with rgba(0,0,0,1) (2:44) |
| 58 | +;; line 3 col 3 #000000 collides with #020202 (2:59) |
| 59 | +;; line 7 col 3 black collides with rgba(0,0,0,1) (2:44) |
| 60 | +;; line 7 col 3 black collides with #020202 (2:59) |
| 61 | +;; line 12 col 3 rgb(0,0,0) collides with rgba(0,0,0,1) (2:44) |
| 62 | +;; line 12 col 3 rgb(0,0,0) collides with #020202 (2:59) |
| 63 | +;; line 13 col 3 rgba(0,0,0,1) collides with #020202 (2:59) |
| 64 | +;; line 16 col 3 hsl(0,0%,0%) collides with rgba(0,0,0,1) (2:44) |
| 65 | +;; line 16 col 3 hsl(0,0%,0%) collides with #020202 (2:59) |
| 66 | +;; line 17 col 3 hsla(0,0%,0%,1) collides with rgba(0,0,0,1) (2:44) |
| 67 | +;; line 17 col 3 hsla(0,0%,0%,1) collides with #020202 (2:59) |
| 68 | +;; line 20 col 3 #010101 collides with rgba(0,0,0,1) (2:44) |
| 69 | +;; line 20 col 3 #010101 collides with #020202 (2:59) |
| 70 | +;; line 52 col 5 #000000 collides with rgba(0,0,0,1) (2:44) |
| 71 | +;; line 52 col 5 #000000 collides with #020202 (2:59) |
| 72 | +;; line 52 col 5 #000000 collides with #010101 (20:20) |
| 73 | + |
| 74 | +;;;; CSS Colorguard output before 1.0.0: |
| 75 | +;; Collision: #000000, #020202 |
| 76 | +;; - rgba(0,0,0,1) (#000000) [line: 2, 3, 7, 12, 13, 16, 17, 52] is too close (0.3146196209793196) to #020202 (#020202) [line: 2] |
| 77 | +;; Collision: #000000, #010101 |
| 78 | +;; - rgba(0,0,0,1) (#000000) [line: 2, 3, 7, 12, 13, 16, 17, 52] is too close (0.15712369811016996) to #010101 (#010101) [line: 20] |
| 79 | +;; Collision: #020202, #010101 |
| 80 | +;; - #020202 (#020202) [line: 2] is too close (0.1574963682909058) to #010101 (#010101) [line: 20] |
| 81 | + |
| 82 | + |
| 83 | +;;; Code: |
| 84 | + |
| 85 | +(require 'flycheck) |
| 86 | + |
| 87 | +;;;; For output before css-colorguard 1.0.0 |
| 88 | +;; (defun flycheck-parse-css-colorguard (output checker buffer) |
| 89 | +;; "Proces OUTPUT from colorguard into flycheck errors. |
| 90 | +;; CHECKER and BUFFER are returned with flycheck error." |
| 91 | +;; (let (errors) |
| 92 | +;; (dolist (line (delete "" (split-string output "Collision: "))) |
| 93 | +;; (let ((good-part (nth 1 (split-string line " - ")))) |
| 94 | +;; (let ((first-color (car (split-string good-part " \\[Line: "))) |
| 95 | +;; (second-color (car (split-string (nth 1 (split-string good-part " to ")) " \\[Line: "))) |
| 96 | +;; (line-numbers (car (split-string (nth 1 (split-string good-part " \\[Line: ")) "]"))) |
| 97 | +;; (second-line (car (split-string (nth 2 (split-string good-part " \\[Line: ")) "]"))) |
| 98 | +;; (collision (car (split-string (nth 1 (split-string good-part "]")) " to "))) |
| 99 | +;; ) |
| 100 | +;; (dolist (error-line (split-string line-numbers ", ")) |
| 101 | +;; (push (flycheck-error-new-at |
| 102 | +;; (flycheck-string-to-number-safe error-line) |
| 103 | +;; nil |
| 104 | +;; 'info |
| 105 | +;; (format "%s is too close to %s at line %s" first-color second-color second-line) |
| 106 | +;; :checker checker |
| 107 | +;; :buffer buffer) |
| 108 | +;; errors) |
| 109 | +;; (push (flycheck-error-new-at |
| 110 | +;; (flycheck-string-to-number-safe second-line) |
| 111 | +;; nil |
| 112 | +;; 'info |
| 113 | +;; (format "%s is too close to %s at line %s" second-color first-color error-line) |
| 114 | +;; :checker checker |
| 115 | +;; :buffer buffer) |
| 116 | +;; errors))))) |
| 117 | +;; (nreverse errors))) |
| 118 | + |
| 119 | +(flycheck-define-checker css-colorguard |
| 120 | + "Detect similar colors in CSS using CSS Colorguard. |
| 121 | +
|
| 122 | +See URL |
| 123 | +`https://github.com/SlexAxton/css-colorguard'." |
| 124 | + :command ("colorguard" "--file" source) |
| 125 | + ;;(option "--threshold" "3") |
| 126 | + ;; :error-parser flycheck-parse-css-colorguard ;; For css-colorguard older then 1.0.0 |
| 127 | + :error-patterns |
| 128 | + ((warning line-start |
| 129 | + " line " line (one-or-more " ") " col " column (one-or-more " ") (message) line-end)) |
| 130 | + :modes (css-mode)) |
| 131 | + |
| 132 | +(add-to-list 'flycheck-checkers 'css-colorguard 'append) |
| 133 | + |
| 134 | +(provide 'flycheck-css-colorguard) |
| 135 | + |
| 136 | +;;; flycheck-css-colorguard.el ends here |
0 commit comments