blob: e6a3071626ec7ca43191b4af9b4e0f9097ea894b [file] [log] [blame]
Shawn Pearce12c8fab2016-05-15 16:55:21 -07001// Copyright (C) 2016 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package com.google.gitiles.doc;
16
Dave Borowitz3b744b12016-08-19 16:11:10 -040017import java.util.regex.Matcher;
18import java.util.regex.Pattern;
Shawn Pearce12c8fab2016-05-15 16:55:21 -070019import org.commonmark.Extension;
20import org.commonmark.node.Node;
21import org.commonmark.node.Text;
Shawn Pearce12c8fab2016-05-15 16:55:21 -070022import org.commonmark.parser.Parser;
23import org.commonmark.parser.Parser.ParserExtension;
Shawn Pearcec77a18b2016-07-26 10:18:37 -070024import org.commonmark.parser.delimiter.DelimiterProcessor;
25import org.commonmark.parser.delimiter.DelimiterRun;
Shawn Pearce12c8fab2016-05-15 16:55:21 -070026
Shawn Pearce12c8fab2016-05-15 16:55:21 -070027/** Parses <code>{#foo}</code> into {@link NamedAnchor}. */
28public class NamedAnchorExtension implements ParserExtension {
29 public static Extension create() {
30 return new NamedAnchorExtension();
31 }
32
33 private NamedAnchorExtension() {}
34
35 @Override
36 public void extend(Parser.Builder builder) {
37 builder.customDelimiterProcessor(new Processor());
38 }
39
40 private static class Processor implements DelimiterProcessor {
41 private static final Pattern ID = Pattern.compile("#([^\\s}]+)");
42
43 @Override
Shawn Pearcec77a18b2016-07-26 10:18:37 -070044 public char getOpeningCharacter() {
Shawn Pearce12c8fab2016-05-15 16:55:21 -070045 return '{';
46 }
47
48 @Override
Shawn Pearcec77a18b2016-07-26 10:18:37 -070049 public char getClosingCharacter() {
Shawn Pearce12c8fab2016-05-15 16:55:21 -070050 return '}';
51 }
52
53 @Override
Shawn Pearcec77a18b2016-07-26 10:18:37 -070054 public int getMinLength() {
Shawn Pearce12c8fab2016-05-15 16:55:21 -070055 return 1;
56 }
57
58 @Override
Shawn Pearcec77a18b2016-07-26 10:18:37 -070059 public int getDelimiterUse(DelimiterRun opener, DelimiterRun closer) {
Shawn Pearce12c8fab2016-05-15 16:55:21 -070060 return 1;
61 }
62
63 @Override
64 public void process(Text opener, Text closer, int delimiterUse) {
65 Node content = opener.getNext();
66 if (content instanceof Text && content.getNext() == closer) {
67 Matcher m = ID.matcher(((Text) content).getLiteral());
68 if (m.matches()) {
69 content.unlink();
70
71 NamedAnchor anchor = new NamedAnchor();
72 anchor.setName(m.group(1));
73 opener.insertAfter(anchor);
74 MarkdownUtil.trimPreviousWhitespace(opener);
75 return;
76 }
77 }
78
79 // If its not exactly one well formed Text node; restore the delimiter text.
80 opener.insertAfter(new Text("{"));
81 closer.insertBefore(new Text("}"));
82 }
83 }
84}