Skip to content

Commit

Permalink
Merge pull request #68 from mrf345/testing
Browse files Browse the repository at this point in the history
Fix fail_safe option, and readme examples
  • Loading branch information
mrf345 authored Dec 7, 2021
2 parents 4e92722 + c0629ef commit eb2b713
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 21 deletions.
24 changes: 11 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,23 @@ In this example the extension will minify every HTML request, unless it's expli

```python
from flask import Flask
from flask_minify import minify
from flask_minify import Minify

app = Flask(__name__)
minify(app=app, html=True, js=True, cssless=True)
Minify(app=app, html=True, js=True, cssless=True)
```

Another approach is using **decorators**, you can set the extension to be `passive` so will only minify the decorated routes

```python
from flask import Flask
from flask_minify import minify, decorators
from flask_minify import Minify, decorators as minify_decorators

app = Flask(__name__)
minify(app=app, passive=True)
Minify(app=app, passive=True)

@app.route('/')
@decorators.minify(html=True, js=True, cssless=True)
@minify_decorators.minify(html=True, js=True, cssless=True)
def example():
return '<h1>Example...</h1>'
```
Expand Down Expand Up @@ -92,7 +92,7 @@ both options can handle regex patterns as input so for example, if you want to b
you can just pass the pattern as such:

```python
minify(app, bypass=['blueprint_name.*'])
Minify(app, bypass=['blueprint_name.*'])
```

#### - `caching_limit`
Expand All @@ -111,19 +111,17 @@ allows passing tag specific options to the module responsible for the minificati
In the following example will replace the default `style` (handles CSS) parser `rcssmin` with `lesscpy`:

```python
from flask_minify import minify
from flask_minify.parsers import as minify_parsers
from flask_minify import Minify, parsers as minify_parsers

parsers = {'style': minify_parsers.Lesscpy}

minify(app=app, parsers=parsers)
Minify(app=app, parsers=parsers)
```

you can override the default parser runtime options as well, as shown in the following example:

```python
from flask_minify import minify
from flask_minify.parsers import as minify_parsers
from flask_minify import Minify, parsers as minify_parsers

class CustomCssParser(minify_parsers.Lesscpy):
runtime_options = {
Expand All @@ -133,7 +131,7 @@ class CustomCssParser(minify_parsers.Lesscpy):

parsers = {'style': CustomCssParser}

minify(app=app, parsers=parsers)
Minify(app=app, parsers=parsers)
```

the **default** parsers are set to `{"html": Html, "script": Jsmin, "style": Rcssmin}` check out [the code](https://github.com/mrf345/flask_minify/blob/master/flask_minify/parsers.py) for more insight.
Expand All @@ -150,4 +148,4 @@ the **default** parsers are set to `{"html": Html, "script": Jsmin, "style": Rcs
#### `0.33`

introduces a breaking change to the expected output, in this release `lesscpy` will be replaced by `cssmin` as
the default css minifier so no more `less` compiling by default. in case you don't want that, follow [this example](https://github.com/mrf345/flask_minify#--parsers).
the default css minifier so no more `less` compiling by default. in case you don't want that, follow [this example](https://github.com/mrf345/flask_minify#--parsers).
2 changes: 1 addition & 1 deletion flask_minify/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from . import decorators
from .about import __author__, __doc__, __email__, __license__, __version__
from .main import Minify
from .main import Minify as minify
2 changes: 1 addition & 1 deletion flask_minify/about.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.34"
__version__ = "0.35"
__doc__ = "Flask extension to minify html, css, js and less."
__license__ = "MIT"
__author__ = "Mohamed Feddad"
Expand Down
7 changes: 3 additions & 4 deletions flask_minify/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,10 @@ def minify(self, content, tag):
}

try:
minified = parser.executer(content, **runtime_options)
minified_or_content = parser.executer(content, **runtime_options)
except Exception as e:
minified = parser.executer(content, **runtime_options)
if not self.fail_safe:
raise e
minified = content
minified_or_content = content

return minified
return minified_or_content
2 changes: 0 additions & 2 deletions tests/integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@ def test_minify_cache(client):
def test_fail_safe(client):
"""testing fail safe enabled with false input"""
store_minify.parser.fail_safe = True
store_minify.cssless = False
store_minify.parser.runtime_options["html"]["minify_inline"] = {}
resp = client.get("/cssless_false")

assert bytes(FALSE_LESS.encode("utf-8")) == resp.data
Expand Down

0 comments on commit eb2b713

Please sign in to comment.