<commit_before># -*- coding: utf-8 -*-
"""
@file
@brief Defines blogpost directives.
See `Tutorial: Writing a simple extension <http://sphinx-doc.org/extdev/tutorial.html>`_
"""

from docutils import nodes
from docutils.parsers.rst import Directive
from sphinx.util.compat import make_admonition
from sphinx.locale import _ as _locale
from docutils.parsers.rst import directives
from sphinx import addnodes
from sphinx.util.nodes import set_source_info, process_index_entry
from .blog_post import BlogPost


class blogpost_node(nodes.Admonition, nodes.Element):

    """
    defines *blogpost* node
    """
    pass


class blogpostagg_node(nodes.Admonition, nodes.Element):

    """
    defines *blogpostagg* node
    """
    pass


class BlogPostDirective(Directive):

    """
    extracts information about a blog post described by a directive ``.. blogpost::``
    and modifies the documentation if *env* is not null
    """
    required_arguments = 0
    optional_arguments = 0
    final_argument_whitespace = True
    option_spec = {'date': directives.unchanged,
                   'title': directives.unchanged,
                   'keywords': directives.unchanged,
                   'categories': directives.unchanged,
                   'author': directives.unchanged,
                   }
    has_content = True
    add_index = True
    blogpost_class = blogpost_node

    def _make_ad(self):
        """
        private function
        """
        ad = make_admonition(self.__class__.blogpost_class,
                             self.name,
                             [_locale(self.options["date"]) + " "],
                             self.options,
                             self.content,
                             self.lineno,
                             self.content_offset,
                             self.block_text,
                             self.state,
                             self.state_machine)
        return ad

    def run(self):
        """
        extracts the information in a dictionary and displays it
        if the environment is not null

        @return      a list of nodes
        """
        sett = self.state.document.settings

        if hasattr(sett, "out_blogpostlist"):
            sett.out_blogpostlist.append(self)

        if hasattr(self.state.document.settings, "env"):
            env = self.state.document.settings.env
        else:
            env = None

        if env is None:
            return []

        # post
        p = {
            'docname': env.docname,
            'lineno': self.lineno,
            'date': self.options["date"],
            'title': self.options["title"],
            'keywords': [_.strip() for _ in self.options["keywords"].split(",")],
            'categories': [_.strip() for _ in self.options["categories"].split(",")],
        }

        # label
        #targetid = "blogpost-%d" % env.new_serialno('blogpost')
        #targetnode = nodes.target('', '', ids=[targetid])
        #p["target"] = targetnode

        tag = BlogPost.build_tag(p["date"], p["title"])
        targetnode = nodes.target('', '', ids=[tag])
        p["target"] = targetnode

        ad = self._make_ad()

        p['blogpost'] = ad[0].deepcopy()

        if not hasattr(env, 'blogpost_all'):
            env.blogpost_all = []
        env.blogpost_all.append(p)

        # we add a title
        section = nodes.section()
        section += nodes.title(text=p["title"])

        # index (see site-packages/sphinx/directives/code.py, class Index)
        if self.__class__.add_index:

            # we add an index
            self.state.document.note_explicit_target(targetnode)
            indexnode = addnodes.index()
            indexnode['entries'] = ne = []
            indexnode['inline'] = False
            set_source_info(self, indexnode)
            for entry in set(p["keywords"] + p["categories"] + [p["date"]]):
                ne.extend(process_index_entry(entry, tag))  # targetid))
            ns = [indexnode, targetnode, section]
        else:
            ns = [targetnode, section]

        return ns + ad


