dart中html转换为文本的输出
Submitted by hubdog on Tue, 2019-04-23 23:36
dart里面没有现成的html转为text的功能,jsoup有现成的功能,可是无法移植到ios上,而且比较重,实在不值得折腾,基于dart的html库,手撸了一个html转换为text的处理
class PlaintextVisitor extends TreeVisitor { final StringBuffer _str; PlaintextVisitor() : _str = StringBuffer(); String toString() => _str.toString(); visitDocument(dom.Document node) { visitChildren(node); } visitDocumentType(dom.DocumentType node) { visitChildren(node); } visitText(dom.Text node) { if (node.text=="\n") return; writeTextNodeAsHtml(_str, node); } visitElement(dom.Element node) { final tag = node.localName; if (tag=="li") _str.write("\n * "); else if (tag=="dt") _str.write(" "); else if (tag=="p" || tag== "h1" || tag== "h2" || tag=="h3" || tag== "h4" || tag== "h5" || tag=="tr") _str.write("\n"); if (node.nodes.isNotEmpty) { visitChildren(node); } else if (isVoidElement(tag)) { return; } if (tag=="br" || tag== "dd" || tag=="dt" || tag== "p" || tag== "h1" || tag== "h2" || tag== "h3" || tag== "h4" || tag== "h5") _str.write("\n"); } } 用法 import 'package:html/parser.dart' show parse; import 'package:html/dom.dart' as dom; import 'package:html/dom_parsing.dart'; var document = parse( htmlString); plaintext=(PlaintextVisitor()..visit(document)).toString();