diff options
author | Jean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com> | 2014-05-07 18:21:00 +0200 |
---|---|---|
committer | Jean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com> | 2014-05-07 18:21:07 +0200 |
commit | 9eae24f08d44a07ea8159b1ceb29f16fcb0ae361 (patch) | |
tree | 66bddb4c1b1c3284f80c09fcf15eb1cfc50479c7 /sonar-markdown | |
parent | 06cd07f0685459b11c9478aa729bea4a8ddbdf5f (diff) | |
download | sonarqube-9eae24f08d44a07ea8159b1ceb29f16fcb0ae361.tar.gz sonarqube-9eae24f08d44a07ea8159b1ceb29f16fcb0ae361.zip |
SONAR-4681 Add support for headings in SQ Markdown
Diffstat (limited to 'sonar-markdown')
3 files changed, 85 insertions, 0 deletions
diff --git a/sonar-markdown/src/main/java/org/sonar/markdown/HtmlHeadingChannel.java b/sonar-markdown/src/main/java/org/sonar/markdown/HtmlHeadingChannel.java new file mode 100644 index 00000000000..e2dc0047b0c --- /dev/null +++ b/sonar-markdown/src/main/java/org/sonar/markdown/HtmlHeadingChannel.java @@ -0,0 +1,77 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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 this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.markdown; + +import org.sonar.channel.RegexChannel; + +/** + * Headings are triggered by equal signs at the beginning of a line. The depth of the heading is determined by the number + * of equal signs (up to 6). + * + * E.g., the input: + * <pre> + * = Level 1 + * == Level 2 + * === Level 3 + * ==== Level 4 + * ===== Level 5 + * ====== Level 6 + * </pre> + * will produce: + * <pre> + * {@literal<h1>}Level 1{@literal</h1>} + * {@literal<h2>}Level 2{@literal</h2>} + * {@literal<h3>}Level 3{@literal</h3>} + * {@literal<h4>}Level 4{@literal</h4>} + * {@literal<h5>}Level 5{@literal</h5>} + * {@literal<h6>}Level 6{@literal</h6>} + * </pre> + * @since 4.4 + * + */ +public class HtmlHeadingChannel extends RegexChannel<MarkdownOutput> { + + private static final int MAX_HEADING_DEPTH = 6; + + public HtmlHeadingChannel() { + super("\\s*=+\\s[^\r\n]*+[\r\n]*"); + } + + @Override + protected void consume(CharSequence token, MarkdownOutput output) { + int index = 0; + int headingLevel = 0; + while(index < token.length() && Character.isWhitespace(token.charAt(index))) { + index ++; + } + while(index < token.length() && index <= MAX_HEADING_DEPTH && token.charAt(index) == '=') { + index ++; + headingLevel ++; + } + while(index < token.length() && Character.isWhitespace(token.charAt(index))) { + index ++; + } + CharSequence headingText = token.subSequence(index, token.length()); + + output.append("<h" + headingLevel + ">"); + output.append(headingText); + output.append("</h" + headingLevel + ">"); + } +} diff --git a/sonar-markdown/src/main/java/org/sonar/markdown/Markdown.java b/sonar-markdown/src/main/java/org/sonar/markdown/Markdown.java index f1ea0a3a4e7..79f3c939d84 100644 --- a/sonar-markdown/src/main/java/org/sonar/markdown/Markdown.java +++ b/sonar-markdown/src/main/java/org/sonar/markdown/Markdown.java @@ -35,6 +35,7 @@ public final class Markdown { .addChannel(new HtmlEndOfLineChannel()) .addChannel(new HtmlEmphasisChannel()) .addChannel(new HtmlListChannel()) + .addChannel(new HtmlHeadingChannel()) .addChannel(new HtmlCodeChannel()) .addChannel(new HtmlMultilineCodeChannel()) .addChannel(new IdentifierAndNumberChannel()) 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 13b0099976a..d2e78c4d4d0 100644 --- a/sonar-markdown/src/test/java/org/sonar/markdown/MarkdownTest.java +++ b/sonar-markdown/src/test/java/org/sonar/markdown/MarkdownTest.java @@ -53,6 +53,13 @@ public class MarkdownTest { } @Test + public void shouldDecorateHeadings() { + assertThat(Markdown.convertToHtml(" = Top\r== Sub\r\n=== Subsub\n ==== \n 1.five")) + .isEqualTo("<h1>Top\r</h1><h2>Sub\r\n</h2><h3>Subsub\n</h3><h4></h4> 1.five"); + } + + + @Test public void shouldDecorateMixedOrderedAndUnorderedList() { assertThat(Markdown.convertToHtml(" 1. one\r* two\r\n1. three\n * \n 1.five")) .isEqualTo("<ol><li>one</li>\r</ol><ul><li>two</li>\r\n</ul><ol><li>three</li>\n</ol><ul><li> </li>\n</ul> 1.five"); |