htmltidy.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*****************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * $Id$
  9. *
  10. * Download a document and use libtidy to parse the HTML.
  11. * Written by Jeff Pohlmeyer
  12. *
  13. * LibTidy => http://tidy.sourceforge.net
  14. *
  15. * gcc -Wall -I/usr/local/include tidycurl.c -lcurl -ltidy -o tidycurl
  16. *
  17. */
  18. #include <stdio.h>
  19. #include <tidy/tidy.h>
  20. #include <tidy/buffio.h>
  21. #include <curl/curl.h>
  22. /* curl write callback, to fill tidy's input buffer... */
  23. uint write_cb(char *in, uint size, uint nmemb, TidyBuffer *out)
  24. {
  25. uint r;
  26. r = size * nmemb;
  27. tidyBufAppend( out, in, r );
  28. return(r);
  29. }
  30. /* Traverse the document tree */
  31. void dumpNode(TidyDoc doc, TidyNode tnod, int indent )
  32. {
  33. TidyNode child;
  34. for ( child = tidyGetChild(tnod); child; child = tidyGetNext(child) )
  35. {
  36. ctmbstr name = tidyNodeGetName( child );
  37. if ( name )
  38. {
  39. /* if it has a name, then it's an HTML tag ... */
  40. TidyAttr attr;
  41. printf( "%*.*s%s ", indent, indent, "<", name);
  42. /* walk the attribute list */
  43. for ( attr=tidyAttrFirst(child); attr; attr=tidyAttrNext(attr) ) {
  44. printf(tidyAttrName(attr));
  45. tidyAttrValue(attr)?printf("=\"%s\" ",
  46. tidyAttrValue(attr)):printf(" ");
  47. }
  48. printf( ">\n");
  49. }
  50. else {
  51. /* if it doesn't have a name, then it's probably text, cdata, etc... */
  52. TidyBuffer buf;
  53. tidyBufInit(&buf);
  54. tidyNodeGetText(doc, child, &buf);
  55. printf("%*.*s\n", indent, indent, buf.bp?(char *)buf.bp:"");
  56. tidyBufFree(&buf);
  57. }
  58. dumpNode( doc, child, indent + 4 ); /* recursive */
  59. }
  60. }
  61. int main(int argc, char **argv )
  62. {
  63. CURL *curl;
  64. char curl_errbuf[CURL_ERROR_SIZE];
  65. TidyDoc tdoc;
  66. TidyBuffer docbuf = {0};
  67. TidyBuffer tidy_errbuf = {0};
  68. int err;
  69. if ( argc == 2) {
  70. curl = curl_easy_init();
  71. curl_easy_setopt(curl, CURLOPT_URL, argv[1]);
  72. curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errbuf);
  73. curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
  74. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  75. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb);
  76. tdoc = tidyCreate();
  77. tidyOptSetBool(tdoc, TidyForceOutput, yes); /* try harder */
  78. tidyOptSetInt(tdoc, TidyWrapLen, 4096);
  79. tidySetErrorBuffer( tdoc, &tidy_errbuf );
  80. tidyBufInit(&docbuf);
  81. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &docbuf);
  82. err=curl_easy_perform(curl);
  83. if ( !err ) {
  84. err = tidyParseBuffer(tdoc, &docbuf); /* parse the input */
  85. if ( err >= 0 ) {
  86. err = tidyCleanAndRepair(tdoc); /* fix any problems */
  87. if ( err >= 0 ) {
  88. err = tidyRunDiagnostics(tdoc); /* load tidy error buffer */
  89. if ( err >= 0 ) {
  90. dumpNode( tdoc, tidyGetRoot(tdoc), 0 ); /* walk the tree */
  91. fprintf(stderr, "%s\n", tidy_errbuf.bp); /* show errors */
  92. }
  93. }
  94. }
  95. }
  96. else
  97. fprintf(stderr, "%s\n", curl_errbuf);
  98. /* clean-up */
  99. curl_easy_cleanup(curl);
  100. tidyBufFree(&docbuf);
  101. tidyBufFree(&tidy_errbuf);
  102. tidyRelease(tdoc);
  103. return(err);
  104. }
  105. else
  106. printf( "usage: %s <url>\n", argv[0] );
  107. return(0);
  108. }