# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import re
import markdown

from django import template
from django.template.defaultfilters import stringfilter
from django.utils.encoding import force_text
from django.utils.safestring import mark_safe, SafeText

register = template.Library()
youtube_url = re.compile(r'https://(?:www\.)?youtu(?:be\.com/watch/?\?v=|\.be/)([^/<&]+)(?:&.*)?')
youtube_iframe = """
<iframe id="youtube-\\1" type="text/html" width="640" height="420"
src="https://www.youtube.com/embed/\\1"
frameborder="0"></iframe>
"""


@register.filter(is_safe=False, name='markdown')
@stringfilter
def my_markdown(value):
    extensions = ["nl2br", "extra", "codehilite", "headerid(level=2)", "sane_lists"]

    html = mark_safe(
        markdown.markdown(
            force_text(value).replace("\\\\", "\\\\\\\\"),
            extensions,
            safe_mode='escape',
            enable_attributes=False,
            output_format="html5"
        )
    )
    return SafeText(youtube_url.sub(youtube_iframe, html))

    class MarkdownDemoNode(template.Node):
        def __init__(self, nodelist):
            self.nodelist = nodelist

            def render(self, context):
                a = map(
                    lambda x: list(map(lambda y: y.strip(), x.render(context).split('\n'))),
                    self.nodelist
                )

                input_text = '\n'.join(sum(list(a), []))

                uid = "d" + hex(abs(hash(input_text)))[2:]
                rendered = my_markdown(input_text)
                input_text = input_text.replace('>', '&gt;').replace('<', '&lt;')
                return """
                <dl class="tabs" data-tab>
                <dd class="active"><a href="#%smd">Markdown</a></dd>
                <dd><a href="#%srender">Aperçu</a></dd>
                </dl>
                <div class="tabs-content">
                <div class="content active" id="%smd"><pre class="codehilite">%s</pre></div>
                <div class="content" id="%srender">%s</div>
                </div>""" % (uid, uid, uid, input_text, uid, rendered)

                @register.tag(name='markdown_demo')
                def do_comment(parser, token):
                    nodelist = parser.parse(('end_markdown_demo',))
                    parser.delete_first_token()
                    return MarkdownDemoNode(nodelist)

@@ -1044,20 +1044,17 @@
 html))%0A%0A
-    
+%0A
 class Ma
@@ -1084,28 +1084,24 @@
 .Node):%0A    
-   
- 
 def __init__
@@ -1118,36 +1118,32 @@
 elist):%0A        
-    
 self.nodelist = 
@@ -1152,24 +1152,16 @@
 delist%0A%0A
-        
     def 
@@ -1183,24 +1183,16 @@
 ntext):%0A
-        
         
@@ -1212,24 +1212,16 @@
         
- 
-       
 lambda x
@@ -1297,24 +1297,16 @@
         
-        
 self.nod
@@ -1323,34 +1323,19 @@
     
-    
-    )%0A%0A       
+)%0A%0A
         
  inp
@@ -1322,33 +1322,32 @@
      )%0A%0A        
- 
 input_text = '%5Cn
@@ -1380,24 +1380,16 @@
         
-       
- 
 uid = %22d
@@ -1431,24 +1431,16 @@
         
-      
-  
 rendered
@@ -1466,24 +1466,16 @@
 t_text)%0A
-        
         
@@ -1544,32 +1544,24 @@
 ;')%0A        
-     
-   
 return %22%22%22%0A 
@@ -1559,24 +1559,16 @@
 urn %22%22%22%0A
-        
         
@@ -1602,24 +1602,16 @@
         
-    
-    
 %3Cdd clas
@@ -1651,32 +1651,24 @@
 wn%3C/a%3E%3C/dd%3E%0A
-        
         %3Cdd%3E
@@ -1715,30 +1715,14 @@
     
-  
-      %3C/dl%3E%0A        
+%3C/dl%3E%0A
     
@@ -1752,24 +1752,16 @@
 ntent%22%3E%0A
-        
         
@@ -1833,32 +1833,24 @@
 /pre%3E%3C/div%3E%0A
-        
         %3Cdiv
@@ -1889,24 +1889,16 @@
 s%3C/div%3E%0A
-        
         
@@ -1953,32 +1953,17 @@
 dered)%0A%0A
-                
+%0A
 @registe
@@ -1990,32 +1990,16 @@
 _demo')%0A
-                
 def do_c
@@ -2021,32 +2021,16 @@
 token):%0A
-                
     node
@@ -2073,32 +2073,16 @@
 emo',))%0A
-                
     pars
@@ -2105,32 +2105,16 @@
 token()%0A
-                
     retu

Fix indentation