Skip to content

Instantly share code, notes, and snippets.

View coryschires's full-sized avatar
💭
Code King

Cory Schires coryschires

💭
Code King
View GitHub Profile
@coryschires
coryschires / wordbreak_helper.rb
Created June 3, 2010 20:12
wordbreak helper method
# Helper method to add html soft hyphen entities to words whose character length exceeds a given integer.
# Note: This is an overkill substitute for the poorly supported CSS word-break property.
def wordbreak(string, max_length = 12)
max_length = max_length < 5 ? 4 : max_length # force min max_length of 4
words = string.scan(/(?:"[\w'\.\-]*"\.?)|(?:'[\w'\.\-]*'\.?)|(?:[\w'\.]+)/)
words.each do |word|
if word.length > max_length
index = word.include?("-") ? word.index('-')+1 : max_length-2
@coryschires
coryschires / scrub_html_helper.rb
Created June 3, 2010 20:27
scrub_html helper method
# removes disallowed opening and closing html tags from a passed string
def scrub_html(string)
disallowed_tags = %w[p ul li ol blockquote cite pre code dl dt dd table tr th frameset from input select option address h1 h2 h3 h4 h5 h6]
disallowed_tags.each do |tag|
opening_tag = /<\s*#{tag}[^>]*>{1}/i
closing_tag = /<\/\s*#{tag}\s*>/i
hr_tag = /<\s*hr\s*\/*>/i
string.gsub!(opening_tag, '')
string.gsub!(closing_tag, '')
@coryschires
coryschires / sentence_truncate_helper.rb
Created June 3, 2010 20:40
sentence_truncate helper
# Based on http://stackoverflow.com/questions/1293573/rails-smart-text-truncation
def sentence_truncate(string, num_sentences, enforce_max_length=true)
pattern = /[\.\?\!]{1,}\s/
matches = string.scan(pattern)
sentences = string.split(pattern)
num_sentences = matches.length if matches.length < num_sentences
truncated_sentence = sentences[0...num_sentences].map do |sentence|
sentence << matches[sentences.index(sentence)]
end.join("").chop
@coryschires
coryschires / seo_helper.rb
Created June 8, 2010 18:51
seo helpers module
module SeoHelper
def meta_keywords(keywords)
tag(:meta, { :name => "keywords", :content => keywords })
end
def meta_description(description)
tag(:meta, { :name => "description", :content => description })
end
def meta_author(author_name)
@coryschires
coryschires / link_to_lightbox_helper.rb
Created June 14, 2010 19:12
link_to_lightbox helper
@coryschires
coryschires / ask_rake_task_helper.rb
Created June 14, 2010 21:53
ask helper for rake tasks
# helper method for asking questions in a rake task - returns true or false
def ask?(question)
puts question
respnose = STDIN.gets.chomp.downcase
%w[yes y yep true].include?(respnose)
end
# example usage
if ask?("Are you having a nice day?")
puts "Great!"
@coryschires
coryschires / browser_redirect.js
Created June 15, 2010 21:57
browser redirect script
// note: this script depends on the jQuery browser plugin
// http://jquery.thewikies.com/browser/
// place this at the very top of application.js
// outside of the document ready block to ensue
// it loads and initiates redirect as quickly
// as possible.
var browser_redirect = function() {
var os_param = $.os.name === 'mac' ? '?os=mac' : '?os=win';
@coryschires
coryschires / jquery.clear_form.js
Created June 17, 2010 04:26
plugin to clear form form data
// tiny plugin to clear form form data.
$.fn.clearForm = function() {
// iterate each matching form
return this.each(function() {
// iterate the elements within the form
$(':input', this).each(function() {
var type = this.type, tag = this.tagName.toLowerCase();
if (type == 'text' || type == 'password' || tag == 'textarea')
@coryschires
coryschires / perserve_linebreaks.js
Created June 21, 2010 05:29
preserve line breaks javascript
var perserve_linebreaks = function(text) {
var html = text.replace(/\r\n\r\n/g, "</p><p>"),
html = html.replace(/\r\n/g, "<br />"),
html = html.replace(/\n\n/g, "</p><p>"),
html = html.replace(/\n/g, "<br />"),
html = "<p>"+html+"</p>";
return html
};
@coryschires
coryschires / todays_date.js
Created June 21, 2010 06:59
todays date javascript
var todays_date = function() {
var month_in_words = function(month_num) {
if (month_num === 0) { return "January" };
if (month_num === 1) { return "February" };
if (month_num === 2) { return "March" };
if (month_num === 3) { return "April" };
if (month_num === 4) { return "May" };
if (month_num === 5) { return "June" };
if (month_num === 6) { return "July" };