import logging

from wok import util

# Check for pygments
try:
    import pygments
    have_pygments = True
except ImportError:
    logging.info('Pygments not enabled.')
    have_pygments = False

# List of available renderers
all = []

class Renderer(object):
    extensions = []

    @classmethod
    def render(cls, plain):
        return plain
all.append(Renderer)

class Plain(Renderer):
    """Plain text renderer. Replaces new lines with html </br>s"""
    extensions = ['txt']

    @classmethod
    def render(cls, plain):
        return plain.replace('\n', '<br>')
all.append(Plain)

# Include markdown, if it is available.
try:
    # Try to get the faster cMarkdown
    try:
        from cMarkdown import markdown
    except ImportError:
        from markdown import markdown

    class Markdown(Renderer):
        """Markdown renderer."""
        extensions = ['markdown', 'mkd', 'md']

        plugins = ['def_list', 'footnotes']
        if have_pygments:
            plugins.append('codehilite(css_class=highlight)')

        @classmethod
        def render(cls, plain):
            return markdown(plain, Markdown.plugins)

    all.append(Markdown)

except ImportError:
    logging.debug("markdown isn't available, trying markdown2")
    markdown = None

# Try Markdown2
if markdown is None:
    try:
        import markdown2
        class Markdown(Renderer):
            """Markdown2 renderer."""
            extensions = ['markdown', 'mkd', 'md']

            extras = ['def_list', 'footnotes']
            if have_pygments:
                extras.append('fenced-code-blocks')

            @classmethod
            def render(cls, plain):
                return markdown2.markdown(plain, extras=extras)

        all.append(Markdown2)
    except:
        logging.info('Markdown not enabled.')


# Include ReStructuredText Parser, if we have docutils
try:

    import docutils.core
    from docutils.writers.html4css1 import Writer as rst_html_writer
    from docutils.parsers.rst import directives

    if have_pygments:
        from wok.rst_pygments import Pygments as RST_Pygments
        directives.register_directive('Pygments', RST_Pygments)

    class ReStructuredText(Renderer):
        """reStructuredText renderer."""
        extensions = ['rst']

        @classmethod
        def render(cls, plain):
            w = rst_html_writer()
            return docutils.core.publish_parts(plain, writer=w)['body']

    all.append(ReStructuredText)
except:
    logging.info('reStructuredText not enabled.')


# Try Textile
try:
    import textile
    class Textile(Renderer):
        """Textile renderer."""
        extensions = ['textile']

        @classmethod
        def render(cls, plain):
            return textile.textile(plain)

    all.append(Markdown2)
except:
    logging.info('Markdown not enabled.')



if len(all) <= 2:
    print("You probably want to install either a Markdown library (one of "
          "'Markdown', or 'markdown2'), 'docutils' (for reStructuredText), or "
          "'textile'. Otherwise only plain text input will be supported.  You "
          "can install any of these like 'sudo pip install PACKAGE'.")

@@ -1113,24 +1113,19 @@
 (plain, 
-Markdown
+cls
 .plugins
@@ -1343,32 +1343,33 @@
   class Markdown
+2
 (Renderer):%0A    
@@ -1701,16 +1701,20 @@
  extras=
+cls.
 extras)%0A

Fix markdown2 renderer: