by djedr · 4y ago · 0 comments
enum Element {
Text(String),
Node {
tag: String,
attributes: Map<String, String>
children: Vec<Element>,
}
}
fn parse_markup(input: &str) -> Element { ... }
> Markup language which nails this perfectly is HTML.The reason HTML nails it perfectly is because this is modeled after HTML.
If I were to make up a markup language, I wouldn't follow that model.
In particular I would get rid of attributes which to me are a restricted kind of children with a specialized syntax.
This is both unnecessary and undesirable in many cases.
The major problem with attributes is the <String, String> mapping. Once something is defined to be an attribute, it cannot be sensibly extended without creating an unnecessary problem.
For example the `class` attribute in HTML looks like it was originally designed to hold a single class name. Then people realized that it would be desirable to have multiple classes per element.
So, to keep things backwards-compatible, the value of the attribute was extended to hold a space-separated list of classes instead. Essentially creating a little DSL inside of the attribute's value.
If `class` was instead a kind of child, initially limited to a single instance per element, extending to multiple instances in a backward-compatible manner would not require introducing the DSL. It would be natural. Just allow many `class` children.
A valid argument in HTML in favor of attributes is conciseness. But that's an artifact of the syntax.
We could make up a language with syntax for nodes as concise as HTML attributes, eliminating that argument.
> It feels like there’s a smaller, simpler language somewhere
Certainly.
I have experimented with many different designs for such a markup language on top of Jevko[0]. One interesting design that trivially maps to HTML looks like this:
h1 [Title]
p [
paragraph
]
p [
[paragraph with a ]
a [
href=[...]
[link]
]
]
Another one looks like this: [h1][Title]
[p][paragraph]
[p][
paragraph with a
[href[...] a][link]
]
Both are extremely simple and minimal, but also extensible and lend themselves to writing by hand.That's not the operative difference between children and attributes in HTML.
In HTML, if an element is unsupported, its contents are shown, while its attributes are ignored. This means, if a browser saw something like this:
<p>
<class>literature</class>
<class>english</class>
Billions of years ago, the Universe was created. This made a lot of people very angry, and has been widely considered a bad idea.
</p>
In browsers that don't support, or even predate classes, you would want them to be ignored. This only works if they're attributes.If, in this language, you wanted to have this feature in combination with what I proposed, you could simply mark the attribute-like children, to inform the engine that it should apply different defaults for them and unmarked children that it doesn't recognize.
This is what I do in the first variant of my language:
a [
href=[...]
[...]
]
the `=` appended to `href` marks it as an attribute.This certainly simplifies things when translating to HTML.
But if we were not constrained by the legacy of HTML then perhaps bothering with this when designing a new language would be unnecessary. Maybe not having this default behavior does not matter in practice and you can have a simpler language without it.
To determine whether that's the case it would help to answer: when is the feature you described useful in HTML? And also: when is this feature harmful?
* as long as you use a build tool the errors will be seen by the author (who will know how to fix them) and not the reader
* graceful degradation and format extensions are a crapshoot due to Hyrum’s Law [1]
Other options would be to ignore everything you don't recognize or ignore all nodes of the form:
unrecognized tag [value]
i.e. where you don't recognize the key and the value is not complex.You could still display stuff like:
unrecognized tag [
[text]
...
]
i.e. text nodes which are children of unrecognized elements.But approaches like these would need a good justification.
You can eliminate attributes to get a simpler format, but it isn't really a markup language anymore, since the distinction between text and markup is removed. It is just a generic data structure, like JSON or s-expressions.
That may have been the idea, but it falls apart in implementation. Lines between content and metadata get blurred.
Consider attributes like: input/value, input/placeholder, optgroup/label, img/alt, title.
And then elements like: script, colgroup, col, title, noscript, noframes, applet, object, the void elements[0].
Then consider the ability of CSS to add content to elements, including lifting it from attributes[1], and, on the other side, completely hiding element contents.
The line between metadata and data is conventional. It's all data in the end.
But let's suppose it isn't and there is a hard line. Why should the metadata in attributes not be allowed to have structure and be extensible in the same way data is?
> You can eliminate attributes to get a simpler format, but it isn't really a markup language anymore, since the distinction between text and markup is removed. It is just a generic data structure, like JSON or s-expressions.
What I'm saying is that HTML-like attributes are not necessary to keep the distinction. You can keep the functionality, simplify things and at the same time make them more flexible.
HTML-like attributes also don't stop people from using the language to describe generic data structures (XML).
On the other side, if JSON or S-expressions had syntax and semantics that made them suitable as markup languages, I don't see a problem with that.
[0] https://developer.mozilla.org/en-US/docs/Glossary/Void_eleme...
A markup language is human-readable text with added markup. A plain text file is basically a valid HTML file (I believe <title> is the only required element). You can then add markup to provide structure and metadata.
This sets it apart from generic data structure formats. You can express the same information in JSON, but in JSON there is no distinction between text content and other information, it is all just structured data.
Something like s-expression is clearly simpler than HTML syntax, and it is endlessly flexible, but it is just not optimized towards writing structured text content the way markup languages are.
Indeed, markup languages are built around text whereas in JSON/S-expressions/syntaxes designed for expressing data, text is secondary.
The syntax I created is uniquely flexible in that it can be used for both data and markup.
The first variant of the two in my original comment is more data-like, but minimal enough that it works sufficiently well as markup too. Text is a little harder to mark up in this variant than in HTML, as individual text nodes have to be wrapped in []. However this gives precise control over the content of the text nodes and separates indentation and other whitespace of the document tree from whitespace which is intended as actual content:
[paragraph with a ]
a [
href=[...]
[link]
]
The second variant is very much like HTML: text here is primary and interleaved with markup in []: paragraph with a [href[...] a][link]
This variant also separates the attributes and tag from the children, much like HTML. However the attributes are technically not restricted to being flat strings. If we weren't translating to HTML, we could have attributes with tree structure.So we have something simpler and more flexible than both HTML and S-expressions. This is the beauty and value of it.
I hope I can succeed in communicating this to potential users, so they can benefit from it.
Take this example of unfortunate mixing:
{% somecomponent attr="value", title="<strong>A:</strong> B" %}
<strong>C:</strong> D
{% endsomeecomponent %}
Trouble is that you basically have at most one markup slot, but many things semantically want more than one. The closest you can generally get in such languages as these is the likes of this: {% somecomponent attr="value" %}
<strong>A:</strong> B
{% body %}
<strong>C:</strong> D
{% endsomecomponent %}
This can be done in some such languages, but not others—and can’t be done in anything like XML. In XML, the solution would be extra nesting, which gets messy quickly: <somecomponent attr="value">
<title>
<strong>A:</strong> B
</title>
<body>
<strong>C:</strong> D
</body>
</somecomponent>
In JSX I suppose you could do something like this, for better or for worse: <SomeComponent attr="value" title={<><strong>A:</strong> B</>}>
<strong>C:</strong> D
</SomeComponent>
If you use a fragment/child nodes for what is logically a string value, you run the risk of people putting non-text in there; things like an href should clearly be strings, not text nodes—or else you have to decide what to do with <a><href>/<b>exam</b>ple</href>…</a>, and poorly-written DOM manipulators will surely occasionally assume the href element contains only one text node at times. But there’s then also the question of whether you want strings to be usable in text node context (probably the more pragmatic approach), or if you want to maintain a strong distinction between the two (probably the more correct approach).For my own language, in the more macroy part (when you’re extending it beyond the basic elements), I’m maintaining a distinction between strings and markup fragments, using different delimiters for the two, with "…" for strings and {…} for fragments. I’m also treating attributes and children equivalently, having all as possibly-named children. The basic example could be something like this in my current syntax:
@somecomponent(attr: "value", title: {**A:** B}, {
**C:** D
})What I show here is in fact even simpler and more flexible than S-exps[0].
[0] For some details and polemic see this thread: https://news.ycombinator.com/item?id=33334789 | TL;DR: Jevko is well-defined, basically just unicode text + escapeable brackets for making trees; it doesn't treat whitespace as a separator/atmosphere (particularly important in markup); it takes advantage of natural name-value pairing tendencies (like tag-children); and it's closed under concatenation by design