Skip to content

Commit 05d0540

Browse files
committed
Add support for alt-text.
1 parent 0e5a763 commit 05d0540

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

lib/rdoc/markdown.kpeg

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -992,9 +992,14 @@ Strike = &{ strike? }
992992
"~~"
993993
{ strike a.join }
994994

995-
# TODO alt text support
996-
Image = "!" ( ExplicitLink | ReferenceLink ):a
997-
{ "rdoc-image:#{a[/\[(.*)\]/, 1]}" }
995+
Image = "!" ExplicitLink:a
996+
{
997+
# Extract alt text and URL
998+
alt_text = a[/\{(.*?)\}/, 1] || ""
999+
url = a[/\[(.*?)\]/, 1] || ""
1000+
1001+
"rdoc-image:#{url}:#{alt_text}"
1002+
}
9981003

9991004
Link = ExplicitLink | ReferenceLink | AutoLink
10001005

lib/rdoc/markup/to_html.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,12 @@ def handle_RDOCLINK url # :nodoc:
9898

9999
gen_url CGI.escapeHTML(url), CGI.escapeHTML(text)
100100
when /^rdoc-image:/
101-
%[<img src=\"#{CGI.escapeHTML($')}\">]
101+
url, alt = $'.split(":", 2) # Split the string after "rdoc-image:" into url and alt
102+
if alt
103+
%[<img src="#{CGI.escapeHTML(url)}" alt="#{CGI.escapeHTML(alt)}">]
104+
else
105+
%[<img src="#{CGI.escapeHTML(url)}">]
106+
end
102107
when /\Ardoc-[a-z]+:/
103108
CGI.escapeHTML($')
104109
end

test/rdoc/test_rdoc_markdown.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ def test_parse_html_no_html
511511
def test_parse_image
512512
doc = parse "image ![alt text](path/to/image.jpg)"
513513

514-
expected = doc(para("image rdoc-image:path/to/image.jpg"))
514+
expected = doc(para("image rdoc-image:path/to/image.jpg:alt text"))
515515

516516
assert_equal expected, doc
517517
end
@@ -523,7 +523,7 @@ def test_parse_image_link
523523

524524
expected =
525525
doc(
526-
para('{rdoc-image:path/to/image.jpg}[http://example.com]'))
526+
para('{rdoc-image:path/to/image.jpg:alt text}[http://example.com]'))
527527

528528
assert_equal expected, doc
529529
end

0 commit comments

Comments
 (0)