# Path: src/holocron/_processors/_misc.py
# class parameters:
#     def __init__(self, *, fallback=None, jsonschema=None):
#         self._fallback = fallback or {}
#         self._jsonschema = jsonschema
# 
#     def __call__(self, fn):
#         @functools.wraps(fn)
#         def wrapper(app, *args, **kwargs):
#             signature = inspect.signature(fn)
#             arguments = signature.bind_partial(app, *args, **kwargs).arguments
# 
#             # First two arguments always are an application instance and a
#             # stream of items to process. Since they are passed by Holocron
#             # core as positional arguments, there's no real need to check their
#             # schema, so we strip them away.
#             arguments = dict(list(arguments.items())[2:])
#             parameters = dict(list(signature.parameters.items())[2:])
# 
#             # If some parameter has not been passed, a value from a fallback
#             # must be used instead (if any).
#             for param in parameters:
#                 if param not in arguments:
#                     try:
#                         value = resolve_json_references(
#                             {"$ref": self._fallback[param]},
#                             {"metadata:": app.metadata},
#                         )
#                     except (jsonpointer.JsonPointerException, KeyError):
#                         continue
# 
#                     # We need to save resolved value in both arguments and
#                     # kwargs mappings, because the former is used to *validate*
#                     # passed arguments, and the latter to supply a value from a
#                     # fallback.
#                     arguments[param] = kwargs[param] = value
# 
#             if self._jsonschema:
#                 try:
#                     format_checker = jsonschema.FormatChecker()
# 
#                     @format_checker.checks("encoding", (LookupError,))
#                     def is_encoding(value):
#                         if isinstance(value, str):
#                             import codecs
# 
#                             return codecs.lookup(value)
# 
#                     @format_checker.checks("timezone", ())
#                     def is_timezone(value):
#                         if isinstance(value, str):
#                             import dateutil.tz
# 
#                             return dateutil.tz.gettz(value)
# 
#                     @format_checker.checks("path", (TypeError,))
#                     def is_path(value):
#                         if isinstance(value, str):
#                             import pathlib
# 
#                             return pathlib.Path(value)
# 
#                     jsonschema.validate(
#                         arguments,
#                         self._jsonschema,
#                         format_checker=format_checker,
#                     )
#                 except jsonschema.exceptions.ValidationError as exc:
#                     message = exc.message
# 
#                     if exc.absolute_path:
#                         message = f"{'.'.join(exc.absolute_path)}: {exc.message}"
# 
#                     raise ValueError(message)
# 
#             return fn(app, *args, **kwargs)
# 
#         return wrapper


import logging
import markdown_it
import pygments
import pygments.formatters.html
import pygments.lexers
import pygments.util
from mdit_py_plugins.container import container_plugin
from mdit_py_plugins.deflist import deflist_plugin
from mdit_py_plugins.footnote import footnote_plugin
from ._misc import parameters
"""Convert CommonMark into HTML."""




_LOGGER = logging.getLogger("holocron")


def _pygmentize(code, language, _):
    try:
        formatter = _pygmentize.formatter
    except AttributeError:

        class HtmlFormatter(pygments.formatters.html.HtmlFormatter):
            def wrap(self, source, _):
                # Since 'markdown-it' creates required '<pre>' & '<code>'
                # containers, there's no need to duplicate them with pygments.
                yield from source

        formatter = _pygmentize.formatter = HtmlFormatter(wrapcode=True)

    try:
        lexer = pygments.lexers.get_lexer_by_name(language)
    except pygments.util.ClassNotFound:
        _LOGGER.warning("pygmentize: no such langauge: '%s'", language)
        return None

    return pygments.highlight(code, lexer, formatter)



# Path: src/holocron/_processors/_misc.py
# class parameters:
#     def __init__(self, *, fallback=None, jsonschema=None):
#         self._fallback = fallback or {}
#         self._jsonschema = jsonschema
# 
#     def __call__(self, fn):
#         @functools.wraps(fn)
#         def wrapper(app, *args, **kwargs):
#             signature = inspect.signature(fn)
#             arguments = signature.bind_partial(app, *args, **kwargs).arguments
# 
#             # First two arguments always are an application instance and a
#             # stream of items to process. Since they are passed by Holocron
#             # core as positional arguments, there's no real need to check their
#             # schema, so we strip them away.
#             arguments = dict(list(arguments.items())[2:])
#             parameters = dict(list(signature.parameters.items())[2:])
# 
#             # If some parameter has not been passed, a value from a fallback
#             # must be used instead (if any).
#             for param in parameters:
#                 if param not in arguments:
#                     try:
#                         value = resolve_json_references(
#                             {"$ref": self._fallback[param]},
#                             {"metadata:": app.metadata},
#                         )
#                     except (jsonpointer.JsonPointerException, KeyError):
#                         continue
# 
#                     # We need to save resolved value in both arguments and
#                     # kwargs mappings, because the former is used to *validate*
#                     # passed arguments, and the latter to supply a value from a
#                     # fallback.
#                     arguments[param] = kwargs[param] = value
# 
#             if self._jsonschema:
#                 try:
#                     format_checker = jsonschema.FormatChecker()
# 
#                     @format_checker.checks("encoding", (LookupError,))
#                     def is_encoding(value):
#                         if isinstance(value, str):
#                             import codecs
# 
#                             return codecs.lookup(value)
# 
#                     @format_checker.checks("timezone", ())
#                     def is_timezone(value):
#                         if isinstance(value, str):
#                             import dateutil.tz
# 
#                             return dateutil.tz.gettz(value)
# 
#                     @format_checker.checks("path", (TypeError,))
#                     def is_path(value):
#                         if isinstance(value, str):
#                             import pathlib
# 
#                             return pathlib.Path(value)
# 
#                     jsonschema.validate(
#                         arguments,
#                         self._jsonschema,
#                         format_checker=format_checker,
#                     )
#                 except jsonschema.exceptions.ValidationError as exc:
#                     message = exc.message
# 
#                     if exc.absolute_path:
#                         message = f"{'.'.join(exc.absolute_path)}: {exc.message}"
# 
#                     raise ValueError(message)
# 
#             return fn(app, *args, **kwargs)
# 
#         return wrapper


# Path: src/holocron/_processors/commonmark.py
import logging
import markdown_it
import pygments
import pygments.formatters.html
import pygments.lexers
import pygments.util
from mdit_py_plugins.container import container_plugin
from mdit_py_plugins.deflist import deflist_plugin
from mdit_py_plugins.footnote import footnote_plugin
from ._misc import parameters

"""Convert CommonMark into HTML."""




_LOGGER = logging.getLogger("holocron")


def _pygmentize(code, language, _):
    try:
        formatter = _pygmentize.formatter
    except AttributeError:

        class HtmlFormatter(pygments.formatters.html.HtmlFormatter):
            def wrap(self, source, _):
                # Since 'markdown-it' creates required '<pre>' & '<code>'
                # containers, there's no need to duplicate them with pygments.
                yield from source

        formatter = _pygmentize.formatter = HtmlFormatter(wrapcode=True)

    try:
        lexer = pygments.lexers.get_lexer_by_name(language)
    except pygments.util.ClassNotFound:
        _LOGGER.warning("pygmentize: no such langauge: '%s'", language)
        return None

    return pygments.highlight(code, lexer, formatter)



@parameters(