From 34d456aa80cffea38d7b5819653a3471adb28006 Mon Sep 17 00:00:00 2001 From: Jim Derry Date: Sat, 28 Nov 2015 14:16:17 +0800 Subject: [PATCH 1/5] Make pretty printer keep track of line numbers as it prints. --- src/pprint.c | 9 ++++++++- src/pprint.h | 1 + 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/pprint.c b/src/pprint.c index 20900c7..2411156 100644 --- a/src/pprint.c +++ b/src/pprint.c @@ -312,6 +312,7 @@ void TY_(InitPrintBuf)( TidyDocImpl* doc ) InitIndent( &doc->pprint.indent[0] ); InitIndent( &doc->pprint.indent[1] ); doc->pprint.allocator = doc->allocator; + doc->pprint.line = 0; } void TY_(FreePrintBuf)( TidyDocImpl* doc ) @@ -613,6 +614,7 @@ static void WrapLine( TidyDocImpl* doc ) TY_(WriteChar)( '\\', doc->docOut ); TY_(WriteChar)( '\n', doc->docOut ); + pprint->line++; ResetLineAfterWrap( pprint ); } @@ -666,6 +668,7 @@ static void WrapAttrVal( TidyDocImpl* doc ) TY_(WriteChar)( ' ', doc->docOut ); TY_(WriteChar)( '\n', doc->docOut ); + pprint->line++; ResetLineAfterWrap( pprint ); } @@ -686,7 +689,7 @@ static void PFlushLineImpl( TidyDocImpl* doc ) for ( i = 0; i < pprint->linelen; ++i ) TY_(WriteChar)( pprint->linebuf[i], doc->docOut ); - + if ( IsInString(pprint) ) TY_(WriteChar)( '\\', doc->docOut ); ResetLine( pprint ); @@ -701,6 +704,7 @@ void TY_(PFlushLine)( TidyDocImpl* doc, uint indent ) PFlushLineImpl( doc ); TY_(WriteChar)( '\n', doc->docOut ); + pprint->line++; pprint->indent[ 0 ].spaces = indent; } @@ -713,6 +717,7 @@ static void PCondFlushLine( TidyDocImpl* doc, uint indent ) PFlushLineImpl( doc ); TY_(WriteChar)( '\n', doc->docOut ); + pprint->line++; pprint->indent[ 0 ].spaces = indent; } } @@ -733,6 +738,7 @@ void TY_(PFlushLineSmart)( TidyDocImpl* doc, uint indent ) /* Issue #228 - cfgBool( doc, TidyVertSpace ); */ if(TidyAddVS) { TY_(WriteChar)( '\n', doc->docOut ); + pprint->line++; } pprint->indent[ 0 ].spaces = indent; @@ -749,6 +755,7 @@ static void PCondFlushLineSmart( TidyDocImpl* doc, uint indent ) /* Issue #228 - cfgBool( doc, TidyVertSpace ); */ if(TidyAddVS) { TY_(WriteChar)( '\n', doc->docOut ); + pprint->line++; } pprint->indent[ 0 ].spaces = indent; diff --git a/src/pprint.h b/src/pprint.h index 4b36a21..8d473a4 100644 --- a/src/pprint.h +++ b/src/pprint.h @@ -53,6 +53,7 @@ typedef struct _TidyPrintImpl uint lbufsize; uint linelen; uint wraphere; + uint line; uint ixInd; TidyIndent indent[2]; /* Two lines worth of indent state */ From dcd8f16f7318debea87ce666d4dde7d61eaa96e0 Mon Sep 17 00:00:00 2001 From: Jim Derry Date: Sat, 28 Nov 2015 15:34:23 +0800 Subject: [PATCH 2/5] Tidying progress callback implemented. --- include/tidy.h | 15 +++++++++++++++ src/pprint.c | 7 +++++++ src/tidy-int.h | 1 + src/tidylib.c | 13 +++++++++++++ 4 files changed, 36 insertions(+) diff --git a/include/tidy.h b/include/tidy.h index 2f3dabd..bca1463 100755 --- a/include/tidy.h +++ b/include/tidy.h @@ -631,6 +631,9 @@ TIDY_EXPORT Bool TIDY_CALL tidyInitSink( TidyOutputSink* sink, TIDY_EXPORT void TIDY_CALL tidyPutByte( TidyOutputSink* sink, uint byteValue ); +/**************** + Errors +****************/ /** Callback to filter messages by diagnostic level: ** info, warning, etc. Just set diagnostic output ** handler to redirect all diagnostics output. Return true @@ -656,6 +659,18 @@ TIDY_EXPORT int TIDY_CALL tidySetErrorBuffer( TidyDoc tdoc, TidyBuffer* errb /** Set error sink to given generic sink */ TIDY_EXPORT int TIDY_CALL tidySetErrorSink( TidyDoc tdoc, TidyOutputSink* sink ); + +/**************** + Printing +****************/ +/** Callback to track the progress of the pretting printing process. +** +*/ +typedef Bool (TIDY_CALL *TidyPPProgress)( TidyDoc tdoc, uint line, uint col, uint destLine ); + +TIDY_EXPORT Bool TIDY_CALL tidySetPrettyPrinterCallback( TidyDoc tdoc, + TidyPPProgress callback ); + /** @} end IO group */ /* TODO: Catalog all messages for easy translation diff --git a/src/pprint.c b/src/pprint.c index 2411156..c42d2fc 100644 --- a/src/pprint.c +++ b/src/pprint.c @@ -2118,10 +2118,17 @@ void TY_(PPrintTree)( TidyDocImpl* doc, uint mode, uint indent, Node *node ) Node *content, *last; uint spaces = cfg( doc, TidyIndentSpaces ); Bool xhtml = cfgBool( doc, TidyXhtmlOut ); + uint lastline = 0; if ( node == NULL ) return; + if (doc->progressCallback) + { + if (doc->pprint.line > lastline) + doc->progressCallback( tidyImplToDoc(doc), node->line, node->column, doc->pprint.line ); + } + if (node->type == TextNode) { PPrintText( doc, mode, indent, node ); diff --git a/src/tidy-int.h b/src/tidy-int.h index f4a4703..51bcf3b 100755 --- a/src/tidy-int.h +++ b/src/tidy-int.h @@ -59,6 +59,7 @@ struct _TidyDocImpl TidyReportFilter mssgFilt; TidyReportFilter2 mssgFilt2; TidyOptCallback pOptCallback; + TidyPPProgress progressCallback; /* Parse + Repair Results */ uint optionErrors; diff --git a/src/tidylib.c b/src/tidylib.c index 0d7dc88..851a752 100755 --- a/src/tidylib.c +++ b/src/tidylib.c @@ -752,6 +752,19 @@ int TIDY_CALL tidySetErrorSink( TidyDoc tdoc, TidyOutputSink* sink ) return -EINVAL; } +/* Use TidyPPProgress to monitor the progress of the pretty printer. + */ +Bool TIDY_CALL tidySetPrettyPrinterCallback(TidyDoc tdoc, TidyPPProgress callback) +{ + TidyDocImpl* impl = tidyDocToImpl( tdoc ); + if ( impl ) + { + impl->progressCallback = callback; + return yes; + } + return no; +} + /* Document info */ int TIDY_CALL tidyStatus( TidyDoc tdoc ) From 4adc07fd659ec0d40864b04a94d85dd542d0e5d0 Mon Sep 17 00:00:00 2001 From: Jim Derry Date: Sat, 28 Nov 2015 15:43:34 +0800 Subject: [PATCH 3/5] Removed the one callback per line filter. Library user can filter this himself. --- src/pprint.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/pprint.c b/src/pprint.c index c42d2fc..775f148 100644 --- a/src/pprint.c +++ b/src/pprint.c @@ -2118,15 +2118,13 @@ void TY_(PPrintTree)( TidyDocImpl* doc, uint mode, uint indent, Node *node ) Node *content, *last; uint spaces = cfg( doc, TidyIndentSpaces ); Bool xhtml = cfgBool( doc, TidyXhtmlOut ); - uint lastline = 0; if ( node == NULL ) return; if (doc->progressCallback) { - if (doc->pprint.line > lastline) - doc->progressCallback( tidyImplToDoc(doc), node->line, node->column, doc->pprint.line ); + doc->progressCallback( tidyImplToDoc(doc), node->line, node->column, doc->pprint.line ); } if (node->type == TextNode) From 3b8ad7482e34a2dc109e9df34c8d0a38b3909db0 Mon Sep 17 00:00:00 2001 From: Jim Derry Date: Sat, 28 Nov 2015 16:02:35 +0800 Subject: [PATCH 4/5] This is probably best as void. --- include/tidy.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/tidy.h b/include/tidy.h index bca1463..8a30d9f 100755 --- a/include/tidy.h +++ b/include/tidy.h @@ -666,7 +666,7 @@ TIDY_EXPORT int TIDY_CALL tidySetErrorSink( TidyDoc tdoc, TidyOutputSink* si /** Callback to track the progress of the pretting printing process. ** */ -typedef Bool (TIDY_CALL *TidyPPProgress)( TidyDoc tdoc, uint line, uint col, uint destLine ); +typedef void (TIDY_CALL *TidyPPProgress)( TidyDoc tdoc, uint line, uint col, uint destLine ); TIDY_EXPORT Bool TIDY_CALL tidySetPrettyPrinterCallback( TidyDoc tdoc, TidyPPProgress callback ); From 873794162ae45356e77a2dce5a3fd2079faad9dc Mon Sep 17 00:00:00 2001 From: Jim Derry Date: Sun, 29 Nov 2015 07:39:33 +0800 Subject: [PATCH 5/5] Callback added to XML printer, too; fixed off-by-one error. --- src/pprint.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/pprint.c b/src/pprint.c index 775f148..bb7eccc 100644 --- a/src/pprint.c +++ b/src/pprint.c @@ -2124,7 +2124,7 @@ void TY_(PPrintTree)( TidyDocImpl* doc, uint mode, uint indent, Node *node ) if (doc->progressCallback) { - doc->progressCallback( tidyImplToDoc(doc), node->line, node->column, doc->pprint.line ); + doc->progressCallback( tidyImplToDoc(doc), node->line, node->column, doc->pprint.line + 1 ); } if (node->type == TextNode) @@ -2391,6 +2391,11 @@ void TY_(PPrintXMLTree)( TidyDocImpl* doc, uint mode, uint indent, Node *node ) if (node == NULL) return; + if (doc->progressCallback) + { + doc->progressCallback( tidyImplToDoc(doc), node->line, node->column, doc->pprint.line + 1 ); + } + if ( node->type == TextNode) { PPrintText( doc, mode, indent, node );