UserJS for HTML Ruby

Forums » » User JavaScript

You need to be logged in to post in the forums. If you do not have an account, please sign up first.

Go to last post

17. October 2006, 06:16:28

kailapis

^^;

Posts: 150

UserJS for HTML Ruby

I actually didn't know this forum existed, and after being sure that there weren't any UserJS for ruby (searching for ruby in userjs.org...), I went on to create a UserJS to render ruby annotation... somewhat. Only to find that White Lynx had created a XHTML one. This one isn't really XML/XHTML compliant and such, but it does work for the basic simple/complex ruby annotations in HTML/XHTML and, to some extent, malformed ruby tags.

html ruby userjs

Examples by White Lynx:
http://xml-maiden.com/userjs/ruby/ruby.xml
http://xml-maiden.com/userjs/ruby/ruby.xhtml

Other Examples:
http://trans.hiragana.jp/ruby/http://www.nintendo-inside.jp/news/186/18628.html
http://en.wikipedia.org/wiki/Chinese_grammar (from iopg)

Malformed Examples:
http://www.htmq.com/html/ruby.shtml (it actually breaks it, but not really noticeable)
http://html.eweb-design.com/0406_rub.html (noticeably broken)
http://files.myopera.com/Kai_Lapis/files/ruby.html (malformed, but within extent)


18. October 2006, 05:41:53

kailapis

^^;

Posts: 150

ver 2.2:
Added a disable button (10sec) when the rendering is likely broken.

18. October 2006, 15:43:08

jesus2099

♡.. \(^Δ^;)/ ..☭

Posts: 356

It is great that yours is not restricted to xhtml pages only !
I never quite understand why White Lynx does this exactly...

But could you add some style cancellation in your td and th borders, margins, paddings etc. ?
See this page and here is why I tell you that. The ふりがな here takes the surrounding table's border. It shouldn't do that sould it ?

Please put something like that on your cells :

style="padding: 0; margin: 0; border: 0;"
as it should solve the situation.
And font is maybe a bit too small.
jesus2099 × Ti = Tristan + patate12 ÷ saucisson7 éditer bug report
※ My bugs and whishlist for Opera
Using latest stable version on Windows XP

18. October 2006, 19:53:09 (edited)

kailapis

^^;

Posts: 150

Thanks for the feedback.

ver 2.3:
style selector .tRuby -> table.tRuby to override the page's class style
ruby text size .5em -> .7em

ver 2.31:
changed fault check ruby.innerHTML.length to rt.textContent.length

ver 2.4:
made easier for user to change ruby text's size and line-height and disable notice duration

19. October 2006, 04:49:23 (edited)

kailapis

^^;

Posts: 150

Version 3 - ruby3.js

Another example page: W3C I18N Test Suite: Ruby markup

ver 3.0:
Added ruby tag cleaning for unclosed rb, rt, and rp tags.

ver 3.01:
Fixed where cleaning resulted in double body tags.

ver 3.02:
Changed document.getElementsByTagName('body') to document.body
Disabled ruby support for huge pages.

ver 3.1:
Changed handling order (clean, remove tag -> remove tag, clean) when there is a unclosed ruby tag so as not to generate an error.

For some reason, the script will mangle the display encoding (?) for some pages... and I can't figure out why. The info panel shows that Opera is using the right encoding, but it still appears as if it's using some other encoding.

19. October 2006, 10:12:42

White Lynx

Posts: 1978

Nice to see another approach smile

19. October 2006, 13:15:50

kailapis

^^;

Posts: 150

ver 3.2:
Added "request.overrideMimeType('text/html; charset=' + document.characterSet);" to try to override defaults, but Opera still seems to treat it as text/xml utf-8(?)

19. October 2006, 14:21:17

jesus2099

♡.. \(^Δ^;)/ ..☭

Posts: 356

Sorry Kai,
If there is a problem with ruby size then I should print bigger characters on my page.
The official implementation guideline for ruby is that it's 50% size of text (so you should maybe revert to .5em or 50%).

Useful links (that you may have already read) :
jesus2099 × Ti = Tristan + patate12 ÷ saucisson7 éditer bug report
※ My bugs and whishlist for Opera
Using latest stable version on Windows XP

19. October 2006, 14:27:29

xErath

javascript guru

Posts: 6588

Originally posted by Ti:

It is great that yours is not restricted to xhtml pages only !I never quite understand why White Lynx does this exactly...


Ruby is only part of XHTML 1.1 - it's a logical line of reasoning.
For a collection of user scripts visit
http://my.opera.com/xErath/blog/

19. October 2006, 18:40:49 (edited)

kailapis

^^;

Posts: 150

ver 3.21:
Fixed disable ruby that I broke with ver 3.2
Reduced regular expression number by one

