Skip to content

Commit

Permalink
fix: improve ruby plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
lepture committed Jan 28, 2025
1 parent acee737 commit 34f5a77
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/mistune/plugins/ruby.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from ..markdown import Markdown


RUBY_PATTERN = r'\[(?:\w+\(\w+\))+\]'
RUBY_PATTERN = r'\[(?:\w+\([\w ]+\))+\]'
_ruby_re = re.compile(RUBY_PATTERN)


Expand Down Expand Up @@ -86,7 +86,7 @@ def _parse_ruby_link(


def render_ruby(renderer: "BaseRenderer", text: str, rt: str) -> str:
return "<ruby><rb>" + text + "</rb><rt>" + rt + "</rt></ruby>"
return "<ruby>" + text + "<rt>" + rt + "</rt></ruby>"


def ruby(md: "Markdown") -> None:
Expand Down
24 changes: 15 additions & 9 deletions tests/fixtures/ruby.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@
```````````````````````````````` example
[漢字(ㄏㄢˋㄗˋ)]
.
<p><ruby><rb>漢字</rb><rt>ㄏㄢˋㄗˋ</rt></ruby></p>
<p><ruby>漢字<rt>ㄏㄢˋㄗˋ</rt></ruby></p>
````````````````````````````````

```````````````````````````````` example
[漢(ㄏㄢˋ)字(ㄗˋ)]
.
<p><ruby><rb>漢</rb><rt>ㄏㄢˋ</rt></ruby><ruby><rb>字</rb><rt>ㄗˋ</rt></ruby></p>
<p><ruby>漢<rt>ㄏㄢˋ</rt></ruby><ruby><rt>ㄗˋ</rt></ruby></p>
````````````````````````````````

```````````````````````````````` example
[漢字(ㄏㄢˋㄗˋ)][]
.
<p><ruby><rb>漢字</rb><rt>ㄏㄢˋㄗˋ</rt></ruby>[]</p>
<p><ruby>漢字<rt>ㄏㄢˋㄗˋ</rt></ruby>[]</p>
````````````````````````````````

```````````````````````````````` example
[漢字(ㄏㄢˋㄗˋ)][link]
.
<p><ruby><rb>漢字</rb><rt>ㄏㄢˋㄗˋ</rt></ruby>[link]</p>
<p><ruby>漢字<rt>ㄏㄢˋㄗˋ</rt></ruby>[link]</p>
````````````````````````````````

## ruby with link
Expand All @@ -34,31 +34,37 @@

[漢字(ㄏㄢˋㄗˋ)][link]
.
<p><a href="/url"><ruby><rb>漢字</rb><rt>ㄏㄢˋㄗˋ</rt></ruby></a></p>
<p><a href="/url"><ruby>漢字<rt>ㄏㄢˋㄗˋ</rt></ruby></a></p>
````````````````````````````````

```````````````````````````````` example
[漢字(ㄏㄢˋㄗˋ)]()
.
<p><a href=""><ruby><rb>漢字</rb><rt>ㄏㄢˋㄗˋ</rt></ruby></a></p>
<p><a href=""><ruby>漢字<rt>ㄏㄢˋㄗˋ</rt></ruby></a></p>
````````````````````````````````

```````````````````````````````` example
[漢字(ㄏㄢˋㄗˋ)](/url)
.
<p><a href="/url"><ruby><rb>漢字</rb><rt>ㄏㄢˋㄗˋ</rt></ruby></a></p>
<p><a href="/url"><ruby>漢字<rt>ㄏㄢˋㄗˋ</rt></ruby></a></p>
````````````````````````````````

## ruby splits

```````````````````````````````` example
[漢(かん)][字(じ)]
.
<p><ruby><rb>漢</rb><rt>かん</rt></ruby><ruby><rb>字</rb><rt>じ</rt></ruby></p>
<p><ruby>漢<rt>かん</rt></ruby><ruby><rt>じ</rt></ruby></p>
````````````````````````````````

```````````````````````````````` example
[[漢(かん)][字(じ)]](https://jisho.org/search/漢字)
.
<p><a href="https://jisho.org/search/%E6%BC%A2%E5%AD%97"><ruby><rb>漢</rb><rt>かん</rt></ruby><ruby><rb>字</rb><rt>じ</rt></ruby></a></p>
<p><a href="https://jisho.org/search/%E6%BC%A2%E5%AD%97"><ruby>漢<rt>かん</rt></ruby><ruby>字<rt>じ</rt></ruby></a></p>
````````````````````````````````

```````````````````````````````` example
[汉字(hàn zì)]
.
<p><ruby>汉字<rt>hàn zì</rt></ruby></p>
````````````````````````````````

0 comments on commit 34f5a77

Please sign in to comment.