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