# Path: statik/errors.py
# class StatikErrorContext(object):
#     """Useful for representing context when exceptions are thrown (e.g. the name of
#     the current file being processed, line number, etc.)"""
#     
#     def __init__(self, filename=None, line_no=None):
#         self.filename = filename
#         self.line_no = line_no
# 
#     def render(self):
#         return ("in file \"%s\"%s" % (
#             self.filename,
#             (", line %d" % self.line_no) if self.line_no is not None else ""
#         )) if self.filename else None
#     
#     def update(self, filename=None, line_no=None):
#         self.filename = filename
#         self.line_no = line_no
#         
#         # line number is irrelevant now if no filename
#         if filename is None:
#             self.filename, self.line_no = None, None
#     
#     def clear(self):
#         self.filename, self.line_no = None, None
# 
# Path: statik/utils.py
# def strip_el_text(el, max_depth=0, cur_depth=0):
#     """Recursively strips the plain text out of the given XML etree element up to the desired depth.
# 
#     Args:
#         el: The etree element to scan.
#         max_depth: The depth to which to recursively strip text (default: 0).
#         cur_depth: The current recursive depth to which we've scanned so far.
# 
#     Returns:
#         The stripped, plain text from within the element.
#     """
#     # text in front of any child elements
#     el_text = strip_str(el.text if el.text is not None else "")
# 
#     if cur_depth < max_depth:
#         for child in el:
#             el_text += " "+strip_el_text(child, max_depth=max_depth, cur_depth=cur_depth+1)
#     else:
#         # add the last child's tail text, if any
#         children = list(el)
#         if children is not None and len(children) > 0:
#             if children[-1].tail is not None:
#                 el_text += " "+strip_str(children[-1].tail)
# 
#     # we skip the root element
#     if cur_depth > 0:
#         # in case there's any text at the end of the element
#         if el.tail is not None:
#             el_text += " "+strip_str(el.tail)
# 
#     return strip_str(el_text)


from slugify import slugify
from markdown.preprocessors import Preprocessor
from markdown.extensions import Extension
from markdown.treeprocessors import Treeprocessor
from markdown.util import etree
from statik.errors import StatikErrorContext
from statik.utils import strip_el_text
import re
import yaml
import lipsum

    def __init__(self, *args, **kwargs):
        self.permalink_text = kwargs.pop('permalink_text', None)
        self.permalink_class = kwargs.pop('permalink_class', None)
        self.permalink_title = kwargs.pop('permalink_title', None)
        super(MarkdownPermalinkProcessor, self).__init__(*args, **kwargs)

    def run(self, root):
        insert_permalinks(
            root,
            permalink_text=self.permalink_text,
            permalink_class=self.permalink_class,
            permalink_title=self.permalink_title
        )


class MarkdownLoremIpsumProcessor(Preprocessor):

    # we search for a single command on a line
    REGEXP = re.compile(r"^!\(lipsum-(?P<kind>word[s]?|sentence[s]?|paragraph[s]?)(:(?P<count>\d+))?\)\w*$")
    GENERATORS = {
        "word": (lipsum.generate_words, 1),
        "words": (lipsum.generate_words, None),
        "sentence": (lipsum.generate_sentences, 1),
        "sentences": (lipsum.generate_sentences, None),
        "paragraph": (lipsum.generate_paragraphs, 1),
        "paragraphs": (lipsum.generate_paragraphs, None)
    }

    def __init__(self, *args, **kwargs):

# Path: statik/errors.py
# class StatikErrorContext(object):
#     """Useful for representing context when exceptions are thrown (e.g. the name of
#     the current file being processed, line number, etc.)"""
#     
#     def __init__(self, filename=None, line_no=None):
#         self.filename = filename
#         self.line_no = line_no
# 
#     def render(self):
#         return ("in file \"%s\"%s" % (
#             self.filename,
#             (", line %d" % self.line_no) if self.line_no is not None else ""
#         )) if self.filename else None
#     
#     def update(self, filename=None, line_no=None):
#         self.filename = filename
#         self.line_no = line_no
#         
#         # line number is irrelevant now if no filename
#         if filename is None:
#             self.filename, self.line_no = None, None
#     
#     def clear(self):
#         self.filename, self.line_no = None, None
# 
# Path: statik/utils.py
# def strip_el_text(el, max_depth=0, cur_depth=0):
#     """Recursively strips the plain text out of the given XML etree element up to the desired depth.
# 
#     Args:
#         el: The etree element to scan.
#         max_depth: The depth to which to recursively strip text (default: 0).
#         cur_depth: The current recursive depth to which we've scanned so far.
# 
#     Returns:
#         The stripped, plain text from within the element.
#     """
#     # text in front of any child elements
#     el_text = strip_str(el.text if el.text is not None else "")
# 
#     if cur_depth < max_depth:
#         for child in el:
#             el_text += " "+strip_el_text(child, max_depth=max_depth, cur_depth=cur_depth+1)
#     else:
#         # add the last child's tail text, if any
#         children = list(el)
#         if children is not None and len(children) > 0:
#             if children[-1].tail is not None:
#                 el_text += " "+strip_str(children[-1].tail)
# 
#     # we skip the root element
#     if cur_depth > 0:
#         # in case there's any text at the end of the element
#         if el.tail is not None:
#             el_text += " "+strip_str(el.tail)
# 
#     return strip_str(el_text)


# Path: statik/markdown_exts.py
from slugify import slugify
from markdown.preprocessors import Preprocessor
from markdown.extensions import Extension
from markdown.treeprocessors import Treeprocessor
from markdown.util import etree
from statik.errors import StatikErrorContext
from statik.utils import strip_el_text
import re
import yaml
import lipsum


    def __init__(self, *args, **kwargs):
        self.permalink_text = kwargs.pop('permalink_text', None)
        self.permalink_class = kwargs.pop('permalink_class', None)
        self.permalink_title = kwargs.pop('permalink_title', None)
        super(MarkdownPermalinkProcessor, self).__init__(*args, **kwargs)

    def run(self, root):
        insert_permalinks(
            root,
            permalink_text=self.permalink_text,
            permalink_class=self.permalink_class,
            permalink_title=self.permalink_title
        )


class MarkdownLoremIpsumProcessor(Preprocessor):

    # we search for a single command on a line
    REGEXP = re.compile(r"^!\(lipsum-(?P<kind>word[s]?|sentence[s]?|paragraph[s]?)(:(?P<count>\d+))?\)\w*$")
    GENERATORS = {
        "word": (lipsum.generate_words, 1),
        "words": (lipsum.generate_words, None),
        "sentence": (lipsum.generate_sentences, 1),
        "sentences": (lipsum.generate_sentences, None),
        "paragraph": (lipsum.generate_paragraphs, 1),
        "paragraphs": (lipsum.generate_paragraphs, None)
    }

    def __init__(self, *args, **kwargs):

        self.error_context = kwargs.pop('error_context', StatikErrorContext())