From f4c64966f021c963bf147f21d9ebc95d17a779ec Mon Sep 17 00:00:00 2001 From: Jim Derry Date: Sat, 26 Aug 2017 12:47:18 -0400 Subject: [PATCH] Added TidyConfigCallback and deprecated TidyOptCallback for consistency with the remainder of the callbacks. TidyConfigCallback is now given a reference to the instance of the TidyDoc that caused the callback to occur. + TidyConfigCallback --- include/tidy.h | 28 ++++++++++++++++++++++++++++ src/config.c | 12 ++++++++++-- src/tidy-int.h | 1 + src/tidylib.c | 11 +++++++++++ 4 files changed, 50 insertions(+), 2 deletions(-) diff --git a/include/tidy.h b/include/tidy.h index a29e263..bc83b79 100644 --- a/include/tidy.h +++ b/include/tidy.h @@ -548,6 +548,8 @@ TIDY_EXPORT int TIDY_CALL tidySetOutCharEncoding(TidyDoc tdoc, /**< The /** This typedef represents the required signature for your provided callback ** function should you wish to register one with tidySetOptionCallback(). ** Your callback function will be provided with the following parameters. + ** Note that this is deprecated and you should instead migrate to + ** tidySetConfigCallback(). ** @param option The option name that was provided. ** @param value The option value that was provided ** @return Your callback function will return `yes` if it handles the provided @@ -560,12 +562,38 @@ typedef Bool (TIDY_CALL *TidyOptCallback)(ctmbstr option, ctmbstr value); ** configuration file options. Setting this callback allows a LibTidy ** application developer to examine command-line and configuration file options ** after LibTidy has examined them and failed to recognize them. + ** Note that this is deprecated and you should instead migrate to + ** tidySetConfigCallback(). ** @result Returns `yes` upon success. */ TIDY_EXPORT Bool TIDY_CALL tidySetOptionCallback(TidyDoc tdoc, /**< The document to apply the callback to. */ TidyOptCallback pOptCallback /**< The name of a function of type TidyOptCallback() to serve as your callback. */ ); +/** This typedef represents the required signature for your provided callback + ** function should you wish to register one with tidySetOptionCallback(). + ** Your callback function will be provided with the following parameters. + ** @param tdoc The document instance for which the callback was invoked. + ** @param option The option name that was provided. + ** @param value The option value that was provided + ** @return Your callback function will return `yes` if it handles the provided + ** option, or `no` if it does not. In the latter case, Tidy will issue + ** an unknown configuration option error. + */ +typedef Bool (TIDY_CALL *TidyConfigCallback)(TidyDoc tdoc, ctmbstr option, ctmbstr value); + +/** Applications using TidyLib may want to augment command-line and + ** configuration file options. Setting this callback allows a LibTidy + ** application developer to examine command-line and configuration file options + ** after LibTidy has examined them and failed to recognize them. + ** Note that this is deprecated and you should instead migrate to + ** tidySetConfigCallback(). + ** @result Returns `yes` upon success. + */ +TIDY_EXPORT Bool TIDY_CALL tidySetConfigCallback(TidyDoc tdoc, /**< The document to apply the callback to. */ + TidyConfigCallback pConfigCallback /**< The name of a function of type TidyConfigCallback() to serve as your callback. */ + ); + /** @} ** @name Option ID Discovery ** @{ diff --git a/src/config.c b/src/config.c index f47e452..cd1d20c 100644 --- a/src/config.c +++ b/src/config.c @@ -817,13 +817,14 @@ int TY_(ParseConfigFileEnc)( TidyDocImpl* doc, ctmbstr file, ctmbstr charenc ) option->parser( doc, option ); else { - if (NULL != doc->pOptCallback) + if ( (NULL != doc->pOptCallback) || (NULL != doc->pConfigCallback) ) { TidyConfigImpl* cfg = &doc->config; tmbchar buf[8192]; uint i = 0; tchar delim = 0; Bool waswhite = yes; + Bool response = yes; tchar c = SkipWhite( cfg ); @@ -854,7 +855,14 @@ int TY_(ParseConfigFileEnc)( TidyDocImpl* doc, ctmbstr file, ctmbstr charenc ) c = AdvanceChar( cfg ); } buf[i] = '\0'; - if (no == (*doc->pOptCallback)( name, buf )) + + if ( doc->pOptCallback ) + response = response && (*doc->pOptCallback)( name, buf ); + + if ( doc->pConfigCallback ) + response = response && (*doc->pConfigCallback)( tidyImplToDoc(doc), name, buf ); + + if (response == no) TY_(ReportUnknownOption)( doc, name ); } else diff --git a/src/tidy-int.h b/src/tidy-int.h index 7973c74..6bcfaf5 100755 --- a/src/tidy-int.h +++ b/src/tidy-int.h @@ -60,6 +60,7 @@ struct _TidyDocImpl TidyReportCallback reportCallback; TidyMessageCallback messageCallback; TidyOptCallback pOptCallback; + TidyConfigCallback pConfigCallback; TidyPPProgress progressCallback; /* Parse + Repair Results */ diff --git a/src/tidylib.c b/src/tidylib.c index 00906ca..9d7c0a2 100755 --- a/src/tidylib.c +++ b/src/tidylib.c @@ -232,6 +232,17 @@ Bool TIDY_CALL tidySetOptionCallback( TidyDoc tdoc, TidyOptCallback pOptC return no; } +Bool TIDY_CALL tidySetConfigCallback(TidyDoc tdoc, TidyConfigCallback pConfigCallback) +{ + TidyDocImpl* impl = tidyDocToImpl( tdoc ); + if ( impl ) + { + impl->pConfigCallback = pConfigCallback; + return yes; + } + return no; +} + int TIDY_CALL tidyLoadConfig( TidyDoc tdoc, ctmbstr cfgfil ) {