|
| 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 | +} |
0 commit comments