mongoid-markdown 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'rdiscount'
4
+
5
+ group :development do
6
+ gem "rspec", ">= 2.8.0"
7
+ gem "rdoc", ">= 3.12"
8
+ gem "bundler", ">= 1.0.0"
9
+ gem "jeweler", ">= 1.8.4"
10
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,33 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.3)
5
+ git (1.2.5)
6
+ jeweler (1.8.4)
7
+ bundler (~> 1.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ rdoc
11
+ json (1.7.5)
12
+ rake (0.9.2.2)
13
+ rdiscount (1.6.8)
14
+ rdoc (3.12)
15
+ json (~> 1.4)
16
+ rspec (2.11.0)
17
+ rspec-core (~> 2.11.0)
18
+ rspec-expectations (~> 2.11.0)
19
+ rspec-mocks (~> 2.11.0)
20
+ rspec-core (2.11.1)
21
+ rspec-expectations (2.11.3)
22
+ diff-lcs (~> 1.1.3)
23
+ rspec-mocks (2.11.2)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ bundler (>= 1.0.0)
30
+ jeweler (>= 1.8.4)
31
+ rdiscount
32
+ rdoc (>= 3.12)
33
+ rspec (>= 2.8.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Kristian Mandrup
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,44 @@
1
+ = mongoid-markdown
2
+
3
+ Add Markdown functionality for any Mongoid object.
4
+
5
+ ```ruby
6
+
7
+ class Message
8
+ include Mongoid::Document
9
+ include Mongoid::Markdown
10
+
11
+ field :body, :markdown => true
12
+ end
13
+
14
+
15
+ message = Message.new :body => my_markdown_text
16
+
17
+ message.body.markdown!
18
+
19
+ message.body = some_other_markedup_txt
20
+
21
+ message.body.reload
22
+
23
+ message.body.marked_down?
24
+ ```
25
+
26
+ Enjoy!
27
+
28
+ Extracted from: https://github.com/baphled/chat-engine/blob/master/lib/mongoid/markdown.rb
29
+
30
+ == Contributing to mongoid-markdown
31
+
32
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
33
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
34
+ * Fork the project.
35
+ * Start a feature/bugfix branch.
36
+ * Commit and push until you are happy with your contribution.
37
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
38
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
39
+
40
+ == Copyright
41
+
42
+ Copyright (c) 2012 Kristian Mandrup. See LICENSE.txt for
43
+ further details.
44
+
data/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "mongoid-markdown"
18
+ gem.homepage = "http://github.com/kristianmandrup/mongoid-markdown"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Add markdown functionality to any mongoid text field}
21
+ gem.description = %Q{Easily use markdown with mongoid texts}
22
+ gem.email = "kmandrup@gmail.com"
23
+ gem.authors = ["Kristian Mandrup"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rdoc/task'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "mongoid-markdown #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,110 @@
1
+ require 'rdiscount'
2
+
3
+ module Mongoid
4
+ module Markdown
5
+ def self.included(base)
6
+ base.extend ClassMethods
7
+ end
8
+
9
+ module ClassMethods
10
+ def markdown(*attributes)
11
+ write_inheritable_attribute(:markdown_attributes, markdown_attributes + Array(attributes))
12
+ @markdown_unicode = String.new.respond_to? :chars
13
+
14
+ type_options = %w( plain source )
15
+
16
+ attributes.each do |attribute|
17
+ define_method(attribute) do |*type|
18
+ type = type.first
19
+ value = read_attribute(attribute)
20
+
21
+ if type.nil? && value
22
+ marked_down[attribute.to_sym] ||= RDiscount.new(value).to_html.html_safe
23
+ elsif type.nil? && value.nil?
24
+ nil
25
+ elsif type_options.include?(type.to_s)
26
+ send("#{attribute}_#{type}")
27
+ else
28
+ raise "I don't understand the `#{type}' option. Try #{type_options.join(' or ')}."
29
+ end
30
+ end
31
+
32
+ define_method("#{attribute}_plain", proc { strip_markdown_html(__send__(attribute)) if __send__(attribute) } )
33
+ define_method("#{attribute}_source", proc { read_attribute(attribute) } )
34
+ end
35
+
36
+ include InstanceMethods
37
+ end
38
+
39
+ def markdown_attributes
40
+ read_inheritable_attribute(:markdown_attributes) ||
41
+ write_inheritable_attribute(:markdown_attributes, [])
42
+ end
43
+
44
+ def field(name, options = {})
45
+ returning super(name, options.reject { |k, v| k == :markdown }) do
46
+ markdown name if options[:markdown]
47
+ end
48
+ end
49
+ end
50
+
51
+ module InstanceMethods
52
+ def marked_down
53
+ marked_down? ? (@marked_down ||= {}) : @attributes.dup
54
+ end
55
+
56
+ def marked_down?
57
+ @is_marked_down != false
58
+ end
59
+
60
+ def marked_down=(value)
61
+ @is_marked_down = !!value
62
+ end
63
+
64
+ def markdown!
65
+ self.class.markdown_attributes.each { |attr| __send__(attr) }
66
+ end
67
+
68
+ def reload
69
+ marked_down.clear
70
+ super
71
+ end
72
+
73
+ def write_attribute(attr_name, value)
74
+ marked_down[attr_name.to_s] = nil
75
+ super
76
+ end
77
+
78
+ private
79
+ def strip_markdown_html(html)
80
+ returning html.dup.gsub(html_regexp, '') do |h|
81
+ markdown_glyphs.each do |(entity, char)|
82
+ sub = [ :gsub!, entity, char ]
83
+ @textiled_unicode ? h.chars.send(*sub) : h.send(*sub)
84
+ end
85
+ end
86
+ end
87
+
88
+ def markdown_glyphs
89
+ [[ '’', "'" ],
90
+ [ '‘', "'" ],
91
+ [ '&lt;', '<' ],
92
+ [ '&gt;', '>' ],
93
+ [ '&#8221;', '"' ],
94
+ [ '&#8220;', '"' ],
95
+ [ '&#8230;', '...' ],
96
+ [ '\1&#8212;', '--' ],
97
+ [ ' &rarr; ', '->' ],
98
+ [ ' &#8211; ', '-' ],
99
+ [ '&#215;', 'x' ],
100
+ [ '&#8482;', '(TM)' ],
101
+ [ '&#174;', '(R)' ],
102
+ [ '&#169;', '(C)' ]]
103
+ end
104
+
105
+ def html_regexp
106
+ %r{<(?:[^>"']+|"(?:\\.|[^\\"]+)*"|'(?:\\.|[^\\']+)*')*>}xm
107
+ end
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,7 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "MongoidMarkdown" do
4
+ it "fails" do
5
+ fail "hey buddy, you should probably rename this file and start specing for real"
6
+ end
7
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'mongoid-markdown'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid-markdown
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kristian Mandrup
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rdiscount
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 2.8.0
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 2.8.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: rdoc
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '3.12'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '3.12'
62
+ - !ruby/object:Gem::Dependency
63
+ name: bundler
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: 1.0.0
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: 1.0.0
78
+ - !ruby/object:Gem::Dependency
79
+ name: jeweler
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: 1.8.4
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: 1.8.4
94
+ description: Easily use markdown with mongoid texts
95
+ email: kmandrup@gmail.com
96
+ executables: []
97
+ extensions: []
98
+ extra_rdoc_files:
99
+ - LICENSE.txt
100
+ - README.rdoc
101
+ files:
102
+ - .document
103
+ - .rspec
104
+ - Gemfile
105
+ - Gemfile.lock
106
+ - LICENSE.txt
107
+ - README.rdoc
108
+ - Rakefile
109
+ - VERSION
110
+ - lib/mongoid-markdown.rb
111
+ - spec/mongoid-markdown_spec.rb
112
+ - spec/spec_helper.rb
113
+ homepage: http://github.com/kristianmandrup/mongoid-markdown
114
+ licenses:
115
+ - MIT
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ segments:
127
+ - 0
128
+ hash: 4336708852903825094
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ none: false
131
+ requirements:
132
+ - - ! '>='
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubyforge_project:
137
+ rubygems_version: 1.8.24
138
+ signing_key:
139
+ specification_version: 3
140
+ summary: Add markdown functionality to any mongoid text field
141
+ test_files: []