<commit_before>"""blogアプリで主に使用するフィルタ."""
import html
import html.parser
import re

from django import template
from django.utils import timezone
from django.utils.html import escape
from django.utils.safestring import mark_safe, SafeData
from blog.models import Image

register = template.Library()
html_parser = html.parser.HTMLParser()
SPLIT_CHAR = html.escape('<split>')


def url(text):
    """[filter url]http://...[end]を、aタグして解釈する.

    >>> url('[filter url]https://torina.top[end]')
    '<a target="_blank" rel="nofollow" href="https://torina.top" \
    rel="nofollow">https://torina.top</a>'

    """
    text = text.replace('<br />', '\n')
    text = text.replace('[filter url]', '').replace('[end]', '')
    if SPLIT_CHAR in text:
        url, text = text.split(SPLIT_CHAR)
        tag = '<a href="{0}" target="_blank" rel="nofollow">{1}</a>'.format(
            url, text)
    else:
        url = text
        tag = '<a href="{0}" target="_blank" rel="nofollow">{0}</a>'.format(url)
    return tag


def html(text):
    """[filter html]your_html[end]を、そのままHTMLとして解釈する.

    >>> html('[filter html]&lt;h1&gt;ヘロー&lt;/h1&gt;[end]')
    '<h1>ヘロー</h1>'

    """
    text = text.replace('<br />', '\n')
    text = text.replace('[filter html]', '').replace('[end]', '')
    tag = html_parser.unescape(text)
    return tag


def img(text):
    """http://spam.png等の画像URLを、imgタグに置換する.

    Bootstrap4に合わせたタグです

    >>> img('[filter img]https://torina.top/a.png[end]')
    '<a href="https://torina.top/a.png" target="_blank" rel="nofollow">\
    <img src="https://torina.top/a.png" class="img-fluid"/></a>'

    """
    text = text.replace('<br />', '\n')
    text = text.replace('[filter img]', '').replace('[end]', '')
    if SPLIT_CHAR in text:
        src, alt = text.split(SPLIT_CHAR)
        tag = (
            '<a href="{0}" target="_blank" rel="nofollow"><img src="{0}" '
            'class="img-fluid" alt="{1}"></a>'
        ).format(src, alt)
    else:
        tag = (
            '<a href="{0}" target="_blank" rel="nofollow"><img src="{0}" '
            'class="img-fluid"/></a>'
        ).format(text)
    return tag


def imgpk(text):
    """[filter imgpk]1[end]を、正しいimgタグにする

    1の部分は、Imageモデルインスタンスのpkとなります。
    """
    text = text.replace('<br />', '\n')
    text = text.replace('[filter imgpk]', '').replace('[end]', '')
    if SPLIT_CHAR in text:
        pk, alt = text.split(SPLIT_CHAR)
    else:
        pk, alt = text, ''

    try:
        src = Image.objects.get(pk=int(pk)).src.url
    except Exception:
        tag = '<img src="">'
    else:
        tag = (
            '<a href="{0}" target="_blank" rel="nofollow"><img src="{0}" '
            'class="img-fluid" alt="{1}"></a>'
        ).format(src, alt)
    return tag


def cord(text):
    """<pre>コード</pre>に置き換える.

    google-code-prettyfyに合わせたタグです

    >>> cord('[filter cord]hello<br />narito[end]')
    '<pre class="prettyprint linenums">hello\\nnarito</pre>'

    """
    text = text.replace('<br />', '\n')
    text = text.replace('[filter cord]', '').replace('[end]', '')
    tag = '<pre class="prettyprint linenums">{}</pre>'.format(text)
    return tag


def quote(text):
    """<blockquote>文字</blockquote>に置き換える.

    Bootstrap4にあわせたタグです

    >>> quote('[filter quote]narito[end]')
    '<blockquote class="blockquote"><p>narito</p></blockquote>'

    """
    text = text.replace('[filter quote]', '').replace('[end]', '')
    tag = '<blockquote class="blockquote"><p>{}</p></blockquote>'.format(text)
    return tag


def h2(text):
    """<h2 class="blog-h2">文字</h2>に置き換える."""
    text = text.replace('[filter h2]', '').replace('[end]', '')
    tag = '<h2 class="blog-h2">{0}</p>'.format(text)
    return tag


