From 4cec243aa0d9478df7abf5e9b5c567d76b5c7653 Mon Sep 17 00:00:00 2001 From: Tom Preston-Werner Date: Mon, 19 Apr 2010 15:50:10 -0700 Subject: [PATCH] get image tag attributes rollin --- README.md | 8 +++++--- lib/gollum/markup.rb | 42 ++++++++++++++++++++++++++++++++++-------- test/test_markup.rb | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 7ef20c44..b6e64b5f 100644 --- a/README.md +++ b/README.md @@ -150,12 +150,14 @@ frame. [[gollum.png|frame|alt=Gollum and his precious wiki]] To specify the alignment of the image on the page, use the `align=` option. -Possible values are `left`, `center`, and `right`. Default is `center`. +Possible values are `left`, `center`, and `right`. Default is `left`. [[gollum.png|align=center]] -To float an image so that text flows around it, use the `float` option. -Default is not floating. +To float an image so that text flows around it, use the `float` option. When +`float` is specified, only `left` and `right` are valid `align` options. +Default is not floating. When floating is activated but no alignment is +specified, default aligntment is `left`. [[gollum.png|float]] diff --git a/lib/gollum/markup.rb b/lib/gollum/markup.rb index 3a5255cf..b4f88a54 100644 --- a/lib/gollum/markup.rb +++ b/lib/gollum/markup.rb @@ -80,19 +80,28 @@ module Gollum if file = find_file(name) opts = parse_image_tag_options(tag) - attrs = [] - styles = [] + floated = false + + classes = [] # applied to whatever the outermost container is + attrs = [] # applied to the image + styles = [] # applied to the image if opts['float'] - if align = opts['align'] - styles << "float: #{align};" - else - attrs << %{align="#{align}"} + floated = true + align = opts['align'] || 'left' + if %w{left right}.include?(align) + classes << "float-#{align};" + end + elsif %w{top texttop middle absmiddle bottom absbottom baseline}.include?(align) + attrs << "align=#{align}" + elsif align = opts['align'] + if %w{left center right}.include?(align) + classes << "align-#{align}" end end if width = opts['width'] - if height =~ /^\d+(\.\d+)?(em|px)$/ + if width =~ /^\d+(\.\d+)?(em|px)$/ styles << "max-width: #{width};" end end @@ -109,7 +118,24 @@ module Gollum attr_string = attrs.size > 0 ? attrs.join(' ') + ' ' : '' - %{} + style_string = + if styles.empty? + '' + else + %{ style="#{styles.join(' ')}"} + end + + if opts['frame'] || floated + classes << 'frame' if opts['frame'] + %{
} + + %{
} + + %{} + + (alt ? %{

#{alt}

} : '') + + %{
} + + %{
} + else + %{} + end end end diff --git a/test/test_markup.rb b/test/test_markup.rb index 62acdbb6..e2a51c7d 100644 --- a/test/test_markup.rb +++ b/test/test_markup.rb @@ -52,6 +52,42 @@ context "Markup" do relative_image(content, output) end + test "image with em or px dimension" do + %w{em px}.each do |unit| + %w{width height}.each do |dim| + content = "a [[alpha.jpg|#{dim}=100#{unit}]] b" + output = "

a b

\n" + relative_image(content, output) + end + end + end + + test "image with bogus dimension" do + %w{width height}.each do |dim| + content = "a [[alpha.jpg|#{dim}=100]] b" + output = "

a b

\n" + relative_image(content, output) + end + end + + test "image with float" do + content = "a\n\n[[alpha.jpg|float]]\n\nb" + output = "

a

\n\n

\n\n

b

\n" + relative_image(content, output) + end + + test "image with frame" do + content = "a\n\n[[alpha.jpg|frame]]\n\nb" + output = "

a

\n\n

\n\n

b

\n" + relative_image(content, output) + end + + test "image with frame and alt" do + content = "a\n\n[[alpha.jpg|frame|alt=Alpha]]\n\nb" + output = "

a

\n\n

\"Alpha\"

Alpha

\n\n

b

\n" + relative_image(content, output) + end + test "file link with absolute path" do index = @wiki.repo.index index.add("alpha.jpg", "hi")