-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8e5b03a
commit 8fef628
Showing
6 changed files
with
177 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,14 +1,10 @@ | |||
source "http://rubygems.org" | source "http://rubygems.org" | ||
# Add dependencies required to use your gem here. | |||
# Example: | |||
# gem "activesupport", ">= 2.3.5" | |||
|
|
||
# Add dependencies to develop your gem here. | gem 'rdiscount' | ||
# Include everything needed to run rake, tests, features, etc. |
|
||
group :development do | group :development do | ||
gem "rspec", "~> 2.8.0" | gem "rspec", ">= 2.8.0" | ||
gem "rdoc", "~> 3.12" | gem "rdoc", ">= 3.12" | ||
gem "bundler", "~> 1.0.0" | gem "bundler", ">= 1.0.0" | ||
gem "jeweler", "~> 1.8.4" | gem "jeweler", ">= 1.8.4" | ||
gem "rcov", ">= 0" | |||
end | end |
This file contains 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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,33 @@ | |||
GEM | |||
remote: http://rubygems.org/ | |||
specs: | |||
diff-lcs (1.1.3) | |||
git (1.2.5) | |||
jeweler (1.8.4) | |||
bundler (~> 1.0) | |||
git (>= 1.2.5) | |||
rake | |||
rdoc | |||
json (1.7.5) | |||
rake (0.9.2.2) | |||
rdiscount (1.6.8) | |||
rdoc (3.12) | |||
json (~> 1.4) | |||
rspec (2.11.0) | |||
rspec-core (~> 2.11.0) | |||
rspec-expectations (~> 2.11.0) | |||
rspec-mocks (~> 2.11.0) | |||
rspec-core (2.11.1) | |||
rspec-expectations (2.11.3) | |||
diff-lcs (~> 1.1.3) | |||
rspec-mocks (2.11.2) | |||
|
|||
PLATFORMS | |||
ruby | |||
|
|||
DEPENDENCIES | |||
bundler (>= 1.0.0) | |||
jeweler (>= 1.8.4) | |||
rdiscount | |||
rdoc (>= 3.12) | |||
rspec (>= 2.8.0) |
This file contains 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
This file contains 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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1 +1 @@ | |||
0.0.0 | 0.1.0 |
This file contains 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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,110 @@ | |||
require 'rdiscount' | |||
|
|||
module Mongoid | |||
module Markdown | |||
def self.included(base) | |||
base.extend ClassMethods | |||
end | |||
|
|||
module ClassMethods | |||
def markdown(*attributes) | |||
write_inheritable_attribute(:markdown_attributes, markdown_attributes + Array(attributes)) | |||
@markdown_unicode = String.new.respond_to? :chars | |||
|
|||
type_options = %w( plain source ) | |||
|
|||
attributes.each do |attribute| | |||
define_method(attribute) do |*type| | |||
type = type.first | |||
value = read_attribute(attribute) | |||
|
|||
if type.nil? && value | |||
marked_down[attribute.to_sym] ||= RDiscount.new(value).to_html.html_safe | |||
elsif type.nil? && value.nil? | |||
nil | |||
elsif type_options.include?(type.to_s) | |||
send("#{attribute}_#{type}") | |||
else | |||
raise "I don't understand the `#{type}' option. Try #{type_options.join(' or ')}." | |||
end | |||
end | |||
|
|||
define_method("#{attribute}_plain", proc { strip_markdown_html(__send__(attribute)) if __send__(attribute) } ) | |||
define_method("#{attribute}_source", proc { read_attribute(attribute) } ) | |||
end | |||
|
|||
include InstanceMethods | |||
end | |||
|
|||
def markdown_attributes | |||
read_inheritable_attribute(:markdown_attributes) || | |||
write_inheritable_attribute(:markdown_attributes, []) | |||
end | |||
|
|||
def field(name, options = {}) | |||
returning super(name, options.reject { |k, v| k == :markdown }) do | |||
markdown name if options[:markdown] | |||
end | |||
end | |||
end | |||
|
|||
module InstanceMethods | |||
def marked_down | |||
marked_down? ? (@marked_down ||= {}) : @attributes.dup | |||
end | |||
|
|||
def marked_down? | |||
@is_marked_down != false | |||
end | |||
|
|||
def marked_down=(value) | |||
@is_marked_down = !!value | |||
end | |||
|
|||
def markdown! | |||
self.class.markdown_attributes.each { |attr| __send__(attr) } | |||
end | |||
|
|||
def reload | |||
marked_down.clear | |||
super | |||
end | |||
|
|||
def write_attribute(attr_name, value) | |||
marked_down[attr_name.to_s] = nil | |||
super | |||
end | |||
|
|||
private | |||
def strip_markdown_html(html) | |||
returning html.dup.gsub(html_regexp, '') do |h| | |||
markdown_glyphs.each do |(entity, char)| | |||
sub = [ :gsub!, entity, char ] | |||
@textiled_unicode ? h.chars.send(*sub) : h.send(*sub) | |||
end | |||
end | |||
end | |||
|
|||
def markdown_glyphs | |||
[[ '’', "'" ], | |||
[ '‘', "'" ], | |||
[ '<', '<' ], | |||
[ '>', '>' ], | |||
[ '”', '"' ], | |||
[ '“', '"' ], | |||
[ '…', '...' ], | |||
[ '\1—', '--' ], | |||
[ ' → ', '->' ], | |||
[ ' – ', '-' ], | |||
[ '×', 'x' ], | |||
[ '™', '(TM)' ], | |||
[ '®', '(R)' ], | |||
[ '©', '(C)' ]] | |||
end | |||
|
|||
def html_regexp | |||
%r{<(?:[^>"']+|"(?:\\.|[^\\"]+)*"|'(?:\\.|[^\\']+)*')*>}xm | |||
end | |||
end | |||
end | |||
end |