summaryrefslogtreecommitdiff
path: root/smartypants
blob: 189adf5e208a46bd9aee7e11a6ca1dcff1dfe61d (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python
# Copyright (c) 2013, 2014 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 _str_attr_to_int(str_attr):
    """
    Convert str-type attr into int

    >>> f = _str_attr_to_int
    >>> f('q') == Attr.q
    True
    >>> f('1') == Attr.set1
    True
    >>> with warnings.catch_warnings(record=True) as w:
    ...     f('bz')
    ...     len(w)
    ...     print(w[-1].message)
    2
    1
    Unknown attribute: z
    """
    attr = 0
    for c in str_attr:
        if '0' <= c <= '3':
            c = 'set' + c
        if not hasattr(smartypants.Attr, c):
            warnings.warn('Unknown attribute: %s' % c, Warning)
            continue
        attr |= getattr(smartypants.Attr, c)

    return attr


def main():

    parser = argparse.ArgumentParser(description=smartypants.__description__)
    parser.add_argument('-v', '--version', action='version',
                        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 = _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()