Advertisement
Guest User

Untitled

a guest
Apr 18th, 2014
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. Hey, *check out* my video: [VMD-z2Xni8U](youtube)
  2.  
  3. # /lib/helpers/markdown_renderer_with_special_links.rb
  4. class MarkdownRendererWithSpecialLinks < Redcarpet::Render::HTML
  5. def autolink(link, link_type)
  6. case link_type
  7. when :url then url_link(link)
  8. when :email then email_link(link)
  9. end
  10. end
  11. def url_link(link)
  12. case link
  13. when /^http://youtube/ then youtube_link(link)
  14. else normal_link(link)
  15. end
  16. end
  17. def youtube_link(link)
  18. parameters_start = link.index('?')
  19. video_id = link[15..(parameters_start ? parameters_start-1 : -1)]
  20. "<iframe width="560" height="315" src="//www.youtube.com/embed/#{video_id}?rel=0" frameborder="0" allowfullscreen></iframe>"
  21. end
  22. def normal_link(link)
  23. "<a href="#{link}">#{link}</a>"
  24. end
  25. def email_link(email)
  26. "<a href="mailto:#{email}">#{email}</a>"
  27. end
  28. end
  29.  
  30. # /app/helpers/application_helper.rb
  31. module ApplicationHelper
  32. require './lib/helpers/markdown_renderer_with_special_links'
  33. def markdown(content)
  34. @markdown ||= Redcarpet::Markdown.new(MarkdownRendererWithSpecialLinks, autolink: true, space_after_headers: true, fenced_code_blocks: true)
  35. @markdown.render(content).html_safe
  36. end
  37. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement