# -*- coding: utf-8 -*-

"""

-------------------------------------------------
   File Name：     custom_markdown.py
   Description :
   Author :       JHao
   date：          2016/10/11
-------------------------------------------------
   Change Activity:
                   2016/10/11:markdown渲染
                   2016/10/11:添加分页
-------------------------------------------------
"""

__author__ = 'JHao'

import markdown
from django import template
from django.template.defaultfilters import stringfilter
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
from django.utils.safestring import mark_safe

register = template.Library()


@register.filter
def slice_list(value, index):
    return value[index]


@register.filter(is_safe=True)
@stringfilter
def custom_markdown(value):
    return mark_safe(markdown.markdown(value, extensions=['markdown.extensions.fenced_code',
                                                          'markdown.extensions.codehilite',
                                                          'markdown.extensions.tables'],
                                       safe_mode=True, enable_attributes=False))


@register.filter(is_safe=True)
@stringfilter
def abstract_content(value):
    import re
    raw = value
    value = ' '.join(re.findall(u'\s*(.*?)\s*```.*?```', value, re.DOTALL))  # 去掉markdown代码块
    value = re.sub(r'##|###|\s*', '', value)
    return raw if not value else value


@register.simple_tag(takes_context=True)
def paginate(context, object_list, page_count):
    left = 3
    right = 3

    paginator = Paginator(object_list, page_count)
    page = context['request'].GET.get('page')

    try:
        object_list = paginator.page(page)
        context['current_page'] = int(page)
        pages = get_left(context['current_page'], left, paginator.num_pages) + get_right(context['current_page'], right,
                                                                                         paginator.num_pages)
    except PageNotAnInteger:
        object_list = paginator.page(1)
        context['current_page'] = 1
        pages = get_right(context['current_page'], right, paginator.num_pages)
    except EmptyPage:
        object_list = paginator.page(paginator.num_pages)
        context['current_page'] = paginator.num_pages
        pages = get_left(context['current_page'], left, paginator.num_pages)

    context['post_list'] = object_list
    context['pages'] = pages
    context['last_page'] = paginator.num_pages
    context['first_page'] = 1
    try:
        context['pages_first'] = pages[0]
        context['pages_last'] = pages[-1] + 1
    except IndexError:
        context['pages_first'] = 1
        context['pages_last'] = 2

    return ''  # 必须加这个，否则首页会显示个None


def get_left(current_page, left, num_pages):
    if current_page == 1:
        return []
    elif current_page == num_pages:
        l = [i - 1 for i in range(current_page, current_page - left, -1) if i - 1 > 1]
        l.sort()
        return l
    l = [i for i in range(current_page, current_page - left, -1) if i > 1]
    l.sort()
    return l


def get_right(current_page, right, num_pages):
    if current_page == num_pages:
        return []
    return [i + 1 for i in range(current_page, current_page + right - 1) if i < num_pages - 1]
