Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .idea/Staticfy.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

783 changes: 783 additions & 0 deletions .idea/workspace.xml

Large diffs are not rendered by default.

55 changes: 53 additions & 2 deletions __config__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,59 @@
# config file to add support for new frameworks in flask
#key => framework, value => pattern to use when staticfying


# <?php echo base_url(); ?>assets/css/bootstrap.min.css"
# <link rel="stylesheet" href="<?php base_url();?>\'css/style.css\'" />


frameworks = {
'flask': "{{ url_for('%(static_endpoint)s', filename='%(asset_location)s') }}",
'django':"{%% static '%(asset_location)s' %%}",
'laravel': "{{ URL::asset('%(asset_location)s') }}"
'django': "{%% static '%(asset_location)s' %%}",
'laravel': "{{ URL::asset('%(asset_location)s') }}",
'codeigniter': "<?php base_url();?>'%(asset_location)s'",
}


"""
I had three formats in mind for the template dict.
1. Could just be another entry in the original frameworks dict.
2. Could be a new dict on its own with the format:
a:
templating_frameworks = {
'php' : {
'smarty_php': "{$BASE_URL}{$SMARTY_VIEW_FOLDER}'%(asset_location)s'?>",
'blaze_php': "",
'twig_php': "",
'plates_php': "",
'dwoo_php': ""
},

'django': {

},

'flask' : {
'genshi': ""

},

b:
templating_frameworks = {
'smarty_php': "{$BASE_URL}{$SMARTY_VIEW_FOLDER}'%(asset_location)s'?>",
'blaze_php': "",
'twig_php': "",
'plates_php': "",
'dwoo_php': ""
'genshi': "",

}

}
"""
templating_frameworks = {
'smarty_php': "{$BASE_URL}{$SMARTY_VIEW_FOLDER}'%(asset_location)s'?>",
'blaze_php': "",
'twig_php': "",
'plates_php': "",
'dwoo_php': ""
}
35 changes: 24 additions & 11 deletions staticfy.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import errno
import argparse
import json
from __config__ import frameworks, templating_frameworks
from __config__ import frameworks


Expand All @@ -21,9 +22,9 @@ def makedir(path):
raise


def staticfy(file_, static_endpoint='static', project_type='flask', **kwargs):
def staticfy(file_, static_endpoint='static', project_type='', template_framework='', **kwargs):
results = [] # list that holds the links, images and scripts as they're found by BeautifulSoup
add_tags = kwargs.get('add_tags', {}) # dangerous to set keyword args as a dict.
add_tags = kwargs.get('add_tags', {}) # dangerous to set keyword args as a dict.
exc_tags = kwargs.get('exc_tags', {})
tags = {'img': 'src', 'link': 'href', 'script': 'src'}

Expand All @@ -36,8 +37,8 @@ def staticfy(file_, static_endpoint='static', project_type='flask', **kwargs):
html_doc = BeautifulSoup(file_handle, 'html.parser')

def condition(tag):
return lambda x: x.name == tag\
and not x.get(attr, 'http').startswith(('http', '//'))
return lambda x: x.name == tag \
and not x.get(attr, 'http').startswith(('http', '//'))

