| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package com.google.gitiles.doc; |
| 16 | |
| Dave Borowitz | 3b744b1 | 2016-08-19 16:11:10 -0400 | [diff] [blame] | 17 | import java.util.regex.Matcher; |
| 18 | import java.util.regex.Pattern; |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 19 | import org.commonmark.Extension; |
| 20 | import org.commonmark.node.Node; |
| 21 | import org.commonmark.node.Text; |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 22 | import org.commonmark.parser.Parser; |
| 23 | import org.commonmark.parser.Parser.ParserExtension; |
| Shawn Pearce | c77a18b | 2016-07-26 10:18:37 -0700 | [diff] [blame] | 24 | import org.commonmark.parser.delimiter.DelimiterProcessor; |
| 25 | import org.commonmark.parser.delimiter.DelimiterRun; |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 26 | |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 27 | /** Parses <code>{#foo}</code> into {@link NamedAnchor}. */ |
| 28 | public 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 Pearce | c77a18b | 2016-07-26 10:18:37 -0700 | [diff] [blame] | 44 | public char getOpeningCharacter() { |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 45 | return '{'; |
| 46 | } |
| 47 | |
| 48 | @Override |
| Shawn Pearce | c77a18b | 2016-07-26 10:18:37 -0700 | [diff] [blame] | 49 | public char getClosingCharacter() { |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 50 | return '}'; |
| 51 | } |
| 52 | |
| 53 | @Override |
| Shawn Pearce | c77a18b | 2016-07-26 10:18:37 -0700 | [diff] [blame] | 54 | public int getMinLength() { |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 55 | return 1; |
| 56 | } |
| 57 | |
| 58 | @Override |
| Shawn Pearce | c77a18b | 2016-07-26 10:18:37 -0700 | [diff] [blame] | 59 | public int getDelimiterUse(DelimiterRun opener, DelimiterRun closer) { |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 60 | 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 | } |