From d923dd7b2dd89694c8a8508c6812ffbbd364618b Mon Sep 17 00:00:00 2001 From: Geoff McLane Date: Fri, 15 May 2015 15:06:52 +0200 Subject: [PATCH] Issue #108 - first cut new option --indent-with-tabs yes. --- include/tidyenum.h | 3 ++- src/config.c | 32 ++++++++++++++++++++++++++++++++ src/localize.c | 10 +++++++++- src/pprint.c | 21 ++++++++++++++++++--- src/pprint.h | 5 +++++ 5 files changed, 66 insertions(+), 5 deletions(-) diff --git a/include/tidyenum.h b/include/tidyenum.h index abf56e0..3b2e7c2 100644 --- a/include/tidyenum.h +++ b/include/tidyenum.h @@ -80,7 +80,7 @@ typedef enum typedef enum { TidyUnknownOption, /**< Unknown option! */ - TidyIndentSpaces, /**< Indentation n spaces */ + TidyIndentSpaces, /**< Indentation n spaces/tabs */ TidyWrapLen, /**< Wrap margin */ TidyTabSize, /**< Expand tabs to n spaces */ @@ -204,6 +204,7 @@ typedef enum TidySortAttributes, /**< Sort attributes */ TidyMergeSpans, /**< Merge multiple SPANs */ TidyAnchorAsName, /**< Define anchors as name attributes */ + TidyPPrintTabs, /**< Indent using tabs istead of spaces */ N_TIDY_OPTIONS /**< Must be last */ } TidyOptionId; diff --git a/src/config.c b/src/config.c index d92ddec..e05158f 100644 --- a/src/config.c +++ b/src/config.c @@ -204,6 +204,13 @@ static ParseProperty ParseDocType; /* keep-first or keep-last? */ static ParseProperty ParseRepeatAttr; +/*\ + * 20150515 - support using tabs instead of spaces - Issue #108 + * (a) parser for 't'/'f', 'true'/'false', 'y'/'n', 'yes'/'no' or '1'/'0' + * (b) sets the TidyIndentSpaces to 1 if 'yes' + * (c) sets the indent_char to '\t' or ' ' +\*/ +static ParseProperty ParseTabs; static const TidyOptionImpl option_defs[] = { @@ -313,6 +320,7 @@ static const TidyOptionImpl option_defs[] = { TidySortAttributes, PP, "sort-attributes", IN, TidySortAttrNone,ParseSorter, sorterPicks }, { TidyMergeSpans, MU, "merge-spans", IN, TidyAutoState, ParseAutoBool, autoBoolPicks }, { TidyAnchorAsName, MU, "anchor-as-name", BL, yes, ParseBool, boolPicks }, + { TidyPPrintTabs, PP, "indent-with-tabs", BL, no, ParseTabs, NULL }, /* 20150515 - Issue #108 */ { N_TIDY_OPTIONS, XX, NULL, XY, 0, NULL, NULL } }; @@ -1204,6 +1212,30 @@ Bool ParseCSS1Selector( TidyDocImpl* doc, const TidyOptionImpl* option ) return yes; } +/*\ + * 20150515 - support using tabs instead of spaces - Issue #108 + * Sets the indent character to a tab if on, and set indent space count to 1 + * and sets indent character to a space if off. +\*/ +Bool ParseTabs( TidyDocImpl* doc, const TidyOptionImpl* entry ) +{ + ulong flag = 0; + Bool status = ParseTriState( TidyNoState, doc, entry, &flag ); + if ( status ) { + Bool tabs = flag != 0 ? yes : no; + TY_(SetOptionBool)( doc, entry->id, tabs ); + if (tabs) { + TY_(PPrintTabs)(); + TY_(SetOptionInt)( doc, TidyIndentSpaces, 1 ); + } else { + TY_(PPrintSpaces)(); + /* optional - TY_(ResetOptionToDefault)( doc, TidyIndentSpaces ); */ + } + } + return status; +} + + /* Coordinates Config update and Tags data */ static void DeclareUserTag( TidyDocImpl* doc, TidyOptionId optId, UserTagType tagType, ctmbstr name ) diff --git a/src/localize.c b/src/localize.c index 0007406..f1c35ab 100755 --- a/src/localize.c +++ b/src/localize.c @@ -745,7 +745,7 @@ static const TidyOptionDoc option_docs[] = {TidyTabSize, "This option specifies the number of columns that Tidy uses between " "successive tab stops. It is used to map tabs to spaces when reading the " - "input. Tidy never outputs tabs. " + "input. " }, {TidyVertSpace, "This option specifies if Tidy should add some empty lines for " @@ -914,6 +914,14 @@ static const TidyOptionDoc option_docs[] = "is added along an existing id attribute if the DTD allows it. " "If set to \"no\", any existing name attribute is removed " "if an id attribute exists or has been added. " + }, + {TidyPPrintTabs, + "Set this option \"on\" to indent using tabs instead of the default " + "spaces. The option TidyIndentSpaces controls the number of tabs output " + "per level of indent, which is reset to 1, when this option is set on. " + "And of course, indent must be enabled for this to have any effect. " + "Note TidyTabSize controls converting input tabs to spaces. Set to zero " + "to retain input tabs. " }, {N_TIDY_OPTIONS, NULL diff --git a/src/pprint.c b/src/pprint.c index 64bb53c..6a50b7b 100644 --- a/src/pprint.c +++ b/src/pprint.c @@ -36,6 +36,21 @@ static int TextStartsWithWhitespace( Lexer *lexer, Node *node, uint start, uint static Bool InsideHead( TidyDocImpl* doc, Node *node ); static Bool ShouldIndent( TidyDocImpl* doc, Node *node ); +/*\ + * 20150515 - support using tabs instead of spaces - Issue #108 + * GH: https://github.com/htacg/tidy-html5/issues/108 - Keep indent with tabs #108 + * SF: https://sourceforge.net/p/tidy/feature-requests/3/ - #3 tabs in place of spaces +\*/ +static uint indent_char = ' '; +void TY_(PPrintTabs)(void) +{ + indent_char = '\t'; +} +void TY_(PPrintSpaces)(void) +{ + indent_char = ' '; +} + #if SUPPORT_ASIAN_ENCODINGS /* #431953 - start RJ Wraplen adjusted for smooth international ride */ @@ -582,7 +597,7 @@ static void WrapLine( TidyDocImpl* doc ) { uint spaces = GetSpaces( pprint ); for ( i = 0; i < spaces; ++i ) - TY_(WriteChar)( ' ', doc->docOut ); + TY_(WriteChar)( indent_char, doc->docOut ); /* 20150515 - Issue #108 */ } for ( i = 0; i < pprint->wraphere; ++i ) @@ -633,7 +648,7 @@ static void WrapAttrVal( TidyDocImpl* doc ) { uint spaces = GetSpaces( pprint ); for ( i = 0; i < spaces; ++i ) - TY_(WriteChar)( ' ', doc->docOut ); + TY_(WriteChar)( indent_char, doc->docOut ); /* 20150515 - Issue #108 */ } for ( i = 0; i < pprint->wraphere; ++i ) @@ -660,7 +675,7 @@ static void PFlushLineImpl( TidyDocImpl* doc ) { uint spaces = GetSpaces( pprint ); for ( i = 0; i < spaces; ++i ) - TY_(WriteChar)( ' ', doc->docOut ); + TY_(WriteChar)( indent_char, doc->docOut ); /* 20150515 - Issue #108 */ } for ( i = 0; i < pprint->linelen; ++i ) diff --git a/src/pprint.h b/src/pprint.h index f3fcbf2..4b36a21 100644 --- a/src/pprint.h +++ b/src/pprint.h @@ -84,5 +84,10 @@ void TY_(PPrintTree)( TidyDocImpl* doc, uint mode, uint indent, Node *node ); void TY_(PPrintXMLTree)( TidyDocImpl* doc, uint mode, uint indent, Node *node ); +/*\ + * 20150515 - support using tabs instead of spaces +\*/ +void TY_(PPrintTabs)(void); +void TY_(PPrintSpaces)(void); #endif /* __PPRINT_H__ */