From 3e265a43011c4bc6c9b8c5df4484fcef8ac7965b Mon Sep 17 00:00:00 2001 From: Freddy Mallet Date: Sat, 23 Apr 2011 15:33:33 +0200 Subject: [PATCH] SONAR-2384 Add a new HtmlCodeChannel to transform ''printf()'' into printf() --- .../org/sonar/markdown/HtmlCodeChannel.java | 36 +++++++++++++++++++ .../sonar/markdown/HtmlEmphasisChannel.java | 2 +- .../org/sonar/markdown/HtmlUrlChannel.java | 2 +- .../markdown/IdentifierAndNumberChannel.java | 2 +- .../org/sonar/markdown/MarkdownEngine.java | 1 + .../sonar/markdown/MarkdownEngineTest.java | 8 ++++- 6 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 sonar-markdown/src/main/java/org/sonar/markdown/HtmlCodeChannel.java diff --git a/sonar-markdown/src/main/java/org/sonar/markdown/HtmlCodeChannel.java b/sonar-markdown/src/main/java/org/sonar/markdown/HtmlCodeChannel.java new file mode 100644 index 00000000000..4fd5d3fd11c --- /dev/null +++ b/sonar-markdown/src/main/java/org/sonar/markdown/HtmlCodeChannel.java @@ -0,0 +1,36 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 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; + +class HtmlCodeChannel extends RegexChannel { + + public HtmlCodeChannel() { + super("''.+?''"); + } + + @Override + protected void consume(CharSequence token, MarkdownOutput output) { + output.append(""); + output.append(token.subSequence(2, token.length() - 2)); + output.append(""); + } +} diff --git a/sonar-markdown/src/main/java/org/sonar/markdown/HtmlEmphasisChannel.java b/sonar-markdown/src/main/java/org/sonar/markdown/HtmlEmphasisChannel.java index 48d42b1114b..ffaf0ac60e7 100644 --- a/sonar-markdown/src/main/java/org/sonar/markdown/HtmlEmphasisChannel.java +++ b/sonar-markdown/src/main/java/org/sonar/markdown/HtmlEmphasisChannel.java @@ -24,7 +24,7 @@ import org.sonar.channel.RegexChannel; class HtmlEmphasisChannel extends RegexChannel { public HtmlEmphasisChannel() { - super("\\*\\S[^\n\r\\*\\s]*\\*"); + super("\\*[^\n\r\\*\\s]++\\*"); } @Override diff --git a/sonar-markdown/src/main/java/org/sonar/markdown/HtmlUrlChannel.java b/sonar-markdown/src/main/java/org/sonar/markdown/HtmlUrlChannel.java index 6bfa51d8508..14933ca029a 100644 --- a/sonar-markdown/src/main/java/org/sonar/markdown/HtmlUrlChannel.java +++ b/sonar-markdown/src/main/java/org/sonar/markdown/HtmlUrlChannel.java @@ -24,7 +24,7 @@ import org.sonar.channel.RegexChannel; class HtmlUrlChannel extends RegexChannel { public HtmlUrlChannel() { - super("https?://[\\w\\d:#@%/;$()~_?\\+-=\\.&]*"); + super("https?://[\\w\\d:#@%/;$()~_?\\+-=\\.&]++"); } @Override diff --git a/sonar-markdown/src/main/java/org/sonar/markdown/IdentifierAndNumberChannel.java b/sonar-markdown/src/main/java/org/sonar/markdown/IdentifierAndNumberChannel.java index cb94f4b205e..74b8dffb3b3 100644 --- a/sonar-markdown/src/main/java/org/sonar/markdown/IdentifierAndNumberChannel.java +++ b/sonar-markdown/src/main/java/org/sonar/markdown/IdentifierAndNumberChannel.java @@ -24,7 +24,7 @@ import org.sonar.channel.RegexChannel; class IdentifierAndNumberChannel extends RegexChannel { public IdentifierAndNumberChannel() { - super("[\\p{Alpha}\\d]++"); + super("[\\w\\d]++"); } @Override diff --git a/sonar-markdown/src/main/java/org/sonar/markdown/MarkdownEngine.java b/sonar-markdown/src/main/java/org/sonar/markdown/MarkdownEngine.java index fb51e9c4b2f..6a7f84dd7d4 100644 --- a/sonar-markdown/src/main/java/org/sonar/markdown/MarkdownEngine.java +++ b/sonar-markdown/src/main/java/org/sonar/markdown/MarkdownEngine.java @@ -37,6 +37,7 @@ public class MarkdownEngine { markdownChannels.add(new HtmlUrlChannel()); markdownChannels.add(new HtmlEndOfLineChannel()); markdownChannels.add(new HtmlEmphasisChannel()); + markdownChannels.add(new HtmlCodeChannel()); markdownChannels.add(new IdentifierAndNumberChannel()); markdownChannels.add(new BlackholeChannel()); dispatcher = new ChannelDispatcher(markdownChannels); diff --git a/sonar-markdown/src/test/java/org/sonar/markdown/MarkdownEngineTest.java b/sonar-markdown/src/test/java/org/sonar/markdown/MarkdownEngineTest.java index f3669cd0181..4054e9a97f6 100644 --- a/sonar-markdown/src/test/java/org/sonar/markdown/MarkdownEngineTest.java +++ b/sonar-markdown/src/test/java/org/sonar/markdown/MarkdownEngineTest.java @@ -36,6 +36,12 @@ public class MarkdownEngineTest { assertThat(MarkdownEngine.convertToHtml("1\r2\r\n3\n"), is("1
2
3
")); } + @Test + public void shouldDecorateCode() { + assertThat(MarkdownEngine.convertToHtml("This is a ''line of code''"), is("This is a line of code")); + assertThat(MarkdownEngine.convertToHtml("This is not a ''line of code"), is("This is not a ''line of code")); + } + @Test public void shouldEmphaseText() { assertThat(MarkdownEngine.convertToHtml("This is *important*"), is("This is important")); @@ -47,7 +53,7 @@ public class MarkdownEngineTest { assertThat(MarkdownEngine.convertToHtml("emphase*inside*word"), is("emphaseinsideword")); // not supported yet - //assertThat(MarkdownEngine.convertToHtml("\\*surrounded by literal asterisks\\*"), is("\\*surrounded by literal asterisks\\*")); + // assertThat(MarkdownEngine.convertToHtml("\\*surrounded by literal asterisks\\*"), is("\\*surrounded by literal asterisks\\*")); } -- 2.39.5