// appends text to the string builder with a simple word wrap method
  private void append2(String text) {
    // if (text.contains('\n'))
    width =
        0; // reset counter if starts with a newline. only from formats above, not in natural text
    if (text.equals(" ")
        && (accum.length() == 0 || StringUtil.in(accum.substring(accum.length() - 1), " ")))
      return; // don't accumulate long runs of empty spaces

    if (text.length() + width > maxWidth) { // won't fit, needs to wrap
      String words[] = text.split("\\s+");
      for (int i = 0; i < words.length; i++) {
        String word = words[i];
        boolean last = i == words.length - 1;
        if (!last) // insert a space if not the last word
        word = word + " ";
        if (word.length() + width > maxWidth) { // wrap and reset counter
          accum.append('\n').append(word);
          width = word.length();
        } else {
          accum.append(word);
          width += word.length();
        }
      }
    } else { // fits as is, without need to wrap text
      accum.append(text);
      width += text.length();
    }
  }
Esempio n. 2
0
 // hit when all of the node's children (if any) have been visited
 public void tail(Node node, int depth) {
   String name = node.nodeName();
   if (StringUtil.in(name, "p", "h1", "h2", "h3", "h4", "h5", "a", "span")) append("<br>");
   if (name.equals("ul")) {
     append("</ul>");
   }
   if (name.equals("li")) {
     append("</li>");
   }
 }
Esempio n. 3
0
 // hit when all of the node's children (if any) have been visited
 public void tail(Node node, int depth) {
   String name = node.nodeName();
   if (name.equals("br")) append("\n");
   else if (StringUtil.in(name, "p", "h1", "h2", "h3", "h4", "h5")) append("\n\n");
   else if (name.equals("a")) append(String.format(" <%s>", node.absUrl("href")));
 }