class BlogPostDirectiveAgg(BlogPostDirective):

    """
    same but for the same post in a aggregated pages<commit_msg>failed try<commit_after># -*- coding: utf-8 -*-
"""
@file
@brief Defines blogpost directives.
See `Tutorial: Writing a simple extension <http://sphinx-doc.org/extdev/tutorial.html>`_
"""

from docutils import nodes
from docutils.parsers.rst import Directive
from sphinx.util.compat import make_admonition
from sphinx.locale import _ as _locale
from docutils.parsers.rst import directives
from sphinx import addnodes
from sphinx.util.nodes import set_source_info, process_index_entry
from .blog_post import BlogPost


class blogpost_node(nodes.Admonition, nodes.Element):

    """
    defines *blogpost* node
    """
    pass


class blogpostagg_node(nodes.Admonition, nodes.Element):

    """
    defines *blogpostagg* node
    """
    pass


class BlogPostDirective(Directive):

    """
    extracts information about a blog post described by a directive ``.. blogpost::``
    and modifies the documentation if *env* is not null
    """
    required_arguments = 0
    optional_arguments = 0
    final_argument_whitespace = True
    option_spec = {'date': directives.unchanged,
                   'title': directives.unchanged,
                   'keywords': directives.unchanged,
                   'categories': directives.unchanged,
                   'author': directives.unchanged,
                   }
    has_content = True
    add_index = True
    blogpost_class = blogpost_node

    def _make_ad(self):
        """
        private function
        """
        ad = make_admonition(self.__class__.blogpost_class,
                             self.name,
                             [_locale(self.options["date"]) + " "],
                             self.options,
                             self.content,
                             self.lineno,
                             self.content_offset,
                             self.block_text,
                             self.state,
                             self.state_machine)
        return ad

    def run(self):
        """
        extracts the information in a dictionary and displays it
        if the environment is not null

        @return      a list of nodes
        """
        sett = self.state.document.settings

        if hasattr(sett, "out_blogpostlist"):
            sett.out_blogpostlist.append(self)

        if hasattr(self.state.document.settings, "env"):
            env = self.state.document.settings.env
        else:
            env = None

        if env is None:
            return []

        # post
        p = {
            'docname': env.docname,
            'lineno': self.lineno,
            'date': self.options["date"],
            'title': self.options["title"],
            'keywords': [_.strip() for _ in self.options["keywords"].split(",")],
            'categories': [_.strip() for _ in self.options["categories"].split(",")],
        }

        # label
        #targetid = "blogpost-%d" % env.new_serialno('blogpost')
        #targetnode = nodes.target('', '', ids=[targetid])
        #p["target"] = targetnode

        tag = BlogPost.build_tag(p["date"], p["title"])
        targetnode = nodes.target('', '', ids=[tag])
        p["target"] = targetnode

        ad = self._make_ad()

        p['blogpost'] = ad[0].deepcopy()

        if not hasattr(env, 'blogpost_all'):
            env.blogpost_all = []
        env.blogpost_all.append(p)

        # we add a title
        section = nodes.section()
        section += nodes.title(text=p["title"])

        # create a reference
        refnode = nodes.reference('', '', internal=True)
        refnode['refid'] = tag
        refnode['reftitle'] = p["title"]
        # still does not work

        # index (see site-packages/sphinx/directives/code.py, class Index)
        if self.__class__.add_index:

            # we add an index
            self.state.document.note_explicit_target(targetnode)
            indexnode = addnodes.index()
            indexnode['entries'] = ne = []
            indexnode['inline'] = False
            set_source_info(self, indexnode)
            for entry in set(p["keywords"] + p["categories"] + [p["date"]]):
                ne.extend(process_index_entry(entry, tag))  # targetid))
            ns = [indexnode, targetnode, section]
        else:
            ns = [targetnode, section]

        return ns + ad


class BlogPostDirectiveAgg(BlogPostDirective):

    """
    same but for the same post in a aggregated pages
{'fraction_non_alphanumeric': 0.11292564280750521, 'fraction_numerical': 0.000926569376882094, 'mean_word_length': 1.8957075788061704, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, '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': '1784660', 'n_tokens_mistral': 2441, 'n_tokens_neox': 2326, 'n_words': 560}