summaryrefslogtreecommitdiff
path: root/smartypants
blob: d6f2f7bf4acf571200638603170b0afaaaa463c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env python
# Copyright (c) 2013 Yu-Jie Lin
# Licensed under the BSD License, for detailed license information, see COPYING

"""
============
Command-line
============

smartypants
===========

``smartypants`` provides a command-line interface for :py:mod:`smartypants`
module, which is named as same as the module.

It takes input from either standard input or files and output the result to
standard output.


Usage
=====

Syntax::

  smartypants [-h] [-v] [-a ATTR] [-s SKIP] [FILE [FILE ...]]

Some examples::

  $ smartypants inputfile

  $ command-foo inputfile | command-bar | smartypants


Options
=======

``-a``, ``--attr``:
  processe attrbutes tells smartypants how to translate,

  The attributes is a string of characters, which are taken from the names of
  attributes of :py:class:`smartypants.Attr <smartypants._Attr>`.

  For example, the default attribute is
  :py:attr:`smartypants.Attr.set1 <smartypants._Attr.set1>`::

    smartypants -a 1

  If you want :py:attr:`smartypants.Attr.q <smartypants._Attr.q>` and
  :py:attr:`smartypants.Attr.w <smartypants._Attr.w>` then it would be invoked
  as::

    smartypants -a qw

``-s``, ``--skip``:
  skip specified HTML elements.

  It is a comma-separated string. For example::

    smartypants -s tag1,tag2,tag3

``FILE``:
  files to be processed.

  If no ``FILE`` is specified, the input is taken from standard input.
"""

from __future__ import print_function

import argparse
import sys
import warnings

import smartypants


def main():

    parser = argparse.ArgumentParser(description=smartypants.__description__,
                                     version=smartypants.__version__)
    parser.add_argument('-a', '--attr', default='1',
                        help='processing attributes (Default: %(default)s)')
    parser.add_argument('-s', '--skip',
                        default=','.join(smartypants.tags_to_skip),
                        help='skip HTML elements (Default: %(default)s)')
    parser.add_argument('files', metavar='FILE', type=argparse.FileType('r'),
                        nargs='*', help='files to be processed ')
    args = parser.parse_args()

    with warnings.catch_warnings(record=True) as w:
        attr = smartypants._str_attr_to_int(args.attr)
        if len(w):
            print(w[-1].message)
            sys.exit(1)

    smartypants.tags_to_skip = args.skip.split(',')

    if args.files:
        for f in args.files:
            print(smartypants.smartypants(f.read(), attr), end='')
    else:
        print(smartypants.smartypants(sys.stdin.read(), attr), end='')


if __name__ == '__main__':
    main()