HtmlToPlainText implements NodeVisitor { public String getPlainText(Element element) { traverse(element); return this.toString(); } HtmlToPlainText(FreetextConfig config); String getPlainText(Element element); void traverse(Node root); boolean filter(Node node); boolean replace(Node node); void head(Node node, int depth); void tail(Node node, int depth); @Override String toString();  }
@Test public void Superscript() { FreetextConfig config = new FreetextConfig(true, false); String xml = "<p>metallic barium ion can lose 2 electrons to become Ba<sup>2+</sup></p>"; String expect = "\nmetallic barium ion can lose 2 electrons to become Ba^{2+}\n"; Document jsoupDoc = Jsoup.parse(xml, "", Parser.xmlParser()); HtmlToPlainText converter = new HtmlToPlainText(config); String plainText = converter.getPlainText(jsoupDoc); assertEquals(expect, plainText); }
@Test public void Subscript() { FreetextConfig config = new FreetextConfig(true, false); String xml = "<p>Water or H<sub>2</sub>O</p>"; String expect = "\nWater or H_{2}O\n"; Document jsoupDoc = Jsoup.parse(xml, "", Parser.xmlParser()); HtmlToPlainText converter = new HtmlToPlainText(config); String plainText = converter.getPlainText(jsoupDoc); assertEquals(expect, plainText); }
@Test public void ParagraphIndent() { FreetextConfig config = new FreetextConfig(true, true); config.setTextIndentSize(3); String xml = "<p>This is a paragraph.</p>"; String expect = "\n This is a paragraph.\n"; Document jsoupDoc = Jsoup.parse(xml, "", Parser.xmlParser()); HtmlToPlainText converter = new HtmlToPlainText(config); String plainText = converter.getPlainText(jsoupDoc); assertEquals(expect, plainText); }
@Test public void Remove_Delete_Annotation() { FreetextConfig config = new FreetextConfig(false, false); config.remove("del"); String xml = "<p>This was <del>deleted</del><ins>inserted</p>"; String expect = "\\nThis was inserted\\n"; Document jsoupDoc = Jsoup.parse(xml, "", Parser.xmlParser()); HtmlToPlainText converter = new HtmlToPlainText(config); String plainText = converter.getPlainText(jsoupDoc); assertEquals(expect, plainText); }
@Test public void HTML_Quote_Tag() { FreetextConfig config = new FreetextConfig(false, false); config.remove("del"); String xml = "<p><q>My quote of the day.</q></p>"; String expect = "\\n\u201CMy quote of the day.\u201d\\n"; Document jsoupDoc = Jsoup.parse(xml, "", Parser.xmlParser()); HtmlToPlainText converter = new HtmlToPlainText(config); String plainText = converter.getPlainText(jsoupDoc); assertEquals(expect, plainText); }
@Test public void HTML_BlockQuote() { FreetextConfig config = new FreetextConfig(false, false); config.setTextIndentSize(5); config.remove("del"); String xml = "<p><blockquote><p>My quote of the day.<br/>My quote second line.</p></blockquote></p>"; String expect = "\\n\\n\\n My quote of the day.\\n My quote second line.\\n\\n"; Document jsoupDoc = Jsoup.parse(xml, "", Parser.xmlParser()); HtmlToPlainText converter = new HtmlToPlainText(config); String plainText = converter.getPlainText(jsoupDoc); assertEquals(expect, plainText); }