Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def htmlify(markup, options = {})
- options = { :only => [:emotes, :emphasis, :swatches, :profile_links, :http_links, :entities, :pants], :truncate => 45 }.merge(options)
- run = Array.new
- run << options[:only].to_a.dup
- run << options[:also].to_a.dup
- run.flatten!
- run.delete_if { |r| options[:except].to_a.flatten.include?(r) }
- # load emote index if it hasn't already been loaded
- if run.include?(:emotes)
- require 'emotes'
- @emoset ||= EmoteSet.from_s(IO.read("#{RAILS_ROOT}/public/images/ipb-emotes/index.txt")) if run.include?(:emotes)
- @emo_scan_len ||= @emoset.map { |i| i.triggers.map { |t| t.length }.max }.max
- end
- markup = StringScanner.new(markup.to_s)
- result = String.new
- while markup.rest?
- case
- when run.include?(:profile_links) && markup.scan(/\[-([0-9]+)-\]/)
- profile = Profile.find(markup[1].to_i, :select => 'id, name') rescue nil
- if profile
- result << link_to(profile.name, profile_url(:id => markup[1]))
- else
- result << content_tag('span', "[-Error, cannot find profile with id '#{markup[1]}'-]")
- end
- when run.include?(:swatches) && markup.scan(/\#([0-9A-F]{6,6}|[0-9A-F]{3,3})/)
- result << colour_swatch(markup.matched)
- when run.include?(:emphasis) && markup.scan(/([*_])(([a-z0-9].*?)?\S)\1/)
- result << '<em>' + htmlify(markup[2], :only => run, :except => [:emphasis, :pants, :paragraphs]) + '</em>'
- when run.include?(:emphasis) && markup.scan(/([*_]{2,2})(([a-z0-9].*?)?\S)\1/)
- result << '<strong>' + htmlify(markup[2], :only => run, :except => [:emphasis, :pants, :paragraphs]) + '</strong>'
- when run.include?(:emotes) && (emote = @emoset.start_match(markup.peek(@emo_scan_len)))
- result << emote.to_html(:style => ('vertical-align: middle; margin: 0; padding: 0; border: 0px none;' if @inline_styles))
- markup.pointer += emote.trigger.length
- when run.include?(:http_links) && markup.scan(/<http:\/\/([a-z0-9\.]+(:[0-9]+)?\/[-_+$@.&*!\#\"\'()?,%a-z0-9\/]*)>/i)
- result << '<' + link_to(h(markup[1]), markup[1]) + '>'
- when run.include?(:entities) && markup.scan(/</)
- result << '<'
- when run.include?(:entities) && markup.scan(/>/)
- result << '>'
- when run.include?(:entities) && !run.include?(:pants) && markup.scan(/"/)
- result << '"'
- when run.include?(:entities) && !run.include?(:pants) && markup.scan(/'/)
- result << '''
- when run.include?(:entities) && markup.scan(/&/)
- result << '&'
- else
- # if none of this picks anything up, we send the character through to the result
- result << markup.getch
- end
- end
- result = pantsify(result) if run.include?(:pants)
- result = simple_format(result) if run.include?(:paragraphs) unless result.empty?
- return result
- end
Add Comment
Please, Sign In to add comment