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 | |
parent | 0aa289a5926819abf28961be3d2f1e772648e9fb (diff) | |
download | sonarqube-431b684334256c215251a6c460e3bc6120f38423.tar.gz sonarqube-431b684334256c215251a6c460e3bc6120f38423.zip |
SONAR-3269 Add 'lang' attribute on <pre> for code blocks
-rw-r--r-- | sonar-markdown/src/main/java/org/sonar/markdown/HtmlMultilineCodeChannel.java | 33 | ||||
-rw-r--r-- | sonar-markdown/src/test/java/org/sonar/markdown/MarkdownTest.java | 10 |
2 files changed, 37 insertions, 6 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>"); } } diff --git a/sonar-markdown/src/test/java/org/sonar/markdown/MarkdownTest.java b/sonar-markdown/src/test/java/org/sonar/markdown/MarkdownTest.java index 982afa58f26..ab629a9717b 100644 --- a/sonar-markdown/src/test/java/org/sonar/markdown/MarkdownTest.java +++ b/sonar-markdown/src/test/java/org/sonar/markdown/MarkdownTest.java @@ -52,11 +52,19 @@ public class MarkdownTest { @Test public void shouldDecorateMultipleLineCode() { - assertThat(Markdown.convertToHtml("This is a ``line of code\nOn multiple lines``"), is("This is a <pre><code>line of code\nOn multiple lines</code></pre>")); + assertThat(Markdown.convertToHtml("This is a ``\nline of code\nOn multiple lines\n``"), is("This is a <pre><code>line of code\nOn multiple lines</code></pre>")); + assertThat(Markdown.convertToHtml("This is not a ``line of code\nOn multiple lines``"), is("This is not a ``line of code<br/>On multiple lines``")); assertThat(Markdown.convertToHtml("This is not a ``line of code\nOn multiple lines"), is("This is not a ``line of code<br/>On multiple lines")); } @Test + public void shouldDecorateMultipleLineCodeWithLanguageSpecified() { + assertThat(Markdown.convertToHtml("This is a ``java\nline of code\nOn multiple lines\n``"), is("This is a <pre lang=\"java\"><code>line of code\nOn multiple lines</code></pre>")); + assertThat(Markdown.convertToHtml("This is not a ``java line of code\nOn multiple lines``"), is("This is not a ``java line of code<br/>On multiple lines``")); + assertThat(Markdown.convertToHtml("This is not a ``java \nline of code\nOn multiple lines``"), is("This is not a ``java <br/>line of code<br/>On multiple lines``")); + } + + @Test public void shouldEmphaseText() { assertThat(Markdown.convertToHtml("This is *important*"), is("This is <em>important</em>")); assertThat(Markdown.convertToHtml("This should not be * \n emphase"), is("This should not be * <br/> emphase")); |