Created
March 21, 2009 14:19
-
-
Save rayboyd/82858 to your computer and use it in GitHub Desktop.
Rails html monkey patch. More hassle than it's worth, handy to have none-the-less.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is an absolutely fucking infuriating monkey patch that allows rails | |
# to output valid html 4 (i.e leave some tag open) | |
# See http://rails.lighthouseapp.com/projects/8994/tickets/1565-patch-allow-disabling-of-self-closing-tags-generation-taghelpertag | |
# Dogmatic, clueless or just plain blind arrogance? | |
module ActionView | |
module Helpers | |
module TagHelper | |
include ERB::Util | |
mattr_accessor :generate_non_self_closing_tags | |
self.generate_non_self_closing_tags = false | |
alias :generate_non_self_closing_tags? :generate_non_self_closing_tags | |
def self.included(base) #:nodoc: | |
base.mattr_accessor :generate_non_self_closing_tags | |
base.generate_non_self_closing_tags ||= generate_non_self_closing_tags | |
end | |
BOOLEAN_ATTRIBUTES = %w(disabled readonly multiple checked).to_set | |
BOOLEAN_ATTRIBUTES.merge(BOOLEAN_ATTRIBUTES.map(&:to_sym)) | |
def tag(name, options = nil, open = generate_non_self_closing_tags?, escape = true) | |
open = options.delete('open') || open | |
escape = options.delete('escape') || escape | |
"<#{name}#{tag_options(options, escape) if options}#{open ? ">" : " />"}" | |
end | |
end | |
# | |
# Override simple format, allow open <br> | |
# | |
module TextHelper | |
def simple_format(text, html_options={}) | |
start_tag = tag('p', html_options, true) | |
text = text.to_s.dup | |
text.gsub!(/\r\n?/, "\n") # \r\n and \r -> \n | |
text.gsub!(/\n\n+/, "</p>\n\n#{start_tag}") # 2+ newline -> paragraph | |
text.gsub!(/([^\n]\n)(?=[^\n])/, '\1<br>') # 1 newline -> br | |
text.insert 0, start_tag | |
text << "</p>" | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment