Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'rdiscount'
- require 'nokogiri'
- require 'coderay'
- module Falcon
- class Content
- # Return a summary if a delimiter is found, otherwise
- # return the full body.
- def self.summary_of(text, delimiter = "~\n")
- summary = if text =~ /#{delimiter}/i
- text.split(/#{delimiter}/i).first.strip
- else
- text
- end
- end
- # Return the full body, excluding any delimiter
- # that may be present
- def self.body_of(text, delimiter = "~\n")
- text.gsub(/#{delimiter}/i, '')
- end
- # Apply markdown and CodeRay syntax highlighting,
- def self.htmlify(text)
- # Mardown => HTML
- html = RDiscount.new(text).to_html
- # Syntax highlighting with Coderay
- doc = Nokogiri::HTML::fragment(html, 'UTF-8')
- nodes = doc.search("pre>code")
- highlighter = :coderay
- nodes.each do |node|
- s = node.inner_html || "[++where is the code?++]"
- node.parent.swap(send(highlighter, s))
- end
- doc.to_html
- end
- private
- def self.coderay(string, pattern = /\A:::(\w+)\s*(\n|
)/i)
- lang = 'unknown'
- refs = pattern.match(string) # extract language name
- if refs
- lang = refs[1]
- str = unescape_html(string.sub(pattern, ""))
- "<pre class='CodeRay'>#{::CodeRay.encoder(:html).encode str, lang}</pre>"
- else
- "<pre class='CodeRay'>#{string}</pre>"
- end
- end
- private
- def self.unescape_html(string)
- string.to_s.gsub(/
/i, "\n").gsub("<", '<').gsub(">", '>').gsub("&", '&')
- end
- end
- end
Add Comment
Please, Sign In to add comment