#!python
# encoding=utf-8
# pylint: disable=C0103
"""Pandoc filter that replaces non-typographic quotes with typographic ones.

Copyright (c) 2018 Odin Kroeger

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
OR OTHER DEALINGS IN THE SOFTWARE.
"""

import sys

from logging import basicConfig, warning
from argparse import ArgumentParser
from os.path import basename
from panflute import run_filter
from pandoc_quotes import QuoMarkReplacer, Error # pylint: disable = E0401

DESCRIPTION = ('Pandoc filter that replaces plain quotation marks with '
               'typographic ones. Respects the language of the document, '
               'as set by the "lang" attribute. Defaults to "en-US". '
               'See pandoc-quotes(1) for details.')

DEBUG = True

def main(doc=None):
    """Replaces plain quotation marks with typgraphic ones.

    See ``replace_quotes`` above for details. ``main`` only
    provides a command line interface.

    Arguments:
        doc (``panflute.Doc``):
            The document in which to replace quotes.
    """
    basicConfig(format='{prog}: %(msg)s'.format(prog=basename(sys.argv[0])))
    parser = ArgumentParser(description=DESCRIPTION)
    parser.parse_known_args()
    try:
        return run_filter(QuoMarkReplacer(), doc=doc)
    except Error as err:
        if DEBUG:
            raise
        warning(str(err))
        sys.exit(1)


if __name__ == '__main__':
    main()
