htmltidy.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at http://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. /* Download a document and use libtidy to parse the HTML.
  23. * Written by Jeff Pohlmeyer
  24. *
  25. * LibTidy => http://tidy.sourceforge.net
  26. *
  27. * gcc -Wall -I/usr/local/include tidycurl.c -lcurl -ltidy -o tidycurl
  28. *
  29. */
  30. #include <stdio.h>
  31. #include <tidy/tidy.h>
  32. #include <tidy/buffio.h>
  33. #include <curl/curl.h>
  34. /* curl write callback, to fill tidy's input buffer... */
  35. uint write_cb(char *in, uint size, uint nmemb, TidyBuffer *out)
  36. {
  37. uint r;
  38. r = size * nmemb;
  39. tidyBufAppend( out, in, r );
  40. return(r);
  41. }
  42. /* Traverse the document tree */
  43. void dumpNode(TidyDoc doc, TidyNode tnod, int indent )
  44. {
  45. TidyNode child;
  46. for ( child = tidyGetChild(tnod); child; child = tidyGetNext(child) )
  47. {
  48. ctmbstr name = tidyNodeGetName( child );
  49. if ( name )
  50. {
  51. /* if it has a name, then it's an HTML tag ... */
  52. TidyAttr attr;
  53. printf( "%*.*s%s ", indent, indent, "<", name);
  54. /* walk the attribute list */
  55. for ( attr=tidyAttrFirst(child); attr; attr=tidyAttrNext(attr) ) {
  56. printf(tidyAttrName(attr));
  57. tidyAttrValue(attr)?printf("=\"%s\" ",
  58. tidyAttrValue(attr)):printf(" ");
  59. }
  60. printf( ">\n");
  61. }
  62. else {
  63. /* if it doesn't have a name, then it's probably text, cdata, etc... */
  64. TidyBuffer buf;
  65. tidyBufInit(&buf);
  66. tidyNodeGetText(doc, child, &buf);
  67. printf("%*.*s\n", indent, indent, buf.bp?(char *)buf.bp:"");
  68. tidyBufFree(&buf);
  69. }
  70. dumpNode( doc, child, indent + 4 ); /* recursive */
  71. }
  72. }
  73. int main(int argc, char **argv )
  74. {
  75. CURL *curl;
  76. char curl_errbuf[CURL_ERROR_SIZE];
  77. TidyDoc tdoc;
  78. TidyBuffer docbuf = {0};
  79. TidyBuffer tidy_errbuf = {0};
  80. int err;
  81. if ( argc == 2) {
  82. curl = curl_easy_init();
  83. curl_easy_setopt(curl, CURLOPT_URL, argv[1]);
  84. curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errbuf);
  85. curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
  86. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  87. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb);
  88. tdoc = tidyCreate();
  89. tidyOptSetBool(tdoc, TidyForceOutput, yes); /* try harder */
  90. tidyOptSetInt(tdoc, TidyWrapLen, 4096);
  91. tidySetErrorBuffer( tdoc, &tidy_errbuf );
  92. tidyBufInit(&docbuf);
  93. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &docbuf);
  94. err=curl_easy_perform(curl);
  95. if ( !err ) {
  96. err = tidyParseBuffer(tdoc, &docbuf); /* parse the input */
  97. if ( err >= 0 ) {
  98. err = tidyCleanAndRepair(tdoc); /* fix any problems */
  99. if ( err >= 0 ) {
  100. err = tidyRunDiagnostics(tdoc); /* load tidy error buffer */
  101. if ( err >= 0 ) {
  102. dumpNode( tdoc, tidyGetRoot(tdoc), 0 ); /* walk the tree */
  103. fprintf(stderr, "%s\n", tidy_errbuf.bp); /* show errors */
  104. }
  105. }
  106. }
  107. }
  108. else
  109. fprintf(stderr, "%s\n", curl_errbuf);
  110. /* clean-up */
  111. curl_easy_cleanup(curl);
  112. tidyBufFree(&docbuf);
  113. tidyBufFree(&tidy_errbuf);
  114. tidyRelease(tdoc);
  115. return(err);
  116. }
  117. else
  118. printf( "usage: %s <url>\n", argv[0] );
  119. return(0);
  120. }