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.util.HashSet;
23 import java.util.Set;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
26
27 import com.vladsch.flexmark.ext.wikilink.internal.WikiLinkLinkResolver;
28 import com.vladsch.flexmark.html.IndependentLinkResolverFactory;
29 import com.vladsch.flexmark.html.LinkResolver;
30 import com.vladsch.flexmark.html.LinkResolverFactory;
31 import com.vladsch.flexmark.html.renderer.LinkResolverContext;
32 import com.vladsch.flexmark.html.renderer.LinkStatus;
33 import com.vladsch.flexmark.html.renderer.LinkType;
34 import com.vladsch.flexmark.html.renderer.ResolvedLink;
35 import com.vladsch.flexmark.util.ast.Node;
36
37 /**
38 * The FlexmarkDoxiaLinkResolver rewrites the md, markdown links to html.
39 *
40 * Sample links it rewrites:
41 * - doc.md to doc.html
42 * - doc.markdown to doc.html
43 * - doc.md#anchor to doc.html#anchor
44 * - doc.markdown#anchor to doc.html#anchor
45 * - ../doc.markdown#anchor to ../doc.html#anchor
46 * - :doc.md to :doc.html
47 * - :doc.markdown to :doc.html
48 *
49 * Sample links it leaves untouched:
50 * - http://doc.md
51 * - https://doc.markdown
52 * - doc.md.badformat
53 * - doc.md#bad#format
54 * - doc.md#bad.format
55 */
56 public class FlexmarkDoxiaLinkResolver implements LinkResolver
57 {
58 final Pattern pattern;
59
60 /**
61 * <p>Constructor for FlexmarkDoxiaLinkResolver.</p>
62 *
63 * @param context a {@link com.vladsch.flexmark.html.renderer.LinkResolverContext} object.
64 */
65 public FlexmarkDoxiaLinkResolver( LinkResolverContext context )
66 {
67 this.pattern = Pattern.compile(
68 "^(?![^:]+:)((?:\\./)?(?:\\.\\./)*[^\\.]+).(?:"
69 + MarkdownParserModule.FILE_EXTENSION
70 + "|"
71 + MarkdownParserModule.ALTERNATE_FILE_EXTENSION
72 + ")(#[^#\\.]*){0,1}$"
73 );
74 }
75
76 /** {@inheritDoc} */
77 @Override
78 public ResolvedLink resolveLink( Node node, LinkResolverContext context, ResolvedLink link )
79 {
80 if ( link.getLinkType() == LinkType.LINK )
81 {
82 Matcher matcher = this.pattern.matcher( link.getUrl() );
83 if ( matcher.matches() )
84 {
85 return link.withStatus( LinkStatus.VALID ).withUrl( matcher.replaceAll( "$1.html$2" ) );
86 }
87 }
88
89 return link;
90 }
91
92 /**
93 * Factory that creates FlexmarkDoxiaLinkResolver objects.
94 */
95 public static class Factory extends IndependentLinkResolverFactory
96 {
97 @Override
98 public Set<Class<? extends LinkResolverFactory>> getBeforeDependents()
99 {
100 Set<Class<? extends LinkResolverFactory>> set = new HashSet<>();
101 set.add( WikiLinkLinkResolver.Factory.class );
102 return set;
103 }
104
105 @Override
106 public LinkResolver create( LinkResolverContext context )
107 {
108 return new FlexmarkDoxiaLinkResolver( context );
109 }
110 }
111 }