From 6bb465a6f2d24ed38d53b9c2d2d7ca2c20b72458 Mon Sep 17 00:00:00 2001 From: Andy Stewart Date: Mon, 17 Apr 2023 14:55:44 +0100 Subject: [PATCH] Add support for Ruby/Rails hashes (`class: "..."`) See #1. --- README.markdown | 13 ++++++++++--- tcs | 9 ++++++--- test/fixtures/rails.expected.html | 1 + test/fixtures/rails.html | 1 + 4 files changed, 18 insertions(+), 6 deletions(-) create mode 100644 test/fixtures/rails.expected.html create mode 100644 test/fixtures/rails.html diff --git a/README.markdown b/README.markdown index b825a61..fc1ca06 100644 --- a/README.markdown +++ b/README.markdown @@ -1,15 +1,22 @@ # tcs - Tailwind Class Sorter -A standalone script which sorts the classes in your HTML following Tailwind's [recommended class order](https://tailwindcss.com/blog/automatic-class-sorting-with-prettier#how-classes-are-sorted). It does not require [Prettier](https://tailwindcss.com/docs/editor-setup#automatic-class-sorting-with-prettier). +A standalone script which sorts the classes in your markup following Tailwind's [recommended class order](https://tailwindcss.com/blog/automatic-class-sorting-with-prettier#how-classes-are-sorted). It does not require [Prettier](https://tailwindcss.com/docs/editor-setup#automatic-class-sorting-with-prettier). You can run it on the command line or integrate it with your text editor. ## Features -- It sorts the classes in your HTML according to Tailwind's recommended order. -- It leaves the rest of your HTML alone. +- It sorts the classes in your markup according to Tailwind's recommended order. +- It leaves the rest of your markup alone. - It does not require Prettier. +- Supports HTML class attributes (`class="..."`) and Ruby/Rails hashes (`class: "..."`). + + +## Constraints + +- Your class names have to be enclosed with double- (not single-) quotation marks (`

`). +- [Slim] You have to use `class="foo bar"` rather than the `.foo.bar` class shortcut. ## Installation diff --git a/tcs b/tcs index 10c1594..d112071 100755 --- a/tcs +++ b/tcs @@ -11,9 +11,12 @@ class Tcs [,{] /x - # Class declaration in HTML, i.e. the stuff inside the quotation marks - # in `class="..."`. - CLASS_LIST = /(?<=class=")([^"]+)(?=")/ + # Class declaration in markup, i.e. the stuff inside the quotation marks + # in `class="..."` or `class: "..."`. + # + # Note that the lookbehind alternatives have to be fixed-length so we cannot + # match a variable number of spaces after `class:`. + CLASS_LIST = /(?<=class="|class:\s")([^"]+)(?=")/ # modifier -> css PSEUDO_CLASSES = { diff --git a/test/fixtures/rails.expected.html b/test/fixtures/rails.expected.html new file mode 100644 index 0000000..d42fe77 --- /dev/null +++ b/test/fixtures/rails.expected.html @@ -0,0 +1 @@ += link_to "Home", root_path, class: "bg-sky-700 px-4 py-2 text-white hover:bg-sky-800 sm:px-8 sm:py-3" diff --git a/test/fixtures/rails.html b/test/fixtures/rails.html new file mode 100644 index 0000000..53b1786 --- /dev/null +++ b/test/fixtures/rails.html @@ -0,0 +1 @@ += link_to "Home", root_path, class: "text-white px-4 sm:px-8 py-2 sm:py-3 bg-sky-700 hover:bg-sky-800"