Permalink
Browse files

initial version

  • Loading branch information...
1 parent 8e5b03a commit 8fef628ee84a897f0457becd1a8526a89aa8c4c9 @kristianmandrup committed Sep 14, 2012
Showing with 177 additions and 13 deletions.
  1. +1 −1 .gitignore
  2. +6 −10 Gemfile
  3. +33 −0 Gemfile.lock
  4. +26 −1 README.rdoc
  5. +1 −1 VERSION
  6. +110 −0 lib/mongoid-markdown.rb
View
@@ -28,7 +28,7 @@ pkg
#
# For MacOS:
#
-#.DS_Store
+.DS_Store
# For TextMate
#*.tmproj
View
16 Gemfile
@@ -1,14 +1,10 @@
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.
-# Include everything needed to run rake, tests, features, etc.
+gem 'rdiscount'
+
group :development do
- gem "rspec", "~> 2.8.0"
- gem "rdoc", "~> 3.12"
- gem "bundler", "~> 1.0.0"
- gem "jeweler", "~> 1.8.4"
- gem "rcov", ">= 0"
+ gem "rspec", ">= 2.8.0"
+ gem "rdoc", ">= 3.12"
+ gem "bundler", ">= 1.0.0"
+ gem "jeweler", ">= 1.8.4"
end
View
@@ -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)
View
@@ -1,6 +1,31 @@
= 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
View
@@ -1 +1 @@
-0.0.0
+0.1.0
View
@@ -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.