MCPcopy Create free account
hub / github.com/XUANXUQAQ/File-Engine / highLight

Method highLight

src/main/java/file/engine/frames/SearchBar.java:4238–4283  ·  view source on GitHub ↗

高亮显示 @param html 待处理的html @param keywords 高亮关键字 @return 处理后带html

(String html, String[] keywords)

Source from the content-addressed store, hash-verified

4236 * @return 处理后带html
4237 */
4238 private String highLight(String html, String[] keywords) {
4239 if (keywords == null) {
4240 keywords = new String[]{""};
4241 }
4242 StringBuilder regexPatternBuilder = new StringBuilder();
4243 List<String> collect = Arrays.stream(keywords).sorted((o1, o2) -> o2.length() - o1.length()).toList();
4244 for (String keyword : collect) {
4245 if (!keyword.isBlank()) {
4246 if (".".equals(keyword)) {
4247 keyword = "\\.";
4248 } else if (keyword.startsWith(File.separator)) {
4249 continue;
4250 }
4251 regexPatternBuilder.append(keyword).append("|");
4252 }
4253 }
4254 if (PinyinUtil.isStringContainChinese(html)) {
4255 // 挑出所有的中文字符
4256 Map<String, String> chinesePinyinMap = PinyinUtil.getChinesePinyinMap(html);
4257 // 转换成拼音后和keywords匹配,如果发现匹配出成功,则添加到正则表达式中
4258 String[] finalKeywords = keywords;
4259 chinesePinyinMap.entrySet()
4260 .stream()
4261 .filter(pair -> {
4262 for (String each : finalKeywords) {
4263 if (each.toLowerCase().indexOf(pair.getValue().toLowerCase()) != -1) {
4264 return true;
4265 }
4266 }
4267 return false;
4268 })
4269 .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))
4270 .forEach((k, v) -> regexPatternBuilder.append(k).append("|"));
4271 }
4272 if (!regexPatternBuilder.isEmpty()) {
4273 String pattern = regexPatternBuilder.substring(0, regexPatternBuilder.length() - 1);
4274 Pattern compile = RegexUtil.getPattern(pattern, Pattern.CASE_INSENSITIVE);
4275 Matcher matcher = compile.matcher(html);
4276 html = matcher.replaceAll((matchResult) -> {
4277 String group = matchResult.group();
4278 String s = "#" + ColorUtil.parseColorHex(fontColorWithCoverage);
4279 return "<span style=\"color: " + s + ";\">" + group + "</span>";
4280 });
4281 }
4282 return html;
4283 }
4284
4285
4286 /**

Callers 2

getHtmlMethod · 0.95
showResultOnLabelMethod · 0.95

Calls 5

getChinesePinyinMapMethod · 0.95
getPatternMethod · 0.95
parseColorHexMethod · 0.95
lengthMethod · 0.80

Tested by

no test coverage detected