From 84159b5d85a0b99790ba8ea90e463076d541ed5c Mon Sep 17 00:00:00 2001 From: Freddy Mallet Date: Sat, 23 Apr 2011 00:46:05 +0200 Subject: SONAR-2384 First implementation : Create a markdown dedicated to Sonar needs --- .../java/org/sonar/markdown/BlackholeChannel.java | 32 ++++++++++++++ .../org/sonar/markdown/HtmlEmphasisChannel.java | 34 +++++++++++++++ .../org/sonar/markdown/HtmlEndOfLineChannel.java | 34 +++++++++++++++ .../java/org/sonar/markdown/HtmlUrlChannel.java | 34 +++++++++++++++ .../sonar/markdown/IdentifierAndNumberChannel.java | 34 +++++++++++++++ .../java/org/sonar/markdown/MarkdownEngine.java | 50 ++++++++++++++++++++++ .../java/org/sonar/markdown/MarkdownOutput.java | 37 ++++++++++++++++ 7 files changed, 255 insertions(+) create mode 100644 sonar-markdown/src/main/java/org/sonar/markdown/BlackholeChannel.java create mode 100644 sonar-markdown/src/main/java/org/sonar/markdown/HtmlEmphasisChannel.java create mode 100644 sonar-markdown/src/main/java/org/sonar/markdown/HtmlEndOfLineChannel.java create mode 100644 sonar-markdown/src/main/java/org/sonar/markdown/HtmlUrlChannel.java create mode 100644 sonar-markdown/src/main/java/org/sonar/markdown/IdentifierAndNumberChannel.java create mode 100644 sonar-markdown/src/main/java/org/sonar/markdown/MarkdownEngine.java create mode 100644 sonar-markdown/src/main/java/org/sonar/markdown/MarkdownOutput.java (limited to 'sonar-markdown/src/main') diff --git a/sonar-markdown/src/main/java/org/sonar/markdown/BlackholeChannel.java b/sonar-markdown/src/main/java/org/sonar/markdown/BlackholeChannel.java new file mode 100644 index 00000000000..e872e3e977a --- /dev/null +++ b/sonar-markdown/src/main/java/org/sonar/markdown/BlackholeChannel.java @@ -0,0 +1,32 @@ +/* + * 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.Channel; +import org.sonar.channel.CodeReader; + +class BlackholeChannel extends Channel { + + @Override + public boolean consume(CodeReader code, MarkdownOutput output) { + output.append((char)code.pop()); + return true; + } +} diff --git a/sonar-markdown/src/main/java/org/sonar/markdown/HtmlEmphasisChannel.java b/sonar-markdown/src/main/java/org/sonar/markdown/HtmlEmphasisChannel.java new file mode 100644 index 00000000000..146797ed7e4 --- /dev/null +++ b/sonar-markdown/src/main/java/org/sonar/markdown/HtmlEmphasisChannel.java @@ -0,0 +1,34 @@ +/* + * 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 HtmlEmphasisChannel extends RegexChannel { + + public HtmlEmphasisChannel() { + super("\\*[^\n\r]*\\*"); + } + + @Override + protected void consume(CharSequence token, MarkdownOutput output) { + output.append("" + token.subSequence(1, token.length() - 1) + ""); + } +} diff --git a/sonar-markdown/src/main/java/org/sonar/markdown/HtmlEndOfLineChannel.java b/sonar-markdown/src/main/java/org/sonar/markdown/HtmlEndOfLineChannel.java new file mode 100644 index 00000000000..d7ed7afcdbd --- /dev/null +++ b/sonar-markdown/src/main/java/org/sonar/markdown/HtmlEndOfLineChannel.java @@ -0,0 +1,34 @@ +/* + * 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 HtmlEndOfLineChannel extends RegexChannel { + + public HtmlEndOfLineChannel() { + super("(\r?\n)|(\r)"); + } + + @Override + protected void consume(CharSequence token, MarkdownOutput output) { + output.append("
"); + } +} diff --git a/sonar-markdown/src/main/java/org/sonar/markdown/HtmlUrlChannel.java b/sonar-markdown/src/main/java/org/sonar/markdown/HtmlUrlChannel.java new file mode 100644 index 00000000000..6bfa51d8508 --- /dev/null +++ b/sonar-markdown/src/main/java/org/sonar/markdown/HtmlUrlChannel.java @@ -0,0 +1,34 @@ +/* + * 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 HtmlUrlChannel extends RegexChannel { + + public HtmlUrlChannel() { + super("https?://[\\w\\d:#@%/;$()~_?\\+-=\\.&]*"); + } + + @Override + protected void consume(CharSequence token, MarkdownOutput output) { + output.append("" + token + ""); + } +} diff --git a/sonar-markdown/src/main/java/org/sonar/markdown/IdentifierAndNumberChannel.java b/sonar-markdown/src/main/java/org/sonar/markdown/IdentifierAndNumberChannel.java new file mode 100644 index 00000000000..cb94f4b205e --- /dev/null +++ b/sonar-markdown/src/main/java/org/sonar/markdown/IdentifierAndNumberChannel.java @@ -0,0 +1,34 @@ +/* + * 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 IdentifierAndNumberChannel extends RegexChannel { + + public IdentifierAndNumberChannel() { + super("[\\p{Alpha}\\d]++"); + } + + @Override + protected void consume(CharSequence token, MarkdownOutput output) { + output.append(token); + } +} diff --git a/sonar-markdown/src/main/java/org/sonar/markdown/MarkdownEngine.java b/sonar-markdown/src/main/java/org/sonar/markdown/MarkdownEngine.java new file mode 100644 index 00000000000..fb51e9c4b2f --- /dev/null +++ b/sonar-markdown/src/main/java/org/sonar/markdown/MarkdownEngine.java @@ -0,0 +1,50 @@ +/* + * 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 java.util.ArrayList; +import java.util.List; + +import org.sonar.channel.Channel; +import org.sonar.channel.ChannelDispatcher; +import org.sonar.channel.CodeReader; + +public class MarkdownEngine { + + private MarkdownOutput output; + private ChannelDispatcher dispatcher; + + private MarkdownEngine() { + output = new MarkdownOutput(); + List markdownChannels = new ArrayList(); + markdownChannels.add(new HtmlUrlChannel()); + markdownChannels.add(new HtmlEndOfLineChannel()); + markdownChannels.add(new HtmlEmphasisChannel()); + markdownChannels.add(new IdentifierAndNumberChannel()); + markdownChannels.add(new BlackholeChannel()); + dispatcher = new ChannelDispatcher(markdownChannels); + } + + public static String convertToHtml(String input) { + MarkdownEngine engine = new MarkdownEngine(); + engine.dispatcher.consume(new CodeReader(input), engine.output); + return engine.output.toString(); + } +} diff --git a/sonar-markdown/src/main/java/org/sonar/markdown/MarkdownOutput.java b/sonar-markdown/src/main/java/org/sonar/markdown/MarkdownOutput.java new file mode 100644 index 00000000000..c4d59e460de --- /dev/null +++ b/sonar-markdown/src/main/java/org/sonar/markdown/MarkdownOutput.java @@ -0,0 +1,37 @@ +/* + * 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; + +class MarkdownOutput { + + private StringBuilder ouput = new StringBuilder(); + + public Appendable append(CharSequence charSequence) { + return ouput.append(charSequence); + } + + public Appendable append(char character) { + return ouput.append(character); + } + + public String toString() { + return ouput.toString(); + } +} -- cgit v1.2.3