all repos — nasg @ 67662b69e95840b66b0da682fd2a55a96d2f8c27

pandoc.py (view raw)

 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
__author__ = "Peter Molnar"
__copyright__ = "Copyright 2017-2019, Peter Molnar"
__license__ = "apache-2.0"
__maintainer__ = "Peter Molnar"
__email__ = "mail@petermolnar.net"

import subprocess
import logging

class PandocMarkdown(str):
   def __new__(cls, text):
        """ Pandoc command line call with piped in- and output """
        cmd = (
            'pandoc',
            '-o-',
            '--from=markdown+%s' % (
                '+'.join([
                    'footnotes',
                    'pipe_tables',
                    'strikeout',
                    #'superscript',
                    #'subscript',
                    'raw_html',
                    'definition_lists',
                    'backtick_code_blocks',
                    'fenced_code_attributes',
                    'shortcut_reference_links',
                    'lists_without_preceding_blankline',
                    'autolink_bare_uris',
                ])
            ),
            '--to=html5',
            '--quiet',
            '--no-highlight'
        )
        p = subprocess.Popen(
            cmd,
            stdin=subprocess.PIPE,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
        )

        stdout, stderr = p.communicate(input=text.encode())
        if stderr:
            logging.warning(
                "Error during pandoc covert:\n\t%s\n\t%s",
                cmd,
                stderr
            )
        r = stdout.decode('utf-8').strip()
        return str.__new__(cls, r)

class PandocHTML(str):
   def __new__(cls, text):
        """ Pandoc command line call with piped in- and output """
        cmd = (
            'pandoc',
            '-o-',
            '--to=markdown+%s' % (
                '+'.join([
                    'footnotes',
                    'pipe_tables',
                    'strikeout',
                    #'superscript',
                    #'subscript',
                    'raw_html',
                    'definition_lists',
                    'backtick_code_blocks',
                    'fenced_code_attributes',
                    'shortcut_reference_links',
                    'lists_without_preceding_blankline',
                    'autolink_bare_uris',
                ])
            ),
            '--from=html',
            '--quiet',
        )
        p = subprocess.Popen(
            cmd,
            stdin=subprocess.PIPE,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
        )

        stdout, stderr = p.communicate(input=text.encode())
        if stderr:
            logging.warning(
                "Error during pandoc covert:\n\t%s\n\t%s",
                cmd,
                stderr
            )
        r = stdout.decode('utf-8').strip()
        return str.__new__(cls, r)