Created
October 12, 2011 10:43
-
-
Save ariejan/1280898 to your computer and use it in GitHub Desktop.
Some useful content-related methods (summaries, markdown, syntax highlighting)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'falcon/content' | |
describe Falcon::Content do | |
context "htmlify" do | |
it "should process markdown" do | |
Falcon::Content.htmlify("This is **clean** markup").should == | |
"<p>This is <strong>clean</strong> markup</p>" | |
end | |
it "should process markdown with syntax highlighting, including language ref" do | |
Falcon::Content.htmlify(" :::ruby\n class Test; end").should match(/CodeRay/i) | |
end | |
it "should process markdown with syntax highlighting, not including language ref" do | |
Falcon::Content.htmlify(" class Test; end").should match(/CodeRay/i) | |
end | |
end | |
context "summary" do | |
it "should return the full body if no delemiter is present" do | |
body = "This is a long.\nlong\ntext." | |
Falcon::Content.summary_of(body).should == body | |
end | |
it "should return the correct summary" do | |
body = "This is a long.\n~\nlong\ntext." | |
Falcon::Content.summary_of(body).should == "This is a long." | |
end | |
it "should accept a custom delimiter" do | |
body = "This is a long.\n-----\nlong\ntext." | |
Falcon::Content.summary_of(body, "-----\n").should == "This is a long." | |
end | |
end | |
context "body" do | |
it "should return the body without the delimiter" do | |
body = "This is a long.\n~\nlong\ntext." | |
Falcon::Content.body_of(body).should == "This is a long.\nlong\ntext." | |
end | |
it "should return the body without the delimiter for a custom delimiter" do | |
body = "This is a long.\n-----\nlong\ntext." | |
Falcon::Content.body_of(body, "-----\n").should == "This is a long.\nlong\ntext." | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Just call them from you AR model like this: | |
class Post < ActiveRecord::Base | |
def summary | |
Falcon::Content.summary_of(body) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment