From 50eaf98c24db283b04d3e895a0f58890dc453957 Mon Sep 17 00:00:00 2001 From: Frederic Hemberger Date: Mon, 26 Sep 2011 11:00:35 +0200 Subject: Makes img tag more flexible, adds support for relative paths. Includes pull request #145, fixes #161 --- plugins/image_tag.rb | 45 +++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 22 deletions(-) (limited to 'plugins') diff --git a/plugins/image_tag.rb b/plugins/image_tag.rb index 25a38df5..92e21765 100644 --- a/plugins/image_tag.rb +++ b/plugins/image_tag.rb @@ -1,46 +1,47 @@ # Title: Simple Image tag for Jekyll -# Author: Brandon Mathis http://brandonmathis.com -# Description: Easily output images with optional class names and title/alt attributes +# Authors: Brandon Mathis http://brandonmathis.com +# Felix Schäfer, Frederic Hemberger +# Description: Easily output images with optional class names, width, height, title and alt attributes # -# Syntax {% image [class name(s)] url [title text] %} +# Syntax {% img [class name(s)] [http[s]:/]/path/to/image [width [height]] [title text | "title text" ["alt text"]] %} # -# Example: -# {% ima left half http://site.com/images/ninja.png Ninja Attack! %} +# Examples: +# {% img /images/ninja.png Ninja Attack! %} +# {% img left half http://site.com/images/ninja.png Ninja Attack! %} +# {% img left half http://site.com/images/ninja.png 150 150 "Ninja Attack!" "Ninja in attack posture" %} # # Output: -# Ninja Attack! +# +# Ninja Attack! +# Ninja in attack posture # module Jekyll class ImageTag < Liquid::Tag @img = nil - @title = nil - @class = '' - @width = '' - @height = '' def initialize(tag_name, markup, tokens) - if markup =~ /(\S.*\s+)?(https?:\/\/|\/)(\S+)(\s+\d+\s+\d+)?(\s+.+)?/i - @class = $1 || '' - @img = $2 + $3 - if $5 - @title = $5.strip - end - if $4 =~ /\s*(\d+)\s+(\d+)/ - @width = $1 - @height = $2 + attributes = ['class', 'src', 'width', 'height', 'title'] + + if markup =~ /(?\S.*\s+)?(?(?:https?:\/\/|\/|\S+\/)\S+)(?:\s+(?\d+))?(?:\s+(?\d+))?(?\s+.+)?/i + @img = attributes.reduce({}) { |img, attr| img[attr] = $~[attr].strip if $~[attr]; img } + if @img['title'] =~ /(?:"|')([^"']+)?(?:"|')\s+(?:"|')([^"']+)?(?:"|')/ + @img['title'] = $1 + @img['alt'] = $2 + else + @img['alt'] = @img['title'].gsub!(/"/, '') end + @img['class'].gsub!(/"/, '') end super end def render(context) - output = super if @img - "<img class='#{@class}' src='#{@img}' width='#{@width}' height='#{@height}' alt='#{@title}' title='#{@title}'>" + "<img #{@img.collect {|k,v| "#{k}=\"#{v}\"" if v}.join(" ")}>" else - "Error processing input, expected syntax: {% img [class name(s)] /url/to/image [width height] [title text] %}" + "Error processing input, expected syntax: {% img [class name(s)] [http[s]:/]/path/to/image [width [height]] [title text | \"title text\" [\"alt text\"]] %}" end end end -- cgit v1.2.1 From 9bd572ffdc907301691e5ff5a62d68ecfa883229 Mon Sep 17 00:00:00 2001 From: Frederic Hemberger <mail@frederic-hemberger.de> Date: Mon, 26 Sep 2011 11:51:44 +0200 Subject: Adds jsFiddle plugin --- plugins/jsfiddle.rb | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 plugins/jsfiddle.rb (limited to 'plugins') diff --git a/plugins/jsfiddle.rb b/plugins/jsfiddle.rb new file mode 100644 index 00000000..3ae173eb --- /dev/null +++ b/plugins/jsfiddle.rb @@ -0,0 +1,40 @@ +# Title: jsFiddle tag for Jekyll +# Author: Brian Arnold (@brianarn) +# Description: +# Given a jsFiddle shortcode, outputs the jsFiddle iframe code. +# Using 'default' will preserve defaults as specified by jsFiddle. +# +# Syntax: {% jsfiddle shorttag [tabs] [skin] [height] [width] %} +# +# Examples: +# +# Input: {% jsfiddle ccWP7 %} +# Output: <iframe style="width: 100%; height: 300px" src="http://jsfiddle.net/ccWP7/embedded/js,resources,html,css,result/light/"></iframe> +# +# Input: {% jsfiddle ccWP7 js,html,result %} +# Output: <iframe style="width: 100%; height: 300px" src="http://jsfiddle.net/ccWP7/embedded/js,html,result/light/"></iframe> +# + +module Jekyll + class JsFiddle < Liquid::Tag + def initialize(tag_name, markup, tokens) + if /(?<fiddle>\w+)(?:\s+(?<sequence>[\w,]+))?(?:\s+(?<skin>\w+))?(?:\s+(?<height>\w+))?(?:\s+(?<width>\w+))?/ =~ markup + @fiddle = fiddle + @sequence = (sequence unless sequence == 'default') || 'js,resources,html,css,result' + @skin = (skin unless skin == 'default') || 'light' + @width = width || '100%' + @height = height || '300px' + end + end + + def render(context) + if @fiddle + "<iframe style=\"width: #{@width}; height: #{@height}\" src=\"http://jsfiddle.net/#{@fiddle}/embedded/#{@sequence}/#{@skin}/\"></iframe>" + else + "Error processing input, expected syntax: {% jsfiddle shorttag [tabs] [skin] [height] [width] %}" + end + end + end +end + +Liquid::Template.register_tag('jsfiddle', Jekyll::JsFiddle) \ No newline at end of file -- cgit v1.2.1 From 19a646b5e9e5f294896afc7552a4941933b8fb22 Mon Sep 17 00:00:00 2001 From: Frederic Hemberger <mail@frederic-hemberger.de> Date: Mon, 26 Sep 2011 11:54:45 +0200 Subject: Escapes double quotes in img tag --- plugins/image_tag.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'plugins') diff --git a/plugins/image_tag.rb b/plugins/image_tag.rb index 92e21765..20595cb9 100644 --- a/plugins/image_tag.rb +++ b/plugins/image_tag.rb @@ -26,11 +26,11 @@ module Jekyll if markup =~ /(?<class>\S.*\s+)?(?<src>(?:https?:\/\/|\/|\S+\/)\S+)(?:\s+(?<width>\d+))?(?:\s+(?<height>\d+))?(?<title>\s+.+)?/i @img = attributes.reduce({}) { |img, attr| img[attr] = $~[attr].strip if $~[attr]; img } - if @img['title'] =~ /(?:"|')([^"']+)?(?:"|')\s+(?:"|')([^"']+)?(?:"|')/ - @img['title'] = $1 - @img['alt'] = $2 + if /(?:"|')(?<title>[^"']+)?(?:"|')\s+(?:"|')(?<alt>[^"']+)?(?:"|')/ =~ @img['title'] + @img['title'] = title + @img['alt'] = alt else - @img['alt'] = @img['title'].gsub!(/"/, '') + @img['alt'] = @img['title'].gsub!(/"/, '"') end @img['class'].gsub!(/"/, '') end -- cgit v1.2.1 From fdf6af1d25ddc991c96dc3b4df5f7e913adcd7ef Mon Sep 17 00:00:00 2001 From: Frederic Hemberger <mail@frederic-hemberger.de> Date: Mon, 26 Sep 2011 15:58:14 +0200 Subject: Fixes img tag properties --- plugins/image_tag.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'plugins') diff --git a/plugins/image_tag.rb b/plugins/image_tag.rb index 20595cb9..45670007 100644 --- a/plugins/image_tag.rb +++ b/plugins/image_tag.rb @@ -30,9 +30,9 @@ module Jekyll @img['title'] = title @img['alt'] = alt else - @img['alt'] = @img['title'].gsub!(/"/, '"') + @img['alt'] = @img['title'].gsub!(/"/, '"') if @img['title'] end - @img['class'].gsub!(/"/, '') + @img['class'].gsub!(/"/, '') if @img['class'] end super end -- cgit v1.2.1