blob: 7df0c13b7b9cb84204b4ddb21e610aaee80fa771 [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
Shawn Pearcec77a18b2016-07-26 10:18:37 -070017import static com.google.gitiles.doc.SmartQuoted.Type.DOUBLE;
18import static com.google.gitiles.doc.SmartQuoted.Type.SINGLE;
Shawn Pearce12c8fab2016-05-15 16:55:21 -070019
20import com.google.gitiles.doc.SmartQuoted.Type;
Shawn Pearce12c8fab2016-05-15 16:55:21 -070021import org.commonmark.Extension;
22import org.commonmark.node.Node;
23import org.commonmark.node.Text;
Shawn Pearce12c8fab2016-05-15 16:55:21 -070024import org.commonmark.parser.Parser;
25import org.commonmark.parser.Parser.ParserExtension;
Shawn Pearcec77a18b2016-07-26 10:18:37 -070026import org.commonmark.parser.delimiter.DelimiterProcessor;
27import org.commonmark.parser.delimiter.DelimiterRun;
Shawn Pearce12c8fab2016-05-15 16:55:21 -070028
29/** Uses smart quotes for ' and ". */
30public 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 Pearcec77a18b2016-07-26 10:18:37 -070065 public char getOpeningCharacter() {
Shawn Pearce12c8fab2016-05-15 16:55:21 -070066 return delim;
67 }
68
69 @Override
Shawn Pearcec77a18b2016-07-26 10:18:37 -070070 public char getClosingCharacter() {
Shawn Pearce12c8fab2016-05-15 16:55:21 -070071 return delim;
72 }
73
74 @Override
Shawn Pearcec77a18b2016-07-26 10:18:37 -070075 public int getMinLength() {
Shawn Pearce12c8fab2016-05-15 16:55:21 -070076 return 1;
77 }
78
79 @Override
Shawn Pearcec77a18b2016-07-26 10:18:37 -070080 public int getDelimiterUse(DelimiterRun opener, DelimiterRun closer) {
Shawn Pearce12c8fab2016-05-15 16:55:21 -070081 return 1;
82 }
83
84 @Override
85 public void process(Text opener, Text closer, int delimiterUse) {
86 quote(type, opener, closer);
87 }
88 }
89}