Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EncodePreTagsPlugin #97

Merged
merged 3 commits into from
Aug 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. The ASF licenses this file to You
* under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License. For additional information regarding
* copyright in this work, please see the NOTICE file in the top level
* directory of this distribution.
*/

package org.apache.roller.weblogger.business.plugins.entry;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.roller.weblogger.WebloggerException;
import org.apache.roller.weblogger.pojos.Weblog;
import org.apache.roller.weblogger.pojos.WeblogEntry;


import static java.util.regex.Pattern.*;

/**
* Encodes angle brackets inside pre tags (code tags which follow right after pre are not encoded).
*/
public class EncodePreTagsPlugin implements WeblogEntryPlugin {

private static final String LT = "&lt;"; // '<'

private static final Pattern PRE_PATTERN = Pattern.compile(
"<pre\\s*[^>]*>" + "(.*?)" + "</pre\\s*>", MULTILINE | DOTALL | CASE_INSENSITIVE);

private static final Pattern CODE_PATTERN = Pattern.compile(
"<code\\s*.[^>]*>" + "(.*?)" + "</code\\s*>", MULTILINE | DOTALL | CASE_INSENSITIVE);

@Override
public String getName() {
return "Pre Tag Encoder";
}

@Override
public String getDescription() {
return "Encodes angle brackets inside pre tags, code tags are kept unaltered.";
}

@Override
public void init(Weblog weblog) throws WebloggerException {}

@Override
public String render(WeblogEntry entry, String str) {

StringBuilder result = new StringBuilder(str.length()+32);

Matcher pre_matcher = PRE_PATTERN.matcher(str);

while(pre_matcher.find()) {

String pre_full = pre_matcher.group(0);
String pre_inner = pre_matcher.group(1);

Matcher code_matcher = CODE_PATTERN.matcher(pre_inner);

if (code_matcher.find()) {
String code_inner = code_matcher.group(1);
pre_matcher.appendReplacement(result, pre_full.replace(code_inner, encode(code_inner)));
} else {
pre_matcher.appendReplacement(result, pre_full.replace(pre_inner, encode(pre_inner)));
}

}
pre_matcher.appendTail(result);

return result.toString();
}

// we only have to encode the opening angle bracket for valid html/xhtml
private static String encode(String code_inner) {
return Matcher.quoteReplacement(code_inner.replace("<", LT)); // matchers hate $ and \
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,8 @@ guice.backend.module=org.apache.roller.weblogger.business.jpa.JPAWebloggerModule
plugins.page=\
org.apache.roller.weblogger.business.plugins.entry.ConvertLineBreaksPlugin \
,org.apache.roller.weblogger.business.plugins.entry.ObfuscateEmailPlugin \
,org.apache.roller.weblogger.business.plugins.entry.SmileysPlugin
,org.apache.roller.weblogger.business.plugins.entry.SmileysPlugin\
,org.apache.roller.weblogger.business.plugins.entry.EncodePreTagsPlugin


# The list of configured WeblogEntryEditors available to users
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. The ASF licenses this file to You
* under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License. For additional information regarding
* copyright in this work, please see the NOTICE file in the top level
* directory of this distribution.
*/

package org.apache.roller.weblogger.business.plugins.entry;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

/**
* @author mbien
*/
public class EncodePreTagsPluginTest {

@Test
public void passthrough() {

EncodePreTagsPlugin instance = new EncodePreTagsPlugin();

String input = "Stay a while and listen.";
assertEquals(input, instance.render(null, input));

input = "<!DOCTYPE html>\n" +
"<html>\n" +
" <head>\n" +
" <title>Hi!</title>\n" +
" </head>\n" +
" <body>\n" +
" <p>Hello There</p>\n" +
" </body>\n" +
"</html>";
assertEquals(input, instance.render(null, input));

input = "<!DOCTYPE html>\n" +
"<html>\n" +
" <head>\n" +
" <title>Hi!</title>\n" +
" </head>\n" +
" <body>\n" +
" <pre>Hello There</pre>\n" + // pre
" <code>Hello There</code>\n" + // code
" </body>\n" +
"</html>";
assertEquals(input, instance.render(null, input));

}

@Test
public void substitution1() {

EncodePreTagsPlugin instance = new EncodePreTagsPlugin();

String input = "<pre><></pre>";
String expected = "<pre>&lt;></pre>";

assertEquals(expected, instance.render(null, input));

}

@Test
public void substitution2() {

EncodePreTagsPlugin instance = new EncodePreTagsPlugin();

String input = "\n<!DOCTYPE html>\n" +
"<html>\n" +
" <head>\n" +
" <title>Hi!</title>\n" +
" </head>\n" +
" <body>\n" +
" <pre><code class='language-java'>private final Map<String, List<?>> map = new HashMap<>();</code></pre>\n"+
" <pre><code class='language-bash'>$JDK/bin/java -version</code></pre>\n"+
" <pre><code class='language-slash'>\\\\</code></pre>\n"+
" </body>\n" +
"</html>";

String expected = "\n<!DOCTYPE html>\n" +
"<html>\n" +
" <head>\n" +
" <title>Hi!</title>\n" +
" </head>\n" +
" <body>\n" +
" <pre><code class='language-java'>private final Map&lt;String, List&lt;?>> map = new HashMap&lt;>();</code></pre>\n"+
" <pre><code class='language-bash'>$JDK/bin/java -version</code></pre>\n"+
" <pre><code class='language-slash'>\\\\</code></pre>\n"+
" </body>\n" +
"</html>";
assertEquals(expected, instance.render(null, input));

}

@Test
public void substitution3() {

EncodePreTagsPlugin instance = new EncodePreTagsPlugin();

String input = "<!DOCTYPE html>\n" +
"<html>\n" +
" <head>\n" +
" <title>Hi!</title>\n" +
" </head>\n" +
" <body>\n" +
" <h3>some java</h3>\n" +
" <pre>\n" +
" <code class='language-java'>\n"+
" private final Map<String, List<?>> map = new HashMap<>();\n"+
" </code>\n" +
" </pre>\n" +
" <h3>some xml</h3>\n" +
" <pre>\n" +
" <code class='language-xml'>\n"+
" <foo id = '5'>\n"+
" <bar>asdf</bar>\n"+
" </foo>\n"+
" </code>\n" +
" </pre>\n" +
" </body>\n" +
"</html>";

String expected = "<!DOCTYPE html>\n" +
"<html>\n" +
" <head>\n" +
" <title>Hi!</title>\n" +
" </head>\n" +
" <body>\n" +
" <h3>some java</h3>\n" +
" <pre>\n" +
" <code class='language-java'>\n"+
" private final Map&lt;String, List&lt;?>> map = new HashMap&lt;>();\n"+
" </code>\n" +
" </pre>\n" +
" <h3>some xml</h3>\n" +
" <pre>\n" +
" <code class='language-xml'>\n"+
" &lt;foo id = '5'>\n"+
" &lt;bar>asdf&lt;/bar>\n"+
" &lt;/foo>\n"+
" </code>\n" +
" </pre>\n" +
" </body>\n" +
"</html>";
assertEquals(expected, instance.render(null, input));

}

}