|
22 | 22 | import org.jruby.embed.ScriptingContainer; |
23 | 23 | import org.ocpsoft.rewrite.transform.StringTransformer; |
24 | 24 |
|
25 | | -public class Textile extends StringTransformer { |
26 | | - |
27 | | - private final boolean completeDocument; |
28 | | - |
29 | | - public Textile() { |
30 | | - this(true); |
31 | | - } |
32 | | - |
33 | | - public Textile(boolean completeDocument) { |
34 | | - this.completeDocument = completeDocument; |
35 | | - } |
36 | | - |
37 | | - @Override |
38 | | - public String transform(String input) { |
39 | | - |
40 | | - // the script for rendering |
41 | | - StringBuilder script = new StringBuilder(); |
42 | | - script.append("require 'redcloth'\n"); |
43 | | - script.append("RedCloth.new('"); |
44 | | - script.append(input); |
45 | | - script.append("').to_html\n"); |
46 | | - |
47 | | - ScriptingContainer container = null; |
48 | | - try { |
49 | | - |
50 | | - // prepare the scripting environment |
51 | | - container = new ScriptingContainer(LocalContextScope.SINGLETHREAD, LocalVariableBehavior.TRANSIENT); |
52 | | - container.setLoadPaths(Arrays.asList("ruby/redcloth/lib")); |
53 | | - |
54 | | - // run the transformation and return the result |
55 | | - Object fragment = container.runScriptlet(script.toString()); |
56 | | - if (fragment != null) { |
57 | | - |
58 | | - // create complete HTML structure |
59 | | - if (completeDocument) { |
60 | | - StringBuilder result = new StringBuilder(); |
61 | | - result.append("<!DOCTYPE html>\n"); |
62 | | - result.append("<html>\n<body>\n"); |
63 | | - result.append(fragment.toString()); |
64 | | - result.append("</body>\n</html>\n"); |
65 | | - return result.toString(); |
66 | | - } |
67 | | - |
68 | | - // output just the fragment |
69 | | - else { |
70 | | - return fragment.toString(); |
71 | | - } |
72 | | - |
| 25 | +public class Textile extends StringTransformer |
| 26 | +{ |
| 27 | + |
| 28 | + private final static String SCRIPT = "require 'redcloth'\n" + |
| 29 | + "RedCloth.new(input).to_html\n"; |
| 30 | + |
| 31 | + private final boolean completeDocument; |
| 32 | + |
| 33 | + public Textile() |
| 34 | + { |
| 35 | + this(true); |
| 36 | + } |
| 37 | + |
| 38 | + public Textile(boolean completeDocument) |
| 39 | + { |
| 40 | + this.completeDocument = completeDocument; |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public String transform(String input) |
| 45 | + { |
| 46 | + |
| 47 | + ScriptingContainer container = null; |
| 48 | + try { |
| 49 | + |
| 50 | + // prepare the scripting environment |
| 51 | + container = new ScriptingContainer(LocalContextScope.SINGLETHREAD, LocalVariableBehavior.TRANSIENT); |
| 52 | + container.setLoadPaths(Arrays.asList("ruby/redcloth/lib")); |
| 53 | + |
| 54 | + // run the transformation and return the result |
| 55 | + container.put("input", input); |
| 56 | + Object fragment = container.runScriptlet(SCRIPT); |
| 57 | + if (fragment != null) { |
| 58 | + |
| 59 | + // create complete HTML structure |
| 60 | + if (completeDocument) { |
| 61 | + StringBuilder result = new StringBuilder(); |
| 62 | + result.append("<!DOCTYPE html>\n"); |
| 63 | + result.append("<html>\n<body>\n"); |
| 64 | + result.append(fragment.toString()); |
| 65 | + result.append("</body>\n</html>\n"); |
| 66 | + return result.toString(); |
73 | 67 | } |
74 | | - return null; |
75 | 68 |
|
76 | | - } finally { |
77 | | - if (container != null) { |
78 | | - container.terminate(); |
| 69 | + // output just the fragment |
| 70 | + else { |
| 71 | + return fragment.toString(); |
79 | 72 | } |
80 | | - } |
81 | 73 |
|
82 | | - } |
| 74 | + } |
| 75 | + return null; |
| 76 | + |
| 77 | + } |
| 78 | + finally { |
| 79 | + if (container != null) { |
| 80 | + container.terminate(); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + } |
83 | 85 |
|
84 | 86 | } |
0 commit comments