Sha256: 3b3bc515eeddf151620a19c6fa88ada4784db1a740d7c39862fb57461b826781

Contents?: true

Size: 1.51 KB

Versions: 1

Compression:

Stored size: 1.51 KB

Contents

module Jekyll

  module Filters
    def textilize(input)
      RedCloth.new(input).to_html
    end

    def date_to_string(date)
      date.strftime("%d %b %Y")
    end

    def date_to_long_string(date)
      date.strftime("%d %B %Y")
    end

    def date_to_xmlschema(date)
      date.xmlschema
    end

    def xml_escape(input)
      CGI.escapeHTML(input)
    end

    def cgi_escape(input)
      CGI::escape(input)
    end

    def number_of_words(input)
      input.split.length
    end

    def array_to_sentence_string(array)
      connector = "and"
      case array.length
      when 0
        ""
      when 1
        array[0].to_s
      when 2
        "#{array[0]} #{connector} #{array[1]}"
      else
        "#{array[0...-1].join(', ')}, #{connector} #{array[-1]}"
      end
    end
    
    def shorten(string, word_count)
      words = string.split(' ')
      return string if string.length <= word_count
      
      count = words.length - 1 # number of spaces
      count += 3 # for "..."
      start_i, end_i = 0, words.length - 1
      prefix, suffix = [], []
      while count <= word_count
        [start_i, end_i].each_with_index do |i, pos|
          if words[i].length + count <= word_count
            if pos == 0
              prefix
            else
              suffix
            end << words[i]
            count += words[i].length
          end
        end
        start_i += 1
        end_i -= 1
        break if start_i >= end_i
      end
      "#{prefix.join(' ')}...#{suffix.reverse.join(' ')}"
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rfelix-jekyll-0.5.4 lib/jekyll/filters.rb