GitXplorerGitXplorer
a

truncation_demo

public
0 stars
0 forks
0 issues

Commits

List of commits on branch master.
Unverified
8262a6f6285cabe7874fa3bf7fc1965d46362bd5

Update readme.md

aadammcarth committed 10 years ago
Unverified
d18b14b9b2fd5e1bc499d4df15fb2106c69b7c74

add link to tutorial

aadammcarth committed 10 years ago
Unverified
3133f5c1b998bda6fdd7e9b435b146d81be1ad77

let's try rackup

aadammcarth committed 11 years ago
Unverified
21952f81c54e09bccad9d7e280d38d0726b53a94

uhhhh... i hate you heroku

aadammcarth committed 11 years ago
Unverified
1f34b5ab49452c5c375171385b11955e92930c06

check in gemfile.lock

aadammcarth committed 11 years ago
Unverified
032e6574540ad52874a69e5ecbda903e1d56b475

add gem file

aadammcarth committed 11 years ago

README

The README file for this repository.

A demo for a smart truncation script I created. http://smart-trunction.herokuapp.com

Visit the tutorial here: Smart Truncation Techniques For Difficult Web Layouts.

# Intelligent string truncation function
# written in Ruby.
def truncate(string, words)
  # Define the maximum number of characters that can be used before
  # standard truncation is applied (prevents long words from ruinning HTML).
  max_chars = 6 * words
  words_array = string.split(" ") # splits each word into an array ["like", "this"]
  index = words - 1 # array indexes start at 0, so let's take one number off the words amount.
  truncated = words_array[0..index].join(" ")

  if truncated.length > max_chars
    # Use standard truncation (set amount of characters)
    truncated = string.slice(0, max_chars) + "..."
  elsif truncated == string
    # `truncated` and the original string are identical, so don't include "..."
    truncated = string
  else
    # If everything's checked out, we'll be using worded truncation. This simply adds
    # "..." to the worded truncation variable.
    truncated = truncated + "..."
  end

  # Output the results
  return truncated
end