Ruby is part of XHTML 1.1, and White Lynx's XHTML Ruby does work for it and, as White Lynx posted, for properly formed ruby tags in HTML.

I'm trying to get my HTML Ruby to work with all the malformed ruby tags that were made since IE5 came out with ruby support in 1999 and for the more than a few places that still use the malformed ruby tags. These ruby tags probably adhere to the early working drafts, which were made for SGML HTML. The closing tags were optional, which creates a problem for a pure DOM solution in Opera.

19. October 2006, 18:56:52 (edited)

jesus2099

♡.. \(^Δ^;)/ ..☭

Posts: 356

Originally posted by xErath:

Ruby is only part of XHTML 1.1 - it's a logical line of reasoning.



By ignorance, I have always written XHTML pages into .html files. I just discoverd recently that Opera would really consider it XHTML if the files ends with .xhtml smile BUT IE doesn't know .xhtml extension so it is quite a problem to name files this way...
White Lynx userjs doesn't trigger with XHTML.html files because Opera doesn't consider it REAL XHTML.

Whoahw my sentences are so confusing ... :-?
jesus2099 × Ti = Tristan + patate12 ÷ saucisson7 éditer bug report
※ My bugs and whishlist for Opera
Using latest stable version on Windows XP

19. October 2006, 19:32:17

kailapis

^^;

Posts: 150

You can serve it as a .html or any other extension, or even without an extension at all. The server is the one that applies the mime type when sending it to the browser, and Opera gives priority to what the server says over what is said in the file. Your server is probably configured to send .xhtml files as application/xhtml+xml, which puts Opera in XHTML mode.

20. October 2006, 09:15:14

jesus2099

♡.. \(^Δ^;)/ ..☭

Posts: 356

Hi Kai,

I tested two identical XHTML compliant files on the same server (one having ".htm" and the other having ".xhtml" extension). See this post for details. smile
jesus2099 × Ti = Tristan + patate12 ÷ saucisson7 éditer bug report
※ My bugs and whishlist for Opera
Using latest stable version on Windows XP

20. October 2006, 14:31:49 (edited)

kailapis

^^;

Posts: 150

I did read it smile In that case, the server doesn't send any mime headers and lets Opera guess, or it's configured to automatically send .xhtml as application/xhtml+xml.

Can anyone confirm that Opera hasn't really implemented XMLHttpRequest.overrideMimeType() ? I've read a few posts on some blogs that say that, at least in up to 9.02, the function doesn't do anything.

23. October 2006, 20:22:43 (edited)

kailapis

^^;

Posts: 150

ver 3.22:
Changed so that it won't run on other load events, should fix this page
Moved variable declarations so it won't declare when no ruby tags are found

ver 3.23:
Reduced tag cleaning to single regex replacement

27. October 2006, 03:12:39 (edited)

kailapis

^^;

Posts: 150

ver: 3.24:
Removed switch global from body tag removal regex
Changed default ruby text size to .5em
Added overflow:display to ruby style

I hope Opera will implement XMLHttpRequest.overrideMimeType() for Opera 9.1. Or even better, add ruby annotation support, but that probably won't happen anytime soon.

I wonder if there's a better way to close the unclosed ruby tags... Any ideas?

27. October 2006, 09:33:47

jesus2099

♡.. \(^Δ^;)/ ..☭

Posts: 356

I think it's not really that important to bother about unclosed tags since it's not well written. The most important thing is that well written HTML renders OK, don't you think ?
As of Opera implementing ruby, it would sound logical to me that they do it sooner or later, as they were chosen for some Japanese machines.
jesus2099 × Ti = Tristan + patate12 ÷ saucisson7 éditer bug report
※ My bugs and whishlist for Opera
Using latest stable version on Windows XP

28. October 2006, 04:54:57 (edited)

kailapis

^^;

Posts: 150

The problem is that "HTML" ruby annotation from the working draft makes the closing tag optional. So unclosed tags will be considered "well formed" too according to the draft. IE continues to support the draft ruby annotation, and because of that, some places are going to continue with draft ruby annotations. I too would like everything to adhere to the latest standard, but that's impossible to enforce.

28. October 2006, 05:07:19

kailapis

^^;

Posts: 150

Version 4

ver 4.00:
Simplified, remade to be a mostly CSS solution

The tag closing still uses XMLHttpRequest, and it still has the problem of assuming "text/html; charset=utf-8" as the content type when the server does not send the header.

4. November 2006, 17:11:46

kailapis

^^;

Posts: 150

ver 4.01:
Differentiate between complex/simple css by class so rubies can be displayed properly under user mode

13. November 2006, 18:00:49

kailapis

^^;

Posts: 150

