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 final static List<Character> KEYWORDS = Arrays.asList('*', '_', '~', '`');
39 private final static List<Character> NO_SUB_PARSING_KEYWORDS = Arrays.asList('`');
40 private final static List<Character> BLOCK_KEYWORDS = Arrays.asList('`');
41 private final static boolean ALLOW_EMPTY = false;
42 private final static 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) && precededByWhiteSpace(text, i, start) && !followedByWhitespace(text, i, end)) {
53 if (BLOCK_KEYWORDS.contains(c) && isCharRepeatedTwoTimes(text, c, i + 1, end)) {
54 int to = seekEndBlock(text, c, i + 3, end);
55 if (to != -1 && (to != i + 5 || ALLOW_EMPTY)) {
56 String keyword = String.valueOf(c) + c + c;
57 styles.add(new Style(keyword, i, to));
58 i = to;
59 continue;
60 }
61 continue;
62 }
63 int to = seekEnd(text, c, i + 1, end);
64 if (to != -1 && (to != i + 1 || ALLOW_EMPTY)) {
65 styles.add(new Style(c, i, to));
66 if (!NO_SUB_PARSING_KEYWORDS.contains(c)) {
67 styles.addAll(parse(text, i + 1, to - 1));
68 }
69 i = to;
70 }
71 }
72 }
73 return styles;
74 }
75
76 private static boolean isCharRepeatedTwoTimes(CharSequence text, char c, int index, int end) {
77 return index + 1 <= end && text.charAt(index) == c && text.charAt(index + 1) == c;
78 }
79
80 private static boolean precededByWhiteSpace(CharSequence text, int index, int start) {
81 return index == start || Character.isWhitespace(text.charAt(index - 1));
82 }
83
84 private static boolean followedByWhitespace(CharSequence text, int index, int end) {
85 return index >= end || Character.isWhitespace(text.charAt(index + 1));
86 }
87
88 private static int seekEnd(CharSequence text, char needle, int start, int end) {
89 for (int i = start; i <= end; ++i) {
90 char c = text.charAt(i);
91 if (c == needle && !Character.isWhitespace(text.charAt(i - 1))) {
92 if (!PARSE_HIGHER_ORDER_END || followedByWhitespace(text, i, end)) {
93 return i;
94 } else {
95 int higherOrder = seekHigherOrderEndWithoutNewBeginning(text, needle, i + 1, end);
96 if (higherOrder != -1) {
97 return higherOrder;
98 }
99 return i;
100 }
101 } else if (c == '\n') {
102 return -1;
103 }
104 }
105 return -1;
106 }
107
108 private static int seekHigherOrderEndWithoutNewBeginning(CharSequence text, char needle, int start, int end) {
109 for (int i = start; i <= end; ++i) {
110 char c = text.charAt(i);
111 if (c == needle && precededByWhiteSpace(text, i, start) && !followedByWhitespace(text, i, end)) {
112 return -1; // new beginning
113 } else if (c == needle && !Character.isWhitespace(text.charAt(i - 1)) && followedByWhitespace(text, i, end)) {
114 return i;
115 } else if (c == '\n') {
116 return -1;
117 }
118 }
119 return -1;
120 }
121
122 private static int seekEndBlock(CharSequence text, char needle, int start, int end) {
123 var foundNewline = false;
124 for (int i = start; i <= end; ++i) {
125 char c = text.charAt(i);
126 if (c == '\n') foundNewline = true;
127 if (foundNewline && c == needle && isCharRepeatedTwoTimes(text, needle, i + 1, end)) {
128 return i + 2;
129 }
130 }
131 return -1;
132 }
133
134 public static class Style {
135
136 private final String keyword;
137 private final int start;
138 private final int end;
139
140 public Style(char character, int start, int end) {
141 this(String.valueOf(character), start, end);
142 }
143
144 public Style(String keyword, int start, int end) {
145 this.keyword = keyword;
146 this.start = start;
147 this.end = end;
148 }
149
150 public String getKeyword() {
151 return keyword;
152 }
153
154 public int getStart() {
155 return start;
156 }
157
158 public int getEnd() {
159 return end;
160 }
161 }
162}