-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhtml_tidy.cc
58 lines (48 loc) · 1.79 KB
/
html_tidy.cc
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
// Copyright (c) 2011 Fei Sun. All Rights Reserved.
// Author: [email protected] (Fei Sun)
// [email protected] (Yukihiro TAGAMI)
// License: GPLv3 <http://www.gnu.org/copyleft/gpl.html>
#include "html_tidy.h"
#include <stdio.h>
#include <errno.h>
#include "tidy.h"
#include "buffio.h"
int HTMLTidy::tidy(const std::string &input, const std::string &in_enc,
std::string *output) {
if (output == NULL) { return -1; }
TidyBuffer outbuf;
TidyBuffer errbuf;
TidyOptionId optid;
int rc = -1;
Bool ok;
TidyDoc tdoc = tidyCreate(); // Initialize "document"
tidyBufInit(&outbuf);
tidyBufInit(&errbuf);
tidySetInCharEncoding(tdoc, in_enc.c_str());
tidySetOutCharEncoding(tdoc, in_enc.c_str());
optid = tidyOptGetIdForName("doctype");
tidyOptSetValue(tdoc, optid, "omit");
optid = tidyOptGetIdForName("output-xhtml");
tidyOptSetBool(tdoc, optid, yes);
optid = tidyOptGetIdForName("wrap");
tidyOptSetInt(tdoc, optid, 0);
ok = tidyOptSetBool(tdoc, TidyXhtmlOut, yes); // Convert to XHTML
if (ok) { rc = tidySetErrorBuffer(tdoc, &errbuf); } // Capture diagnostics
if (rc >= 0) { rc = tidyParseString(tdoc, input.c_str()); } // Parse the input
if (rc >= 0) { rc = tidyCleanAndRepair(tdoc); } // Tidy it up!
if (rc >= 0) { rc = tidyRunDiagnostics(tdoc); } // Kvetch
if (rc > 1) { rc = (tidyOptSetBool(tdoc, TidyForceOutput, yes) ? rc : -1 ); } // If error, force output.
if (rc >= 0) { rc = tidySaveBuffer(tdoc, &outbuf); } // Pretty Print
if (rc >= 0) {
char tmp[outbuf.size + 1];
memcpy(tmp, outbuf.bp, outbuf.size);
tmp[outbuf.size] = '\0';
*output = tmp;
} else {
printf("A severe error (\%d) occurred.\\n", rc);
}
tidyBufFree(&outbuf);
tidyBufFree(&errbuf);
tidyRelease(tdoc);
return rc;
}