In the quest of supporting even more malformed pages...

ver 4.02:
If <head> does not exist, create and append

18. November 2006, 07:44:31

xErath

javascript guru

Posts: 6588

Originally posted by Kai_Lapis:

ver 4.02:If <head> does not exist, create and append


You might want to insert if before the body
if (!document.getElementsByTagName('head')[0]) {
        //document.appendChild(document.createElement('head'));
        document.insertBefore(document.createElement('head'),document.body);
    }
p
For a collection of user scripts visit
http://my.opera.com/xErath/blog/

18. November 2006, 08:16:26

kailapis

^^;

Posts: 150

Thanks, xErath.

ver 4.03:
Changed to insert <head> before <body>

29. November 2006, 04:46:41

kailapis

^^;

Posts: 150

ver 4.04:
Added option to enable tag closing at all page sizes
Modified style and class setting so it won't be applied for rubies that fail tag closing

3. December 2006, 00:43:28

Darken

Dragonfly rulez!

Posts: 356

Thanx for the update, good job! up

15. December 2006, 01:22:46

kailapis

^^;

Posts: 150

4.05:
Changed so it works with pages that don't have a closing body tag

15. December 2006, 13:21:00

Darken

Dragonfly rulez!

Posts: 356

Good! smile

6. January 2007, 00:51:46

kailapis

^^;

Posts: 150

UserJS for HTML Ruby

ver 4.06:
Removed one regexp string replace
Better unclosed tag detection

6. January 2007, 06:24:35

kailapis

^^;

Posts: 150

So the same site that gave me the idea for the new unclosed tag detection (by having awful ruby usage) got worse as I delved in deeper. Missing closing ruby tag. IE seems like it's okay with this, although it's not even supported by the HTML Ruby drafts. And so I "had" to... no, I didn't have to, but...

ver 4.07:
Better unclosed tag closing (to accomodate missing closing ruby tag)
Better performance (just a tiny little bit)

6. January 2007, 17:05:50

Darken

Dragonfly rulez!

Posts: 356

Good job, thanx for the update. up

15. January 2007, 18:02:20

kailapis

^^;

Posts: 150

I couldn't get the "closing of the ruby tag" to work in a general context, so I'm taking it out.

ver 4.08:
Removed closing unclosed ruby tag
Detect and ignore unclosed ruby tag

15. January 2007, 18:57:13

Darken

Dragonfly rulez!

Posts: 356

up

Edit >>

Not Found

The requested URL /Kai_Lapis/userjs/ruby2.js was not found on this server.


16. January 2007, 02:25:31

Darken

Dragonfly rulez!

Posts: 356

Oki, thanx smile

16. January 2007, 03:32:10

kailapis

^^;

Posts: 150

Because Opera defaults to UTF-8(?) for XMLHttpRequest with no way of overriding it, the tag closing caused encoding problems on pages where the server does not send the encoding in the headers. I added a "revert" button that should appear in the top right corner when tag closing is used. When clicked, it should discard the XMLHttpRequest'ed tag-closed contents and replace it with the original contents, making the page at least readable.

ver 4.09:
Added "revert" button when tag closing

15. February 2007, 18:27:56

kailapis

^^;

Posts: 150

ver 4.10:
Made the revert feature use classes instead of styles so it works under user mode CSS.

Also, the CSS to add in user stylesheet:
ruby.rSimple,ruby.rComplex{text-align:center;vertical-align:baseline;font-size:1em;}
ruby.rSimple *,ruby.rComplex *{margin:0px;padding:0px;line-height:1.3em;}
ruby.rSimple rt,ruby.rComplex rt{font-size:.5em;}
ruby.rSimple rt{display:block;}
ruby.rSimple rp,ruby.rComplex rp{display:none;}
ruby.rComplex rtc,ruby.rComplex rbc{display:table-row;}
ruby.rComplex rt,ruby.rComplex rb{display:table-cell;}
ruby.rComplex td{line-height:0em;}
ruby.rComplex td rt{display:inline;}
ruby.rComplex{display:inline-table;position:relative;margin-top:1.15em;margin-bottom:-.75em;top:-1.15em;}
ruby.rSimple{display:inline-block;}
#rSafe.rHide{display:none;}
#rRevert{position:absolute;top:0px;right:0px;}

2. March 2007, 12:47:39

kailapis

^^;

Posts: 150

ver 4.11:
Fixed regression since 4.08(?) where the ruby won't be rendered correctly if it was the last child of its parent.

5. March 2007, 03:12:39

kailapis

^^;

Posts: 150

ver 4.12:
Removed detection for unclosed ruby tag because of too many false positives.

5. March 2007, 22:28:49

Darken

Dragonfly rulez!

Posts: 356

