title: Can't get HTML text using libtidy
body: <p>everybody - long time listener, first time caller.</p>
  <p>I've been playing around with libtidy in C on macOS 10.13. I started with the sample code <a href="https://curl.se/libcurl/c/htmltidy.html" rel="nofollow noreferrer">here</a> and modified it to read a local html file instead of using curl. Everything seems to work okay except for text. It will find and output every tag in my test file, but does not seem able to get the text at all, and it's driving me nuts.</p>
  <p>The code in question occurs in the <code>DumpNode</code> tree-walking function. My hacked-up version:</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;tidy.h&gt;
  #include &lt;tidybuffio.h&gt;
  
  /* Wrapper functions for file i/o */
  int w_getc(void* ptr)
  {
    return getc((FILE *)ptr);
  }
  void w_ungetc(void *ptr, unsigned char bv)
  {
    ungetc((int)bv, (FILE *)ptr);
  }
  Bool w_feof(void *ptr)
  {
    return (Bool)feof((FILE *)ptr);
  }
  
  /* Traverse the document tree */
  void dumpNode(TidyDoc doc, TidyNode tnod, int indent)
  {
    TidyNode child;
    for(child = tidyGetChild(tnod); child; child = tidyGetNext(child) ) {
      ctmbstr name = tidyNodeGetName(child);
      if (!name) {
        /* if it doesn't have a name, then it's probably text, cdata, etc... */
        TidyBuffer buf;
        tidyBufInit(&amp;buf);
        if (tidyNodeHasText(doc, child) &amp;&amp; tidyNodeGetText(doc, child, &amp;buf)) {
          printf(&quot;%u, %u, %u\n&quot;, buf.size, buf.allocated, buf.next);
          printf(&quot;%*.*s\n&quot;, indent, indent, (buf.bp &amp;&amp; buf.size &gt; 0)?(char *)buf.bp:&quot;&quot;);
        }
        tidyBufFree(&amp;buf);
      }
      dumpNode(doc, child, indent + 4); /* recursive */
    }
  }
  
  int main(int argc, char **argv)
  {
    if(argc == 2) {
      TidyDoc tdoc;
      int err;
      FILE *fp;
      TidyInputSource insrc;
  
      tdoc = tidyCreate();
  
      fp = fopen(argv[1], &quot;r&quot;);
      if (!fp) return -1;
      
      if (tidyInitSource(&amp;insrc, fp, &amp;w_getc, &amp;w_ungetc, &amp;w_feof)) {
        err = tidyParseSource(tdoc, &amp;insrc); /* parse the input */
        if(err &gt;= 0) dumpNode(tdoc, tidyGetRoot(tdoc), 0); /* walk the tree */      
      }
  
      /* clean-up */
      fclose(fp);
      tidyRelease(tdoc);
      return err;
  
    }
    return 0;
  }
  </code></pre>
  <p>And my compiler string: <code>gcc -o TidyExample tidyexample.c -ltidy -DENABLE_DEBUG_LOG -DDEBUG_PPRINT -DDEBUG_INDENT</code></p>
  <p>Here's what I've deduced so far:</p>
  <ul>
  <li>When encountering a text node, <code>tidyNodeHasText(doc, child);</code> and <code>tidyNodeGetText(doc, child, &amp;buf);</code> both return <code>yes</code>.</li>
  <li>That tells me that <a href="https://github.com/htacg/tidy-html5/blob/next/src/tidylib.c#L2400" rel="nofollow noreferrer"><code>tidyNodeGetText</code></a> is calling the pretty print functions like it should. I've verified that neither <code>TidyXmlOut</code> or <code>TidyXhtmlOut</code> are set for <code>doc</code>, so <code>TY_(PPrintTree)</code> should fire.</li>
  <li>Since I use <code>-DENABLE_DEBUG_LOG</code> and  <code>-DDEBUG_PPRINT</code>, <a href="https://github.com/htacg/tidy-html5/blob/next/src/pprint.c#L2044" rel="nofollow noreferrer"><code>TY_(PPrintTree)</code></a> should call <code>dbg_show_node</code>, but it doesn't appear to. This could be because the <code>node == NULL</code> condition prompted an immediate return, but I asserted the existence of the node, and <code>tidyNodeGetText</code> would return <code>no</code> if it didn't exist, so that can't be what's happening.</li>
  <li>I also tried to set a progress callback to get an better view of what was going on in there, but weirdly, the linker didn't recognize the symbol <code>_tidySetPrettyPrinterCallback</code>.
  <ul>
  <li>ETA: I figured this out; there's a second linked library needed for this to work: <code>-ltidys</code>. I can now get pretty printer progress.</li>
  </ul>
  </li>
  <li>The only output the above snippet generates is <code>0, 0, 0\n</code> from the first <code>printf</code> statement, and <code>\n</code> from the second.</li>
  <li>Incidentally, that curl sample code I ripped off? It has the same problem. If you run it, you get a segfault as soon as it hits any text, because it doesn't check whether there's anything in the buffer before it calls <code>printf</code>.</li>
  </ul>
  <p>I am out of ideas. Either I'm doing something majorly wrong (likely), or there's a good-sized bug in libtidy (less likely, but possible).</p>
  <p>ETA: Here's a teeny-tiny HTML file, that when you invoke <code>TidyExample minimal.html</code> results in the usual empty buffers:</p>
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;Test&lt;/title&gt;
  &lt;p&gt;This is text.&lt;/p&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>