acts_as_markup 1.3.4 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore DELETED
@@ -1,4 +0,0 @@
1
- coverage
2
- doc
3
- pkg
4
- docs
@@ -1,204 +0,0 @@
1
- require 'active_record'
2
-
3
- module ActiveRecord # :nodoc:
4
- module Acts # :nodoc:
5
- module AsMarkup
6
- def self.included(base) # :nodoc:
7
- base.extend(ClassMethods)
8
- end
9
-
10
- module ClassMethods
11
-
12
- # This allows you to specify columns you want to define as containing
13
- # Markdown, Textile, Wikitext or RDoc content.
14
- # Then you can simply call <tt>.to_html</tt> method on the attribute.
15
- #
16
- # You can also specify the language as <tt>:variable</tt>. The language used
17
- # to process the column will be based on another column. By default a column
18
- # named "<tt>markup_language</tt>" is used, but this can be changed by providing
19
- # a <tt>:language_column</tt> option. When a value is accessed it will create
20
- # the correct object (Markdown, Textile, Wikitext or RDoc) based on the value
21
- # of the language column. If any value besides markdown, textile, wikitext, or
22
- # RDoc is supplied for the markup language the text will pass through as a string.
23
- #
24
- # You can specify additional options to pass to the markup library by using
25
- # <tt>:markdown_options</tt>, <tt>:textile_options</tt> or <tt>:wikitext_options</tt>.
26
- # RDoc does not support any useful options. The options should be given as an array
27
- # of arguments. You can specify options for more than one language when using
28
- # <tt>:variable</tt>. See each library's documentation for more details on what
29
- # options are available.
30
- #
31
- #
32
- # ==== Examples
33
- #
34
- # ===== Using Markdown language
35
- #
36
- # class Post < ActiveRecord
37
- # acts_as_markup :language => :markdown, :columns => [:body]
38
- # end
39
- #
40
- # @post = Post.find(:first)
41
- # @post.body.to_s # => "## Markdown Headline"
42
- # @post.body.to_html # => "<h2> Markdown Headline</h2>"
43
- #
44
- #
45
- # ===== Using variable language
46
- #
47
- # class Post < ActiveRecord
48
- # acts_as_markup :language => :variable, :columns => [:body], :language_column => 'language_name'
49
- # end
50
- #
51
- # @post = Post.find(:first)
52
- # @post.language_name # => "markdown"
53
- # @post.body.to_s # => "## Markdown Headline"
54
- # @post.body.to_html # => "<h2> Markdown Headline</h2>"
55
- #
56
- #
57
- # ===== Using options
58
- #
59
- # class Post < ActiveRecord
60
- # acts_as_markup :language => :markdown, :columns => [:body], :markdown_options => [ :filter_html ]
61
- # end
62
- #
63
- # class Post < ActiveRecord
64
- # acts_as_markup :language => :textile, :columns => [:body], :textile_options => [ [ :filter_html ] ]
65
- # end
66
- #
67
- # class Post < ActiveRecord
68
- # acts_as_markup :language => :wikitext, :columns => [:body], :wikitext_options => [ { :space_to_underscore => true } ]
69
- # end
70
- #
71
- #
72
- def acts_as_markup(options)
73
- case options[:language].to_sym
74
- when :markdown, :textile, :wikitext, :rdoc
75
- klass = require_library_and_get_class(options[:language].to_sym)
76
- when :variable
77
- markup_klasses = {}
78
- [:textile, :wikitext, :rdoc, :markdown].each do |language|
79
- markup_klasses[language] = require_library_and_get_class(language)
80
- end
81
- options[:language_column] ||= :markup_language
82
- else
83
- raise ActsAsMarkup::UnsupportedMarkupLanguage, "#{options[:langauge]} is not a currently supported markup language."
84
- end
85
-
86
- unless options[:language].to_sym == :variable
87
- markup_options = options["#{options[:language]}_options".to_sym] || []
88
- options[:columns].each do |col|
89
- define_method col do
90
- if instance_variable_defined?("@#{col}")
91
- unless send("#{col}_changed?")
92
- return instance_variable_get("@#{col}")
93
- end
94
- end
95
- instance_variable_set("@#{col}", klass.new(self[col].to_s, *markup_options))
96
- end
97
- end
98
- else
99
- options[:columns].each do |col|
100
- define_method col do
101
- if instance_variable_defined?("@#{col}")
102
- unless send("#{col}_changed?") || send("#{options[:language_column]}_changed?")
103
- return instance_variable_get("@#{col}")
104
- end
105
- end
106
- instance_variable_set("@#{col}", case send(options[:language_column])
107
- when /markdown/i
108
- markup_klasses[:markdown].new self[col].to_s, *(options[:markdown_options] || [])
109
- when /textile/i
110
- markup_klasses[:textile].new self[col].to_s, *(options[:textile_options] || [])
111
- when /wikitext/i
112
- markup_klasses[:wikitext].new self[col].to_s, *(options[:wikitext_options] || [])
113
- when /rdoc/i
114
- markup_klasses[:rdoc].new self[col].to_s
115
- else
116
- self[col]
117
- end)
118
- end
119
- end
120
- end
121
- end
122
-
123
- # This is a convenience method for
124
- # `<tt>acts_as_markup :language => :markdown, :columns => [:body]</tt>`
125
- # Additional options can be given at the end, if necessary.
126
- #
127
- def acts_as_markdown(*columns)
128
- options = columns.extract_options!
129
- acts_as_markup options.merge(:language => :markdown, :columns => columns)
130
- end
131
-
132
- # This is a convenience method for
133
- # `<tt>acts_as_markup :language => :textile, :columns => [:body]</tt>`
134
- # Additional options can be given at the end, if necessary.
135
- #
136
- def acts_as_textile(*columns)
137
- options = columns.extract_options!
138
- acts_as_markup options.merge(:language => :textile, :columns => columns)
139
- end
140
-
141
- # This is a convenience method for
142
- # `<tt>acts_as_markup :language => :wikitext, :columns => [:body]</tt>`
143
- # Additional options can be given at the end, if necessary.
144
- #
145
- def acts_as_wikitext(*columns)
146
- options = columns.extract_options!
147
- acts_as_markup options.merge(:language => :wikitext, :columns => columns)
148
- end
149
-
150
- # This is a convenience method for
151
- # `<tt>acts_as_markup :language => :rdoc, :columns => [:body]</tt>`
152
- # Additional options can be given at the end, if necessary.
153
- #
154
- def acts_as_rdoc(*columns)
155
- options = columns.extract_options!
156
- acts_as_markup options.merge(:language => :rdoc, :columns => columns)
157
- end
158
-
159
-
160
- private
161
- def get_markdown_class
162
- if ActsAsMarkup::MARKDOWN_LIBS.keys.include? ActsAsMarkup.markdown_library
163
- markdown_library_names = ActsAsMarkup::MARKDOWN_LIBS[ActsAsMarkup.markdown_library]
164
- require markdown_library_names[:lib_name]
165
- require_extensions(markdown_library_names[:lib_name])
166
- return markdown_library_names[:class_name].constantize
167
- else
168
- raise ActsAsMarkup::UnsportedMarkdownLibrary, "#{ActsAsMarkup.markdown_library} is not currently supported."
169
- end
170
- end
171
-
172
- def require_extensions(library)# :nodoc:
173
- if ActsAsMarkup::LIBRARY_EXTENSIONS.include? library.to_s
174
- require "#{ActsAsMarkup::LIBPATH}/acts_as_markup/exts/#{library}"
175
- end
176
- end
177
-
178
- def require_library_and_get_class(language)
179
- case language
180
- when :markdown
181
- return get_markdown_class
182
- when :textile
183
- require 'redcloth'
184
- return RedCloth
185
- when :wikitext
186
- require 'wikitext'
187
- require_extensions 'wikitext'
188
- return WikitextString
189
- when :rdoc
190
- require 'rdoc/markup/simple_markup'
191
- require 'rdoc/markup/simple_markup/to_html'
192
- require_extensions 'rdoc'
193
- return RDocText
194
- else
195
- return String
196
- end
197
- end
198
-
199
- end
200
- end
201
- end
202
- end
203
-
204
- ActiveRecord::Base.send :include, ActiveRecord::Acts::AsMarkup
@@ -1,77 +0,0 @@
1
- require File.dirname(__FILE__) + '/test_helper'
2
-
3
- class ActsAsWikitextTest < ActsAsMarkupTestCase
4
- context 'acts_as_wikitext' do
5
- setup do
6
- @wikitext = "== Wikitext Test Text =="
7
- class ::Post < ActiveRecord::Base
8
- acts_as_wikitext :body
9
- end
10
- @post = Post.create!(:title => 'Blah', :body => @wikitext)
11
- end
12
-
13
- should "have a WikitextString object returned for the column value" do
14
- assert_kind_of WikitextString, @post.body
15
- end
16
-
17
- should "return original wikitext text for a `to_s` method call on the column value" do
18
- assert_equal @wikitext, @post.body.to_s
19
- end
20
-
21
- should 'return false for .blank?' do
22
- assert !@post.body.blank?
23
- end
24
-
25
- should "return formated html for a `to_html` method call on the column value" do
26
- assert_match(/<h2>Wikitext Test Text<\/h2>/, @post.body.to_html)
27
- end
28
-
29
- should "underscore spaces in URLs" do
30
- @post.body = "[[foo bar]]"
31
- assert_match(/<a href="\/wiki\/foo_bar">foo bar<\/a>/, @post.body.to_html)
32
- end
33
-
34
- context "changing value of wikitext field should return new wikitext object" do
35
- setup do
36
- @old_body = @post.body
37
- @post.body = "`@count = 20`"
38
- end
39
-
40
- should "still have an WikitextString object but not the same object" do
41
- assert_kind_of WikitextString, @post.body
42
- assert_not_same @post.body, @old_body
43
- end
44
-
45
- should "return correct text for `to_s`" do
46
- assert_equal "`@count = 20`", @post.body.to_s
47
- end
48
-
49
- should "return correct HTML for the `to_html` method" do
50
- assert_match(/<p><code>\@count\s\=\s20<\/code><\/p>/, @post.body.to_html)
51
- end
52
-
53
- teardown do
54
- @old_body = nil
55
- end
56
- end
57
-
58
- teardown do
59
- @wikitext, @post = nil
60
- Post.delete_all
61
- end
62
- end
63
-
64
- context 'acts_as_wikitext with options' do
65
- setup do
66
- class ::Post
67
- acts_as_wikitext :body, :wikitext_options => [ { :space_to_underscore => false } ]
68
- end
69
- @post = Post.new(:title => 'Blah')
70
- end
71
-
72
- should "not underscore spaces in URLs because of :space_to_underscore option" do
73
- @post.body = "[[foo bar]]"
74
- assert_match(/<a href="\/wiki\/foo%20bar">foo bar<\/a>/, @post.body.to_html)
75
- end
76
- end
77
- end