Skip to content

Commit 53b6670

Browse files
committed
adding first commit source code
0 parents  commit 53b6670

File tree

7 files changed

+249
-0
lines changed

7 files changed

+249
-0
lines changed

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Mohamed Feddad
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

LICENSE.txt

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Mohamed Feddad
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<h1 align='center'> flask_minify </h1>
2+
<h4 align='center'>A Flask extension to minify flask response for html, javascript, css and less compilation as well.</h4>
3+
4+
## Install:
5+
#### - With pip
6+
> - `pip install Flask-Minify` <br />
7+
8+
#### - From the source:
9+
> - `git clone https://github.com/mrf345/flask_minify.git`<br />
10+
> - `cd flask_minify` <br />
11+
> - `python setup.py install`
12+
13+
## Setup:
14+
#### - Inside Flask app:
15+
16+
```python
17+
from flask import Flask
18+
from flask_minify import minify
19+
20+
app = Flask(__name__)
21+
minify(app=app)
22+
```
23+
24+
#### - Result:
25+
26+
> Before:
27+
```html
28+
<html>
29+
<head>
30+
<script>
31+
if (true) {
32+
console.log('working !')
33+
}
34+
</script>
35+
<style>
36+
body {
37+
background-color: red;
38+
}
39+
</style>
40+
</head>
41+
<body>
42+
<h1>Flask-Less Example !</h1>
43+
</body>
44+
</html>
45+
```
46+
> After:
47+
```html
48+
<html> <head><script>if(true){console.log('working !')}</script><style>body{background-color:red;}</style></head> <body> <h1>Flask-Less Example !</h1> </body> </html>
49+
```
50+
51+
## Options:
52+
```python
53+
def __init__(self,
54+
app=None,
55+
html=True,
56+
js=False,
57+
cssless=True):
58+
"""
59+
A Flask extension to minify flask response for html,
60+
javascript, css and less.
61+
@param: app Flask app instance to be passed (default:None).
62+
@param: js To minify the css output (default:False).
63+
@param: cssless To minify spaces in css (default:True).
64+
"""
65+
```
66+
67+
## Credit:
68+
> - [htmlmin][1322354e]: HTML python minifier.
69+
> - [lesscpy][1322353e]: Python less compiler and css minifier.
70+
> - [jsmin][1322355e]: JavaScript python minifier.
71+
72+
[1322353e]: https://github.com/lesscpy/lesscpy "lesscpy repo"
73+
[1322354e]: https://github.com/mankyd/htmlmin "htmlmin repo"
74+
[1322355e]: https://github.com/tikitu/jsmin "jsmin repo"

example.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from flask import Flask, render_template
2+
from flask_minify import minify
3+
from atexit import register
4+
from os import remove
5+
6+
app = Flask(__name__, template_folder='.')
7+
minify(app=app, js=True)
8+
9+
def cleanUp():
10+
try:
11+
remove('index.html')
12+
except Exception:
13+
pass
14+
15+
register(cleanUp)
16+
17+
@app.route('/')
18+
def root():
19+
with open('index.html', 'w+') as file:
20+
file.write("<html>\n<head>\n")
21+
file.write("<script>\nif (true) {\n\tconsole.log('working !')\n}\n</script>")
22+
file.write("<style>\nbody\n{\n\tbackground-color: red;\n}\n</style>")
23+
file.write("\n</head>\n<body>\n<h1>Flask-Less Example !</h1>\n</body>\n</html>\n")
24+
return render_template('index.html')
25+
26+
27+
app.run(debug=True, port=4000)

flask_minify/__init__.py

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from os import remove, path, name as osName
2+
from lesscpy import compile
3+
from jsmin import jsmin
4+
from six import StringIO
5+
from htmlmin import minify as minifyHtml
6+
# Fixing file not found for py2
7+
from sys import version_info
8+
if version_info.major == 2:
9+
FileNotFoundError = IOError
10+
11+
12+
class minify(object):
13+
def __init__(self, app=None, html=True, js=False, cssless=True):
14+
"""
15+
A Flask extension to minify flask response for html,
16+
javascript, css and less.
17+
@param: app Flask app instance to be passed (default:None).
18+
@param: js To minify the css output (default:False).
19+
@param: cssless To minify spaces in css (default:True).
20+
"""
21+
self.app = app
22+
self.html = html
23+
self.js = js
24+
self.cssless = cssless
25+
self.tags = []
26+
if self.js:
27+
self.tags.append('script')
28+
if self.cssless:
29+
self.tags.append('style')
30+
if self.app is None:
31+
raise(AttributeError("minify(app=) requires Flask app instance"))
32+
for arg in [
33+
['cssless', cssless],
34+
['js', js]]:
35+
if not isinstance(arg[1], bool):
36+
raise(TypeError("minify(" + arg[0] + "=) requires True or False"))
37+
app.after_request(self.toLoopTag)
38+
39+
40+
def toLoopTag(self, response):
41+
if response.content_type == u'text/html; charset=utf-8':
42+
response.direct_passthrough = False
43+
text = response.get_data(as_text=True)
44+
for tag in self.tags:
45+
tagType = 'javascript' if tag == 'script' else 'css'
46+
if '<' + tag + ' type="' + tagType + '">' in text or '<' + tag + '>' in text:
47+
for i in range(1, len(text.split('<' + tag))):
48+
toReplace = text.split('<' + tag, i)[i].split('</' + tag + '>')[0]
49+
if 'type="text/' + tagType + '"' in toReplace:
50+
toReplace = toReplace.replace('type="text/' + tag + '"', '')
51+
if toReplace[:1] == '>':
52+
toReplace = toReplace[1:]
53+
if tag == 'style':
54+
text = text.replace(toReplace, compile(StringIO(toReplace), minify=True))
55+
elif tag == 'script':
56+
text = text.replace(toReplace, jsmin(toReplace).replace('\n', ';'))
57+
if self.html:
58+
response.set_data(minifyHtml(text))
59+
else:
60+
response.set_data(text)
61+
return response

setup.cfg

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[metadata]
2+
description-file = README.md

setup.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""
2+
Flask-Minify
3+
-------------
4+
5+
A Flask extension to minify flask response for html,
6+
javascript, css and less compilation as well.
7+
8+
"""
9+
from setuptools import setup
10+
11+
setup(
12+
name='Flask-Minify',
13+
version='0.1',
14+
url='https://github.com/mrf345/flask_minify/',
15+
download_url='https://github.com/mrf345/flask_minify/archive/0.1.tar.gz',
16+
license='MIT',
17+
author='Mohamed Feddad',
18+
author_email='[email protected]',
19+
description='flask extension to minify html, css, js and less',
20+
long_description=__doc__,
21+
py_modules=['minify'],
22+
packages=['flask_minify'],
23+
zip_safe=False,
24+
include_package_data=True,
25+
platforms='any',
26+
install_requires=[
27+
'Flask',
28+
'htmlmin',
29+
'jsmin',
30+
'lesscpy'
31+
],
32+
keywords=['flask', 'extension', 'minifer', 'htmlmin', 'lesscpy',
33+
'jsmin', 'html', 'js', 'less', 'css'],
34+
classifiers=[
35+
'Environment :: Web Environment',
36+
'Intended Audience :: Developers',
37+
'License :: OSI Approved :: MIT License',
38+
'Operating System :: OS Independent',
39+
'Programming Language :: Python',
40+
'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
41+
'Topic :: Software Development :: Libraries :: Python Modules'
42+
]
43+
)

0 commit comments

Comments
 (0)