Guest User

Untitled

a guest
Feb 21st, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. def htmlify(markup, options = {})
  2. options = { :only => [:emotes, :emphasis, :swatches, :profile_links, :http_links, :entities, :pants], :truncate => 45 }.merge(options)
  3. run = Array.new
  4. run << options[:only].to_a.dup
  5. run << options[:also].to_a.dup
  6. run.flatten!
  7. run.delete_if { |r| options[:except].to_a.flatten.include?(r) }
  8.  
  9. # load emote index if it hasn't already been loaded
  10. if run.include?(:emotes)
  11. require 'emotes'
  12. @emoset ||= EmoteSet.from_s(IO.read("#{RAILS_ROOT}/public/images/ipb-emotes/index.txt")) if run.include?(:emotes)
  13. @emo_scan_len ||= @emoset.map { |i| i.triggers.map { |t| t.length }.max }.max
  14. end
  15.  
  16. markup = StringScanner.new(markup.to_s)
  17. result = String.new
  18.  
  19. while markup.rest?
  20. case
  21. when run.include?(:profile_links) && markup.scan(/\[-([0-9]+)-\]/)
  22. profile = Profile.find(markup[1].to_i, :select => 'id, name') rescue nil
  23. if profile
  24. result << link_to(profile.name, profile_url(:id => markup[1]))
  25. else
  26. result << content_tag('span', "[-Error, cannot find profile with id '#{markup[1]}'-]")
  27. end
  28.  
  29. when run.include?(:swatches) && markup.scan(/\#([0-9A-F]{6,6}|[0-9A-F]{3,3})/)
  30. result << colour_swatch(markup.matched)
  31.  
  32. when run.include?(:emphasis) && markup.scan(/([*_])(([a-z0-9].*?)?\S)\1/)
  33. result << '<em>' + htmlify(markup[2], :only => run, :except => [:emphasis, :pants, :paragraphs]) + '</em>'
  34.  
  35. when run.include?(:emphasis) && markup.scan(/([*_]{2,2})(([a-z0-9].*?)?\S)\1/)
  36. result << '<strong>' + htmlify(markup[2], :only => run, :except => [:emphasis, :pants, :paragraphs]) + '</strong>'
  37.  
  38. when run.include?(:emotes) && (emote = @emoset.start_match(markup.peek(@emo_scan_len)))
  39. result << emote.to_html(:style => ('vertical-align: middle; margin: 0; padding: 0; border: 0px none;' if @inline_styles))
  40. markup.pointer += emote.trigger.length
  41.  
  42. when run.include?(:http_links) && markup.scan(/<http:\/\/([a-z0-9\.]+(:[0-9]+)?\/[-_+$@.&*!\#\"\'()?,%a-z0-9\/]*)>/i)
  43. result << '<' + link_to(h(markup[1]), markup[1]) + '>'
  44.  
  45. when run.include?(:entities) && markup.scan(/</)
  46. result << '<'
  47. when run.include?(:entities) && markup.scan(/>/)
  48. result << '>'
  49. when run.include?(:entities) && !run.include?(:pants) && markup.scan(/"/)
  50. result << '"'
  51. when run.include?(:entities) && !run.include?(:pants) && markup.scan(/'/)
  52. result << '&apos;'
  53. when run.include?(:entities) && markup.scan(/&/)
  54. result << '&'
  55. else
  56. # if none of this picks anything up, we send the character through to the result
  57. result << markup.getch
  58. end
  59. end
  60.  
  61. result = pantsify(result) if run.include?(:pants)
  62. result = simple_format(result) if run.include?(:paragraphs) unless result.empty?
  63.  
  64. return result
  65. end
Add Comment
Please, Sign In to add comment