Skip to content

Commit

Permalink
Full Python 3 support
Browse files Browse the repository at this point in the history
  • Loading branch information
j33f authored and peterbe committed Jan 18, 2014
1 parent 4c06209 commit 1ede34e
Show file tree
Hide file tree
Showing 13 changed files with 283 additions and 134 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -5,3 +5,5 @@ mincss.egg-info/
/build/
/dist/
simple.js
*.pyc
__pycache__/
12 changes: 12 additions & 0 deletions .travis.yml
@@ -0,0 +1,12 @@
language: python

python:
- "2.7"
- "3.3"

install:
- python setup.py --quiet install

script:
- nosetests
- mincss https://travis-ci.org
2 changes: 1 addition & 1 deletion MANIFEST.in
@@ -1,5 +1,5 @@
include LICENSE
include README.md
include README.rst
include docs/changelog.rst
include requirements.txt
include mincss/download.js
31 changes: 26 additions & 5 deletions README.md → README.rst
@@ -1,17 +1,36 @@
mincss
======

.. image:: https://travis-ci.org/myint/mincss.png?branch=master
:target: https://travis-ci.org/myint/mincss
:alt: Build status

Clears the junk out of your CSS by finding out which selectors are
actually not used in your HTML.

By Peter Bengtsson, 2012-2013

Example
-------

::

$ mincss https://github.com


Installation
------------

From pip::

$ pip install --upgrade git+https://github.com/myint/mincss

Why?
----

With the onslaught of Twitter Bootstrap upon the world it's very
tempting to just download their whole fat 80+Kb CSS and serve it up
even though you're not using half of the HTML that it styles.
tempting to just download their whole fat 80+Kb CSS and serve it up even
though you're not using half of the HTML that it styles.

There's also the case of websites that have changed over time but
without the CSS getting the same amount of love refactoring. Then it's
Expand All @@ -24,8 +43,8 @@ you're not using.
Whitespace compression?
-----------------------

No, that's a separate concern. This tool works independent of
whitespace compression/optimization.
No, that's a separate concern. This tool works independent of whitespace
compression/optimization.

For example, if you have a build step or a runtime step that converts
all your CSS files into one (concatenation) and trims away all the
Expand All @@ -42,9 +61,11 @@ because at the moment ``mincss`` is entirely static.
So what is a web developer to do? Simple, use ``/* no mincss */`` like
this for example:

::

.logged-in-info {
/* no mincss */
color: pink;
color: pink;
}

That tells ``mincss`` to ignore the whole block and all its selectors.
46 changes: 24 additions & 22 deletions docs/conf.py
Expand Up @@ -3,28 +3,28 @@
# mincss documentation build configuration file, created by
# sphinx-quickstart on Fri Jan 11 14:08:28 2013.
#
# This file is execfile()d with the current directory set to its containing dir.
# This file is execfile()d with the current directory set to its containing
# dir.
#
# Note that not all possible configuration values are present in this
# autogenerated file.
#
# All configuration values have a default; values that are commented out
# serve to show the default.

import sys, os

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#sys.path.insert(0, os.path.abspath('.'))

# -- General configuration -----------------------------------------------------
# -- General configuration -----------------------------------------------

# If your documentation needs a minimal Sphinx version, state it here.
#needs_sphinx = '1.0'

# Add any Sphinx extension module names here, as strings. They can be extensions
# coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
extensions = []

# Add any paths that contain templates here, relative to this directory.
Expand Down Expand Up @@ -66,7 +66,8 @@
# directories to ignore when looking for source files.
exclude_patterns = ['_build']

# The reST default role (used for this markup: `text`) to use for all documents.
# The reST default role (used for this markup: `text`) to use for all
# documents.
#default_role = None

# If true, '()' will be appended to :func: etc. cross-reference text.
Expand All @@ -87,7 +88,7 @@
#modindex_common_prefix = []


# -- Options for HTML output ---------------------------------------------------
# -- Options for HTML output ---------------------------------------------

# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
Expand Down Expand Up @@ -167,24 +168,25 @@
htmlhelp_basename = 'mincssdoc'


