-
Notifications
You must be signed in to change notification settings - Fork 0
/
htmltidy.c
131 lines (113 loc) · 3.53 KB
/
htmltidy.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <curl/curl.h>
#include <stdio.h>
#include <tidy.h>
#include <tidybuffio.h>
//////////////////// MACRO SECTION ////////////////////
#define TIDY_INT_MAX (4096)
#define IS_CURL_HAS_ERROR(err) (err)
#define IS_TIDY_HAS_ERROR(err) (err > 0) /* 0: Normal, 1: Warning, 2: Error */
#define IS_TIDY_HAS_VALID_CODE(err) (err >= 0)
//////////////////// DECLARATION SECTION ////////////////////
uint write_callback(char *inp_buffer, uint size, uint nr_memory_buffer,
TidyBuffer *outp_buffer);
void dump_node(TidyDoc doc, TidyNode tidy_node, int indent);
//////////////////// MAIN SECTION ////////////////////
int main(int argc, char **argv)
{
CURL *curl;
char curl_error_buffer[CURL_ERROR_SIZE];
TidyDoc tidy_doc;
TidyBuffer doc_buffer = { 0 };
TidyBuffer tidy_err_buffer = { 0 };
int curl_err = 0, tidy_err = 0;
if (argc == 2) {
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL,
argv[1]); /* Get URL from argument */
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_error_buffer);
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(
curl, CURLOPT_WRITEFUNCTION,
write_callback); /* write callback function setting */
tidy_doc = tidyCreate();
tidyOptSetBool(tidy_doc, TidyForceOutput, yes);
tidyOptSetInt(tidy_doc, TidyWrapLen, TIDY_INT_MAX);
tidySetErrorBuffer(tidy_doc, &tidy_err_buffer);
tidyBufInit(&doc_buffer);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &doc_buffer);
curl_err = curl_easy_perform(curl);
if (IS_CURL_HAS_ERROR(curl_err)) {
goto exception;
}
tidy_err = tidyParseBuffer(tidy_doc, &doc_buffer);
if (!IS_TIDY_HAS_VALID_CODE(tidy_err)) {
goto exception;
}
tidy_err = tidyCleanAndRepair(tidy_doc);
if (!IS_TIDY_HAS_VALID_CODE(tidy_err)) {
goto exception;
}
tidy_err = tidyRunDiagnostics(tidy_doc);
if (!IS_TIDY_HAS_VALID_CODE(tidy_err)) {
goto exception;
}
dump_node(tidy_doc, tidyGetRoot(tidy_doc), 0);
if (IS_TIDY_HAS_ERROR(tidy_err)) {
goto exception;
}
} else {
printf("usage: %s <url>\n", argv[0]);
} // end of if
exception:
curl_easy_cleanup(curl);
tidyBufFree(&doc_buffer);
tidyBufFree(&tidy_err_buffer);
tidyRelease(tidy_doc);
if (IS_CURL_HAS_ERROR(curl)) {
fprintf(stderr, "%s\n", curl_error_buffer);
}
if (IS_TIDY_HAS_ERROR(tidy_err)) {
fprintf(stderr, "%s\n", tidy_err_buffer.bp);
}
return (curl_err | tidy_err);
}
//////////////////// DEFINITION SECTION ////////////////////
uint write_callback(char *inp_buffer, uint size, uint nr_memory_buffer,
TidyBuffer *outp_buffer)
{
uint ret;
ret = size * nr_memory_buffer;
tidyBufAppend(outp_buffer, inp_buffer, ret);
return ret;
}
void dump_node(TidyDoc doc, TidyNode tidy_node, int indent)
{
TidyNode child;
for (child = tidyGetChild(tidy_node); child;
child = tidyGetNext(child)) {
ctmbstr name = tidyNodeGetName(child);
if (name) { /* Case TAG Inforamtion*/
TidyAttr attr;
printf("%*.*s%s ", indent, indent, "<",
name); /* Ignore the `indent` size characters */
for (attr = tidyAttrFirst(child); attr;
attr = tidyAttrNext(attr)) {
printf("%s", tidyAttrName(attr));
tidyAttrValue(attr) ?
printf("=\"%s\" ",
tidyAttrValue(attr)) :
printf(" ");
} // end of for
printf(">\n");
} else { /* Case: Text Information*/
TidyBuffer buf;
tidyBufInit(&buf);
tidyNodeGetText(doc, child, &buf);
printf("%*.*s\n", indent, indent,
buf.bp ? (char *)buf.bp : " ");
tidyBufFree(&buf);
} // end of if
dump_node(doc, child, indent + 4);
} // end of for
}