plugins/blockquote.rb
8698a276
 #
ab29d45a
 # Author: Brandon Mathis
6a6de74a
 # A full rewrite based on the work of: Josediaz Gonzalez - https://github.com/josegonzalez/josediazgonzalez.com/blob/master/_plugins/blockquote.rb
8698a276
 #
f77db800
 # Outputs a string with a given attribution as a quote
 #
6a6de74a
 #   {% blockquote Bobby Willis http://google.com/search?q=pants the search for bobby's pants %}
f77db800
 #   Wheeee!
 #   {% endblockquote %}
 #   ...
 #   <blockquote>
 #     <p>Wheeee!</p>
 #     <footer>
6a6de74a
 #     <strong>Bobby Willis</strong><cite><a href="http://google.com/search?q=pants">The Search For Bobby's Pants</a>
f77db800
 #   </blockquote>
 #
acd1ba34
 require './plugins/titlecase.rb'
 
8698a276
 module Jekyll
 
   class Blockquote < Liquid::Block
967eeb46
     FullCiteWithTitle = /(\S.*)\s+(https?:\/\/)(\S+)\s+(.+)/i
     FullCite = /(\S.*)\s+(https?:\/\/)(\S+)/i
d686105b
     AuthorTitle = /([^,]+),([^,]+)/
967eeb46
     Author =  /(.+)/
8698a276
 
     def initialize(tag_name, markup, tokens)
       @by = nil
       @source = nil
       @title = nil
       if markup =~ FullCiteWithTitle
         @by = $1
         @source = $2 + $3
cd6926e4
         @title = $4.titlecase.strip
8698a276
       elsif markup =~ FullCite
         @by = $1
         @source = $2 + $3
d686105b
       elsif markup =~ AuthorTitle
         @by = $1
cd6926e4
         @title = $2.titlecase.strip
8698a276
       elsif markup =~ Author
d686105b
         @by = $1
8698a276
       end
       super
     end
 
     def render(context)
e53b26ad
       quote = paragraphize(super)
a71a709c
       author = "<strong>#{@by.strip}</strong>" if @by
da514a65
       if @source
         url = @source.match(/https?:\/\/(.+)/)[1].split('/')
         parts = []
         url.each do |part|
           if (parts + [part]).join('/').length < 32
             parts << part
           end
         end
         source = parts.join('/')
         source << '/&hellip;' unless source == @source
       end
967eeb46
       if !@source.nil?
4e0fec0e
         cite = " <cite><a href='#{@source}'>#{(@title || source)}</a></cite>"
967eeb46
       elsif !@title.nil?
4e0fec0e
         cite = " <cite>#{@title}</cite>"
967eeb46
       end
       blockquote = if @by.nil?
a71a709c
         quote
967eeb46
       elsif cite
a71a709c
         "#{quote}<footer>#{author + cite}</footer>"
8698a276
       else
a71a709c
         "#{quote}<footer>#{author}</footer>"
8698a276
       end
967eeb46
       "<blockquote>#{blockquote}</blockquote>"
8698a276
     end
da514a65
 
     def paragraphize(input)
e53b26ad
       "<p>#{input.lstrip.rstrip.gsub(/\n\n/, '</p><p>').gsub(/\n/, '<br/>')}</p>"
da514a65
     end
8698a276
   end
 end
 
 Liquid::Template.register_tag('blockquote', Jekyll::Blockquote)