| 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 | |
| Shawn Pearce | c77a18b | 2016-07-26 10:18:37 -0700 | [diff] [blame] | 17 | import static com.google.gitiles.doc.SmartQuoted.Type.DOUBLE; |
| 18 | import static com.google.gitiles.doc.SmartQuoted.Type.SINGLE; |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 19 | |
| 20 | import com.google.gitiles.doc.SmartQuoted.Type; |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 21 | import org.commonmark.Extension; |
| 22 | import org.commonmark.node.Node; |
| 23 | import org.commonmark.node.Text; |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 24 | import org.commonmark.parser.Parser; |
| 25 | import org.commonmark.parser.Parser.ParserExtension; |
| Shawn Pearce | c77a18b | 2016-07-26 10:18:37 -0700 | [diff] [blame] | 26 | import org.commonmark.parser.delimiter.DelimiterProcessor; |
| 27 | import org.commonmark.parser.delimiter.DelimiterRun; |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 28 | |
| 29 | /** Uses smart quotes for ' and ". */ |
| 30 | public class SmartQuotedExtension implements ParserExtension { |
| 31 | public static Extension create() { |
| 32 | return new SmartQuotedExtension(); |
| 33 | } |
| 34 | |
| 35 | private SmartQuotedExtension() {} |
| 36 | |
| 37 | @Override |
| 38 | public void extend(Parser.Builder builder) { |
| 39 | builder.customDelimiterProcessor(new QuotedProcessor(SINGLE, '\'')); |
| 40 | builder.customDelimiterProcessor(new QuotedProcessor(DOUBLE, '"')); |
| 41 | } |
| 42 | |
| 43 | private static void quote(Type type, Text opener, Text closer) { |
| 44 | SmartQuoted quote = new SmartQuoted(); |
| 45 | quote.setType(type); |
| 46 | for (Node t = opener.getNext(); t != null && t != closer; ) { |
| 47 | Node next = t.getNext(); |
| 48 | quote.appendChild(t); |
| 49 | t = next; |
| 50 | } |
| 51 | opener.insertAfter(quote); |
| 52 | } |
| 53 | |
| 54 | /** Parses single and double quoted strings for smart quotes. */ |
| 55 | private static class QuotedProcessor implements DelimiterProcessor { |
| 56 | private final SmartQuoted.Type type; |
| 57 | private final char delim; |
| 58 | |
| 59 | QuotedProcessor(SmartQuoted.Type type, char open) { |
| 60 | this.type = type; |
| 61 | this.delim = open; |
| 62 | } |
| 63 | |
| 64 | @Override |
| Shawn Pearce | c77a18b | 2016-07-26 10:18:37 -0700 | [diff] [blame] | 65 | public char getOpeningCharacter() { |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 66 | return delim; |
| 67 | } |
| 68 | |
| 69 | @Override |
| Shawn Pearce | c77a18b | 2016-07-26 10:18:37 -0700 | [diff] [blame] | 70 | public char getClosingCharacter() { |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 71 | return delim; |
| 72 | } |
| 73 | |
| 74 | @Override |
| Shawn Pearce | c77a18b | 2016-07-26 10:18:37 -0700 | [diff] [blame] | 75 | public int getMinLength() { |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 76 | return 1; |
| 77 | } |
| 78 | |
| 79 | @Override |
| Shawn Pearce | c77a18b | 2016-07-26 10:18:37 -0700 | [diff] [blame] | 80 | public int getDelimiterUse(DelimiterRun opener, DelimiterRun closer) { |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 81 | return 1; |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public void process(Text opener, Text closer, int delimiterUse) { |
| 86 | quote(type, opener, closer); |
| 87 | } |
| 88 | } |
| 89 | } |