001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2011 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * Sonar is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * Sonar is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public
017     * License along with Sonar; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019     */
020    package org.sonar.markdown;
021    
022    import org.sonar.channel.Channel;
023    import org.sonar.channel.ChannelDispatcher;
024    import org.sonar.channel.CodeReader;
025    
026    import java.util.Arrays;
027    import java.util.List;
028    
029    /**
030     * Entry point of the Markdown library
031     */
032    public final class Markdown {
033    
034      private ChannelDispatcher<MarkdownOutput> dispatcher;
035    
036      private Markdown() {
037        List<Channel> markdownChannels = Arrays.<Channel>asList(
038            new HtmlUrlChannel(),
039            new HtmlEndOfLineChannel(),
040            new HtmlEmphasisChannel(),
041            new HtmlListChannel(),
042            new HtmlCodeChannel(),
043            new IdentifierAndNumberChannel(),
044            new BlackholeChannel());
045        dispatcher = new ChannelDispatcher<MarkdownOutput>(markdownChannels);
046      }
047    
048      private String convert(String input) {
049        CodeReader reader = new CodeReader(input);
050        try {
051          MarkdownOutput output = new MarkdownOutput();
052          dispatcher.consume(reader, output);
053          return output.toString();
054          
055        } finally {
056          reader.close();
057        }
058      }
059    
060      public static String convertToHtml(String input) {
061        return new Markdown().convert(input);
062      }
063    }