View Javadoc

1   package org.apache.maven.doxia.module.markdown;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.IOException;
23  import java.io.Reader;
24  import java.io.StringReader;
25  
26  import org.apache.maven.doxia.module.xhtml.XhtmlParser;
27  import org.apache.maven.doxia.parser.ParseException;
28  import org.apache.maven.doxia.parser.Parser;
29  import org.apache.maven.doxia.sink.Sink;
30  
31  import org.codehaus.plexus.component.annotations.Component;
32  import org.codehaus.plexus.util.IOUtil;
33  
34  import org.pegdown.Extensions;
35  import org.pegdown.PegDownProcessor;
36  import org.pegdown.ast.RootNode;
37  
38  /**
39   * Implementation of {@link org.apache.maven.doxia.parser.Parser} for Markdown documents.
40   * <p/>
41   * Defers parsing to the <a href="http://pegdown.org">PegDown library</a>.
42   *
43   * @author Julien Nicoulaud <julien.nicoulaud@gmail.com>
44   * @since 1.3
45   */
46  @Component( role = Parser.class, hint = "markdown" )
47  public class MarkdownParser
48      extends XhtmlParser
49  {
50  
51      /**
52       * The role hint for the {@link MarkdownParser} Plexus component.
53       */
54      public static final String ROLE_HINT = "markdown";
55  
56      /**
57       * The {@link PegDownProcessor} used to convert Pegdown documents to HTML.
58       */
59      protected static final PegDownProcessor PEGDOWN_PROCESSOR =
60          new PegDownProcessor( Extensions.ALL & ~Extensions.HARDWRAPS );
61  
62      /**
63       * {@inheritDoc}
64       */
65      @Override
66      public void parse( Reader source, Sink sink )
67          throws ParseException
68      {
69          try
70          {
71              RootNode rootNode = PEGDOWN_PROCESSOR.parseMarkdown( IOUtil.toString( source ).toCharArray() );
72              String markdownAsHtml = new MarkdownToDoxiaHtmlSerializer().toHtml( rootNode );
73              super.parse( new StringReader( "<html><body>" + markdownAsHtml + "</body></html>" ), sink );
74          }
75          catch ( IOException e )
76          {
77              throw new ParseException( "Failed reading Markdown source document", e );
78          }
79      }
80  }