1/*
2 * Copyright (c) 2017, Daniel Gultsch All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation and/or
12 * other materials provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its contributors
15 * may be used to endorse or promote products derived from this software without
16 * specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30package eu.siacs.conversations.utils;
31
32import java.util.ArrayList;
33import java.util.Arrays;
34import java.util.List;
35
36public class ImStyleParser {
37
38 private static final List<Character> KEYWORDS = Arrays.asList('*', '_', '~', '`');
39 private static final List<Character> NO_SUB_PARSING_KEYWORDS = List.of('`');
40 private static final List<Character> BLOCK_KEYWORDS = List.of('`');
41 private static final boolean ALLOW_EMPTY = false;
42 private static final boolean PARSE_HIGHER_ORDER_END = true;
43
44 public static List<Style> parse(CharSequence text) {
45 return parse(text, 0, text.length() - 1);
46 }
47
48 public static List<Style> parse(CharSequence text, int start, int end) {
49 List<Style> styles = new ArrayList<>();
50 for (int i = start; i <= end; ++i) {
51 char c = text.charAt(i);
52 if (KEYWORDS.contains(c)
53 && precededByWhiteSpace(text, i, start)
54 && !followedByWhitespace(text, i, end)) {
55 if (BLOCK_KEYWORDS.contains(c) && isCharRepeatedTwoTimes(text, c, i + 1, end)) {
56 int to = seekEndBlock(text, c, i + 3, end);
57 if (to != -1 && (to != i + 5 || ALLOW_EMPTY)) {
58 String keyword = String.valueOf(c) + c + c;
59 styles.add(new Style(keyword, i, to));
60 i = to;
61 continue;
62 }
63 }
64 int to = seekEnd(text, c, i + 1, end);
65 if (to != -1 && (to != i + 1 || ALLOW_EMPTY)) {
66 styles.add(new Style(c, i, to));
67 if (!NO_SUB_PARSING_KEYWORDS.contains(c)) {
68 styles.addAll(parse(text, i + 1, to - 1));
69 }
70 i = to;
71 }
72 }
73 }
74 return styles;
75 }
76
77 private static boolean isCharRepeatedTwoTimes(CharSequence text, char c, int index, int end) {
78 return index + 1 <= end && text.charAt(index) == c && text.charAt(index + 1) == c;
79 }
80
81 private static boolean precededByWhiteSpace(CharSequence text, int index, int start) {
82 return index == start || Character.isWhitespace(text.charAt(index - 1));
83 }
84
85 private static boolean followedByWhitespace(CharSequence text, int index, int end) {
86 return index >= end || Character.isWhitespace(text.charAt(index + 1));
87 }
88
89 private static int seekEnd(CharSequence text, char needle, int start, int end) {
90 for (int i = start; i <= end; ++i) {
91 char c = text.charAt(i);
92 if (c == needle && !Character.isWhitespace(text.charAt(i - 1))) {
93 if (!PARSE_HIGHER_ORDER_END || followedByWhitespace(text, i, end)) {
94 return i;
95 } else {
96 int higherOrder =
97 seekHigherOrderEndWithoutNewBeginning(text, needle, i + 1, end);
98 if (higherOrder != -1) {
99 return higherOrder;
100 }
101 return i;
102 }
103 } else if (c == '\n') {
104 return -1;
105 }
106 }
107 return -1;
108 }
109
110 private static int seekHigherOrderEndWithoutNewBeginning(
111 CharSequence text, char needle, int start, int end) {
112 for (int i = start; i <= end; ++i) {
113 char c = text.charAt(i);
114 if (c == needle
115 && precededByWhiteSpace(text, i, start)
116 && !followedByWhitespace(text, i, end)) {
117 return -1; // new beginning
118 } else if (c == needle
119 && !Character.isWhitespace(text.charAt(i - 1))
120 && followedByWhitespace(text, i, end)) {
121 return i;
122 } else if (c == '\n') {
123 return -1;
124 }
125 }
126 return -1;
127 }
128
129 private static int seekEndBlock(CharSequence text, char needle, int start, int end) {
130 for (int i = start; i <= end; ++i) {
131 char c = text.charAt(i);
132 if (c == needle && isCharRepeatedTwoTimes(text, needle, i + 1, end)) {
133 return i + 2;
134 }
135 }
136 return -1;
137 }
138
139 public static class Style {
140
141 private final String keyword;
142 private final int start;
143 private final int end;
144
145 public Style(char character, int start, int end) {
146 this(String.valueOf(character), start, end);
147 }
148
149 public Style(String keyword, int start, int end) {
150 this.keyword = keyword;
151 this.start = start;
152 this.end = end;
153 }
154
155 public String getKeyword() {
156 return keyword;
157 }
158
159 public int getStart() {
160 return start;
161 }
162
163 public int getEnd() {
164 return end;
165 }
166 }
167}