diff --git a/lib/rdoc/markdown.kpeg b/lib/rdoc/markdown.kpeg index 66a4aa74fd..fe4e74b912 100644 --- a/lib/rdoc/markdown.kpeg +++ b/lib/rdoc/markdown.kpeg @@ -992,9 +992,14 @@ Strike = &{ strike? } "~~" { strike a.join } -# TODO alt text support -Image = "!" ( ExplicitLink | ReferenceLink ):a - { "rdoc-image:#{a[/\[(.*)\]/, 1]}" } +Image = "!" ExplicitLink:a + { + # Extract alt text and URL + alt_text = a[/\{(.*?)\}/, 1] || "" + url = a[/\[(.*?)\]/, 1] || "" + + "rdoc-image:#{url}:#{alt_text}" + } Link = ExplicitLink | ReferenceLink | AutoLink diff --git a/lib/rdoc/markup/heading.rb b/lib/rdoc/markup/heading.rb index 02476e5226..693320953f 100644 --- a/lib/rdoc/markup/heading.rb +++ b/lib/rdoc/markup/heading.rb @@ -66,7 +66,13 @@ def label context = nil # element. def plain_html - self.class.to_html.to_html(text.dup) + text = self.text.dup + + if matched = text.match(/rdoc-image:[^:]+:(.*)/) + text = matched[1] + end + + self.class.to_html.to_html(text) end def pretty_print q # :nodoc: diff --git a/lib/rdoc/markup/to_html.rb b/lib/rdoc/markup/to_html.rb index dd37bf9eb3..60894de98c 100644 --- a/lib/rdoc/markup/to_html.rb +++ b/lib/rdoc/markup/to_html.rb @@ -98,7 +98,12 @@ def handle_RDOCLINK url # :nodoc: gen_url CGI.escapeHTML(url), CGI.escapeHTML(text) when /^rdoc-image:/ - %[] + url, alt = $'.split(":", 2) # Split the string after "rdoc-image:" into url and alt + if alt && !alt.empty? + %[#{CGI.escapeHTML(alt)}] + else + %[] + end when /\Ardoc-[a-z]+:/ CGI.escapeHTML($') end diff --git a/test/rdoc/test_rdoc_markdown.rb b/test/rdoc/test_rdoc_markdown.rb index 61098ae2b5..996ae4e8db 100644 --- a/test/rdoc/test_rdoc_markdown.rb +++ b/test/rdoc/test_rdoc_markdown.rb @@ -511,7 +511,7 @@ def test_parse_html_no_html def test_parse_image doc = parse "image ![alt text](path/to/image.jpg)" - expected = doc(para("image rdoc-image:path/to/image.jpg")) + expected = doc(para("image rdoc-image:path/to/image.jpg:alt text")) assert_equal expected, doc end @@ -523,7 +523,7 @@ def test_parse_image_link expected = doc( - para('{rdoc-image:path/to/image.jpg}[http://example.com]')) + para('{rdoc-image:path/to/image.jpg:alt text}[http://example.com]')) assert_equal expected, doc end diff --git a/test/rdoc/test_rdoc_markup_heading.rb b/test/rdoc/test_rdoc_markup_heading.rb index de92af91a0..5dd60ed3ab 100644 --- a/test/rdoc/test_rdoc_markup_heading.rb +++ b/test/rdoc/test_rdoc_markup_heading.rb @@ -26,4 +26,9 @@ def test_plain_html assert_equal 'Hello Friend!', @h.plain_html end + def test_plain_html_using_image_alt_as_text + h = RDoc::Markup::Heading.new 1, 'rdoc-image:foo.png:Hello World' + + assert_equal 'Hello World', h.plain_html + end end