Sha256: 8ee7c92de144624b538f1bf7f1d26b56d87b70957da6ef061f5df020c2440a4a

Contents?: true

Size: 1.35 KB

Versions: 10

Compression:

Stored size: 1.35 KB

Contents

# encoding: utf-8

module Nanoc3::Helpers

  # Nanoc3::Helpers::Text contains several useful text-related helper functions.
  module Text

    # Returns an excerpt for the given string. HTML tags are ignored, so if
    # you don't want them to turn up, they should be stripped from the string
    # before passing it to the excerpt function.
    #
    # +params+ is a hash where the following keys can be set:
    #
    # +length+:: The maximum number of characters this excerpt can contain,
    #            including the omission. Defaults to 25.
    #
    # +omission+:: The string to append to the excerpt when the excerpt is
    #              shorter than the original string. Defaults to '...' (but in
    #              HTML, you may want to use something more fancy, like
    #              '…').
    def excerptize(string, params={})
      # Initialize params
      params[:length]   ||= 25
      params[:omission] ||= '...'

      # Get excerpt
      length = params[:length] - params[:omission].length
      length = 0 if length < 0
      (string.length > params[:length] ? string[0...length] + params[:omission] : string)
    end

    # Strips all HTML tags out of the given string.
    def strip_html(string)
      # FIXME will need something more sophisticated than this, because it sucks
      string.gsub(/<[^>]*(>+|\s*\z)/m, '').strip
    end

  end

end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
nanoc3-3.0.9 lib/nanoc3/helpers/text.rb
nanoc3-3.0.8 lib/nanoc3/helpers/text.rb
nanoc3-3.0.7 lib/nanoc3/helpers/text.rb
nanoc3-3.0.6 lib/nanoc3/helpers/text.rb
nanoc3-3.0.5 lib/nanoc3/helpers/text.rb
nanoc3-3.0.4 lib/nanoc3/helpers/text.rb
nanoc3-3.0.3 lib/nanoc3/helpers/text.rb
nanoc3-3.0.2 lib/nanoc3/helpers/text.rb
nanoc3-3.0.1 lib/nanoc3/helpers/text.rb
nanoc3-3.0.0 lib/nanoc3/helpers/text.rb