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