Skip to content

Commit

Permalink
allow command to read from stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
mankyd committed Feb 17, 2013
1 parent ad81c4d commit 5570729
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
9 changes: 5 additions & 4 deletions docs/quickstart.rst
Expand Up @@ -44,17 +44,18 @@ htmlmin is invoked by running::

htmlmin input.html output.html

If no output file is specified, it will print to stdout. Help with options can
be retrieved at any time by running `htmlmin -h`::
If no output file is specified, it will print to ``stdout``. If not input
specified, it reads form ``stdin``. Help with options can be retrieved at
any time by running `htmlmin -h`::

htmlmin -h
usage: htmlmin [-h] [-c] [-s] [--remove-all-empty-space] [-H] [-k] [-p [TAG [TAG ...]]] [-e ENCODING]
INPUT [OUTPUT]
[INPUT] [OUTPUT]

Minify HTML

positional arguments:
INPUT File path to html file to minify.
INPUT File path to html file to minify. Defaults to stdin.
OUTPUT File path to output to. Defaults to stdout.

optional arguments:
Expand Down
22 changes: 15 additions & 7 deletions htmlmin/command.py
Expand Up @@ -2,18 +2,20 @@

import argparse
import codecs
import sys

#import htmlmin
from . import minify
from . import Minifier

parser = argparse.ArgumentParser(
description='Minify HTML',
formatter_class=argparse.RawTextHelpFormatter
)

parser.add_argument('input_file',
nargs='?',
metavar='INPUT',
help='File path to html file to minify.',
help='File path to html file to minify. Defaults to stdin.',
)

parser.add_argument('output_file',
Expand Down Expand Up @@ -94,18 +96,24 @@

def main():
args = parser.parse_args()
inp = codecs.open(args.input_file, encoding=args.encoding).read()
result = minify(inp,
minifier = Minifier(
remove_comments=args.remove_comments,
remove_empty_space=args.remove_empty_space,
in_head=args.in_head,
pre_tags=args.pre_tags,
keep_pre=args.keep_pre_attr,
)
if args.input_file:
inp = codecs.open(args.input_file, encoding=args.encoding)
else:
inp = sys.stdin

for line in sys.stdin.readlines():
minifier.input(line)

if args.output_file:
codecs.open(args.output_file, 'w', encoding=args.encoding).write(result)
codecs.open(args.output_file, 'w', encoding=args.encoding).write(minifier.output)
else:
print result
print minifier.output

if __name__ == '__main__':
main()
Expand Down
2 changes: 2 additions & 0 deletions htmlmin/parser.py
Expand Up @@ -197,6 +197,8 @@ def handle_data(self, data):
# from between two blocks of text: a <!-- B --> c => a c.
if data[0] == ' ' and self._data_buffer[-1][-1] == ' ':
data = data[1:]
if not data:
return
self._data_buffer.append(data)

def handle_entityref(self, data):
Expand Down

0 comments on commit 5570729

Please sign in to comment.