for tags in all_tags:
for tag, attr in tags.items():
Expand All @@ -52,9 +53,17 @@ def condition(tag):
"{{ url_for('static', filename='images/staticfy.jpg') }}"
)
"""
res = (attr, elem[attr], frameworks[project_type] %
{'static_endpoint':static_endpoint, "asset_location": elem[attr]})
results.append(res)

if not project_type and template_framework in templating_frameworks:
res = (attr, elem[attr], frameworks[templating_frameworks] %
{'static_endpoint': static_endpoint, "asset_location": elem[attr]})
results.append(res)
elif not template_framework and project_type in frameworks:
res = (attr, elem[attr], frameworks[project_type] %
{'static_endpoint': static_endpoint, "asset_location": elem[attr]})
results.append(res)
elif project_type and template_framework:
raise Exception("You cannot specify both a project and a template framework.")

file_handle.close()

Expand Down Expand Up @@ -96,7 +105,8 @@ def parse_cmd_arguments():
parser.add_argument('--add-tags', type=str,
help='additional tags to staticfy')
parser.add_argument('--project-type', type=str,
help='Project Type (default: flask)')
help='Project Type (default: No default)')
parser.add_argument('--template_framework', type=str, help='Template framework in use.')
parser.add_argument('--exc-tags', type=str, help='tags to exclude')
args = parser.parse_args()

Expand All @@ -107,6 +117,8 @@ def main():
args = parse_cmd_arguments()
files = args.file
static_endpoint = args.static_endpoint
project_type = args.project_type
template_framework = args.template_framework
project_type = args.project_type or os.getenv('STATICFY_FRAMEWORK', 'flask')
add_tags = args.add_tags or '{}'
exc_tags = args.exc_tags or '{}'
Expand All @@ -123,15 +135,16 @@ def main():
try:
if os.path.isfile(file_) and file_.endswith(('htm', 'html')):
staticfy(file_, static_endpoint=static_endpoint, add_tags=add_tags,
exc_tags=exc_tags, project_type=project_type)
exc_tags=exc_tags, project_type=project_type,
template_framework=template_framework)
else:
# it's a directory so loop through and staticfy
for filename in os.listdir(file_):
if filename.endswith(('htm', 'html')):
temp_filename = file_ + os.path.sep + filename
staticfy(temp_filename, static_endpoint=static_endpoint,
add_tags=add_tags, exc_tags=exc_tags, project_type=project_type)

add_tags=add_tags, exc_tags=exc_tags, project_type=project_type,
template_framework=template_framework)
except IOError:
print(
'\033[91m' + 'Unable to read/find the specified file or directory' + '\033[0m')
Expand Down
124 changes: 89 additions & 35 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ class StaticfyTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.filename = 'test.html'
data = (
"""<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.css" />\n"""
"""<img src="images/staticfy.jpg" />\n"""
"""<img data-url="images/staticfy.jpg" />\n"""
"""<link rel="stylesheet" href="css/style.css" />\n"""
"""<script src="js/script.js">alert('hello world')</script>\n"""
)
data = ("""<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.css" />\n"""
"""<img src="images/staticfy.jpg" />\n"""
"""<img data-url="images/staticfy.jpg" />\n"""
Expand All @@ -19,47 +26,50 @@ def setUpClass(cls):
with open(cls.filename, 'w+') as f:
f.write(data)

def test_normal_staticfy(self):
def test_abnormal_staticfy(self):
out_file = staticfy(self.filename)

with open(out_file, 'r') as f:
file_contents = f.read()

expected_result = ("""<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.css" />\n"""
"""<img src="{{ url_for('static', filename='images/staticfy.jpg') }}" />\n"""
"""<img data-url="images/staticfy.jpg" />\n"""
"""<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}" />\n"""
"""<script src="{{ url_for('static', filename='js/script.js') }}">alert("hello world")</script>\n"""
)

self.assertEqual(file_contents, expected_result)
with self.assertRaises(Exception) as context:
staticfy(self.filename)
self.assertTrue("No project type or template specified." in context.exception)

def test_static_endpoint(self):
out_file = staticfy(self.filename, static_endpoint='my_static')

with open(out_file, 'r') as f:
file_contents = f.read()

expected_result = ("""<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.css" />\n"""
"""<img src="{{ url_for('my_static', filename='images/staticfy.jpg') }}" />\n"""
"""<img data-url="images/staticfy.jpg" />\n"""
"""<link rel="stylesheet" href="{{ url_for('my_static', filename='css/style.css') }}" />\n"""
"""<script src="{{ url_for('my_static', filename='js/script.js') }}">alert("hello world")</script>\n"""
)
expected_result = (
"""<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.css" />\n"""
"""<img src="images/staticfy.jpg" />\n"""
"""<img data-url="images/staticfy.jpg" />\n"""
"""<link rel="stylesheet" href="css/style.css" />\n"""
"""<script src="js/script.js">alert(\'hello world\')</script>\n"""
)
self.assertEqual(file_contents, expected_result)

def test_additional_tags(self):
out_file = staticfy(self.filename, add_tags={'img': 'data-url'})
out_file = staticfy(self.filename, add_tags={'img': 'data-url'}, )

with open(out_file, 'r') as f:
file_contents = f.read()

expected_result = ("""<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.css" />\n"""
"""<img src="{{ url_for('static', filename='images/staticfy.jpg') }}" />\n"""
"""<img data-url="{{ url_for('static', filename='images/staticfy.jpg') }}" />\n"""
"""<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}" />\n"""
"""<script src="{{ url_for('static', filename='js/script.js') }}">alert("hello world")</script>\n"""
)
expected_result = (
"""<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.css" />\n"""
"""<img src="images/staticfy.jpg" />\n"""
"""<img data-url="images/staticfy.jpg" />\n"""
"""<link rel="stylesheet" href="css/style.css" />\n"""
"""<script src="js/script.js">alert(\'hello world\')</script>\n"""
)
print file_contents
self.assertEqual(file_contents, expected_result)

def test_exclusive_tags(self):
Expand All @@ -68,26 +78,44 @@ def test_exclusive_tags(self):
with open(out_file, 'r') as f:
file_contents = f.read()

expected_result = ("""<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.css" />\n"""
"""<img src="images/staticfy.jpg" />\n"""
"""<img data-url="images/staticfy.jpg" />\n"""
"""<link rel="stylesheet" href="css/style.css" />\n"""
"""<script src="{{ url_for('static', filename='js/script.js') }}">alert("hello world")</script>\n"""
)
expected_result = (
"""<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.css" />\n"""
"""<img src="images/staticfy.jpg" />\n"""
"""<img data-url="images/staticfy.jpg" />\n"""
"""<link rel="stylesheet" href="css/style.css" />\n"""
"""<script src="js/script.js">alert(\'hello world\')</script>\n"""
)
self.assertEqual(file_contents, expected_result)

def test_flask_project(self):
out_file = staticfy(self.filename, project_type="flask")

with open(out_file, 'r') as f:
file_contents = f.read()

expected_result = (
"""<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.css" />\n"""
"""<img src="{{ url_for('static', filename='images/staticfy.jpg') }}" />\n"""
"""<img data-url="images/staticfy.jpg" />\n"""
"""<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}" />\n"""
"""<script src="{{ url_for('static', filename='js/script.js') }}">alert("hello world")</script>\n"""
)

self.assertEqual(file_contents, expected_result)

def test_django_project(self):
out_file = staticfy(self.filename, project_type='django')

with open(out_file, 'r') as f:
file_contents = f.read()

expected_result = ("""<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.css" />\n"""
"""<img src="{% static 'images/staticfy.jpg' %}" />\n"""
"""<img data-url="images/staticfy.jpg" />\n"""
"""<link rel="stylesheet" href="{% static 'css/style.css' %}" />\n"""
"""<script src="{% static 'js/script.js' %}">alert("hello world")</script>\n"""
)
expected_result = (
"""<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.css" />\n"""
"""<img src="{% static 'images/staticfy.jpg' %}" />\n"""
"""<img data-url="images/staticfy.jpg" />\n"""
"""<link rel="stylesheet" href="{% static 'css/style.css' %}" />\n"""
"""<script src="{% static 'js/script.js' %}">alert("hello world")</script>\n"""
)
self.assertEqual(file_contents, expected_result)

def test_laravel_project(self):
Expand All @@ -97,14 +125,32 @@ def test_laravel_project(self):

with open(out_file, 'r') as f:
file_contents = f.read()
expected_result = ("""<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.css" />\n"""
"""<img src="{{ URL::asset('images/staticfy.jpg') }}" />\n"""
"""<img data-url="images/staticfy.jpg" />\n"""
"""<link rel="stylesheet" href="{{ URL::asset('css/style.css') }}" />\n"""
"""<script src="{{ URL::asset('js/script.js') }}">alert("hello world")</script>\n"""
)
expected_result = (
"""<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.css" />\n"""
"""<img src="{{ URL::asset('images/staticfy.jpg') }}" />\n"""
"""<img data-url="images/staticfy.jpg" />\n"""
"""<link rel="stylesheet" href="{{ URL::asset('css/style.css') }}" />\n"""
"""<script src="{{ URL::asset('js/script.js') }}">alert("hello world")</script>\n"""
)
self.assertEqual(file_contents, expected_result)

def test_codeigniter_project(self):
out_file = staticfy(self.filename, project_type='codeigniter')

with open(out_file, 'r') as file:
contents = file.read()
expected_result = (
"""<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.css" />\n"""
"""<img src="<?php base_url();?>'images/staticfy.jpg'" />\n"""
"""<img data-url="images/staticfy.jpg" />\n"""
"""<link rel="stylesheet" href="<?php base_url();?>'css/style.css'" />\n"""
"""<script src="<?php base_url();?>'js/script.js'">alert("hello world")</script>\n"""
)

# <link rel="stylesheet" href="<?php base_url();?>\'css/style.css\'" />

self.assertEqual(contents, expected_result)

def test_filenotfound_exception(self):
self.assertRaises(IOError, staticfy, 'Invalid file')

Expand All @@ -117,3 +163,11 @@ def tearDownClass(cls):

if __name__ == '__main__':
unittest.main()



#
# <img src="<?php base_url();?>\'images/staticfy.jpg\'?>" />
#
# <img src="<?php base_url();?>(\'images/staticfy.jpg\')" />