From 431b684334256c215251a6c460e3bc6120f38423 Mon Sep 17 00:00:00 2001 From: Fabrice Bellingard Date: Fri, 17 Feb 2012 14:41:41 +0100 Subject: SONAR-3269 Add 'lang' attribute on
 for code blocks

---
 .../sonar/markdown/HtmlMultilineCodeChannel.java   | 33 ++++++++++++++++++----
 1 file changed, 28 insertions(+), 5 deletions(-)

(limited to 'sonar-markdown/src/main/java')

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 
} tag.
  * 
  * E.g., the input:
- * ``This code
- *   spans on 2 lines`` 
+ * 
+ * ``
+ * This code
+ * spans on 2 lines
+ * ``
+ * 
* will produce: * {@literal
}{@literal}This code
  * spans on 2 lines{@literal}{@literal
} */ class HtmlMultilineCodeChannel extends RegexChannel { + 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("
");
-    output.append(token.subSequence(2, token.length() - 2));
+    regexpMatcher.reset(token);
+    regexpMatcher.matches();
+    output.append("");
+    output.append(regexpMatcher.group(2));
     output.append("
"); } } -- cgit v1.2.3