]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3269 Add multiline code support on Sonar markdown
authorFabrice Bellingard <bellingard@gmail.com>
Thu, 16 Feb 2012 18:01:04 +0000 (19:01 +0100)
committerFabrice Bellingard <bellingard@gmail.com>
Thu, 16 Feb 2012 18:01:04 +0000 (19:01 +0100)
sonar-markdown/src/main/java/org/sonar/markdown/HtmlMultilineCodeChannel.java [new file with mode: 0644]
sonar-markdown/src/main/java/org/sonar/markdown/Markdown.java
sonar-markdown/src/test/java/org/sonar/markdown/MarkdownTest.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
new file mode 100644 (file)
index 0000000..1b03212
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.markdown;
+
+import org.sonar.channel.RegexChannel;
+
+/**
+ * Markdown treats double backtick quote (``) as indicators of code. Text wrapped with two `` and that spans on multiple lines will be wrapped with 
+ * an HTML {@literal <pre><code>} tag.
+ * 
+ * E.g., the input:
+ * ``This code
+ *   spans on 2 lines`` 
+ * will produce:
+ * {@literal<pre>}{@literal<code>}This code
+ * spans on 2 lines{@literal</code>}{@literal</pre>}
+ */
+class HtmlMultilineCodeChannel extends RegexChannel<MarkdownOutput> {
+
+  public HtmlMultilineCodeChannel() {
+    super("``[\\s\\S]+?``");
+  }
+
+  @Override
+  protected void consume(CharSequence token, MarkdownOutput output) {
+    output.append("<pre><code>");
+    output.append(token.subSequence(2, token.length() - 2));
+    output.append("</code></pre>");
+  }
+}
index e04060508d369ce7bc4b93a69683ec0cbf01d8cf..4e16fffb261f21ca0ce89864ad1f3b0ea9e59a92 100644 (file)
@@ -36,6 +36,7 @@ public final class Markdown {
       .addChannel(new HtmlEmphasisChannel())
       .addChannel(new HtmlListChannel())
       .addChannel(new HtmlCodeChannel())
+      .addChannel(new HtmlMultilineCodeChannel())
       .addChannel(new IdentifierAndNumberChannel())
       .addChannel(new BlackholeChannel())
       .build();
index 7ececab3ebccd582adc9a4559cf056d9c98ed71e..982afa58f26476bd0ad9c986470639b534ec15e0 100644 (file)
@@ -50,6 +50,12 @@ public class MarkdownTest {
     assertThat(Markdown.convertToHtml("This is not a ``line of code"), is("This is not a ``line of code"));
   }
 
+  @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 not a ``line of code\nOn multiple lines"), is("This is not a ``line of code<br/>On multiple lines"));
+  }
+
   @Test
   public void shouldEmphaseText() {
     assertThat(Markdown.convertToHtml("This is *important*"), is("This is <em>important</em>"));