diff --git a/.idea/Staticfy.iml b/.idea/Staticfy.iml new file mode 100644 index 0000000..53d8ab6 --- /dev/null +++ b/.idea/Staticfy.iml @@ -0,0 +1,11 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..eb86ae6 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..4abc271 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..ec41c98 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,783 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + DEFINITION_ORDER + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + project + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1474275109373 + + + 1474615914848 + + + 1474615920483 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/__config__.py b/__config__.py index ec6c8da..da7a508 100644 --- a/__config__.py +++ b/__config__.py @@ -1,8 +1,59 @@ # config file to add support for new frameworks in flask #key => framework, value => pattern to use when staticfying + +# assets/css/bootstrap.min.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': "'%(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': "" } diff --git a/staticfy.py b/staticfy.py index 9ebf285..cf640b6 100644 --- a/staticfy.py +++ b/staticfy.py @@ -6,6 +6,7 @@ import errno import argparse import json +from __config__ import frameworks, templating_frameworks from __config__ import frameworks @@ -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'} @@ -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(): @@ -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() @@ -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() @@ -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 '{}' @@ -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') diff --git a/test.py b/test.py index cd80955..c97eedb 100644 --- a/test.py +++ b/test.py @@ -9,6 +9,13 @@ class StaticfyTest(unittest.TestCase): @classmethod def setUpClass(cls): cls.filename = 'test.html' + data = ( + """\n""" + """\n""" + """\n""" + """\n""" + """\n""" + ) data = ("""\n""" """\n""" """\n""" @@ -19,12 +26,11 @@ 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 = ("""\n""" """\n""" """\n""" @@ -32,7 +38,9 @@ def test_normal_staticfy(self): """\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') @@ -40,26 +48,28 @@ def test_static_endpoint(self): with open(out_file, 'r') as f: file_contents = f.read() - expected_result = ("""\n""" - """\n""" - """\n""" - """\n""" - """\n""" - ) + expected_result = ( + """\n""" + """\n""" + """\n""" + """\n""" + """\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 = ("""\n""" - """\n""" - """\n""" - """\n""" - """\n""" - ) + expected_result = ( + """\n""" + """\n""" + """\n""" + """\n""" + """\n""" + ) + print file_contents self.assertEqual(file_contents, expected_result) def test_exclusive_tags(self): @@ -68,26 +78,44 @@ def test_exclusive_tags(self): with open(out_file, 'r') as f: file_contents = f.read() - expected_result = ("""\n""" - """\n""" - """\n""" - """\n""" - """\n""" - ) + expected_result = ( + """\n""" + """\n""" + """\n""" + """\n""" + """\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 = ( + """\n""" + """\n""" + """\n""" + """\n""" + """\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 = ("""\n""" - """\n""" - """\n""" - """\n""" - """\n""" - ) + expected_result = ( + """\n""" + """\n""" + """\n""" + """\n""" + """\n""" + ) self.assertEqual(file_contents, expected_result) def test_laravel_project(self): @@ -97,14 +125,32 @@ def test_laravel_project(self): with open(out_file, 'r') as f: file_contents = f.read() - expected_result = ("""\n""" - """\n""" - """\n""" - """\n""" - """\n""" - ) + expected_result = ( + """\n""" + """\n""" + """\n""" + """\n""" + """\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 = ( + """\n""" + """\n""" + """\n""" + """\n""" + """\n""" + ) + + # + + self.assertEqual(contents, expected_result) + def test_filenotfound_exception(self): self.assertRaises(IOError, staticfy, 'Invalid file') @@ -117,3 +163,11 @@ def tearDownClass(cls): if __name__ == '__main__': unittest.main() + + + +# +# +# +# +