
# Copyright (c) 2006 Duncan Cragg

import re
from django import template
from django.utils.html import escape
from django.template.defaultfilters import truncatewords

SUMM_RE=re.compile( r"\$\[(?P<summ_text>.+?)\]\$", re.DOTALL)
LINK_RE=re.compile(   r"\[(?P<link_text>[^\[]+?)\]\[(?P<link_link>\w+?://[^ ]+?)\]")
URLS_RE=re.compile(                             r"\[(?P<link_link>\w+?://[^ ]+?)\]")
ITAL_RE=re.compile( r"\/\[(?P<ital_text>.+?)\]\/", re.DOTALL)
BOLD_RE=re.compile(  r"!\[(?P<bold_text>.+?)\]!", re.DOTALL)
MONO_RE=re.compile( r"\|\[(?P<mono_text>.+?)\]\|")
CODE_RE=re.compile( r"\|\[(?P<code_text>.+?)\]\|", re.DOTALL)
TABL_RE=re.compile( r"\+\[(?P<tabl_text>.+?)\]\+", re.DOTALL)
TROW_RE=re.compile(r"\s*=\[(?P<lcel_text>.+?)\|(?P<rcel_text>.+?)\]=\s*", re.DOTALL)
QUOT_RE=re.compile( r"\[\[(?P<quot_text>.+?)\]\]", re.DOTALL)
ULIS_RE=re.compile(  r"-\[(?P<ulis_text>.+?)\]-", re.DOTALL)
LIS1_RE=re.compile(r"\r\n\r\n[ \t]*[*\-+]:*")
LIST_RE=re.compile(    r"\r\n[ \t]*[*\-+]:*")
PAR3_RE=re.compile(r"\r\n\r\n\r\n", re.DOTALL)
PARA_RE=re.compile(r"\r\n\r\n", re.DOTALL)
PIC1_RE=re.compile(r"\[#\]")
PIC2_RE=re.compile(r"\[##\]")
PIC3_RE=re.compile(r"\[###\]")

def firstpart(text):
    m = SUMM_RE.search(text)
    return (m and m.group('summ_text')) or text

def dewikify(text, post):
    text = SUMM_RE.sub(summify, text)
    text = LINK_RE.sub(linkify, text)
    text = URLS_RE.sub(urlsify, text)
    text = ITAL_RE.sub(italify, text)
    text = BOLD_RE.sub(boldify, text)
    text = MONO_RE.sub(monoify, text)
    text = CODE_RE.sub(codeify, text)
    text = TABL_RE.sub(tablify, text)
    text = TROW_RE.sub(trowify, text)
    text = QUOT_RE.sub(quotify, text)
    text = ULIS_RE.sub(ulisify, text)
    text = LIS1_RE.sub(listify, text)
    text = LIST_RE.sub(listify, text)
    text = PAR3_RE.sub(par3ify, text)
    text = PARA_RE.sub(paraify, text)
    if hasattr(post, 'picture') and post.picture:
        url=post.get_picture_url()
        pic1ify='<a href="%s"><img class="photo post float-right" src="%s" alt="picture" height="140" /></a>' % (url,url)
        pic2ify='<a href="%s"><img class="photo post resizeable"  src="%s" alt="picture" width="300"  /></a>' % (url,url)
        pic3ify='<a href="%s"><img class="photo post"             src="%s" alt="picture"              /></a>' % (url,url)
        text = PIC1_RE.sub(pic1ify, text)
        text = PIC2_RE.sub(pic2ify, text)
        text = PIC3_RE.sub(pic3ify, text)
    return text

def summify(match):
    return match.expand('</p><div class="summary"><p>\g<summ_text></p></div><p>')

def linkify(match):
    return match.expand('<a href="\g<link_link>">\g<link_text></a>')

def urlsify(match):
    return match.expand('<a href="\g<link_link>">\g<link_link></a>')

def italify(match):
    return match.expand('<i>\g<ital_text></i>')

def boldify(match):
    return match.expand('<b>\g<bold_text></b>')

def monoify(match):
    return match.expand('<code>\g<mono_text></code>')

def codeify(match):
    return match.expand('</p><pre>\g<code_text></pre><p>')

def tablify(match):
    return match.expand('</p><table class="two-col-table">\g<tabl_text></table><p>')

def trowify(match):
    return match.expand('<tr><td><p>\g<lcel_text></p></td><td><p>\g<rcel_text></p></td></tr>\r\n')

def quotify(match):
    return match.expand('</p><blockquote class="others-content"><div><p>\g<quot_text></p></div></blockquote><p>')

def ulisify(match):
    list = match.expand('\g<ulis_text>')
    items = list.split('\r\n')
    text = ''
    for item in items:
        if item:
            text += '<li>' + item + '</li>\r\n'
    return '</p><ul>\r\n'+text+'</ul><p>'

def par3ify(match):
    return match.expand('\r\n</p><p>&#160;</p><p>\r\n')

def paraify(match):
    return match.expand('\r\n</p><p>\r\n')

def listify(match):
    return match.expand('\r\n</p><p>&#160;&#160;&#160;<b>&#8226;</b>&#160;')

#-----------------------------------------------------------------------------------

PARN_RE=re.compile(r"\r\n\r\n(\r\n)*")
PAR1_RE=re.compile(r"\r\n")
EMPT_RE=re.compile(r"\"\",")
P2NL_RE=re.compile(r"UXONPARAMARKER")

def uxonify(text, post):
    text = PARN_RE.sub(parnify, text)
    text = ULIS_RE.sub(ulijify, text)
    text = PAR1_RE.sub(par1ify, text)
    text = SUMM_RE.sub(sumjify, text)
    text = TABL_RE.sub(tabjify, text)
    text = TROW_RE.sub(trojify, text)
    text = EMPT_RE.sub(emptify, text)
    text = P2NL_RE.sub(p2nlify, text)
    return text

def parnify(match):
    return match.expand('UXONPARAMARKER')

def ulijify(match):
    list = match.expand('\g<ulis_text>')
    items = list.split('\r\n')
    text = ''
    for item in items:
        if item:
            text += '"' + item + '", '
    return '", ['+text+'], "'

def par1ify(match):
    return match.expand(' ')

def sumjify(match):
    return match.expand('",\r\n    "\g<summ_text>",\r\n    "')

def tabjify(match):
    return match.expand('",\r\n    [ \g<tabl_text> ], \r\n    "')

def trojify(match):
    return match.expand(' [ "\g<lcel_text>", "\g<rcel_text>" ], \r\n')

def emptify(match):
    return match.expand('')

def p2nlify(match):
    return match.expand('",\r\n    "')

#-----------------------------------------------------------------------------------

def content_process(post, mode):
    if mode == "summary":
        rend='\r\n<p>\r\n'+dewikify(escape(firstpart(post.content)), post) + ' &#160; ...\r\n</p>\r\n'
    elif mode == "atomjson":
        rend=uxonify('[ "'+escape(post.content)+'" ]', post)
    else:
        rend='\r\n<p>\r\n'+dewikify(escape(          post.content),  post) + '\r\n</p>\r\n'
    return rend

#-----------------------------------------------------------------------------------

def nbspify(fixable):
    return str(fixable).replace(" ", "&#160;")   

register = template.Library()
register.filter('content_process', content_process)
register.filter('nbspify',         nbspify)


