Guest User

Untitled

a guest
Aug 15th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. require 'rdiscount'
  2. require 'nokogiri'
  3. require 'coderay'
  4.  
  5. module Falcon
  6. class Content
  7. # Return a summary if a delimiter is found, otherwise
  8. # return the full body.
  9. def self.summary_of(text, delimiter = "~\n")
  10. summary = if text =~ /#{delimiter}/i
  11. text.split(/#{delimiter}/i).first.strip
  12. else
  13. text
  14. end
  15. end
  16.  
  17. # Return the full body, excluding any delimiter
  18. # that may be present
  19. def self.body_of(text, delimiter = "~\n")
  20. text.gsub(/#{delimiter}/i, '')
  21. end
  22.  
  23. # Apply markdown and CodeRay syntax highlighting,
  24. def self.htmlify(text)
  25. # Mardown => HTML
  26. html = RDiscount.new(text).to_html
  27.  
  28. # Syntax highlighting with Coderay
  29. doc = Nokogiri::HTML::fragment(html, 'UTF-8')
  30. nodes = doc.search("pre>code")
  31. highlighter = :coderay
  32. nodes.each do |node|
  33. s = node.inner_html || "[++where is the code?++]"
  34. node.parent.swap(send(highlighter, s))
  35. end
  36.  
  37. doc.to_html
  38. end
  39.  
  40. private
  41. def self.coderay(string, pattern = /\A:::(\w+)\s*(\n|
)/i)
  42. lang = 'unknown'
  43. refs = pattern.match(string) # extract language name
  44. if refs
  45. lang = refs[1]
  46. str = unescape_html(string.sub(pattern, ""))
  47. "<pre class='CodeRay'>#{::CodeRay.encoder(:html).encode str, lang}</pre>"
  48. else
  49. "<pre class='CodeRay'>#{string}</pre>"
  50. end
  51. end
  52.  
  53. private
  54. def self.unescape_html(string)
  55. string.to_s.gsub(/&#x000A;/i, "\n").gsub("<", '<').gsub(">", '>').gsub("&", '&')
  56. end
  57. end
  58. end
Add Comment
Please, Sign In to add comment