Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
MarkdownParser |
|
| 3.0;3 |
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.sink.Sink; | |
29 | ||
30 | import org.codehaus.plexus.util.IOUtil; | |
31 | ||
32 | import org.pegdown.Extensions; | |
33 | import org.pegdown.PegDownProcessor; | |
34 | import org.pegdown.ast.RootNode; | |
35 | ||
36 | /** | |
37 | * Implementation of {@link org.apache.maven.doxia.parser.Parser} for Markdown documents. | |
38 | * <p/> | |
39 | * Defers parsing to the <a href="http://pegdown.org">PegDown library</a>. | |
40 | * | |
41 | * @author Julien Nicoulaud <julien.nicoulaud@gmail.com> | |
42 | * @plexus.component role="org.apache.maven.doxia.parser.Parser" role-hint="markdown" | |
43 | * @since 1.3 | |
44 | */ | |
45 | 20 | public class MarkdownParser |
46 | extends XhtmlParser | |
47 | { | |
48 | ||
49 | /** | |
50 | * The role hint for the {@link MarkdownParser} Plexus component. | |
51 | */ | |
52 | public static final String ROLE_HINT = "markdown"; | |
53 | ||
54 | /** | |
55 | * The {@link PegDownProcessor} used to convert Pegdown documents to HTML. | |
56 | */ | |
57 | 2 | protected static final PegDownProcessor PEGDOWN_PROCESSOR = |
58 | new PegDownProcessor( Extensions.ALL & ~Extensions.HARDWRAPS ); | |
59 | ||
60 | /** | |
61 | * {@inheritDoc} | |
62 | */ | |
63 | @Override | |
64 | public void parse( Reader source, Sink sink ) | |
65 | throws ParseException | |
66 | { | |
67 | try | |
68 | { | |
69 | 20 | RootNode rootNode = PEGDOWN_PROCESSOR.parseMarkdown( IOUtil.toString( source ).toCharArray() ); |
70 | 20 | String markdownAsHtml = new MarkdownToDoxiaHtmlSerializer().toHtml( rootNode ); |
71 | 20 | super.parse( new StringReader( "<html><body>" + markdownAsHtml + "</body></html>" ), sink ); |
72 | } | |
73 | 0 | catch ( IOException e ) |
74 | { | |
75 | 0 | throw new ParseException( "Failed reading Markdown source document", e ); |
76 | 20 | } |
77 | 20 | } |
78 | } |