@register.filter(is_safe=True, needs_autoescape=True)
def blog(value, autoescape=True):
    """本文中の[filter name]text[end]を、適切なHTMLタグに変換する.
<commit_msg>h2の閉じタグを修正...<commit_after>"""blogアプリで主に使用するフィルタ."""
import html
import html.parser
import re

from django import template
from django.utils import timezone
from django.utils.html import escape
from django.utils.safestring import mark_safe, SafeData
from blog.models import Image

register = template.Library()
html_parser = html.parser.HTMLParser()
SPLIT_CHAR = html.escape('<split>')


def url(text):
    """[filter url]http://...[end]を、aタグして解釈する.

    >>> url('[filter url]https://torina.top[end]')
    '<a target="_blank" rel="nofollow" href="https://torina.top" \
    rel="nofollow">https://torina.top</a>'

    """
    text = text.replace('<br />', '\n')
    text = text.replace('[filter url]', '').replace('[end]', '')
    if SPLIT_CHAR in text:
        url, text = text.split(SPLIT_CHAR)
        tag = '<a href="{0}" target="_blank" rel="nofollow">{1}</a>'.format(
            url, text)
    else:
        url = text
        tag = '<a href="{0}" target="_blank" rel="nofollow">{0}</a>'.format(url)
    return tag


def html(text):
    """[filter html]your_html[end]を、そのままHTMLとして解釈する.

    >>> html('[filter html]&lt;h1&gt;ヘロー&lt;/h1&gt;[end]')
    '<h1>ヘロー</h1>'

    """
    text = text.replace('<br />', '\n')
    text = text.replace('[filter html]', '').replace('[end]', '')
    tag = html_parser.unescape(text)
    return tag


def img(text):
    """http://spam.png等の画像URLを、imgタグに置換する.

    Bootstrap4に合わせたタグです

    >>> img('[filter img]https://torina.top/a.png[end]')
    '<a href="https://torina.top/a.png" target="_blank" rel="nofollow">\
    <img src="https://torina.top/a.png" class="img-fluid"/></a>'

    """
    text = text.replace('<br />', '\n')
    text = text.replace('[filter img]', '').replace('[end]', '')
    if SPLIT_CHAR in text:
        src, alt = text.split(SPLIT_CHAR)
        tag = (
            '<a href="{0}" target="_blank" rel="nofollow"><img src="{0}" '
            'class="img-fluid" alt="{1}"></a>'
        ).format(src, alt)
    else:
        tag = (
            '<a href="{0}" target="_blank" rel="nofollow"><img src="{0}" '
            'class="img-fluid"/></a>'
        ).format(text)
    return tag


def imgpk(text):
    """[filter imgpk]1[end]を、正しいimgタグにする

    1の部分は、Imageモデルインスタンスのpkとなります。
    """
    text = text.replace('<br />', '\n')
    text = text.replace('[filter imgpk]', '').replace('[end]', '')
    if SPLIT_CHAR in text:
        pk, alt = text.split(SPLIT_CHAR)
    else:
        pk, alt = text, ''

    try:
        src = Image.objects.get(pk=int(pk)).src.url
    except Exception:
        tag = '<img src="">'
    else:
        tag = (
            '<a href="{0}" target="_blank" rel="nofollow"><img src="{0}" '
            'class="img-fluid" alt="{1}"></a>'
        ).format(src, alt)
    return tag


def cord(text):
    """<pre>コード</pre>に置き換える.

    google-code-prettyfyに合わせたタグです

    >>> cord('[filter cord]hello<br />narito[end]')
    '<pre class="prettyprint linenums">hello\\nnarito</pre>'

    """
    text = text.replace('<br />', '\n')
    text = text.replace('[filter cord]', '').replace('[end]', '')
    tag = '<pre class="prettyprint linenums">{}</pre>'.format(text)
    return tag


def quote(text):
    """<blockquote>文字</blockquote>に置き換える.

    Bootstrap4にあわせたタグです

    >>> quote('[filter quote]narito[end]')
    '<blockquote class="blockquote"><p>narito</p></blockquote>'

    """
    text = text.replace('[filter quote]', '').replace('[end]', '')
    tag = '<blockquote class="blockquote"><p>{}</p></blockquote>'.format(text)
    return tag


def h2(text):
    """<h2 class="blog-h2">文字</h2>に置き換える."""
    text = text.replace('[filter h2]', '').replace('[end]', '')
    tag = '<h2 class="blog-h2">{0}</h2>'.format(text)
    return tag


@register.filter(is_safe=True, needs_autoescape=True)
def blog(value, autoescape=True):
    """本文中の[filter name]text[end]を、適切なHTMLタグに変換する.

{'fraction_non_alphanumeric': 0.21909456479439615, 'fraction_numerical': 0.007523673628226748, 'mean_word_length': 3.1630669546436283, 'pattern_counts': {'":': 0, '<': 99, '<?xml version=': 0, '>': 129, 'https://': 12, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7034887', 'n_tokens_mistral': 3012, 'n_tokens_neox': 2919, 'n_words': 492}