<ruby>: The Ruby Annotation element
developer.mozilla.org
developer.mozilla.org
I noticed "It can also be used for annotating other kinds of text" and wanted to experiment with being able to number specific letters in a string.
More specifically, SMILES is a linear molecular structure notation. "O" is water, "COO" is ethyl alcohol, "c1ccccc1" is a benzene ring, and much more. (See https://en.wikipedia.org/wiki/Simplified_molecular-input_lin... .)
I want to annotate atom positions in the SMILES string. I currently do this with ("pip install smiview") text over the string, as this example using phenol.
1 23456 7
c1ccccc1O
I wanted to try it with ruby so I used: <ruby>
c<rt>1</rt>1c<rt>2</rt>c<rt>3</rt>c<rt>4</rt>c<rt>5</rt>c<rt>6</rt>1
</ruby>
The "2" is located over the center of "1c" instead of over the second "c" like I wanted.How do I get it to center only over the "c"?
I tried changing the CSS too, using this catch-all:
ruby {
font-size: 2em;
ruby-align: center;
text-align: center;
}
* {ruby-align: center;}
No luck. I also tried wrapping things in a span, like: c<rt>1</rt>1<span>c<rt>2</rt></span>c<rt>3</rt>
but got the 3 as a new ruby line, centered over the "1cc", itself with a ruby "2" between the second and third "c".I tried other combinations of <span>, to no avail.
<ruby>
c<rt>1</rt>
</ruby>1<ruby>
c<rt>2</rt>
</ruby><ruby>
c<rt>3</rt>c<rt>4</rt>c<rt>5</rt>c<rt>6</rt>1
</ruby>SMILES is a lovely standard, really simple and easy enough to write by hand, but powerful enough to describe real molecules in detail. Not long ago, i saw a chemical structure used as part of an illustration, and wondered what it was. I transcribed it as SMILES, put it into a chemical search engine, and found out what it was (nothing interesting!).
I get a good result with this:
<ruby>c<rt>1</rt></ruby>1<ruby>c<rt>2</rt>c<rt>3</rt>c<rt>4</rt>c<rt>5</rt>c<rt>6</rt></ruby>1UPDATE: Here's an example for theobromine - https://jsfiddle.net/j84z1kyb/ .
It looks great!
But it's not as useful as I hoped it would be. Copy&paste captures the numeric annotations. I probably should have expected that, but didn't.
And highlighting is wonky. In Safari and Firefox, I seem to get a character and the ruby annotation for the character next to it, more often than I do the one overhead.
<ruby>C<rt class="ruby-1"></rt></ruby>
With this CSS: .ruby-1::after {
content: "1";
}
(with corresponding treatment for other numbers)Then you should get an annotation that resists copying. This renders correctly for me in Firefox; no idea about other browsers.
<ruby class="smiles">C<rt data-atom="1"></rt></ruby>
With this CSS: .smiles rt::after {
content: attr(data-atom);
}New jsfiddle at https://jsfiddle.net/hnw1zbed/2/ .
Yes, selection does also highlight the annotations but the good part is that copying ignores them.
I just selected the whole formula, pressed Cmd-C, Cmd-V and got this:
Cn1cnc2c1c(=O)[nH]c(=O)n2C C1n21c3n4c52c61c7(=O8)[nH]9c10(=O11)n122C13
but in Firefox gives: Cn1cnc2c1c(=O)[nH]c(=O)n2CThat's very likely a browser bug. Think about the accessibility implications (for e.g. screen readers) if ruby text was supposed to be modelled in the DOM as being interpolated into the text it's annotating.
I'm not sure what the standard says about how it should be treated, but my guess is that each annotation should be thought of as alternative for the text it annotates — ala the image "alt" and "srcset" attributes, or ala videos with multiple audio tracks in the same language, where one of those is Described Video or director's commentary or whatever.
In other words, the "correct" behavior would likely be that your browser knows the user's language prefs, and then chooses to select (or copy, or speak, etc.) either the text or its annotation, depending on which one the user is more likely to be able to read/understand.
The <rp> tag exists explicitly for the purpose of interpolating the annotations into the text. So e.g. <ruby>漢字<rp>(</rp><rt>かんじ</rt><rp>)</rp></ruby> will look like 漢字(かんじ) to a client that doesn't support ruby text and a screenreader could read it the same way it would read any other text with parenthetical annotations in it.
The standard doesn't actually say what screenreaders are supposed to do, so I guess they could also try something fancy. But they don't have to.
If the box you were pasting into supported annotations then it would paste perfectly. Pasting into a plain text text-area field leaves the browser with a hard choice to make on how to interpret the data in the clipboard when transliterating it into plain text.
I just now tried pasting into a Jupyter notebook, and into the HTML entry box of the JSFiddle I linked to.
Again, Safari copy&paste to those elements in Safari includes the annotations.
Firefox does not.
Pasting to the terminal and pasting to a Jupyter notebook are my two primary expected paste destinations.
ruby rt { user-select: none; }It's something I've toyed with putting into my toy font renderers but it always seemed like it had a lot of edge cases. Length of the ruby text overflowing the width of the parent, in some to most cases a little overflow is ok but it's certainly not guaranteed. Scaling down the ruby text isn't the ideal solution because it quickly becomes unreadable. The other option is to scale the spacing in the parent text, which seems to be done for <ruby>境界面<rt>インターフェース</ruby> in the specification https://html.spec.whatwg.org/multipage/text-level-semantics.... but then that's going to impact the line wrapping and so on. Kudos to the implementators!
Sadly, the rp page doesn't show any examples of what fallback behavior might look like.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/rp
<ruby>
明日 <rp>(</rp><rt>Ashita</rt><rp>)</rp>
</ruby>
But the browser didn't have support <ruby>, it would just render as:明日 (Ashita)
The idea is that a <ruby>-supporting browser wouldn't need to render the parentheses, because it's going to display "Ashita" in a special way that sets it off from the regular text (so you "annotate" the parens with <rp>). But in a browser that doesn't support <ruby>, you'd want it to still display in a sane, understandable way, where it would still be easy to understand that the added text is a pronunciation hint.
Would this make sense for putting romanized text above non-roman languages?
Currently the standard is just to write the native text and then the romanized. See, e.g.: https://en.wikipedia.org/wiki/Weekly_Sh%C5%8Dnen_Jump
I'm thinking Hepburn above the kana: https://en.wikipedia.org/wiki/Hepburn_romanization
Anyone with more language knowledge want to cuss me out this idea?
Wikipedia [1] offers a few additional examples for other languages.
Chinese (pinyin):
<ruby>
北 <rp>(</rp><rt>běi</rt><rp>)</rp>
京 <rp>(</rp><rt>jīng</rt><rp>)</rp>
</ruby>
Chinese (zhuyin): <ruby>
北 <rp>(</rp><rt>ㄅㄟˇ</rt><rp>)</rp>
京 <rp>(</rp><rt>ㄐ丨ㄥ</rt><rp>)</rp>
</ruby>
Korean (hangul): <ruby>
韓 <rp>(</rp><rt>한</rt><rp>)</rp>
國 <rp>(</rp><rt>국</rt><rp>)</rp>
</ruby>
Vietnamese (chữ Quốc ngữ): <ruby>
河 <rp>(</rp><rt>Hà</rt><rp>)</rp>
內 <rp>(</rp><rt>Nội</rt><rp>)</rp>
</ruby>
[1]: https://en.wikipedia.org/wiki/Ruby_characterIt shows the rt tag on hover or focus and works for me for both mouse and touch on Anki and AnkiDroid. Maybe this or some variation might help others as well.
ruby {
text-decoration: underline dotted;
}
ruby rt {
visibility: hidden;
}
ruby:hover rt, ruby:focus rt {
visibility: visible;
}This is definitely not going to be confusing... /s
The <ruby> HTML element
But the ruby tag got stripped by HN and I ended up with The HTML element <ruby>
? The andlt;Ruby> HTML element
https://cln.sh/LteC0SYou haven't been confused for the last 11 years, so this doesn't seem to have been a problem.
[1] The W3C spec is even older.
You underestimate me! Or over.
For example, this script[1] will show original English word above Japanese loan words, using the ruby element.
[1]: https://greasyfork.org/en/scripts/33268-katakana-terminator
It's commonly used in textbooks for different Chinese languages. I have ministry of education textbooks from Taiwan, and ruby characters are used for both Hokkien and Mandarin (the Hokkien one has two different ruby character scripts which is quite visually busy).
I would imagine it would be handy for Hindi learners as well. And probably hundreds of other languages, though I can't speak if it is used.
I got a little worried that browsers were going to support that horrible programming language with the same name as a script tag or something.