tristandunn-acts_as_markup 1.3.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,75 @@
1
+ require 'active_support'
2
+
3
+ module ActsAsMarkup
4
+ # :stopdoc:
5
+ VERSION = '1.3.3'.freeze
6
+ LIBPATH = ::File.expand_path(::File.dirname(__FILE__)) + ::File::SEPARATOR
7
+ PATH = ::File.dirname(LIBPATH) + ::File::SEPARATOR
8
+ # :startdoc:
9
+
10
+ # This exception is raised when an unsupported markup language is supplied to acts_as_markup.
11
+ class UnsupportedMarkupLanguage < ArgumentError
12
+ end
13
+
14
+ # This exception is raised when an unsupported Markdown library is set to the config value.
15
+ class UnsportedMarkdownLibrary < ArgumentError
16
+ end
17
+
18
+ DEFAULT_MARKDOWN_LIB = :rdiscount
19
+
20
+ MARKDOWN_LIBS = { :rdiscount => {:class_name => "RDiscount",
21
+ :lib_name => "rdiscount"},
22
+ :bluecloth => {:class_name => "BlueCloth",
23
+ :lib_name => "bluecloth"},
24
+ :rpeg => {:class_name => "PEGMarkdown",
25
+ :lib_name => "peg_markdown"},
26
+ :maruku => {:class_name => "Maruku",
27
+ :lib_name => "maruku"} }
28
+
29
+ LIBRARY_EXTENSIONS = Set.new(Dir[ActsAsMarkup::LIBPATH + 'acts_as_markup/exts/*.rb'].map {|file| File.basename(file, '.rb')}).delete('string')
30
+
31
+ @@markdown_library = DEFAULT_MARKDOWN_LIB
32
+ mattr_accessor :markdown_library
33
+
34
+ # :stopdoc:
35
+ # Returns the version string for the library.
36
+ #
37
+ def self.version
38
+ VERSION
39
+ end
40
+
41
+ # Returns the library path for the module. If any arguments are given,
42
+ # they will be joined to the end of the library path using
43
+ # <tt>File.join</tt>.
44
+ #
45
+ def self.libpath( *args )
46
+ args.empty? ? LIBPATH : ::File.join(LIBPATH, *args)
47
+ end
48
+
49
+ # Returns the path for the module. If any arguments are given,
50
+ # they will be joined to the end of the path using
51
+ # <tt>File.join</tt>.
52
+ #
53
+ def self.path( *args )
54
+ args.empty? ? PATH : ::File.join(PATH, *args)
55
+ end
56
+
57
+ # Utility method used to rquire all files ending in .rb that lie in the
58
+ # directory below this file that has the same name as the filename passed
59
+ # in. Optionally, a specific _directory_ name can be passed in such that
60
+ # the _filename_ does not have to be equivalent to the directory.
61
+ #
62
+ def self.require_all_libs_relative_to( fname, dir = nil )
63
+ dir ||= ::File.basename(fname, '.*')
64
+ search_me = ::File.expand_path(
65
+ ::File.join(::File.dirname(fname), dir, '**', '*.rb'))
66
+
67
+ Dir.glob(search_me).sort.each {|rb| require rb}
68
+ end
69
+ # :startdoc:
70
+
71
+ end # module ActsAsMarkup
72
+
73
+ require 'acts_as_markup/exts/object'
74
+ require 'acts_as_markup/stringlike'
75
+ ActsAsMarkup.require_all_libs_relative_to __FILE__, 'acts'
@@ -0,0 +1,70 @@
1
+ require 'maruku'
2
+
3
+ class Maruku
4
+ include Stringlike
5
+
6
+ attr_reader :text
7
+
8
+ def initialize(s=nil, meta={})
9
+ super(nil)
10
+ self.attributes.merge! meta
11
+ if s
12
+ @text = s
13
+ parse_doc(s)
14
+ end
15
+ end
16
+
17
+ # Used to get the original Markdown text.
18
+ def to_s
19
+ @text
20
+ end
21
+
22
+ # used to be compatable with Rails/ActiveSupport
23
+ def blank?
24
+ @text.blank?
25
+ end
26
+
27
+ end
28
+
29
+ class String
30
+ alias_method :to_html, :to_s
31
+
32
+ def to_xml
33
+ REXML::Text.new(self)
34
+ end
35
+ end
36
+
37
+ module MaRuKu # :nodoc:
38
+ module Out # :nodoc:
39
+ module HTML
40
+
41
+ # We patch this method to play nicely with our own modifications of String.
42
+ #
43
+ # It originally used a +to_html+ method on String we've swapped this out for a +to_xml+
44
+ # method because we need +to_html+ to return the original text on plain text fields of
45
+ # the variable language option on +acts_as_markup+
46
+ def array_to_html(array)
47
+ elements = []
48
+ array.each do |item|
49
+ method = item.kind_of?(MDElement) ? "to_html_#{item.node_type}" : "to_xml"
50
+ unless item.respond_to?(method)
51
+ next
52
+ end
53
+
54
+ html_text = item.send(method)
55
+ if html_text.nil?
56
+ raise "Nil html created by method #{method}:\n#{html_text.inspect}\n for object #{item.inspect[0,300]}"
57
+ end
58
+
59
+ if html_text.kind_of?Array
60
+ elements = elements + html_text
61
+ else
62
+ elements << html_text
63
+ end
64
+ end
65
+ elements
66
+ end
67
+
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,5 @@
1
+ class Object
2
+ def to_html
3
+ self.to_s
4
+ end
5
+ end
@@ -0,0 +1,10 @@
1
+ require 'peg_markdown'
2
+
3
+ class PEGMarkdown
4
+ include Stringlike
5
+
6
+ # used to be compatable with Rails/ActiveSupport
7
+ def blank?
8
+ self.text.blank?
9
+ end
10
+ end
@@ -0,0 +1,15 @@
1
+ require 'rdiscount'
2
+
3
+ class RDiscount
4
+ include Stringlike
5
+
6
+ # Used to get the original Markdown text.
7
+ def to_s
8
+ self.text
9
+ end
10
+
11
+ # used to be compatable with Rails/ActiveSupport
12
+ def blank?
13
+ self.text.blank?
14
+ end
15
+ end
@@ -0,0 +1,100 @@
1
+ require 'rdoc/markup/simple_markup'
2
+ require 'rdoc/markup/simple_markup/to_html'
3
+
4
+ class RDocWithHyperlinkToHtml < SM::ToHtml
5
+
6
+ # Generate a hyperlink for url, labeled with text. Handle the
7
+ # special cases for img: and link: described under handle_special_HYPEDLINK
8
+ #
9
+ def gen_url(url, text)
10
+ if url =~ /([A-Za-z]+):(.*)/
11
+ type = $1
12
+ path = $2
13
+ else
14
+ type = "http"
15
+ path = url
16
+ url = "http://#{url}"
17
+ end
18
+
19
+ if type == "link"
20
+ if path[0,1] == '#' # is this meaningful?
21
+ url = path
22
+ else
23
+ url = HTMLGenerator.gen_url(@from_path, path)
24
+ end
25
+ end
26
+
27
+ if (type == "http" || type == "link") && url =~ /\.(gif|png|jpg|jpeg|bmp)$/
28
+ "<img src=\"#{url}\" />"
29
+ else
30
+ "<a href=\"#{url}\">#{text.sub(%r{^#{type}:/*}, '')}</a>"
31
+ end
32
+ end
33
+
34
+ # And we're invoked with a potential external hyperlink mailto:
35
+ # just gets inserted. http: links are checked to see if they
36
+ # reference an image. If so, that image gets inserted using an
37
+ # <img> tag. Otherwise a conventional <a href> is used. We also
38
+ # support a special type of hyperlink, link:, which is a reference
39
+ # to a local file whose path is relative to the --op directory.
40
+ #
41
+ def handle_special_HYPERLINK(special)
42
+ url = special.text
43
+ gen_url(url, url)
44
+ end
45
+
46
+ # Here's a hypedlink where the label is different to the URL
47
+ # <label>[url]
48
+ #
49
+ def handle_special_TIDYLINK(special)
50
+ text = special.text
51
+ unless text =~ /\{(.*?)\}\[(.*?)\]/ or text =~ /(\S+)\[(.*?)\]/
52
+ return text
53
+ end
54
+ label = $1
55
+ url = $2
56
+ gen_url(url, label)
57
+ end
58
+
59
+ end
60
+
61
+ # This allows a us to create a wrapper object similar to those provided by the
62
+ # Markdown and Textile libraries. It stores the original and formated HTML text
63
+ # in instance variables. It also stores the SimpleMarkup parser objects in
64
+ # instance variables.
65
+ #
66
+ class RDocText < String
67
+ attr_reader :text
68
+ attr_reader :html
69
+ attr_reader :markup
70
+ attr_reader :html_formater
71
+
72
+ def initialize(str)
73
+ super(str)
74
+ @text = str.to_s
75
+ @markup = SM::SimpleMarkup.new
76
+
77
+ # external hyperlinks
78
+ @markup.add_special(/((link:|https?:|mailto:|ftp:|www\.)\S+\w)/, :HYPERLINK)
79
+
80
+ # and links of the form <text>[<url>]
81
+ @markup.add_special(/(((\{.*?\})|\b\S+?)\[\S+?\.\S+?\])/, :TIDYLINK)
82
+
83
+ # Convert leading comment markers to spaces, but only
84
+ # if all non-blank lines have them
85
+
86
+ if str =~ /^(?>\s*)[^\#]/
87
+ content = str
88
+ else
89
+ content = str.gsub(/^\s*(#+)/) { $1.tr('#',' ') }
90
+ end
91
+
92
+ @html_formatter = RDocWithHyperlinkToHtml.new
93
+
94
+ @html = @markup.convert(@text, @html_formatter)
95
+ end
96
+
97
+ def to_html
98
+ @html
99
+ end
100
+ end
@@ -0,0 +1,18 @@
1
+ require 'action_view'
2
+
3
+ class SimpleFormat
4
+ include ActionView::Helpers::TagHelper
5
+ include ActionView::Helpers::TextHelper
6
+
7
+ def initialize(text)
8
+ @text = text
9
+ end
10
+
11
+ def to_s
12
+ @text
13
+ end
14
+
15
+ def to_html
16
+ simple_format @text
17
+ end
18
+ end
@@ -0,0 +1,20 @@
1
+ require 'wikitext'
2
+
3
+ # This allows a us to create a wrapper object similar to those provided by the
4
+ # Markdown and Textile libraries. It stores the original and formated HTML text
5
+ # in instance variables.
6
+ #
7
+ class WikitextString < String
8
+ attr_reader :text
9
+ attr_reader :html
10
+
11
+ def initialize(str, *options)
12
+ super(str)
13
+ @text = str.to_s
14
+ @html = Wikitext::Parser.new(*options).parse(@text)
15
+ end
16
+
17
+ def to_html
18
+ @html
19
+ end
20
+ end
@@ -0,0 +1,7 @@
1
+ # This mixin allows our markup objects (like RDiscount or RedCloth) to have
2
+ # all the normal string methods that are available.
3
+ module Stringlike
4
+ def method_missing(method, *params)
5
+ self.to_s.send(method, *params)
6
+ end
7
+ end
data/tasks/bones.rake ADDED
@@ -0,0 +1,21 @@
1
+ # $Id$
2
+
3
+ if HAVE_BONES
4
+
5
+ namespace :bones do
6
+
7
+ desc 'Show the PROJ open struct'
8
+ task :debug do |t|
9
+ atr = if t.application.top_level_tasks.length == 2
10
+ t.application.top_level_tasks.pop
11
+ end
12
+
13
+ if atr then Bones::Debug.show_attr(PROJ, atr)
14
+ else Bones::Debug.show PROJ end
15
+ end
16
+
17
+ end # namespace :bones
18
+
19
+ end # HAVE_BONES
20
+
21
+ # EOF
data/tasks/gem.rake ADDED
@@ -0,0 +1,201 @@
1
+
2
+ require 'find'
3
+ require 'rake/packagetask'
4
+ require 'rubygems/user_interaction'
5
+ require 'rubygems/builder'
6
+
7
+ module Bones
8
+ class GemPackageTask < Rake::PackageTask
9
+ # Ruby GEM spec containing the metadata for this package. The
10
+ # name, version and package_files are automatically determined
11
+ # from the GEM spec and don't need to be explicitly provided.
12
+ #
13
+ attr_accessor :gem_spec
14
+
15
+ # Tasks from the Bones gem directory
16
+ attr_reader :bones_files
17
+
18
+ # Create a GEM Package task library. Automatically define the gem
19
+ # if a block is given. If no block is supplied, then +define+
20
+ # needs to be called to define the task.
21
+ #
22
+ def initialize(gem_spec)
23
+ init(gem_spec)
24
+ yield self if block_given?
25
+ define if block_given?
26
+ end
27
+
28
+ # Initialization tasks without the "yield self" or define
29
+ # operations.
30
+ #
31
+ def init(gem)
32
+ super(gem.name, gem.version)
33
+ @gem_spec = gem
34
+ @package_files += gem_spec.files if gem_spec.files
35
+ @bones_files = []
36
+
37
+ local_setup = File.join(Dir.pwd, %w[tasks setup.rb])
38
+ if !test(?e, local_setup)
39
+ Dir.glob(::Bones.path(%w[lib bones tasks *])).each {|fn| bones_files << fn}
40
+ end
41
+ end
42
+
43
+ # Create the Rake tasks and actions specified by this
44
+ # GemPackageTask. (+define+ is automatically called if a block is
45
+ # given to +new+).
46
+ #
47
+ def define
48
+ super
49
+ task :prereqs
50
+ task :package => ['gem:prereqs', "#{package_dir_path}/#{gem_file}"]
51
+ file "#{package_dir_path}/#{gem_file}" => [package_dir_path] + package_files + bones_files do
52
+ when_writing("Creating GEM") {
53
+ chdir(package_dir_path) do
54
+ Gem::Builder.new(gem_spec).build
55
+ verbose(true) {
56
+ mv gem_file, "../#{gem_file}"
57
+ }
58
+ end
59
+ }
60
+ end
61
+
62
+ file package_dir_path => bones_files do
63
+ mkdir_p package_dir rescue nil
64
+
65
+ gem_spec.files = (gem_spec.files +
66
+ bones_files.map {|fn| File.join('tasks', File.basename(fn))}).sort
67
+
68
+ bones_files.each do |fn|
69
+ base_fn = File.join('tasks', File.basename(fn))
70
+ f = File.join(package_dir_path, base_fn)
71
+ fdir = File.dirname(f)
72
+ mkdir_p(fdir) if !File.exist?(fdir)
73
+ if File.directory?(fn)
74
+ mkdir_p(f)
75
+ else
76
+ raise "file name conflict for '#{base_fn}' (conflicts with '#{fn}')" if test(?e, f)
77
+ safe_ln(fn, f)
78
+ end
79
+ end
80
+ end
81
+ end
82
+
83
+ def gem_file
84
+ if @gem_spec.platform == Gem::Platform::RUBY
85
+ "#{package_name}.gem"
86
+ else
87
+ "#{package_name}-#{@gem_spec.platform}.gem"
88
+ end
89
+ end
90
+ end # class GemPackageTask
91
+ end # module Bones
92
+
93
+ namespace :gem do
94
+
95
+ PROJ.gem._spec = Gem::Specification.new do |s|
96
+ s.name = PROJ.name
97
+ s.version = PROJ.version
98
+ s.summary = PROJ.summary
99
+ s.authors = Array(PROJ.authors)
100
+ s.email = PROJ.email
101
+ s.homepage = Array(PROJ.url).first
102
+ s.rubyforge_project = PROJ.rubyforge.name
103
+
104
+ s.description = PROJ.description
105
+
106
+ PROJ.gem.dependencies.each do |dep|
107
+ s.add_dependency(*dep)
108
+ end
109
+
110
+ PROJ.gem.development_dependencies.each do |dep|
111
+ s.add_development_dependency(*dep)
112
+ end
113
+
114
+ s.files = PROJ.gem.files
115
+ s.executables = PROJ.gem.executables.map {|fn| File.basename(fn)}
116
+ s.extensions = PROJ.gem.files.grep %r/extconf\.rb$/
117
+
118
+ s.bindir = 'bin'
119
+ dirs = Dir["{#{PROJ.libs.join(',')}}"]
120
+ s.require_paths = dirs unless dirs.empty?
121
+
122
+ incl = Regexp.new(PROJ.rdoc.include.join('|'))
123
+ excl = PROJ.rdoc.exclude.dup.concat %w[\.rb$ ^(\.\/|\/)?ext]
124
+ excl = Regexp.new(excl.join('|'))
125
+ rdoc_files = PROJ.gem.files.find_all do |fn|
126
+ case fn
127
+ when excl; false
128
+ when incl; true
129
+ else false end
130
+ end
131
+ s.rdoc_options = PROJ.rdoc.opts + ['--main', PROJ.rdoc.main]
132
+ s.extra_rdoc_files = rdoc_files
133
+ s.has_rdoc = true
134
+
135
+ if test ?f, PROJ.test.file
136
+ s.test_file = PROJ.test.file
137
+ else
138
+ s.test_files = PROJ.test.files.to_a
139
+ end
140
+
141
+ # Do any extra stuff the user wants
142
+ PROJ.gem.extras.each do |msg, val|
143
+ case val
144
+ when Proc
145
+ val.call(s.send(msg))
146
+ else
147
+ s.send "#{msg}=", val
148
+ end
149
+ end
150
+ end # Gem::Specification.new
151
+
152
+ Bones::GemPackageTask.new(PROJ.gem._spec) do |pkg|
153
+ pkg.need_tar = PROJ.gem.need_tar
154
+ pkg.need_zip = PROJ.gem.need_zip
155
+ end
156
+
157
+ desc 'Show information about the gem'
158
+ task :debug => 'gem:prereqs' do
159
+ puts PROJ.gem._spec.to_ruby
160
+ end
161
+
162
+ desc 'Write the gemspec '
163
+ task :spec => 'gem:prereqs' do
164
+ File.open("#{PROJ.name}.gemspec", 'w') do |f|
165
+ f.write PROJ.gem._spec.to_ruby
166
+ end
167
+ end
168
+
169
+ desc 'Install the gem'
170
+ task :install => [:clobber, 'gem:package'] do
171
+ sh "#{SUDO} #{GEM} install --local pkg/#{PROJ.gem._spec.full_name}"
172
+
173
+ # use this version of the command for rubygems > 1.0.0
174
+ #sh "#{SUDO} #{GEM} install --no-update-sources pkg/#{PROJ.gem._spec.full_name}"
175
+ end
176
+
177
+ desc 'Uninstall the gem'
178
+ task :uninstall do
179
+ installed_list = Gem.source_index.find_name(PROJ.name)
180
+ if installed_list and installed_list.collect { |s| s.version.to_s}.include?(PROJ.version) then
181
+ sh "#{SUDO} #{GEM} uninstall --version '#{PROJ.version}' --ignore-dependencies --executables #{PROJ.name}"
182
+ end
183
+ end
184
+
185
+ desc 'Reinstall the gem'
186
+ task :reinstall => [:uninstall, :install]
187
+
188
+ desc 'Cleanup the gem'
189
+ task :cleanup do
190
+ sh "#{SUDO} #{GEM} cleanup #{PROJ.gem._spec.name}"
191
+ end
192
+ end # namespace :gem
193
+
194
+
195
+ desc 'Alias to gem:package'
196
+ task :gem => 'gem:package'
197
+
198
+ task :clobber => 'gem:clobber_package'
199
+ remove_desc_for_task 'gem:clobber_package'
200
+
201
+ # EOF