Skip to content

Commit

Permalink
initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
kristianmandrup committed Sep 14, 2012
1 parent 8e5b03a commit 8fef628
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pkg
# #
# For MacOS: # For MacOS:
# #
#.DS_Store .DS_Store


# For TextMate # For TextMate
#*.tmproj #*.tmproj
Expand Down
16 changes: 6 additions & 10 deletions Gemfile
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
33 changes: 33 additions & 0 deletions Gemfile.lock
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)
27 changes: 26 additions & 1 deletion README.rdoc
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,31 @@
= mongoid-markdown = mongoid-markdown


Description goes here. Add Markdown functionality for any Mongoid object.

```ruby

class Message
include Mongoid::Document
include Mongoid::Markdown

field :body, :markdown => true
end


message = Message.new :body => my_markdown_text

message.body.markdown!

message.body = some_other_markedup_txt

message.body.reload

message.body.marked_down?
```

Enjoy!

Extracted from: https://github.com/baphled/chat-engine/blob/master/lib/mongoid/markdown.rb


== Contributing to mongoid-markdown == Contributing to mongoid-markdown


Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Original file line Diff line number Diff line change
@@ -1 +1 @@
0.0.0 0.1.0
110 changes: 110 additions & 0 deletions lib/mongoid-markdown.rb
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
[[ '’', "'" ],
[ '‘', "'" ],
[ '&lt;', '<' ],
[ '&gt;', '>' ],
[ '&#8221;', '"' ],
[ '&#8220;', '"' ],
[ '&#8230;', '...' ],
[ '\1&#8212;', '--' ],
[ ' &rarr; ', '->' ],
[ ' &#8211; ', '-' ],
[ '&#215;', 'x' ],
[ '&#8482;', '(TM)' ],
[ '&#174;', '(R)' ],
[ '&#169;', '(C)' ]]
end

def html_regexp
%r{<(?:[^>"']+|"(?:\\.|[^\\"]+)*"|'(?:\\.|[^\\']+)*')*>}xm
end
end
end
end

0 comments on commit 8fef628

Please sign in to comment.