htmltidy.c 3.9 KB

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