htmltidy.c 3.9 KB

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