𝌔 Fossil Hub
Login | Fossil manual |
phptags tag tidier

Check-in [9d41f01461]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add crude -Unicode whitespace (NBSP, NEL, LS, PS) detection.
Timelines: family | ancestors | trunk
Files: files | file ages | folders
SHA1:9d41f014619ff632d85296a4766ef68e107d6bd2
User & Date: mario 2014-11-28 11:47:56
Context
2014-11-28
11:47
Add crude -Unicode whitespace (NBSP, NEL, LS, PS) detection. Leaf check-in: 9d41f01461 user: mario tags: trunk
11:47
commit 1.2 NEWS date check-in: 17edfccd92 user: mario tags: trunk
Changes

Changes to phptags.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
..
71
72
73
74
75
76
77
78

79
80
81
82
83
84
85
...
115
116
117
118
119
120
121

122
123
124
125
126
127
128
...
179
180
181
182
183
184
185




186
187
188
189
190
191
192
#!/usr/bin/php -qCdshort_open_tag=1 
<?php
/**
 * api: cli
 * type: application
 * title: PHP tag tidier
 * description: Rewrites PHP short/long open tags, close tags, fixes whitespace padding
 * version: 1.2
 * license: Public Domain
 * author: mario <mario#include-once:org>
 * architecture: all
 * category: utilities
 * config: <file type="array" value="$HOME/.config/php/phptags.php" title="configuration defaults file" description="an ordinary return(array(...)); script to set interna options like regex=>1 or verbose=>1" />
 * url: http://freshcode.club/projects/phptags
 * pack: phptags=/usr/bin/phptags, *.1=/usr/share/man/man1/phptags.1,
................................................................................
        "php54" => argv("-54", "-php54", "--php54"),      # don't rewrite <?= for PHP 5.4
        "php7" => argv("-7", "-php7", "--php7"),      # test for <% ASP %> and <script language=PHP> tags
        "short" => argv("-s", "-short", "--short", "--short-open"),
        "shortall" => argv("-a", "-all", "--all", "-shortall", "--shortall", "--short-all", "-sa"),
        "unclosed" => argv("-u", "-unclosed", "--unclosed", "--remove-closing", "--unclose", "--open", "--opened"),
        "close" => argv("-c", "-close", "--close", "-closed", "--closed", "--add-closing"),
        "white" => argv("-w", "-white", "--white", "--whitespace", "-ws", "--ws", "-space", "--space", "--fix-whitespace", "--bom"),
        "warn" => argv("-W", "-warn", "--warn", "--warning"),

//"html" => argv("-H", "-html", "--html", "-text", "--text", "-content", "--content"), # warn also about plain HTML outside of PHP code
        "recursive" => true + argv("-r"),        # doing that anyway
        "regex" => argv("--regex", "--rx", "-rx"),   # use regex
        "token" => argv("--tokenizer", "--token", "-t"),# use tokenizer
        "color" => argv("-c", "--color", "--ansi") xor isset($_SERVER["TERM"]),    # colorize some output
        "quiet" => argv("--quiet", "-quiet", "-q"),
        "verbose" => argv("--verbose", "-verbose", "-v"),
................................................................................
   -W  --warn      Just warn about whitespace issues.
   -t  --token     Use tokenizer for --short and --long conversion. (More diligent than --regex mode, but doesn't preserve indentation as well.)
   -h  --help      This very helpful help text.
   -v  --verbose   Extra output, use -h -v for all options.\n\n
HELP;
   $action->verbose and print <<<VERBOSE
More options:

   -D  --debug     Development notices.
   -d  --dry       Dry run, don't update files.
   -n  --new       Create file.php.new for updated files.
   -b  --backup    Create file.php~ backup files on edits.
   -54 --php54     Keep <?= always for --long conversion.
   -7  --php7      Probe for ASP style <% and super long PHP <script> tags. Use in --warn mode, no rewriting is attempted (too rare).
   -r  --recursive Traversing subdirectories is enabled per default.
................................................................................
            preg_test("/\?\>(?!\\n<)\s+<\?/", $src, "Consecutive open+close tags with spacing (template?)")
            or preg_test("/\?\>\\r?\\n\<\?/", $src, "Consecutive open+close tags with harmless linebreak");

            // Warn about ambigious PHP tags like `<?print(123)`
            preg_test("/[\s\pZ]+\<\?((?!php|=|xml[-\s]|\w+:\w+)\w++)/i", $src, "Ambiguous PHP tag or unknown XML PI");
        }






        // remove whitespace
        if ($action->white) {
            // spaces after ? >
            preg_modify("/(\?\>)[\s\pZ\\0]+(\z)/", "$1$2", $src, "Removed trailing whitespace");
            // UTF-8 BOM before < ? - but retains spaces until next rule
            preg_modify("/^\\xEF\\xBB\\xBF([\s\pZ\\0]*<\?)(php|\W)/i", "$1$2", $src, "Removed leading UTF-8 BOM");







|







 







|
>







 







>







 







>
>
>
>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
..
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
...
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
...
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/php -qCdshort_open_tag=1 
<?php
/**
 * api: cli
 * type: application
 * title: PHP tag tidier
 * description: Rewrites PHP short/long open tags, close tags, fixes whitespace padding
 * version: 1.3
 * license: Public Domain
 * author: mario <mario#include-once:org>
 * architecture: all
 * category: utilities
 * config: <file type="array" value="$HOME/.config/php/phptags.php" title="configuration defaults file" description="an ordinary return(array(...)); script to set interna options like regex=>1 or verbose=>1" />
 * url: http://freshcode.club/projects/phptags
 * pack: phptags=/usr/bin/phptags, *.1=/usr/share/man/man1/phptags.1,
................................................................................
        "php54" => argv("-54", "-php54", "--php54"),      # don't rewrite <?= for PHP 5.4
        "php7" => argv("-7", "-php7", "--php7"),      # test for <% ASP %> and <script language=PHP> tags
        "short" => argv("-s", "-short", "--short", "--short-open"),
        "shortall" => argv("-a", "-all", "--all", "-shortall", "--shortall", "--short-all", "-sa"),
        "unclosed" => argv("-u", "-unclosed", "--unclosed", "--remove-closing", "--unclose", "--open", "--opened"),
        "close" => argv("-c", "-close", "--close", "-closed", "--closed", "--add-closing"),
        "white" => argv("-w", "-white", "--white", "--whitespace", "-ws", "--ws", "-space", "--space", "--fix-whitespace", "--bom"),
        "warn" => argv("-W", "-UW", "-WU", "-warn", "--warn", "--warning"),
        "unicode" => argv("-U", "-UW", "-WU", "-unicode", "--unicode"),
//"html" => argv("-H", "-html", "--html", "-text", "--text", "-content", "--content"), # warn also about plain HTML outside of PHP code
        "recursive" => true + argv("-r"),        # doing that anyway
        "regex" => argv("--regex", "--rx", "-rx"),   # use regex
        "token" => argv("--tokenizer", "--token", "-t"),# use tokenizer
        "color" => argv("-c", "--color", "--ansi") xor isset($_SERVER["TERM"]),    # colorize some output
        "quiet" => argv("--quiet", "-quiet", "-q"),
        "verbose" => argv("--verbose", "-verbose", "-v"),
................................................................................
   -W  --warn      Just warn about whitespace issues.
   -t  --token     Use tokenizer for --short and --long conversion. (More diligent than --regex mode, but doesn't preserve indentation as well.)
   -h  --help      This very helpful help text.
   -v  --verbose   Extra output, use -h -v for all options.\n\n
HELP;
   $action->verbose and print <<<VERBOSE
More options:
   -U  --unicode   Also warn of *any* Unicode whitespace (NBSP, NEL, LS, PS).
   -D  --debug     Development notices.
   -d  --dry       Dry run, don't update files.
   -n  --new       Create file.php.new for updated files.
   -b  --backup    Create file.php~ backup files on edits.
   -54 --php54     Keep <?= always for --long conversion.
   -7  --php7      Probe for ASP style <% and super long PHP <script> tags. Use in --warn mode, no rewriting is attempted (too rare).
   -r  --recursive Traversing subdirectories is enabled per default.
................................................................................
            preg_test("/\?\>(?!\\n<)\s+<\?/", $src, "Consecutive open+close tags with spacing (template?)")
            or preg_test("/\?\>\\r?\\n\<\?/", $src, "Consecutive open+close tags with harmless linebreak");

            // Warn about ambigious PHP tags like `<?print(123)`
            preg_test("/[\s\pZ]+\<\?((?!php|=|xml[-\s]|\w+:\w+)\w++)/i", $src, "Ambiguous PHP tag or unknown XML PI");
        }

        // Unicode whitespace (NBSP, NEL, LS, PS) anywhere within code/html
        if ($action->unicode) {
            preg_test("/[\\x{100A0}\\x{0085}\\x{2028}\\x{2029}]/u", $src, "Unicode whitespace");
        }

        // remove whitespace
        if ($action->white) {
            // spaces after ? >
            preg_modify("/(\?\>)[\s\pZ\\0]+(\z)/", "$1$2", $src, "Removed trailing whitespace");
            // UTF-8 BOM before < ? - but retains spaces until next rule
            preg_modify("/^\\xEF\\xBB\\xBF([\s\pZ\\0]*<\?)(php|\W)/i", "$1$2", $src, "Removed leading UTF-8 BOM");