From 354372705f190171f5b22f7fe976da19c240a7f4 Mon Sep 17 00:00:00 2001 From: Tom Preston-Werner Date: Mon, 19 Apr 2010 11:49:46 -0700 Subject: [PATCH] image alt --- lib/gollum/markup.rb | 23 ++++++++++++++++++++++- test/test_markup.rb | 18 +++++++++++++++++- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/lib/gollum/markup.rb b/lib/gollum/markup.rb index 27ed110f..344993e5 100644 --- a/lib/gollum/markup.rb +++ b/lib/gollum/markup.rb @@ -78,10 +78,31 @@ module Gollum name = parts[0].strip if file = find_file(name) - %{} + attrs = process_image_tag_attributes(tag) + %{} end end + # Process any attributes present on the image tag and format them as an + # image tag fragment. + # + # tag - The String tag contents (the stuff inside the double brackets). + # + # Returns the String image tag fragment. + def process_image_tag_attributes(tag) + attrs = tag.split('|')[1..-1].inject({}) do |memo, attr| + parts = attr.split('=').map { |x| x.strip } + memo[parts[0]] = (parts.size == 1 ? true : parts[1]) + memo + end + + fragments = [] + if alt = attrs['alt'] + fragments << %{alt="#{alt}"} + end + fragments.size > 0 ? fragments.join(' ') + ' ' : '' + end + # Attempt to process the tag as a file link tag. # # tag - The String tag contents (the stuff inside the double brackets). diff --git a/test/test_markup.rb b/test/test_markup.rb index da07578c..62acdbb6 100644 --- a/test/test_markup.rb +++ b/test/test_markup.rb @@ -46,6 +46,12 @@ context "Markup" do assert_equal %{

a b

\n}, output end + test "image with alt" do + content = "a [[alpha.jpg|alt=Alpha Dog]] b" + output = %{

a Alpha Dog b

\n} + relative_image(content, output) + end + test "file link with absolute path" do index = @wiki.repo.index index.add("alpha.jpg", "hi") @@ -68,4 +74,14 @@ context "Markup" do assert_equal %{

a Alpha b

\n}, output end -end + def relative_image(content, output) + index = @wiki.repo.index + index.add("greek/alpha.jpg", "hi") + index.add("greek/Bilbo-Baggins.md", content) + index.commit("Add alpha.jpg") + + page = @wiki.page("Bilbo Baggins") + rendered = Gollum::Markup.new(page).render + assert_equal output, rendered + end +end \ No newline at end of file