#!/usr/bin/python
# -*- coding: UTF-8 -*-

###############################################################################
#                                                                             #
# Bazaar Browser - A simple web application for browsing Bazaar repository    #
#                                                                             #
# Copyright (C) 2011-2012  Fabien LOISON <http://www.flogisoft.com/>          #
#                                                                             #
# This program is free software: you can redistribute it and/or modify        #
# it under the terms of the GNU Affero General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or           #
# (at your option) any later version.                                         #
#                                                                             #
# This program is distributed in the hope that it will be useful,             #
# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
# GNU Affero General Public License for more details.                         #
#                                                                             #
# You should have received a copy of the GNU Affero General Public License    #
# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
#                                                                             #
###############################################################################


"""Some filters for templates."""


from re import sub

from flask import Markup

from application import app


@app.template_filter("dtformat")
def datetime_format(dt):
    """Makes human readable dates"""
    return dt.strftime("%A %d %B %Y, %H:%M:%S")


@app.template_filter("hideemail")
def hide_email(text):
    """Hide emails for avoiding spam"""
    return sub(
            r"<(.+)@(..?.?).*?(.?)\.([a-zA-Z0-9]{2,8}>)",
            r"<\1 AT \2[...]\3 DOT \4",
            text,
            )


@app.template_filter("rmemail")
def remove_email(text):
    """Hide emails for avoiding spam"""
    return sub(
            r"\s*<(.+)@(..?.?).*?(.?)\.([a-zA-Z0-9]{2,8}>)", r"", text)


@app.template_filter("parsereadme")
def parse_readme(text):
    """Hide emails for avoiding spam"""
    #Hide emails
    text = hide_email(text)
    #Esxape html
    text = text.replace("&", "&amp;")
    text = text.replace(">", "&gt;")
    text = text.replace("<", "&lt;")
    #Links
    text = sub(
            r"([a-z0-9]+://)(.+?)(&lt;|&gt;|\(|\)|\[|\]|\[|\}|\s|$|'|\")",
            r'<a href="\1\2">\1\2</a>\3',
            text,
            )
    text = sub(
            r"([^/])(www\.)(.+?)(&lt;|&gt;|\(|\)|\[|\]|\[|\}|\s|$|'|\")",
            r'\1<a href="http://\2\3">\2\3</a>\4',
            text,
            )
    text = sub(
            r"([^/])(ftp\.)(.+?)(&lt;|&gt;|\(|\)|\[|\]|\[|\}|\s|$|'|\")",
            r'\1<a href="ftp://\2\3">\2\3</a>\4',
            text,
            )
    return Markup(text)
