WIP with functioning concept.

This commit is contained in:
Jim Derry 2017-10-06 16:14:54 -04:00
parent 4e6d76eb01
commit 2e2062ddec

View file

@ -303,6 +303,16 @@ static const TidyOptionImpl option_defs[] =
}; };
/* List of deprecated options and their replacements. */
static const struct {
ctmbstr name; /**< name of the deprecated option */
TidyOptionId replacementId; /**< Id of the replacement option, or 0 if none. */
} deprecatedOptions[] = {
{ "show-body-only", TidyBodyOnly }, /* WIP, for development purposes! */
{ NULL }
};
/* Should only be called by options set by name /* Should only be called by options set by name
** thus, it is cheaper to do a few scans than set ** thus, it is cheaper to do a few scans than set
** up every option in a hash table. ** up every option in a hash table.
@ -492,6 +502,72 @@ static void ReparseTagDecls( TidyDocImpl* doc, uint changedUserTags )
REPARSE_USERTAGS(TidyPreTags,tagtype_pre); REPARSE_USERTAGS(TidyPreTags,tagtype_pre);
} }
/* returns the option id of the replacement Tidy option for optName. */
static TidyOptionId getOptionReplacement( ctmbstr optName )
{
uint i = 0;
ctmbstr testName;
while ( (testName = deprecatedOptions[i].name) )
{
if ( TY_(tmbstrcasecmp)( optName, testName ) == 0 )
return deprecatedOptions[i].replacementId;
i++;
}
return N_TIDY_OPTIONS;
}
/* indicates whether or not optName is deprecated */
static Bool isOptionDeprecated( ctmbstr optName )
{
return getOptionReplacement( optName ) != N_TIDY_OPTIONS;
}
/* forward declaration */
Bool GetPickListValue();
/* substitute the new option for the deprecated one. */
static Bool subDeprecatedOption( TidyDocImpl* doc, ctmbstr oldName, ctmbstr oldValue)
{
TidyOptionId newOptId = getOptionReplacement( oldName );
if ( newOptId == TidyUnknownOption )
{
printf("%s has been removed, and there is no replacement.\n", oldName);
return no;
}
if ( TY_(tmbstrcasecmp)( oldName, "show-body-only" ) == 0 )
{
uint value;
/* `show-body-only` used to use the boolPicks */
if ( GetPickListValue( oldValue, boolPicks, &value ) )
{
if ( value == TidyNoState )
{
printf("show-body-only is deprecated; use show-markup. No action taken.\n");
TY_(SetOptionBool)( doc, newOptId, value );
}
else
{
TY_(SetOptionBool)( doc, newOptId, value );
printf("show-body-only has been removed; use show-markup, which has been set to `body-only`.\n");
}
}
else
{
printf("Report bad argument %s\n", oldValue);
}
return yes;
}
return no;
}
void TY_(ResetConfigToDefault)( TidyDocImpl* doc ) void TY_(ResetConfigToDefault)( TidyDocImpl* doc )
{ {
uint ixVal; uint ixVal;
@ -875,20 +951,24 @@ int TY_(ParseConfigFileEnc)( TidyDocImpl* doc, ctmbstr file, ctmbstr charenc )
Bool TY_(ParseConfigOption)( TidyDocImpl* doc, ctmbstr optnam, ctmbstr optval ) Bool TY_(ParseConfigOption)( TidyDocImpl* doc, ctmbstr optnam, ctmbstr optval )
{ {
const TidyOptionImpl* option = TY_(lookupOption)( optnam ); const TidyOptionImpl* option = TY_(lookupOption)( optnam );
Bool status = ( option != NULL ); Bool isDeprecated = isOptionDeprecated( optnam );
Bool status = ( option != NULL ) && !isDeprecated;
if ( !status ) if ( !status )
{ {
/* Not a standard tidy option. Check to see if the user application /* Unknown or deprecated, so check to see if the user application
recognizes it */ wants to deal with it first. */
if (NULL != doc->pOptCallback) if (NULL != doc->pOptCallback)
status = (*doc->pOptCallback)( optnam, optval ); status = (*doc->pOptCallback)( optnam, optval );
if (NULL != doc->pConfigCallback ) if (NULL != doc->pConfigCallback )
status = status && (*doc->pConfigCallback)( tidyImplToDoc(doc), optnam, optval ); status = status && (*doc->pConfigCallback)( tidyImplToDoc(doc), optnam, optval );
if (!status && isDeprecated)
status = subDeprecatedOption( doc, optnam, optval);
if (!status) if (!status)
TY_(ReportUnknownOption)( doc, optnam ); TY_(ReportUnknownOption)( doc, optnam );
} }
else else
status = TY_(ParseConfigValue)( doc, option->id, optval ); status = TY_(ParseConfigValue)( doc, option->id, optval );
return status; return status;
} }
@ -1236,6 +1316,33 @@ Bool ParseCSS1Selector( TidyDocImpl* doc, const TidyOptionImpl* option )
return yes; return yes;
} }
/* Given a string, return the picklist value from an arbitrary picklist. */
Bool GetPickListValue( ctmbstr value, PickListItems* pickList, uint *result )
{
const PickListItem *item = NULL;
uint ix = 0;
while ( (item = &(*pickList)[ ix ]) && item->label )
{
ctmbstr input;
uint i = 0;
while ( ( input = &(*item->inputs[i]) ) )
{
if (TY_(tmbstrcasecmp)(value, input) == 0 )
{
*result = ix;
return yes;
}
++i;
}
++ix;
}
return no;
}
/* A general parser for anything using pick lists. This provides the engine to /* A general parser for anything using pick lists. This provides the engine to
determine the proper option value, and can be used by parsers in addition to determine the proper option value, and can be used by parsers in addition to
ParsePickList that require special handling. ParsePickList that require special handling.
@ -1246,33 +1353,20 @@ Bool GetParsePickListValue( TidyDocImpl* doc, const TidyOptionImpl* entry, uint
tchar c = SkipWhite( cfg ); tchar c = SkipWhite( cfg );
tmbchar work[ 16 ] = {0}; tmbchar work[ 16 ] = {0};
tmbstr cp = work, end = work + sizeof(work); tmbstr cp = work, end = work + sizeof(work);
const PickListItem *item = NULL;
uint ix = 0;
while ( c!=EndOfStream && cp < end && !TY_(IsWhite)(c) && c != '\r' && c != '\n' ) while ( c!=EndOfStream && cp < end && !TY_(IsWhite)(c) && c != '\r' && c != '\n' )
{ {
*cp++ = (tmbchar) c; *cp++ = (tmbchar) c;
c = AdvanceChar( cfg ); c = AdvanceChar( cfg );
} }
while ( (item = &(*entry->pickList)[ ix ]) && item->label ) if ( GetPickListValue( work, entry->pickList, result ) != yes )
{ {
ctmbstr input; TY_(ReportBadArgument)( doc, entry->name );
uint i = 0; return no;
while ( ( input = &(*item->inputs[i]) ) )
{
if (TY_(tmbstrcasecmp)(work, input) == 0 )
{
*result = ix;
return yes;
}
++i;
}
++ix;
} }
TY_(ReportBadArgument)( doc, entry->name ); return yes;
return no;
} }