Skip to content

Commit

Permalink
#124 faster parser using multiprocessing
Browse files Browse the repository at this point in the history
  • Loading branch information
tbela99 committed Jul 30, 2022
1 parent bfac7ad commit 2a53e70
Show file tree
Hide file tree
Showing 759 changed files with 17,494 additions and 8,578 deletions.
24 changes: 12 additions & 12 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
composer.phar
#composer.lock
phpunit.phar
/composer.phar
/composer.lock
/phpunit.phar
phpunit
phpdocumentor.sh
benchmark/
vendor/
test/debug.txt
.idea/
.git/
.phpunit.result.cache
xpath.txt
parsing.txt
test/color.php
test/*.json
/vendor/
/test/debug.txt
/test/*.php
/test/*.css
/.idea/
/.git/
/.phpunit.result.cache
/xpath.txt
/parsing.txt
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ A CSS parser, beautifier and minifier written in PHP. It supports the following

- multibyte characters encoding
- sourcemap
- multiprocessing: process large CSS input very fast
- CSS Nesting module
- partially implemented CSS Syntax module level 3
- partial CSS validation
Expand Down Expand Up @@ -599,7 +600,7 @@ echo $renderer->render($parser->parse());
## Parser Options

- flatten_import: process @import directive and import the content into the css document. default to false.
- allow_duplicate_rules: allow duplicated rules. By default duplicate rules except @font-face are merged
- allow_duplicate_rules: allow duplicated rules. By default, duplicate rules except @font-face are merged
- allow_duplicate_declarations: allow duplicated declarations in the same rule.
- capture_errors: silently capture parse error if true, otherwise throw a parse exception. Default to true

Expand Down
65 changes: 32 additions & 33 deletions cli/css-parser
Original file line number Diff line number Diff line change
Expand Up @@ -40,39 +40,38 @@ try {

$cli->
setStrict(true)->
add('version', 'print version number', 'bool', 'v', multiple: false);
// --data-src="%s" --data-position-index=%s --data-position-line=%s --data-position-column=%s
$cli->addGroup('internal', "internal commands are used by the multithreading feature:\n", true);
$cli->add('parse-ast-src', 'src value of the ast nodes', 'string', group: 'internal');
$cli->add('parse-ast-position-index', 'initial index position of the ast nodes', 'int', group: 'internal');
$cli->add('parse-ast-position-line', 'initial line number of the ast nodes', 'int', group: 'internal');
$cli->add('parse-ast-position-column', 'initial column number of the ast nodes', 'int', group: 'internal');

$cli->addGroup('parse', "parse options:\n");
$cli->add('capture-errors', 'ignore parse error', 'bool', 'e', multiple: false, group: 'parse');
$cli->add('flatten-import', 'process @import', 'bool', 'm', multiple: false, group: 'parse');
$cli->add('parse-allow-duplicate-rules', 'allow duplicate rule', 'bool', 'p', multiple: false, group: 'parse');
$cli->add('parse-allow-duplicate-declarations', 'allow duplicate declaration', type: 'auto', alias: 'd', multiple: false, group: 'parse');
$cli->add('file', 'input css file or url', 'string', 'f', multiple: true, group: 'parse');
$cli->add('parse-multi-processing', 'enable multi-process parser', 'bool', 'M', multiple: false, defaultValue: true, group: 'parse');
$cli->add('parse-children-process', 'maximum children process', 'int', 'P', multiple: false, defaultValue: 20, group: 'parse');

$cli->addGroup('render', "render options:\n");
$cli->add('css-level', 'css color module', 'int', 'l', multiple: false, defaultValue: 4, options: [3, 4], group: 'render');
$cli->add('charset', 'remove @charset', 'bool', 'S', multiple: false, defaultValue: true, group: 'render');
$cli->add('compress', 'minify output', 'bool', 'c', multiple: false, group: 'render');
$cli->add('sourcemap', 'generate sourcemap', 'bool', 's', multiple: false, dependsOn: 'file', group: 'render');
$cli->add('remove-comments', 'remove comments', 'bool', 'C', multiple: false, group: 'render');
$cli->add('preserve-license', 'preserve license comments', 'bool', 'L', multiple: false, group: 'render');
$cli->add('legacy-rendering', 'legacy rendering', 'bool', 'G', multiple: false, group: 'render');
$cli->add('compute-shorthand', 'compute shorthand properties', 'bool', 'u', multiple: false, group: 'render');
$cli->add('remove-empty-nodes', 'remove empty nodes', 'bool', 'E', multiple: false, group: 'render');
$cli->add('render-duplicate-declarations', 'render duplicate declarations', 'bool', 'r', multiple: false, group: 'render');
$cli->add('output', 'output file name', 'string', 'o', multiple: false, group: 'render');
$cli->add('ast', 'dump ast as JSON', 'bool', 'a', multiple: false, group: 'render');
$cli->add('output-format', 'ast export format', 'string', 'F', multiple: false, options: ['json', 'serialize'], dependsOn: 'ast', group: 'render');

$cli->parse();
add('version', 'print version number', 'bool', 'v', multiple: false)
->addGroup('internal', "internal commands are used by the multithreading feature:\n", true)
->add('parse-ast-src', 'src value of the ast nodes', 'string', group: 'internal')
->add('parse-ast-position-index', 'initial index position of the ast nodes', 'int', group: 'internal')
->add('parse-ast-position-line', 'initial line number of the ast nodes', 'int', group: 'internal')
->add('parse-ast-position-column', 'initial column number of the ast nodes', 'int', group: 'internal')

->addGroup('parse', "parse options:\n")
->add('capture-errors', 'ignore parse error', 'bool', 'e', multiple: false, group: 'parse')
->add('flatten-import', 'process @import', 'bool', 'm', multiple: false, group: 'parse')
->add('parse-allow-duplicate-rules', 'allow duplicate rule', 'bool', 'p', multiple: false, group: 'parse')
->add('parse-allow-duplicate-declarations', 'allow duplicate declaration', type: 'auto', alias: 'd', multiple: false, group: 'parse')
->add('file', 'input css file or url', 'string', 'f', multiple: true, group: 'parse')
->add('parse-multi-processing', 'enable multi-process parser', 'bool', 'M', multiple: false, defaultValue: true, group: 'parse')
->add('parse-children-process', 'maximum children process', 'int', 'P', multiple: false, defaultValue: 20, group: 'parse')

->addGroup('render', "render options:\n")
->add('css-level', 'css color module', 'int', 'l', multiple: false, defaultValue: 4, options: [3, 4], group: 'render')
->add('charset', 'remove @charset', 'bool', 'S', multiple: false, defaultValue: true, group: 'render')
->add('compress', 'minify output', 'bool', 'c', multiple: false, group: 'render')
->add('sourcemap', 'generate sourcemap', 'bool', 's', multiple: false, dependsOn: 'file', group: 'render')
->add('remove-comments', 'remove comments', 'bool', 'C', multiple: false, group: 'render')
->add('preserve-license', 'preserve license comments', 'bool', 'L', multiple: false, group: 'render')
->add('legacy-rendering', 'legacy rendering', 'bool', 'G', multiple: false, group: 'render')
->add('compute-shorthand', 'compute shorthand properties', 'bool', 'u', multiple: false, group: 'render')
->add('remove-empty-nodes', 'remove empty nodes', 'bool', 'E', multiple: false, group: 'render')
->add('render-duplicate-declarations', 'render duplicate declarations', 'bool', 'r', multiple: false, group: 'render')
->add('output', 'output file name', 'string', 'o', multiple: false, group: 'render')
->add('ast', 'dump ast as JSON', 'bool', 'a', multiple: false, group: 'render')
->add('output-format', 'ast export format', 'string', 'F', multiple: false, options: ['json', 'serialize'], dependsOn: 'ast', group: 'render')

->parse();

$parseOptions = [];
$renderOptions = [];
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"symfony/process": "^6.1"
},
"scripts": {
"test": "./bin/runtest.sh"
"test": "./vendor/phpunit/phpunit/phpunit"
},
"require-dev": {
"phpunit/phpunit": "^9.5"
Expand Down
7 changes: 4 additions & 3 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ A CSS parser, beautifier and minifier written in PHP. It supports the following

- multibyte characters encoding
- sourcemap
- multiprocessing: process large CSS input very fast
- CSS Nesting module
- partially implemented CSS Syntax module level 3
- partial CSS validation
Expand Down Expand Up @@ -599,7 +600,7 @@ echo $renderer->render($parser->parse());
## Parser Options

- flatten_import: process @import directive and import the content into the css document. default to false.
- allow_duplicate_rules: allow duplicated rules. By default duplicate rules except @font-face are merged
- allow_duplicate_rules: allow duplicated rules. By default, duplicate rules except @font-face are merged
- allow_duplicate_declarations: allow duplicated declarations in the same rule.
- capture_errors: silently capture parse error if true, otherwise throw a parse exception. Default to true

Expand Down Expand Up @@ -683,15 +684,15 @@ $ echo 'a, div {display:none} b {}' | ./cli/css-parser -c
### Minify css file

```bash
$ ./cli/css-parser -f nested.css -f 'https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css' -c
$ ./cli/css-parser -f nested.css -c
#
$ ./cli/css-parser -f 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/brands.min.css' -c
```

### Dump ast

```bash
$ ./cli/css-parser -f nested.css -c -a
$ ./cli/css-parser -f nested.css -f 'https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css' -c -a
#
$ ./cli/css-parser 'a, div {display:none} b {}' -c -a
#
Expand Down
Loading

0 comments on commit 2a53e70

Please sign in to comment.