PDA

View Full Version : Static libs Libtidy on QT4 clean html/xml



patrik08
27th May 2006, 12:39
I have build successful libtidy on qt4

Now how i cann translate this C++ sample to make

bool Clean_Code_utf8(QString incomming_file, QString output_file, QString config_file)

Have anibody experience on tidy? I understand tidy only from php....




#include <tidy.h>
#include <buffio.h>
#include <stdio.h>
#include <errno.h>


int main(int argc, char **argv )
{
const char* input = "<title>Foo</title><p>Foo!";
TidyBuffer output = {0};
TidyBuffer errbuf = {0};
int rc = -1;
Bool ok;

TidyDoc tdoc = tidyCreate(); // Initialize "document"
printf( "Tidying:\t%s\n", input );

ok = tidyOptSetBool( tdoc, TidyXhtmlOut, yes ); // Convert to XHTML
if ( ok )
rc = tidySetErrorBuffer( tdoc, &errbuf ); // Capture diagnostics
if ( rc >= 0 )
rc = tidyParseString( tdoc, input ); // Parse the input
if ( rc >= 0 )
rc = tidyCleanAndRepair( tdoc ); // Tidy it up!
if ( rc >= 0 )
rc = tidyRunDiagnostics( tdoc ); // Kvetch
if ( rc > 1 ) // If error, force output.
rc = ( tidyOptSetBool(tdoc, TidyForceOutput, yes) ? rc : -1 );
if ( rc >= 0 )
rc = tidySaveBuffer( tdoc, &output ); // Pretty Print

if ( rc >= 0 )
{
if ( rc > 0 )
printf( "\nDiagnostics:\n\n%s", errbuf.bp );
printf( "\nAnd here is the result:\n\n%s", output.bp );
}
else
printf( "A severe error (%d) occurred.\n", rc );

tidyBufFree( &output );
tidyBufFree( &errbuf );
tidyRelease( tdoc );
return rc;
}


Subversion dir...
svn co http://ciz.ch/svnciz/forms_shop/lib_tidy_src/ libtydy
Compile ok on window...


pro file ....


DEPENDPATH += . include
INCLUDEPATH += . include
TEMPLATE =lib
CONFIG += qt warn_off release staticlib
LANGUAGE = C++
#DEFINES -= UNICODE
DEFINES += NDEBUG THREAD_SAFE=1 TEMP_STORE=2

DESTDIR = ../all_os_libs/
win32:TARGET = tidy
unix:TARGET = tidy
mac:TARGET = tidy

rickbsgu
27th May 2006, 19:06
Fundamentally, assuming the code is working, you simply need to re-encapsulate it in a function, open the "incomming_file", bring it into a buffer and feed it to tidy. Then take the output buffer and write it out to the "output_file" specified in the second arg.

What's the "config_file" arg for? If it contains tidy configuration directives (projecting, here), you'll have to open that up, parse out the directives, and then set up a construct to set the directives found.

rickb

patrik08
27th May 2006, 20:53
tidy config file... preference html or xml and so show-warnings: no ....

so i can parse html or xml utf8 or iso...


// sample config file for HTML tidy
indent: auto
indent-spaces: 2
wrap: 72
markup: yes
output-xml: no
input-xml: no
show-warnings: yes
numeric-entities: yes
quote-marks: yes
quote-nbsp: yes
quote-ampersand: no
break-before-br: no
uppercase-tags: no
uppercase-attributes: no
char-encoding: latin1
new-inline-tags: cfif, cfelse, math, mroot,
mrow, mi, mn, mo, msqrt, mfrac, msubsup, munderover,
munder, mover, mmultiscripts, msup, msub, mtext,
mprescripts, mtable, mtr, mtd, mth
new-blocklevel-tags: cfoutput, cfquery
new-empty-tags: cfelse


Tanks i try .... if a problem i post on c++ forum...

rickbsgu
28th May 2006, 02:33
Exactly on the config file.

Make sure you also test for invalid configuration keys, so you can flag up error.

rickb

patrik08
28th May 2006, 09:32
user not write config file....
Is a CMS - CRM ... and must have clean xml report to xslt's (pdfs) and clean xhtml to webpage...

But i think is possibel to make chekbox GUI to write a new config file...

patrik08
29th May 2006, 09:10
Is running .... and clean file.... so....



/* similar command http://php.net/tidy */
Tidy::Tidy()
{
status = 0;
ErrorMSG ="";
doc = tidyCreate();
}

/* prepare config tidy config file */
bool Tidy::SetUp(QString configfile)
{
qDebug() << "### load config.... " << configfile;
ConfigFileTidy = configfile;
status = tidyLoadConfig( doc, qtchar(configfile) ); /* http://ch2.php.net/manual/de/function.tidy-load-config.php */
if ( status != 0 ) {
ErrorMSG ="Not possibel to load Config File!";
return false;
} else {
return true;
}
}
/* QString clean file inputfile and put to outfile */
bool Tidy::CleanTidy(QString inputfile, QString outfile)
{
qDebug() << "### load inputfile.... " << inputfile;
qDebug() << "### load outfile.... " << outfile;
if (!ConfigFileTidy.size() > 0 && status != 0) {
ErrorMSG ="Set up a config file!";
return false;
}
Bool ok;
int rc = -1;
/*TidyBuffer output;*/
TidyBuffer errbuf;
QString resultwork;
QString inside = "";
QFile file(inputfile);
if (file.exists()) {
if (file.open(QFile::ReadOnly | QFile::Text)) {
inside =file.readAll();
file.close();
}
} else {
ErrorMSG ="No input file found!";
}
QByteArray ba = inside.toAscii();
/*qDebug() << "### load input.... " << ba.data();*/

qDebug() << "### steep 1 ok ";
rc = tidyParseString( doc, ba.data() ); /* http://ch2.php.net/manual/de/function.tidy-parse-string.php*/
qDebug() << "### steep 1a ok " << rc; /* rc become error message */
if ( rc > 0 ) {
ErrorMSG ="Success";
qDebug() << "### steep 2 ok ";
rc = tidySaveFile( doc, qtchar(outfile) );
qDebug() << "### steep 3 ok " << rc;
tidyRelease( doc );
return true;
}


/*tidyBufFree( &errbuf );*/
tidyRelease( doc );
return false;
}
/* QString to const char* */
const char* Tidy::qtchar( QString xml )
{
QByteArray ba = xml.toAscii();
const char* hack = ba.data();
return hack;
}

rickbsgu
29th May 2006, 16:04
Great that you got it working.

Just looking over the code, I don't see where the configfile is used. It doesn't look like you're processing the directives or making set calls, anywhere.

Maybe it's not necessary?

Oops - on re-examination, I see where it's getting submitted to the tidy object.
Never mind.

Carry on,
rickb

patrik08
29th May 2006, 17:28
Yes ist not need .... default file ...
Each config have a default value ...

numeric-entities: yes ..... ecc...

If You chanche default value .... is possibel to write each line ... same as php....
but I wand to add & write my own tag 18 pieces.... xml / xslt and a list is long.... so i make a file....

default value i found here.... http://perens.com/Slides/Copenhagen0/tidy.conf