For a while I’ve been talking about making the CSS formal syntax more usable. See the previous post Pretty-printing the CSS formal syntax for more context.
I’ve been working on this and now have something to show for it. Here’s what the old formal syntax for color
looks like:
Here’s what the new macro produces:
Here are a couple more examples:
It makes 2 main changes:
- some basic indentation/pretty printing
- some syntax highlighting
It uses the (amazing) css-tree npm package to parse formal syntax definitions. This enables us to do more with shorter, simpler macro code.
If you want to try it out
-
install a local Kuma: https://kuma.readthedocs.io/en/latest/installation.html
-
point its kumascript directory at the “better-formal-syntax” branch of https://github.com/wbamberg/kumascript/
cd kumascript/
git remote set-url origin https://github.com/wbamberg/kumascript
git fetch origin better-formal-syntax
git checkout better-formal-syntax
(https://github.com/mdn/kumascript#updating-macros)
- rebuild the KumaScript image:
cd ..; KS_VERSION=latest make build-kumascript; cd kumascript
(https://github.com/mdn/kumascript#updating-the-dockerfile-andor-packagejson-file)
- restart the Docker containers:
docker-compose stop
docker-compose up -d
- scrape some CSS reference pages:
docker exec kuma_web_1 python manage.py scrape_document https://developer.mozilla.org/en-US/docs/Web/CSS/border
- open the pages, and look at the “Formal syntax” block. The new macro keeps the old name (csssyntax.ejs) and the same interface, so you don’t need to edit any MDN pages for you to see the difference.
Questions, notes, and stuff
-
It’s not finished yet. It only supports CSS properties, it only adds links for types that are in the same block (e.g. it doesn’t add links to basic types), it doesn’t add tooltips, or support l10n. But these should all be fairly simple to add. Oh, and it has bugs, and no tests.
-
In the first version of this, I wanted to use a “legend” section at the bottom (for things like combinator syntax help) instead of tooltips. Having tried this out, I think tooltips are probably a better option whenever the formal syntax is more than a screenful (which it often is). I still don’t like tooltips, but can’t think of a better approach that doesn’t involve front-end work.
-
For the syntax highlighting, I’ve used the same classes that we already use for CSS syntax highlighting, so we get the same colours. I don’t like these colours very much (in particular, the blue-on-blue contrast is poor, and I think blue and red are too much like links and links-to-nonexistent-pages). But I wanted to be consistent.
-
Having tried it out, the most obvious regression from the original is that the formal syntax is much longer - it seems to come out around three times as long on average. This is OK for shorter items, but for long ones like
mask
it’s about 7 screenfuls. It would perhaps be better to hide the whole thing (or at least everything after the first, top-level, type) under a “Details” disclosure arrow.