# -- Options for LaTeX output --------------------------------------------------
# -- Options for LaTeX output --------------------------------------------

latex_elements = {
# The paper size ('letterpaper' or 'a4paper').
#'papersize': 'letterpaper',
# The paper size ('letterpaper' or 'a4paper').
#'papersize': 'letterpaper',

# The font size ('10pt', '11pt' or '12pt').
#'pointsize': '10pt',
# The font size ('10pt', '11pt' or '12pt').
#'pointsize': '10pt',

# Additional stuff for the LaTeX preamble.
#'preamble': '',
# Additional stuff for the LaTeX preamble.
#'preamble': '',
}

# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title, author, documentclass [howto/manual]).
# (source start file, target name, title, author, documentclass
# [howto/manual]).
latex_documents = [
('index', 'mincss.tex', u'mincss Documentation',
u'Peter Bengtsson', 'manual'),
('index', 'mincss.tex', u'mincss Documentation',
u'Peter Bengtsson', 'manual'),
]

# The name of an image file (relative to this directory) to place at the top of
Expand All @@ -208,7 +210,7 @@
#latex_domain_indices = True


# -- Options for manual page output --------------------------------------------
# -- Options for manual page output --------------------------------------

# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
Expand All @@ -221,15 +223,15 @@
#man_show_urls = False


# -- Options for Texinfo output ------------------------------------------------
# -- Options for Texinfo output ------------------------------------------

# Grouping the document tree into Texinfo files. List of tuples
# (source start file, target name, title, author,
# dir menu entry, description, category)
texinfo_documents = [
('index', 'mincss', u'mincss Documentation',
u'Peter Bengtsson', 'mincss', 'One line description of project.',
'Miscellaneous'),
('index', 'mincss', u'mincss Documentation',
u'Peter Bengtsson', 'mincss', 'One line description of project.',
'Miscellaneous'),
]

# Documents to append as an appendix to all manuals.
Expand Down
9 changes: 9 additions & 0 deletions mincss/__main__.py
@@ -0,0 +1,9 @@
#!/usr/bin/env python

import sys

from . import main


if __name__ == '__main__':
sys.exit(main.main())
70 changes: 70 additions & 0 deletions mincss/main.py
@@ -0,0 +1,70 @@
from __future__ import print_function

import io
import os
import time

from .processor import Processor


def run(args):
options = {'debug': args.verbose}
if args.phantomjs_path:
options['phantomjs'] = args.phantomjs_path
elif args.phantomjs:
options['phantomjs'] = True
p = Processor(**options)
t0 = time.time()
p.process(args.url)
t1 = time.time()
print('TOTAL TIME ', t1 - t0)
for inline in p.inlines:
print('ON', inline.url)
print('AT line', inline.line)
print('BEFORE '.ljust(79, '-'))
print(inline.before)
print('AFTER '.ljust(79, '-'))
print(inline.after)
print()

output_dir = args.outputdir
if not os.path.isdir(output_dir):
os.mkdir(output_dir)
for link in p.links:
print('FOR', link.href)
orig_name = link.href.split('/')[-1]
with io.open(os.path.join(output_dir, orig_name), 'w') as f:
f.write(link.after)
before_name = 'before_' + link.href.split('/')[-1]
with io.open(os.path.join(output_dir, before_name), 'w') as f:
f.write(link.before)
print('Files written to', output_dir)
print()
print(
'(from %d to %d saves %d)' %
(len(link.before), len(link.after),
len(link.before) - len(link.after))
)

return 0


def main():
import argparse
parser = argparse.ArgumentParser()
add = parser.add_argument
add('url', type=str,
help='URL to process')
add('--outputdir', action='store',
default='./output',
help='directory where to put output (default ./output)')
add('-v', '--verbose', action='store_true',
help='increase output verbosity')
add('--phantomjs', action='store_true',
help='Use PhantomJS to download the source')
add('--phantomjs-path', action='store',
default='',
help='Where is the phantomjs executable')

args = parser.parse_args()
return run(args)

0 comments on commit 1ede34e

Please sign in to comment.