pluginaweek-tiny_mce_helper 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,46 @@
1
+ == master
2
+
3
+ == 0.3.0 / 2009-04-14
4
+
5
+ * No longer require that the Sourceforge project page be downloaded when determining the location of a custom version
6
+ * Require at least TinyMCE 3.2.2 be installed
7
+ * Update to new TinyMCE file structure
8
+ * Update to new Sourceforge downloads location
9
+
10
+ == 0.2.0 / 2008-12-14
11
+
12
+ * Remove the PluginAWeek namespace
13
+ * Update tests to use mocha
14
+ * Update tests to use ActionView::TestCase
15
+
16
+ == 0.1.1 / 2008-10-19
17
+
18
+ * Fix TinyMCE temp file getting closed/deleted too soon after being downloaded [Brandon Goodin]
19
+ * Add more descriptive output during installation
20
+
21
+ == 0.1.0 / 2008-08-30
22
+
23
+ * Remove references to system wget/unzip calls for cross-platform compatibility
24
+ * Add dependency on RubyZip
25
+
26
+ == 0.0.4 / 2008-06-22
27
+
28
+ * Remove log files from gems
29
+
30
+ == 0.0.3 / 2008-05-05
31
+
32
+ * Updated documentation
33
+ * Change references from RAILS_ROOT to Rails.root
34
+ * Use zipped version of TinyMCE instead of tar
35
+
36
+ == 0.0.2 / 2007-09-26
37
+
38
+ * Remove gem dependency on actionpack
39
+
40
+ == 0.0.1 / 2007-08-20
41
+
42
+ * Add README documentation
43
+ * Add tiny_mce:uninstall and tiny_mce:update rake tasks
44
+ * Add working unit tests
45
+ * Add installation and uninstallation scripts
46
+ * Add rake tasks tiny_mce:install and tiny_mce:update_options
@@ -0,0 +1,185 @@
1
+ = tiny_mce_helper
2
+
3
+ +tiny_mce_helper+ adds helper methods for creating the TinyMCE initialization
4
+ script.
5
+
6
+ == Resources
7
+
8
+ API
9
+
10
+ * http://api.pluginaweek.org/tiny_mce_helper
11
+
12
+ Bugs
13
+
14
+ * http://pluginaweek.lighthouseapp.com/projects/13291-tiny_mce_helper
15
+
16
+ Development
17
+
18
+ * http://github.com/pluginaweek/tiny_mce_helper
19
+
20
+ Source
21
+
22
+ * git://github.com/pluginaweek/tiny_mce_helper.git
23
+
24
+ == Description
25
+
26
+ TinyMCE is a rich text editor written in Javascript. This helper plugin helps
27
+ make it easier to validate your code and include the TinyMCE initialization
28
+ script. It also provides various tasks for installing/updating TinyMCE.
29
+
30
+ === Usage
31
+
32
+ === Installing TinyMCE
33
+
34
+ To install TinyMCE, you can use the tiny_mce:install task. This will download
35
+ the latest version (or one specified by you) from Sourceforge and extract it
36
+ into your application's public/javascripts folder.
37
+
38
+ For example, to install the latest version:
39
+
40
+ $ rake tiny_mce:install
41
+ (in /my/project)
42
+ Downloading TinyMCE source...
43
+ Extracting...
44
+ Done!
45
+
46
+ To install a custom version:
47
+
48
+ $ rake tiny_mce:install VERSION=3.2.2
49
+ (in /my/project)
50
+ Downloading TinyMCE source...
51
+ Extracting...
52
+ Done!
53
+
54
+ If tinymce already exists in your application's public/javascript folder, the
55
+ rake task will prompt you as to whether or not it should be overwritten:
56
+
57
+ $ rake tiny_mce:install
58
+ (in /my/project)
59
+ TinyMCE already be installed in /my/project/config/../public/javascripts/tiny_mce. Overwrite? (y/n): y
60
+ Downloading TinyMCE source...
61
+ Extracting...
62
+ Done!
63
+
64
+ === Updating TinyMCE
65
+
66
+ If you have already installed TinyMCE and wish to update to the latest version,
67
+ you can run the tiny_mce:update task. This will overwrite the current TinyMCE
68
+ installation and WILL NOT prompt you if the folder already exists. For example,
69
+
70
+ $ rake tiny_mce:update
71
+ (in /my/project)
72
+ Downloading TinyMCE source...
73
+ Extracting...
74
+ Done!
75
+
76
+ Like tiny_mce:install, you can also specify a custom version:
77
+
78
+ $ rake tiny_mce:update VERSION=3.2.2
79
+ (in /my/project)
80
+ Downloading TinyMCE source...
81
+ Extracting...
82
+ Done!
83
+
84
+ === Updating configuration options
85
+
86
+ In addition to installing the Javascript source, tiny_mce_helper is also able to
87
+ validate the options that are passed in to initialize TinyMCE. Since these
88
+ options can change over time, a configuration file is generated based on
89
+ information from the TinyMCE wiki. This configuration file contains a list of
90
+ all of the current possible options that can be specified.
91
+
92
+ For example, to create a new configuration file:
93
+
94
+ $ rake tiny_mce:update VERSION=3.2.2
95
+ (in /my/project)
96
+ Downloading configuration options from TinyMCE Wiki...
97
+ Done!
98
+
99
+ This will create config/tiny_mce_options.yml. The content of the configuration
100
+ should look similar to:
101
+
102
+ ---
103
+ - accessibility_focus
104
+ - accessibility_warnings
105
+ - add_form_submit_trigger
106
+ - add_unload_trigger
107
+ - apply_source_formatting
108
+ - ask
109
+ - auto_focus
110
+ - auto_reset_designmode
111
+ - auto_resize
112
+ - browsers
113
+ - button_tile_map
114
+ - cleanup
115
+ - cleanup_callback
116
+ - cleanup_on_startup
117
+ - cleanup_serializer
118
+ ...
119
+
120
+ === Uninstalling TinyMCE
121
+
122
+ Uninstalling TinyMCE will remove the javascript source and the configuration
123
+ options. To invoke the installation, run the rake task tiny_mce:uninstall.
124
+
125
+ $ rake tiny_mce:uninstall
126
+ (in /my/project)
127
+
128
+ === Creating TinyMCE script
129
+
130
+ To create the TinyMCE initialization script:
131
+
132
+ application.rhtml:
133
+ <%=
134
+ tiny_mce_init_script(
135
+ :theme => 'advanced',
136
+ :editor_selector => 'rich_text',
137
+ :content_css => '/stylesheets/tiny_mce_content.css',
138
+ :editor_css => '/stylesheets/tiny_mce_editor.css',
139
+ :auto_reset_designmode => true
140
+ )
141
+ %>
142
+
143
+ will generate the following javascript:
144
+
145
+ tinyMCE.init({
146
+ 'mode' : 'textareas',
147
+ 'theme' : 'advanced',
148
+ 'editor_selected' : 'rich_text',
149
+ 'content_css' : '/stylesheets/tiny_mce_content.css'
150
+ });
151
+
152
+ To see additional initialization helpers, see the API for TinyMCEHelper
153
+
154
+ == Testing
155
+
156
+ Before you can run any tests, the following gem must be installed:
157
+ * plugin_test_helper[http://github.com/pluginaweek/plugin_test_helper]
158
+
159
+ To run against a specific version of Rails:
160
+
161
+ rake test RAILS_FRAMEWORK_ROOT=/path/to/rails
162
+
163
+ Since the rake tasks for installing TinyMCE and updating the configuration
164
+ options are part of the unit tests, already-downloaded files are included with
165
+ the plugin. If you want to perform a "live" test which actually downloads the
166
+ files off the Internet (rather than using the local versions), you must set
167
+ the LIVE environment variable to true. For example,
168
+
169
+ rake test LIVE=true
170
+
171
+ == Dependencies
172
+
173
+ * Rails 2.0 or later
174
+ * TinyMCE 3.2.2 or later
175
+ * hpricot[http://code.whytheluckystiff.net/hpricot]
176
+ * rubyzip[http://rubyzip.sourceforge.net]
177
+
178
+ == References
179
+
180
+ This plugin provides for the installation and utilization of TinyMCE in Ruby on
181
+ Rails applications. TinyMCE is a WYSIWYG HTML editing component released under
182
+ the GNU Public License (GPL) by Moxiecode Systems (http://tinymce.moxiecode.com/).
183
+
184
+ This plugin was originally created by by Blake Watters <blake@near-time.com> and
185
+ later modified by Aaron Pfeifer.
@@ -0,0 +1,96 @@
1
+ require 'rake/testtask'
2
+ require 'rake/rdoctask'
3
+ require 'rake/gempackagetask'
4
+ require 'rake/contrib/sshpublisher'
5
+
6
+ spec = Gem::Specification.new do |s|
7
+ s.name = 'tiny_mce_helper'
8
+ s.version = '0.3.0'
9
+ s.platform = Gem::Platform::RUBY
10
+ s.summary = 'Adds helper methods for creating the TinyMCE initialization script in Rails'
11
+ s.description = s.summary
12
+
13
+ s.files = FileList['{lib,tasks,test}/**/*'] + %w(CHANGELOG.rdoc init.rb install.rb Rakefile README.rdoc uninstall.rb) - FileList['test/app_root/{log,log/*,script,script/*}']
14
+ s.require_path = 'lib'
15
+ s.has_rdoc = true
16
+ s.test_files = Dir['test/**/*_test.rb']
17
+
18
+ s.author = 'Aaron Pfeifer'
19
+ s.email = 'aaron@pluginaweek.org'
20
+ s.homepage = 'http://www.pluginaweek.org'
21
+ s.rubyforge_project = 'pluginaweek'
22
+ end
23
+
24
+ desc 'Default: run all tests.'
25
+ task :default => :test
26
+
27
+ desc "Test the #{spec.name} plugin."
28
+ Rake::TestTask.new(:test) do |t|
29
+ t.libs << 'lib'
30
+ t.test_files = spec.test_files
31
+ t.verbose = true
32
+ end
33
+
34
+ begin
35
+ require 'rcov/rcovtask'
36
+ namespace :test do
37
+ desc "Test the #{spec.name} plugin with Rcov."
38
+ Rcov::RcovTask.new(:rcov) do |t|
39
+ t.libs << 'lib'
40
+ t.test_files = spec.test_files
41
+ t.rcov_opts << '--exclude="^(?!lib/)"'
42
+ t.verbose = true
43
+ end
44
+ end
45
+ rescue LoadError
46
+ end
47
+
48
+ desc "Generate documentation for the #{spec.name} plugin."
49
+ Rake::RDocTask.new(:rdoc) do |rdoc|
50
+ rdoc.rdoc_dir = 'rdoc'
51
+ rdoc.title = spec.name
52
+ rdoc.template = '../rdoc_template.rb'
53
+ rdoc.options << '--line-numbers' << '--inline-source'
54
+ rdoc.rdoc_files.include('README.rdoc', 'CHANGELOG.rdoc', 'lib/**/*.rb')
55
+ end
56
+
57
+ desc 'Generate a gemspec file.'
58
+ task :gemspec do
59
+ File.open("#{spec.name}.gemspec", 'w') do |f|
60
+ f.write spec.to_ruby
61
+ end
62
+ end
63
+
64
+ Rake::GemPackageTask.new(spec) do |p|
65
+ p.gem_spec = spec
66
+ p.need_tar = true
67
+ p.need_zip = true
68
+ end
69
+
70
+ desc 'Publish the beta gem.'
71
+ task :pgem => [:package] do
72
+ Rake::SshFilePublisher.new('aaron@pluginaweek.org', '/home/aaron/gems.pluginaweek.org/public/gems', 'pkg', "#{spec.name}-#{spec.version}.gem").upload
73
+ end
74
+
75
+ desc 'Publish the API documentation.'
76
+ task :pdoc => [:rdoc] do
77
+ Rake::SshDirPublisher.new('aaron@pluginaweek.org', "/home/aaron/api.pluginaweek.org/public/#{spec.name}", 'rdoc').upload
78
+ end
79
+
80
+ desc 'Publish the API docs and gem'
81
+ task :publish => [:pgem, :pdoc, :release]
82
+
83
+ desc 'Publish the release files to RubyForge.'
84
+ task :release => [:gem, :package] do
85
+ require 'rubyforge'
86
+
87
+ ruby_forge = RubyForge.new.configure
88
+ ruby_forge.login
89
+
90
+ %w(gem tgz zip).each do |ext|
91
+ file = "pkg/#{spec.name}-#{spec.version}.#{ext}"
92
+ puts "Releasing #{File.basename(file)}..."
93
+
94
+ ruby_forge.add_release(spec.rubyforge_project, spec.name, spec.version, file)
95
+ end
96
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'tiny_mce_helper'
@@ -0,0 +1,7 @@
1
+ # Install TinyMCE
2
+ puts 'Installing TinyMCE...'
3
+ TinyMCEHelper.install(:version => ENV['VERSION'], :target => ENV['TARGET'])
4
+
5
+ # Update the configuration options
6
+ puts 'Updating TinyMCE configuration options...'
7
+ TinyMCEHelper.update_options
@@ -0,0 +1,262 @@
1
+ # Adds helper methods for generating the TinyMCE initialization script
2
+ # within your views
3
+ module TinyMCEHelper
4
+ # The path to the file which contains all valid options that can be used
5
+ # to configure TinyMCE
6
+ OPTIONS_FILE_PATH = "#{Rails.root}/config/tiny_mce_options.yml"
7
+
8
+ # A regular expression matching options that are dynamic (i.e. they can
9
+ # vary on an integer or string basis)
10
+ DYNAMIC_OPTIONS = /theme_advanced_buttons|theme_advanced_container/
11
+
12
+ # Whether or not to use verbose output
13
+ mattr_accessor :verbose
14
+ @@verbose = true
15
+
16
+ # A list of all valid options that can be used to configure TinyMCE
17
+ mattr_accessor :valid_options
18
+ @@valid_options = File.exists?(OPTIONS_FILE_PATH) ? File.open(OPTIONS_FILE_PATH) {|f| YAML.load(f.read)} : []
19
+
20
+ class << self
21
+ # Installs TinyMCE by downloading it and adding it to your application's
22
+ # javascripts folder.
23
+ #
24
+ # Configuration options:
25
+ # * <tt>:version</tt> - The version of TinyMCE to install. Default is the
26
+ # latest version.
27
+ # * <tt>:target</tt> - The path to install TinyMCE to, relative to the
28
+ # project root. Default is "public/javascripts/tiny_mce"
29
+ # * <tt>:force</tt> - Whether to install TinyMCE, regardless of whether it
30
+ # already exists on the filesystem.
31
+ #
32
+ # == Versions
33
+ #
34
+ # By default, this will install the latest version of TinyMCE. You can
35
+ # install a specific version of TinyMCE (if you are using an old API) by
36
+ # passing in the version number.
37
+ #
38
+ # For example,
39
+ # TinyMCEHelper.install # Installs the latest version
40
+ # TinyMCEHelper.install(:version => '3.2.2') # Installs version 3.2.2
41
+ #
42
+ # An exception will be raised if the specified version cannot be found.
43
+ #
44
+ # == Target path
45
+ #
46
+ # By default, this will install TinyMCE into Rails.root/public/javascripts/tiny_mce.
47
+ # If you want to install it to a different directory, you can pass in a
48
+ # parameter with the relative path from Rails.root.
49
+ #
50
+ # For example,
51
+ # TinyMCEHelper.install(:target => 'public/javascripts/richtext')
52
+ #
53
+ # == Conflicting paths
54
+ #
55
+ # If TinyMCE is found to already be installed on the filesystem, a prompt
56
+ # will be displayed for whether to overwrite the existing directory. This
57
+ # prompt can be automatically skipped by passing in the :force option.
58
+ def install(options = {})
59
+ options.assert_valid_keys(:version, :target, :force)
60
+ options.reverse_merge!(:force => false)
61
+
62
+ version = options[:version]
63
+ base_target = options[:target] || 'public/javascripts/tiny_mce'
64
+ source_path = 'tinymce'
65
+ target_path = File.expand_path(File.join(Rails.root, base_target))
66
+
67
+ # If TinyMCE is already installed, make sure the user wants to continue
68
+ if !options[:force] && File.exists?(target_path)
69
+ print "TinyMCE already installed in #{target_path}. Overwrite? (y/n): "
70
+ while !%w(y n).include?(option = STDIN.gets.chop)
71
+ print "Invalid option. Overwrite #{target_path}? (y/n): "
72
+ end
73
+
74
+ return if option == 'n'
75
+ end
76
+
77
+ require 'open-uri'
78
+
79
+ # Get the latest TinyMCE version
80
+ unless version
81
+ require 'hpricot'
82
+
83
+ puts 'Finding latest version of TinyMCE to download...' if verbose
84
+ doc = Hpricot(open('http://sourceforge.net/project/showfiles.php?group_id=103281&package_id=111430'))
85
+ file_element = (doc/'tr[@id^="pkg0_1rel0_"] a').detect {|file| file.innerHTML.to_s =~ /tinymce_([\d_]+).zip$/}
86
+ raise ArgumentError, 'Could not find latest TinyMCE version' if !file_element
87
+
88
+ version = $1. gsub('_', '.')
89
+ end
90
+
91
+ file_url = "http://prdownloads.sourceforge.net/tinymce/tinymce_#{version.gsub('.', '_')}.zip?download"
92
+
93
+ # Download the file
94
+ puts "Downloading TinyMCE-#{version} source from #{file_url}..." if verbose
95
+ open(file_url) do |file|
96
+ file_path = file.path
97
+
98
+ # Extract and install
99
+ puts "Extracting source from #{file_path}..." if verbose
100
+
101
+ require 'zip/zip'
102
+ require 'zip/zipfilesystem'
103
+
104
+ Zip::ZipFile.open(file_path) do |zipfile|
105
+ zipfile.entries.each do |entry|
106
+ if match = /jscripts\/tiny_mce\/(.*)/.match(entry.name)
107
+ FileUtils.mkdir_p("#{target_path}/#{File.dirname(match[1])}")
108
+ entry.extract("#{target_path}/#{match[1]}") { true }
109
+ end
110
+ end
111
+ end
112
+
113
+ puts 'Done!' if verbose
114
+ end
115
+ end
116
+
117
+ # Uninstalls the TinyMCE installation and optional configuration file
118
+ #
119
+ # Configuration options:
120
+ # * +target+ - The path that TinyMCE was installed to. Default is "public/javascripts/tiny_mce"
121
+ def uninstall(options = {})
122
+ # Remove the TinyMCE configuration file
123
+ File.delete(OPTIONS_FILE_PATH)
124
+
125
+ # Remove the TinyMCE installation
126
+ FileUtils.rm_rf(options[:target] || "#{Rails.root}/public/javascripts/tiny_mce")
127
+ end
128
+
129
+ # Updates the list of possible configuration options that can be used
130
+ # when initializing the TinyMCE script. These are always installed to
131
+ # the application folder, config/tiny_mce_options.yml. If this file
132
+ # does not exist, then the TinyMCE helper will not be able to verify
133
+ # that all of the initialization options are valid.
134
+ def update_options
135
+ require 'hpricot'
136
+ require 'open-uri'
137
+ require 'yaml'
138
+
139
+ puts 'Downloading configuration options from TinyMCE Wiki...' if verbose
140
+ doc = Hpricot(open('http://wiki.moxiecode.com/index.php/TinyMCE:Configuration'))
141
+ options = (doc/'a[@title*="Configuration/"]/').collect {|option| option.to_s}.sort
142
+ options.reject! {|option| option =~ DYNAMIC_OPTIONS}
143
+
144
+ File.open(OPTIONS_FILE_PATH, 'w') do |out|
145
+ YAML.dump(options, out)
146
+ end
147
+ puts 'Done!' if verbose
148
+ end
149
+ end
150
+
151
+ # Is TinyMCE being used?
152
+ def using_tiny_mce?
153
+ @uses_tiny_mce
154
+ end
155
+
156
+ # Create the TinyMCE initialization scripts. The default configuration
157
+ # is for a simple theme that replaces all textareas on the page. For
158
+ # example, the default initialization script will generate the following:
159
+ #
160
+ # tinyMCE.init({
161
+ # 'mode' : 'textareas',
162
+ # 'theme' : 'simple'
163
+ # });
164
+ #
165
+ # == Customizing initialization options
166
+ #
167
+ # To customize the options to be included in the initialization script,
168
+ # you can pass in a hash to +tiny_mce_init_script+. For example,
169
+ #
170
+ # tiny_mce_init_script(
171
+ # :theme => 'advanced',
172
+ # :editor_selector => 'rich_text',
173
+ # :content_css => '/stylesheets/tiny_mce_content.css',
174
+ # :editor_css => '/stylesheets/tiny_mce_editor.css',
175
+ # :auto_reset_designmode => true
176
+ # )
177
+ #
178
+ # will generate:
179
+ #
180
+ # tinyMCE.init({
181
+ # 'mode' : 'textareas',
182
+ # 'theme' : 'advanced',
183
+ # 'editor_selected' : 'rich_text',
184
+ # 'content_css' : '/stylesheets/tiny_mce_content.css'
185
+ # });
186
+ #
187
+ # == Validating options
188
+ #
189
+ # If additional options are passed in to initialize TinyMCE, they will be
190
+ # validated against the list of valid options in TinyMCEHelper#valid_options.
191
+ # These options are configured in the file config/tiny_mce_options.yml.
192
+ # You can generate this file by invoke the rake task tiny_mce:update_options.
193
+ def tiny_mce_init_script(options = @tiny_mce_options)
194
+ options ||= {}
195
+ options.stringify_keys!.reverse_merge!(
196
+ 'mode' => 'textareas',
197
+ 'theme' => 'simple'
198
+ )
199
+
200
+ # Check validity
201
+ plugins = options['plugins']
202
+ options_to_validate = options.reject {|option, value| plugins && plugins.include?(option.split('_')[0]) || option =~ DYNAMIC_OPTIONS}
203
+ options_to_validate.assert_valid_keys(@@valid_options) if @@valid_options && @@valid_options.any?
204
+
205
+ init_script = 'tinyMCE.init({'
206
+
207
+ options.sort.each do |key, value|
208
+ init_script += "\n#{key} : "
209
+
210
+ case value
211
+ when String, Symbol, Fixnum
212
+ init_script << "'#{value}'"
213
+ when Array
214
+ init_script << "'#{value.join(',')}'"
215
+ when TrueClass
216
+ init_script << 'true'
217
+ when FalseClass
218
+ init_script << 'false'
219
+ else
220
+ raise ArgumentError, "Cannot parse value of type #{value.class} passed for TinyMCE option #{key}"
221
+ end
222
+
223
+ init_script << ','
224
+ end
225
+
226
+ init_script.chop << "\n});"
227
+ end
228
+
229
+ # Generate the TinyMCE. Any arguments will be passed to tiny_mce_init_script.
230
+ def tiny_mce(*args)
231
+ javascript_tag tiny_mce_init_script(*args)
232
+ end
233
+
234
+ # The name of the TinyMCE javascript file to use. In development, this
235
+ # will use the source (uncompressed) file in order to help with debugging
236
+ # issues that occur within TinyMCE. In production, the compressed version
237
+ # of TinyMCE will be used in order to increased download speed.
238
+ def tiny_mce_file_name
239
+ Rails.env == 'development' ? 'tiny_mce/tiny_mce_src' : 'tiny_mce/tiny_mce'
240
+ end
241
+
242
+ # Generates the javascript include for TinyMCE. For example,
243
+ #
244
+ # javascript_include_tiny_mce
245
+ #
246
+ # will generate:
247
+ #
248
+ # <script type="text/javascript" src="/javascripts/tiny_mce/tiny_mce.js"></script>
249
+ def javascript_include_tiny_mce
250
+ javascript_include_tag tiny_mce_file_name
251
+ end
252
+
253
+ # Conditionally includes the TinyMCE javascript file if the variable
254
+ # @uses_tiny_mce has been set to true.
255
+ def javascript_include_tiny_mce_if_used
256
+ javascript_include_tiny_mce if using_tiny_mce?
257
+ end
258
+ end
259
+
260
+ ActionController::Base.class_eval do
261
+ helper TinyMCEHelper
262
+ end