| 1 |
|
| #include "EXTERN.h" |
| 2 |
|
| #include "perl.h" |
| 3 |
|
| #include "XSUB.h" |
| 4 |
|
| |
| 5 |
|
| #include <tidy.h> |
| 6 |
|
| #include <tidybuffio.h> |
| 7 |
|
| #include <stdio.h> |
| 8 |
|
| #include <errno.h> |
| 9 |
|
| |
| 10 |
|
| |
| 11 |
|
| static void |
| 12 |
34 |
| _load_config_hash(TidyDoc tdoc, HV *tidy_options) |
| 13 |
|
| { |
| 14 |
|
| HE *entry; |
| 15 |
|
| |
| 16 |
34 |
| (void) hv_iterinit(tidy_options); |
| 17 |
|
| |
| 18 |
64 |
▶ | while ( (entry = hv_iternext(tidy_options)) != NULL ) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ( (entry = hv_iternext(tidy_options)) != NULL ) { |
30 |
34 |
64 |
|
| 19 |
|
| I32 key_len; |
| 20 |
|
| |
| 21 |
30 |
| const char * const key = hv_iterkey(entry,&key_len); |
| 22 |
30 |
| const TidyOption opt = tidyGetOptionByName(tdoc,key); |
| 23 |
|
| |
| 24 |
30 |
▶ | if (!opt) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!opt) { |
0 |
30 |
30 |
|
| 25 |
0 |
| warn( "HTML::Tidy: Unrecognized option: \"%s\"\n",key ); |
| 26 |
|
| } |
| 27 |
|
| else { |
| 28 |
30 |
| const TidyOptionId id = tidyOptGetId(opt); |
| 29 |
30 |
| SV * const sv_data = hv_iterval(tidy_options,entry); |
| 30 |
|
| STRLEN data_len; |
| 31 |
30 |
| const char * const data = SvPV(sv_data,data_len); |
| 32 |
|
| |
| 33 |
30 |
▶ | if ( ! tidyOptSetValue(tdoc,id,data) ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( ! tidyOptSetValue(tdoc,id,data) ) { |
0 |
30 |
30 |
|
| 34 |
0 |
| warn( "HTML::Tidy: Can't set option: \"%s\" to \"%s\"\n", key, data ); |
| 35 |
|
| } |
| 36 |
|
| } |
| 37 |
|
| } |
| 38 |
34 |
| } |
| 39 |
|
| MODULE = HTML::T5 PACKAGE = HTML::T5 |
| 40 |
|
| |
| 41 |
|
| PROTOTYPES: ENABLE |
| 42 |
|
| |
| 43 |
|
| void |
| 44 |
|
| _tidy_messages(input, configfile, tidy_options) |
| 45 |
|
| INPUT: |
| 46 |
|
| const char *input |
| 47 |
|
| const char *configfile |
| 48 |
|
| HV *tidy_options |
| 49 |
|
| PREINIT: |
| 50 |
21 |
| TidyBuffer errbuf = {0}; |
| 51 |
21 |
| TidyDoc tdoc = tidyCreate(); /* Initialize "document" */ |
| 52 |
|
| const char* newline; |
| 53 |
21 |
| int rc = 0; |
| 54 |
|
| PPCODE: |
| 55 |
21 |
| tidyBufInit(&errbuf); |
| 56 |
21 |
▶ | rc = ( tidyOptSetValue( tdoc, TidyCharEncoding, "utf8" ) ? rc : -1 ); |
Branch: 1 branch
| Branch | True | False | Total |
| rc = ( tidyOptSetValue( tdoc, TidyCharEncoding, "utf8" ) ? rc : -1 ); |
21 |
0 |
21 |
|
| 57 |
|
| |
| 58 |
21 |
▶ | if ( (rc >= 0 ) && configfile && *configfile ) { |
Branch: 3 branches
| Branch | True | False | Total |
| if ( (rc >= 0 ) && configfile && *configfile ) { |
21 |
0 |
21 |
| if ( (rc >= 0 ) && configfile && *configfile ) { |
21 |
0 |
21 |
| if ( (rc >= 0 ) && configfile && *configfile ) { |
1 |
20 |
21 |
|
| 59 |
1 |
| rc = tidyLoadConfig( tdoc, configfile ); |
| 60 |
|
| } |
| 61 |
|
| |
| 62 |
21 |
▶ | if ( rc >= 0 ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( rc >= 0 ) { |
21 |
0 |
21 |
|
| 63 |
21 |
| _load_config_hash(tdoc,tidy_options); |
| 64 |
|
| } |
| 65 |
|
| |
| 66 |
21 |
▶ | if ( rc >= 0 ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( rc >= 0 ) { |
21 |
0 |
21 |
|
| 67 |
|
| /* Capture diagnostics */ |
| 68 |
21 |
| rc = tidySetErrorBuffer( tdoc, &errbuf ); |
| 69 |
|
| } |
| 70 |
|
| |
| 71 |
21 |
▶ | if ( rc >= 0 ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( rc >= 0 ) { |
21 |
0 |
21 |
|
| 72 |
|
| /* Parse the input */ |
| 73 |
21 |
| rc = tidyParseString( tdoc, input ); |
| 74 |
|
| } |
| 75 |
|
| |
| 76 |
21 |
▶ | if ( rc >= 0 && errbuf.bp) { |
Branch: 2 branches
| Branch | True | False | Total |
| if ( rc >= 0 && errbuf.bp) { |
21 |
0 |
21 |
| if ( rc >= 0 && errbuf.bp) { |
19 |
2 |
21 |
|
| 77 |
19 |
▶ | XPUSHs( sv_2mortal(newSVpvn((char *)errbuf.bp, errbuf.size)) ); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs( sv_2mortal(newSVpvn((char *)errbuf.bp, errbuf.size)) ); |
0 |
19 |
19 |
|
| 78 |
|
| |
| 79 |
|
| /* TODO: Make this a function */ |
| 80 |
19 |
| switch ( tidyOptGetInt(tdoc,TidyNewline) ) { |
| 81 |
19 |
| case TidyLF: |
| 82 |
19 |
| newline = "\n"; |
| 83 |
19 |
| break; |
| 84 |
0 |
| case TidyCR: |
| 85 |
0 |
| newline = "\r"; |
| 86 |
0 |
| break; |
| 87 |
0 |
| default: |
| 88 |
0 |
| newline = "\r\n"; |
| 89 |
0 |
| break; |
| 90 |
|
| } |
| 91 |
19 |
▶ | XPUSHs( sv_2mortal(newSVpv(newline, 0)) ); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs( sv_2mortal(newSVpv(newline, 0)) ); |
0 |
19 |
19 |
|
| 92 |
|
| } |
| 93 |
|
| else { |
| 94 |
2 |
| rc = -1; |
| 95 |
|
| } |
| 96 |
|
| |
| 97 |
21 |
▶ | if ( errbuf.bp ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( errbuf.bp ) |
19 |
2 |
21 |
|
| 98 |
19 |
| tidyBufFree( &errbuf ); |
| 99 |
21 |
| tidyRelease( tdoc ); |
| 100 |
|
| |
| 101 |
21 |
▶ | if ( rc < 0 ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( rc < 0 ) { |
2 |
19 |
21 |
|
| 102 |
2 |
| XSRETURN_UNDEF; |
| 103 |
|
| } |
| 104 |
|
| |
| 105 |
|
| |
| 106 |
|
| void |
| 107 |
|
| _tidy_clean(input, configfile, tidy_options) |
| 108 |
|
| INPUT: |
| 109 |
|
| const char *input |
| 110 |
|
| const char *configfile |
| 111 |
|
| HV *tidy_options |
| 112 |
|
| PREINIT: |
| 113 |
13 |
| TidyBuffer errbuf = {0}; |
| 114 |
13 |
| TidyBuffer output = {0}; |
| 115 |
13 |
| TidyDoc tdoc = tidyCreate(); /* Initialize "document" */ |
| 116 |
|
| const char* newline; |
| 117 |
13 |
| int rc = 0; |
| 118 |
|
| PPCODE: |
| 119 |
13 |
| tidyBufInit(&output); |
| 120 |
13 |
| tidyBufInit(&errbuf); |
| 121 |
|
| /* Set our default first. */ |
| 122 |
|
| /* Don't word-wrap */ |
| 123 |
13 |
▶ | rc = ( tidyOptSetInt( tdoc, TidyWrapLen, 0 ) ? rc : -1 ); |
Branch: 1 branch
| Branch | True | False | Total |
| rc = ( tidyOptSetInt( tdoc, TidyWrapLen, 0 ) ? rc : -1 ); |
13 |
0 |
13 |
|
| 124 |
|
| |
| 125 |
13 |
▶ | if ( (rc >= 0 ) && configfile && *configfile ) { |
Branch: 3 branches
| Branch | True | False | Total |
| if ( (rc >= 0 ) && configfile && *configfile ) { |
13 |
0 |
13 |
| if ( (rc >= 0 ) && configfile && *configfile ) { |
13 |
0 |
13 |
| if ( (rc >= 0 ) && configfile && *configfile ) { |
2 |
11 |
13 |
|
| 126 |
2 |
| rc = tidyLoadConfig( tdoc, configfile ); |
| 127 |
|
| } |
| 128 |
|
| |
| 129 |
|
| /* XXX I think this cascade is a bug waiting to happen */ |
| 130 |
|
| |
| 131 |
13 |
▶ | if ( rc >= 0 ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( rc >= 0 ) { |
13 |
0 |
13 |
|
| 132 |
13 |
▶ | rc = ( tidyOptSetValue( tdoc, TidyCharEncoding, "utf8" ) ? rc : -1 ); |
Branch: 1 branch
| Branch | True | False | Total |
| rc = ( tidyOptSetValue( tdoc, TidyCharEncoding, "utf8" ) ? rc : -1 ); |
13 |
0 |
13 |
|
| 133 |
|
| } |
| 134 |
|
| |
| 135 |
13 |
▶ | if ( rc >= 0 ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( rc >= 0 ) { |
13 |
0 |
13 |
|
| 136 |
13 |
| _load_config_hash( tdoc, tidy_options ); |
| 137 |
|
| } |
| 138 |
|
| |
| 139 |
13 |
▶ | if ( rc >= 0 ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( rc >= 0 ) { |
13 |
0 |
13 |
|
| 140 |
13 |
| rc = tidySetErrorBuffer( tdoc, &errbuf ); /* Capture diagnostics */ |
| 141 |
|
| } |
| 142 |
|
| |
| 143 |
13 |
▶ | if ( rc >= 0 ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( rc >= 0 ) { |
13 |
0 |
13 |
|
| 144 |
13 |
| rc = tidyParseString( tdoc, input ); /* Parse the input */ |
| 145 |
|
| } |
| 146 |
|
| |
| 147 |
13 |
▶ | if ( rc >= 0 ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( rc >= 0 ) { |
13 |
0 |
13 |
|
| 148 |
13 |
| rc = tidyCleanAndRepair(tdoc); |
| 149 |
|
| } |
| 150 |
|
| |
| 151 |
13 |
▶ | if ( rc > 1 ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( rc > 1 ) { |
2 |
11 |
13 |
|
| 152 |
2 |
▶ | rc = ( tidyOptSetBool( tdoc, TidyForceOutput, yes ) ? rc : -1 ); |
Branch: 1 branch
| Branch | True | False | Total |
| rc = ( tidyOptSetBool( tdoc, TidyForceOutput, yes ) ? rc : -1 ); |
2 |
0 |
2 |
|
| 153 |
|
| } |
| 154 |
|
| |
| 155 |
13 |
▶ | if ( rc >= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( rc >= 0) { |
13 |
0 |
13 |
|
| 156 |
13 |
| rc = tidySaveBuffer( tdoc, &output ); |
| 157 |
|
| } |
| 158 |
|
| |
| 159 |
13 |
▶ | if ( rc >= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( rc >= 0) { |
13 |
0 |
13 |
|
| 160 |
13 |
| rc = tidyRunDiagnostics( tdoc ); |
| 161 |
|
| } |
| 162 |
|
| |
| 163 |
13 |
▶ | if ( rc >= 0 && output.bp && errbuf.bp ) { |
Branch: 3 branches
| Branch | True | False | Total |
| if ( rc >= 0 && output.bp && errbuf.bp ) { |
13 |
0 |
13 |
| if ( rc >= 0 && output.bp && errbuf.bp ) { |
13 |
0 |
13 |
| if ( rc >= 0 && output.bp && errbuf.bp ) { |
13 |
0 |
13 |
|
| 164 |
13 |
▶ | XPUSHs( sv_2mortal(newSVpvn((char *)output.bp, output.size)) ); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs( sv_2mortal(newSVpvn((char *)output.bp, output.size)) ); |
0 |
13 |
13 |
|
| 165 |
13 |
▶ | XPUSHs( sv_2mortal(newSVpvn((char *)errbuf.bp, errbuf.size)) ); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs( sv_2mortal(newSVpvn((char *)errbuf.bp, errbuf.size)) ); |
0 |
13 |
13 |
|
| 166 |
|
| |
| 167 |
|
| /* TODO: Hoist this into a function */ |
| 168 |
13 |
| switch ( tidyOptGetInt(tdoc,TidyNewline) ) { |
| 169 |
12 |
| case TidyLF: |
| 170 |
12 |
| newline = "\n"; |
| 171 |
12 |
| break; |
| 172 |
1 |
| case TidyCR: |
| 173 |
1 |
| newline = "\r"; |
| 174 |
1 |
| break; |
| 175 |
0 |
| default: |
| 176 |
0 |
| newline = "\r\n"; |
| 177 |
0 |
| break; |
| 178 |
|
| } |
| 179 |
13 |
▶ | XPUSHs( sv_2mortal(newSVpv(newline, 0)) ); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs( sv_2mortal(newSVpv(newline, 0)) ); |
0 |
13 |
13 |
|
| 180 |
|
| } |
| 181 |
|
| else { |
| 182 |
0 |
| rc = -1; |
| 183 |
|
| } |
| 184 |
|
| |
| 185 |
13 |
| tidyBufFree( &output ); |
| 186 |
13 |
| tidyBufFree( &errbuf ); |
| 187 |
13 |
| tidyRelease( tdoc ); |
| 188 |
|
| |
| 189 |
13 |
▶ | if ( rc < 0 ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( rc < 0 ) { |
0 |
13 |
13 |
|
| 190 |
0 |
| XSRETURN_UNDEF; |
| 191 |
|
| } |
| 192 |
|
| |
| 193 |
|
| |
| 194 |
|
| SV* |
| 195 |
|
| _tidy_library_version() |
| 196 |
|
| PREINIT: |
| 197 |
|
| const char* version; |
| 198 |
|
| CODE: |
| 199 |
3 |
| version = tidyLibraryVersion(); |
| 200 |
3 |
| RETVAL = newSVpv(version,0); /* will be automatically "mortalized" */ |
| 201 |
|
| OUTPUT: |
| 202 |
|
| RETVAL |