Coder Social home page Coder Social logo

danidee10 / staticfy Goto Github PK

View Code? Open in Web Editor NEW
35.0 6.0 12.0 671 KB

:wrench: Automatically convert hardcoded links to assets in your project, to dynamic links for your web framework

License: GNU General Public License v3.0

Python 100.00%
flask django laravel static-files convert

staticfy's Introduction

Status

Build Status Code Climate

Staticfy

You just got a brand new template fron the front-end designer, Everything's going well, Until you realize the amount of time you'll spend manually changing the links in the html templates you until all the static files and assets are properly linked and the file looks exactly like the demo he/she showed you. with Staticfy you can save that time (and some of your hair) by automatically converting the static urls in your template to dynamic url's that wouldn't break if you decide to move your file to another location.

It transforms html links (in this example Flask) from this:

<img src="img/staticfy.jpg" />

To this:

<img src="{{ url_for('static', filename='img/staticfy.jpg') }}" />

and then your web framework's templating language can resolve the tags to the right url.

Get it in 10 seconds!

It's available as a package on PyPi so you can install it with

pip install staticfy

That's all!

Run it straight from the command line with:

staticfy staticfy.html --static-endpoint=static --add-tags='{"img": "data-url"}'

--static-endpoint and --add-tags are optional

By default Staticfy writes to stdout but you can specify an output file with -o or --output

staticfy staticfy.html -o new.html

Before Staticfying

alt tag

After Staticfying

alt tag

Notice how it preserves the font-awesome css link at the top of the file?, external resources (images, scripts, font-awesome, google-fonts, bootstrap files, disqus e.t.c) which aren't hosted locally with your website won't be transformed. Staticfy also accepts an optional argument --static-endpoint in case you're not using the default static endpoint.

Staticy also preserves the indentation and formatting of any html file given to it, so your html file(s) are still look the same way and are still readablebe just the way they were before you transformed them.

Additional tags and attributes

By default staticfy identifies and transforms the following tags:

  1. img tags with src attributes -- <img src="" />
  2. link tags with rel attributes -- <link rel="" />
  3. script tags with src attributes -- <script src="" />

But it's common these days for javascript libraries (for a slider or animation) that have link to images (mostly) or other static resources. you can easily transform those tags by specifying the --add-tags argument and passing in a valid JSON string, an example is this slider from http://www.pixedelic.com/plugins/camera/, you can transform the div tags like this:

staticfy staticfy.html --add-tags='{"div": "data-src"}'

Sure enough it gets transformed.

Before staticfying

alt tag

After staticfying

alt tag

You can exclude certain tags you don't want to be transformed by specifying the --exc-tags parameter, like --add-tags it expects a valid JSON string.

staticfy staticfy.html --exc-tags='{"img": "src"}'

Running that on a template should leave the img tags with the src attribute as they were in the original file.

Bulk Options

If you need to transform a lot of templates. You can write a oneliner in bash that does that. An example is this:

for i in folder/*.html; do staticfy $i | install -D /dev/stdin staticfy/$i; done

That takes all the html files in folder, transforms them and outputs them to a directory called staticfy.

Namespacing

When your project gets big, It's necessary to namespace static assets to avoid name collision, you can achieve this by passing a string to the --namespace argument. The string would automatically be prepended to the url. For example in django

staticfy staticfy.html --namespace 'admin'

Would convert <img src="img/staticfy.jpg" /> to this <img src="{% url static admin/img/staticfy.jpg %}" />

Inconsistency with single and double quotes in html

Staticfy also converts all single quoted html attributes to double quoted attributes. It's very common to see html files with mixed quotes.

<html>

  <head>
    <link rel='stylesheet' href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.css" />
    <img src="/static/images/staticfy.jpg" />
    <img data-url='images/staticfy.jpg' />
    <link rel="stylesheet" href='css/style.css' />
    <script src="/js/script.js"></script>
  </head>

</html>

You can easily fix all the inconsistencies by running staticfy on that file. There's also little performance gains when you gzip a html file that has consistent use of quotes (either double or single) against an inconsistent one. Don't believe it...Look at this

alt tag

You can see a reduction from 219 bytes to 204 bytes, of course this difference is very little, but depending on how inconsistent and large your file is, you could save a save a few kilobytes which means faster page load times for your end users.

HTML is a very forgiving, and you're allowed to use single quotes or double quotes. but double quotes are the Unofficial Standard

Using staticfy with other frameworks

Staticfy was initially built with flask in mind, but it can also be extended to support other frameworks easily. Out of the box it supports:

  1. flask
  2. django and
  3. laravel

For example to use staticfy with django, specify --framework=django, You can also set the environment variable STATICFY_FRAMEWORK to any supported framework, so you can avoid using --framework everytime. if you're running linux you can easily do this.

export STATICFY_FRAMEWORK=django

If you specify a framework that isn't found, staticfy would cry and gracefully fall back to it's flask roots.

Tests

The tests are located in the test.py file and can be run with:

python -m unittest

Python support

Staticfy supports both python2 and python3 (python 2.7 >)

Requirements and 3rd party stuff

Beautiful soup 4 pip3 install bs4

You can use the requirements file pip3 install -r requirements.txt

If you have issues with importing HTML.parser on python 3.5, upgrade beautifulSoup:

pip install --upgrade beautifulsoup4

Contribution

Pull requests and issues are highly welcome. I'm still looking to support as many web frameworks as possible.

Describe clearly what your PR attempts to fix (if it's a bug) or improve (If it's an existing feature).

If you want to add support for a new web framework, add the required pattern for the framework to the frameworks dictionary in __config__.py, make your tests pass and submit your Pull request.

staticfy's People

Contributors

danidee10 avatar gbozee avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

staticfy's Issues

Making this repo compatible with django templates too

Nice initiative @danidee10 But it would be nice if this project was also compatible with django templates. So what this would mean is an additional args would be passed to the command line specifying the project type django or flask. You could make the default flask if nothing is passed.

In order to accomplish this, the functionality needs to be abstracted out first. I would look into issueing a PR on this later.

Staticfy folder

I've always been able to run staticfy *path* where path is a directory containing html files but for some reason, it doesn't seem to be working now.

I keep getting an IsADirectoryError error.

screen shot 2018-02-09 at 7 57 32 pm

Going through the traceback, I noticed the error comes up in the get_elements function which attempts to open a file so it's only logical for it to raise an error if a directory is passed.
screen shot 2018-02-09 at 7 59 09 pm

My suggestion would be to run a check on what is being passed.

Files are not correctly generated in Python3

The last change to add encode('utf-8') breaks the generation of the html files in Python3. The binary is not correctly interpreted (including tabs and newlines) and is printed as is to the html file.

Add support for Rails

I don't have any experience with rails.

but by googling around, i found out that rails links look like this

<%= stylesheet_link_tag "filename" %> // for css

<%= javascript_include_tag "customjsFile", "data-turbolinks-track" => true  %> // for js 

<%= image_tag("rss.jpg", :alt => "rss feed") %> // for images

This differs from python frameworks where you have to write the enclosing html by yourself

e.g <script>{{url_generator}}</script>

In the case of rails the <script> tags are generated for you.

Design contribution

Hi, I see that Staticfy does not have a logo. As I have done before, I am contributing design to open source projects. If you need a logo, I can do it. I also have a design idea in my mind.

I look forward to your feedback...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.