diff options
author | Fabrice Bellingard <bellingard@gmail.com> | 2012-02-17 14:41:41 +0100 |
---|---|---|
committer | Fabrice Bellingard <bellingard@gmail.com> | 2012-02-17 14:45:25 +0100 |
commit | 431b684334256c215251a6c460e3bc6120f38423 (patch) | |
tree | 99f9b01478d90f44c83631180effdf92281fd900 /sonar-markdown/src/main/java/org/sonar | |
parent | 0aa289a5926819abf28961be3d2f1e772648e9fb (diff) | |
download | sonarqube-431b684334256c215251a6c460e3bc6120f38423.tar.gz sonarqube-431b684334256c215251a6c460e3bc6120f38423.zip |
SONAR-3269 Add 'lang' attribute on <pre> for code blocks
Diffstat (limited to 'sonar-markdown/src/main/java/org/sonar')
-rw-r--r-- | sonar-markdown/src/main/java/org/sonar/markdown/HtmlMultilineCodeChannel.java | 33 |
1 files changed, 28 insertions, 5 deletions
diff --git a/sonar-markdown/src/main/java/org/sonar/markdown/HtmlMultilineCodeChannel.java b/sonar-markdown/src/main/java/org/sonar/markdown/HtmlMultilineCodeChannel.java index 1b032126031..f3a8b37d503 100644 --- a/sonar-markdown/src/main/java/org/sonar/markdown/HtmlMultilineCodeChannel.java +++ b/sonar-markdown/src/main/java/org/sonar/markdown/HtmlMultilineCodeChannel.java @@ -19,6 +19,9 @@ */ package org.sonar.markdown; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + import org.sonar.channel.RegexChannel; /** @@ -26,22 +29,42 @@ import org.sonar.channel.RegexChannel; * an HTML {@literal <pre><code>} tag. * * E.g., the input: - * ``This code - * spans on 2 lines`` + * <pre> + * `` + * This code + * spans on 2 lines + * `` + * </pre> * will produce: * {@literal<pre>}{@literal<code>}This code * spans on 2 lines{@literal</code>}{@literal</pre>} */ class HtmlMultilineCodeChannel extends RegexChannel<MarkdownOutput> { + private static final String NEWLINE = "(?:\\n\\r|\\r|\\n)"; + private static final String LANGUAGE = "([a-zA-Z][a-zA-Z0-9_]*+)?"; + private static final String DETECTION_REGEXP = "``" + LANGUAGE + NEWLINE + "([\\s\\S]+?)" + NEWLINE + "``"; + + private final Matcher regexpMatcher; + public HtmlMultilineCodeChannel() { - super("``[\\s\\S]+?``"); + super(DETECTION_REGEXP); + regexpMatcher = Pattern.compile(DETECTION_REGEXP).matcher(""); } @Override protected void consume(CharSequence token, MarkdownOutput output) { - output.append("<pre><code>"); - output.append(token.subSequence(2, token.length() - 2)); + regexpMatcher.reset(token); + regexpMatcher.matches(); + output.append("<pre"); + String language = regexpMatcher.group(1); + if (language != null) { + output.append(" lang=\""); + output.append(language); + output.append("\""); + } + output.append("><code>"); + output.append(regexpMatcher.group(2)); output.append("</code></pre>"); } } |