Skip to content

Commit 5d309b6

Browse files
committed
Added Textile support
1 parent 75c8542 commit 5d309b6

14 files changed

Lines changed: 3543 additions & 1 deletion

File tree

‎transform-markup/download-gems.sh‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ mkdir -p $BASE/install
66
mkdir -p $BASE/resources/ruby
77

88
# install gems into install directory
9-
jruby -S gem install maruku sass -i $BASE/install --no-rdoc --no-ri
9+
jruby -S gem install maruku sass RedCloth -i $BASE/install --no-rdoc --no-ri
1010

1111
# copy required files to resource directory
1212
rsync -a --include '/lib/***' --exclude '*' $BASE/install/gems/maruku-*/ $BASE/resources/ruby/maruku
1313
rsync -a --include '/lib/***' --include '/VERSION*' --exclude '*' $BASE/install/gems/sass-*/ $BASE/resources/ruby/sass
14+
rsync -a --include '/lib/***' --exclude '*' $BASE/install/gems/RedCloth-*/ $BASE/resources/ruby/redcloth
1415

1516
echo "Done!"
1617
echo "You can now copy the contents of $BASE/resources to src/main/resources"
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.ocpsoft.rewrite.transform.markup;
17+
18+
import java.util.Arrays;
19+
20+
import org.jruby.embed.ScriptingContainer;
21+
import org.ocpsoft.rewrite.transform.StringTransformer;
22+
23+
public class Textile extends StringTransformer {
24+
25+
private final boolean completeDocument;
26+
27+
public Textile() {
28+
this(true);
29+
}
30+
31+
public Textile(boolean completeDocument) {
32+
this.completeDocument = completeDocument;
33+
}
34+
35+
@Override
36+
public String transform(String input) {
37+
38+
// the script for rendering
39+
StringBuilder script = new StringBuilder();
40+
script.append("require 'redcloth'\n");
41+
script.append("RedCloth.new('");
42+
script.append(input);
43+
script.append("').to_html\n");
44+
45+
// prepare the scripting environment
46+
ScriptingContainer container = new ScriptingContainer();
47+
container.setLoadPaths(Arrays.asList("ruby/redcloth/lib"));
48+
49+
// run the transformation and return the result
50+
Object fragment = container.runScriptlet(script.toString());
51+
if (fragment != null) {
52+
53+
// create complete HTML structure
54+
if (completeDocument) {
55+
StringBuilder result = new StringBuilder();
56+
result.append("<!DOCTYPE html>\n");
57+
result.append("<html>\n<body>\n");
58+
result.append(fragment.toString());
59+
result.append("</body>\n</html>\n");
60+
return result.toString();
61+
}
62+
63+
// output just the fragment
64+
else {
65+
return fragment.toString();
66+
}
67+
68+
}
69+
return null;
70+
71+
}
72+
73+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# A workaround to make Rails 2.1 gem dependency easier on case-sensitive filesystems.
2+
# Since the gem name is RedCloth and the file is redcloth.rb, config.gem 'RedCloth' doesn't
3+
# work. You'd have to use config.gem 'RedCloth', :lib => 'redcloth', and that's not
4+
# immediately obvious. This file remedies that.
5+
#
6+
require File.join(File.dirname(__FILE__), '..', 'redcloth')
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# If this is a frozen gem in Rails 2.1 and RedCloth 3.x was already
2+
# loaded by Rails' ActionView::Helpers::TextHelper, the user will get
3+
# "redcloth_scan.bundle: Class is not a module (TypeError)"
4+
# This hack is to work around that Rails loading problem. The problem
5+
# appears to be fixed in Edge Rails [51e4106].
6+
Object.send(:remove_const, :RedCloth) if Object.const_defined?(:RedCloth) && RedCloth.is_a?(Class)
7+
8+
require 'rbconfig'
9+
begin
10+
conf = Object.const_get(defined?(RbConfig) ? :RbConfig : :Config)::CONFIG
11+
prefix = conf['arch'] =~ /mswin|mingw/ ? "#{conf['MAJOR']}.#{conf['MINOR']}/" : ''
12+
lib = "#{prefix}redcloth_scan"
13+
require lib
14+
rescue LoadError => e
15+
e.message << %{\nCouldn't load #{lib}\nThe $LOAD_PATH was:\n#{$LOAD_PATH.join("\n")}}
16+
raise e
17+
end
18+
19+
require 'redcloth/version'
20+
require 'redcloth/textile_doc'
21+
require 'redcloth/formatters/base'
22+
require 'redcloth/formatters/html'
23+
require 'redcloth/formatters/latex'
24+
25+
module RedCloth
26+
27+
# A convenience method for creating a new TextileDoc. See
28+
# RedCloth::TextileDoc.
29+
def self.new( *args, &block )
30+
RedCloth::TextileDoc.new( *args, &block )
31+
end
32+
33+
# Include extension modules (if any) in TextileDoc.
34+
def self.include(*args)
35+
RedCloth::TextileDoc.send(:include, *args)
36+
end
37+
38+
end
39+
40+
begin
41+
require 'erb'
42+
require 'redcloth/erb_extension'
43+
include ERB::Util
44+
rescue LoadError
45+
end
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class ERB
2+
module Util
3+
4+
#
5+
# A utility method for transforming Textile in _s_ to HTML.
6+
#
7+
# require "erb"
8+
# include ERB::Util
9+
#
10+
# puts textilize("Isn't ERB *great*?")
11+
#
12+
# _Generates_
13+
#
14+
# <p>Isn&#8217;t <span class="caps">ERB</span> <strong>great</strong>?</p>
15+
#
16+
def textilize( s )
17+
if s && s.respond_to?(:to_s)
18+
RedCloth.new( s.to_s ).to_html
19+
end
20+
end
21+
22+
alias t textilize
23+
module_function :t
24+
module_function :textilize
25+
26+
end
27+
end
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
module RedCloth::Formatters
2+
module Base
3+
4+
def ignore(opts)
5+
opts[:text]
6+
end
7+
alias_method :notextile, :ignore
8+
9+
def redcloth_version(opts)
10+
p(:text => "#{opts[:prefix]}#{RedCloth::VERSION}")
11+
end
12+
13+
def inline_redcloth_version(opts)
14+
RedCloth::VERSION::STRING
15+
end
16+
17+
[:del_phrase, :sup_phrase, :sub_phrase, :span_phrase].each do |phrase_method|
18+
method = phrase_method.to_s.split('_')[0]
19+
define_method(phrase_method) do |opts|
20+
"#{opts[:beginning_space]}#{self.send(method, opts)}"
21+
end
22+
end
23+
24+
private
25+
26+
def pba(opts)
27+
opts.delete(:style) if filter_styles
28+
opts.delete(:class) if filter_classes
29+
opts.delete(:id) if filter_ids
30+
31+
atts = ''
32+
opts[:"text-align"] = opts.delete(:align)
33+
opts[:style] += ';' if opts[:style] && (opts[:style][-1..-1] != ';')
34+
[:float, :"text-align", :"vertical-align"].each do |a|
35+
opts[:style] = "#{a}:#{opts[a]};#{opts[:style]}" if opts[a]
36+
end
37+
[:"padding-right", :"padding-left"].each do |a|
38+
opts[:style] = "#{a}:#{opts[a]}em;#{opts[:style]}" if opts[a]
39+
end
40+
[:style, :class, :lang, :id, :colspan, :rowspan, :title, :start, :align].each do |a|
41+
atts << " #{a}=\"#{ html_esc(opts[a].to_s, :html_escape_attributes) }\"" if opts[a]
42+
end
43+
atts
44+
end
45+
46+
def method_missing(method, opts)
47+
opts[:text] || ""
48+
end
49+
50+
def before_transform(text)
51+
52+
end
53+
54+
def after_transform(text)
55+
56+
end
57+
58+
def formatter_methods
59+
singleton_methods.map! {|method| method.to_sym }
60+
end
61+
62+
end
63+
end

0 commit comments

Comments
 (0)