Thanx for all updates, good job. smile

5. April 2007, 01:32:23

kailapis

^^;

Posts: 150

ver 4.13: fixed text not centering for the 2nd test in W3C I18N Test Suite: Ruby markup 1.

I only noticed this on b8769, but I don't know when it started. I'm pretty sure 4.12 had no problem before.

13. April 2007, 23:10:28 (edited)

kailapis

^^;

Posts: 150

CSS to add in user stylesheet for user mode changed with 4.13:
ruby.rSimple,ruby.rComplex{text-align:center;vertical-align:baseline;font-size:1em;}
ruby.rSimple *,ruby.rComplex *{margin:0px;padding:0px;line-height:1.3em;text-align:center;}
ruby.rSimple rt,ruby.rComplex rt{font-size:.5em;}
ruby.rSimple rt{display:block;}
ruby.rSimple rp,ruby.rComplex rp{display:none;}
ruby.rComplex rtc,ruby.rComplex rbc{display:table-row;}
ruby.rComplex rt,ruby.rComplex rb{display:table-cell;}
ruby.rComplex td{line-height:0em;}
ruby.rComplex td rt{display:inline;}
ruby.rComplex{display:inline-table;position:relative;margin-top:1.15em;margin-bottom:-.75em;top:-1.15em;}
ruby.rSimple{display:inline-block;}
#rSafe.rHide{display:none;}
#rRevert{position:absolute;top:0px;right:0px;}

Also, some samples images of rendering:
Asahi.com with furigana from Kids Goo service (url)
W3C I18N Test Suite: Ruby markup 2, test 7 and 8 (url)
Unclosed rb/rt/rp tags on html.eWeb-Design.com (url)

10. September 2007, 10:56:20

White Lynx

Posts: 1978

Kai, Opera 9.5 implements table-baseline property (it is supposed to be added to CSS3 tables module) that makes it easier to format Ruby and similar stuff. It is -o-table-baseline in current (experimental) implementation. You can reuse style sheet from my UserJS if you find it useful.

P.S. property is defined as follows
Name: -o-table-baseline
Value: <integer> | inherit
Initial: 1
Applies to: inline-tables
Inherited: no
Percentages: N/A
Media: visual
Computed value: specified value (except for initial and inherit)

The '-o-table-baseline' property determines which row of a inline-table should be used
as baseline of inline-table.

<integer>
	Baseline of nth row (as determined by the integer value) is baseline of inline-table.
	Value 1 corresponds to first row and is initial value. Negative values are allowed,
	-1 corresponds to last row of inline-table, -n stands for nth line from the bottom.
	If absolute value of property is larger then number of rows in inline-table then initial value
	should be used instead. If value is 0 then bottom margin edge of inline-table should be treated as baseline.	

10. September 2007, 17:36:45

kailapis

^^;

Posts: 150

Thanks, Lynx.

Updated script to use -o-table-baseline.

ver 5.00:
for Opera 9.5+ only
smaller, faster, less DOM manipulation smile

user stylesheet:
ruby,ruby *{text-align:center}
ruby rt{font-size:.5em}
ruby{display:inline-table;-o-table-baseline:2;line-height:1em}
ruby rt,ruby rbc,ruby rtc{display:table-row}
ruby rtc rt,ruby rbc rb{display:table-cell}
ruby rp{display:none}
rbc+rtc,ruby>rt{display:table-header-group;}


ver 4.13, since ver 5.00 won't work with below 9.5

10. September 2007, 18:13:19

jesus2099

♡.. \(^Δ^;)/ ..☭

Posts: 356

Too bad they did not include RUBY in 9.5 yet !
jesus2099 × Ti = Tristan + patate12 ÷ saucisson7 éditer bug report
※ My bugs and whishlist for Opera
Using latest stable version on Windows XP

11. September 2007, 04:14:31

Darken

Dragonfly rulez!

Posts: 356

Originally posted by Kai_Lapis:

Updated script to use -o-table-baseline.


Good works, thanks.

15. September 2007, 03:57:33

kailapis

^^;

Posts: 150

ver 5.01:
fix for unclosed ruby detection
ruby css will not apply when unclosed ruby tags are detected but not closed

17. September 2007, 11:09:45

dantesoft

No, really

Posts: 1262

Where's the link ?

17. September 2007, 16:12:07

dantesoft

No, really

Posts: 1262

Cheers. Maybe you should update the link in the first post.

17. September 2007, 17:01:13

kailapis

^^;

Posts: 150

I would if I could...they only let you edit posts less than a day old.
I really shouldn't have put the version number in the file name in the first place, but...

1. December 2007, 20:29:38

arashpour

Posts: 43

Could somebody tell me what this script is used for?

Forums » » User JavaScript