HTML Tidy  5.4.0
The HTACG Tidy HTML Project
Sample Program
1 #include <tidy.h>
2 #include <tidybuffio.h>
3 #include <stdio.h>
4 #include <errno.h>
5 
6 int main(int argc, char **argv )
7 {
8  const char* input = "<title>Foo</title><p>Foo!";
9  TidyBuffer output = {0};
10  TidyBuffer errbuf = {0};
11  int rc = -1;
12  Bool ok;
13 
14  TidyDoc tdoc = tidyCreate(); // Initialize "document"
15  printf( "Tidying:\t%s\n", input );
16 
17  ok = tidyOptSetBool( tdoc, TidyXhtmlOut, yes ); // Convert to XHTML
18  if ( ok )
19  rc = tidySetErrorBuffer( tdoc, &errbuf ); // Capture diagnostics
20  if ( rc >= 0 )
21  rc = tidyParseString( tdoc, input ); // Parse the input
22  if ( rc >= 0 )
23  rc = tidyCleanAndRepair( tdoc ); // Tidy it up!
24  if ( rc >= 0 )
25  rc = tidyRunDiagnostics( tdoc ); // Kvetch
26  if ( rc > 1 ) // If error, force output.
27  rc = ( tidyOptSetBool(tdoc, TidyForceOutput, yes) ? rc : -1 );
28  if ( rc >= 0 )
29  rc = tidySaveBuffer( tdoc, &output ); // Pretty Print
30 
31  if ( rc >= 0 )
32  {
33  if ( rc > 0 )
34  printf( "\nDiagnostics:\n\n%s", errbuf.bp );
35  printf( "\nAnd here is the result:\n\n%s", output.bp );
36  }
37  else
38  printf( "A severe error (%d) occurred.\n", rc );
39 
40  tidyBufFree( &output );
41  tidyBufFree( &errbuf );
42  tidyRelease( tdoc );
43  return rc;
44 }