在 TextView 中显示正确缩进和解析的源代码 [英] Show source code in TextView correctly indented and parsed
问题描述
是否可以逐字模式或类似方式解析 HTML 代码,以便最终可能出现的源代码片段(包含在 pre 和 code HTML 标记之间)能正常显示吗?
我想做的是以用户友好的模式显示源代码(易于与文本的其余部分区分开,保持缩进等),就像 Stack Overflow 一样:)
Html.fromHtml() 似乎只支持 减少的 HTML 标签子集.
最后我自己预解析了收到的 HTML 代码,因为 Html.fromHtml 不支持 pre
和 code
标签,y 用我的自定义格式替换它们,并预先解析这些标签内的代码,用 <br/>
替换 "\n" 和 代码>.
然后我把结果发送到Html.fromHtml,结果就好了:
公共类 HtmlParser {公共静态跨度解析(字符串文本){if (text == null) 返回 null;文本 = 解析源代码(文本);Spanned textSpanned = Html.fromHtml(text);返回 textSpanned;}私有静态字符串解析源代码(字符串文本){如果 (text.indexOf(ORIGINAL_PATTERN_BEGIN) <0) 返回文本;StringBuilder 结果 = new StringBuilder();int开始;打算;int beginIndexToProcess = 0;而 (text.indexOf(ORIGINAL_PATTERN_BEGIN) >= 0) {begin = text.indexOf(ORIGINAL_PATTERN_BEGIN);end = text.indexOf(ORIGINAL_PATTERN_END);String code = parseCodeSegment(text, begin, end);result.append(text.substring(beginIndexToProcess, begin));result.append(PARSED_PATTERN_BEGIN);结果.追加(代码);result.append(PARSED_PATTERN_END);//在原文中替换以查找下一次出现text = text.replaceFirst(ORIGINAL_PATTERN_BEGIN, PARSED_PATTERN_BEGIN);text = text.replaceFirst(ORIGINAL_PATTERN_END,PARSED_PATTERN_END);//更新要处理的字符串索引beginIndexToProcess = text.lastIndexOf(PARSED_PATTERN_END) + PARSED_PATTERN_END.length();}//添加字符串的其余部分result.append(text.substring(beginIndexToProcess, text.length()));返回 result.toString();}私有静态字符串 parseCodeSegment(String text, int begin, int end) {String code = text.substring(begin + ORIGINAL_PATTERN_BEGIN.length(), end);code = code.replace(" ", " ");code = code.replace("\n","
");返回码;}private static final String ORIGINAL_PATTERN_BEGIN = "";private static final String ORIGINAL_PATTERN_END = "</code></pre>";private static final String PARSED_PATTERN_BEGIN = "<font color=\"#888888\"><tt>";private static final String PARSED_PATTERN_END = "</tt></font>";}
Is it possible to parse HTML code in a verbatim mode or something similar so that the source code fragments that eventually may appear (enclosed between pre and code HTML tags) can be displayed properly?
What I want to do is show source code in a user-friendly mode (easy to distinguish from the rest of the text, keep indentation, etc.), as Stack Overflow does :)
It seems that Html.fromHtml() supports only a reduced subset of HTML tags.
解决方案 Finally I preparsed by myself the HTML code received, since Html.fromHtml does not support the pre
and code
tags, y replaced them with my custom format and pre-parsed the code inside those tags replacing "\n" with <br/>
and " " with
.
Then I send the results to Html.fromHtml, and the result is just fine:
public class HtmlParser {
public static Spanned parse(String text) {
if (text == null) return null;
text = parseSourceCode(text);
Spanned textSpanned = Html.fromHtml(text);
return textSpanned;
}
private static String parseSourceCode(String text) {
if (text.indexOf(ORIGINAL_PATTERN_BEGIN) < 0) return text;
StringBuilder result = new StringBuilder();
int begin;
int end;
int beginIndexToProcess = 0;
while (text.indexOf(ORIGINAL_PATTERN_BEGIN) >= 0) {
begin = text.indexOf(ORIGINAL_PATTERN_BEGIN);
end = text.indexOf(ORIGINAL_PATTERN_END);
String code = parseCodeSegment(text, begin, end);
result.append(text.substring(beginIndexToProcess, begin));
result.append(PARSED_PATTERN_BEGIN);
result.append(code);
result.append(PARSED_PATTERN_END);
//replace in the original text to find the next appearance
text = text.replaceFirst(ORIGINAL_PATTERN_BEGIN, PARSED_PATTERN_BEGIN);
text = text.replaceFirst(ORIGINAL_PATTERN_END, PARSED_PATTERN_END);
//update the string index to process
beginIndexToProcess = text.lastIndexOf(PARSED_PATTERN_END) + PARSED_PATTERN_END.length();
}
//add the rest of the string
result.append(text.substring(beginIndexToProcess, text.length()));
return result.toString();
}
private static String parseCodeSegment(String text, int begin, int end) {
String code = text.substring(begin + ORIGINAL_PATTERN_BEGIN.length(), end);
code = code.replace(" ", " ");
code = code.replace("\n","<br/>");
return code;
}
private static final String ORIGINAL_PATTERN_BEGIN = "<pre><code>";
private static final String ORIGINAL_PATTERN_END = "</code></pre>";
private static final String PARSED_PATTERN_BEGIN = "<font color=\"#888888\"><tt>";
private static final String PARSED_PATTERN_END = "</tt></font>";
}
这篇关于在 TextView 中显示正确缩进和解析的源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!