1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 * Download a document and use libtidy to parse the HTML.
27 * LibTidy => http://tidy.sourceforge.net
31 #include <tidy/tidy.h>
32 #include <tidy/buffio.h>
33 #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)
40 tidyBufAppend(out, in, r);
44 /* Traverse the document tree */
45 void dumpNode(TidyDoc doc, TidyNode tnod, int indent)
48 for(child = tidyGetChild(tnod); child; child = tidyGetNext(child) ) {
49 ctmbstr name = tidyNodeGetName(child);
51 /* if it has a name, then it's an HTML tag ... */
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(" ");
63 /* if it doesn't have a name, then it's probably text, cdata, etc... */
66 tidyNodeGetText(doc, child, &buf);
67 printf("%*.*s\n", indent, indent, buf.bp?(char *)buf.bp:"");
70 dumpNode(doc, child, indent + 4); /* recursive */
75 int main(int argc, char **argv)
78 char curl_errbuf[CURL_ERROR_SIZE];
80 TidyBuffer docbuf = {0};
81 TidyBuffer tidy_errbuf = {0};
84 curl = curl_easy_init();
85 curl_easy_setopt(curl, CURLOPT_URL, argv[1]);
86 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errbuf);
87 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
88 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
89 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb);
92 tidyOptSetBool(tdoc, TidyForceOutput, yes); /* try harder */
93 tidyOptSetInt(tdoc, TidyWrapLen, 4096);
94 tidySetErrorBuffer(tdoc, &tidy_errbuf);
97 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &docbuf);
98 err=curl_easy_perform(curl);
100 err = tidyParseBuffer(tdoc, &docbuf); /* parse the input */
102 err = tidyCleanAndRepair(tdoc); /* fix any problems */
104 err = tidyRunDiagnostics(tdoc); /* load tidy error buffer */
106 dumpNode(tdoc, tidyGetRoot(tdoc), 0); /* walk the tree */
107 fprintf(stderr, "%s\n", tidy_errbuf.bp); /* show errors */
113 fprintf(stderr, "%s\n", curl_errbuf);
116 curl_easy_cleanup(curl);
117 tidyBufFree(&docbuf);
118 tidyBufFree(&tidy_errbuf);
124 printf("usage: %s <url>\n", argv[0]);