# -*- coding: utf-8 -*-
#
# Desc:    This file is part of the ecromedos Document Preparation System
# Author:  Tobias Koch <tobias@tobijk.de>
# License: MIT
# URL:     http://www.ecromedos.net
#

import os, re
from lxml import etree
import com.lepture.mistune as mistune
from net.ecromedos.configreader import ECMDSConfigReader
from net.ecromedos.dtdresolver  import ECMDSDTDResolver

class ECMLRendererError(Exception):
    pass

class ECMLRenderer(mistune.Renderer):

    def __init__(self, config):
        mistune.Renderer.__init__(self)
        self.section_level = 0
        self.footnotes_map = {}
        self.config = config
    #end if

    # BLOCK ELEMENTS

    def block_code(self, code, language=None):
        if language == None:
            language = "text"

        return """<listing><code syntax="%(lang)s" strip="yes"
                tabspaces="4">%(code)s</code></listing>""" % {
            "lang": language,
            "code": mistune.escape(code)
        }
    #end function

    def block_quote(self, text):
        return "<blockquote>%s</blockquote>" % text

    def block_html(self, ecml):
        return ecml

    def header(self, text, level, raw=None):
        retval = ""

        diff = level - self.section_level

        if diff > 1:
            msg = "Heading '%s' skips a section level." % text
            raise ECMLRendererError(msg)
        else:
            sign = int(diff > 0) - int(diff < 0)
            diff = sign * diff