class UserDefinedMacro(Macro):

    def __init__(self, flap, name, signature, body):
        super().__init__(flap, name, signature, body)
        self.is_user_defined = True class CatCode(Macro):
class Def(Macro):
class Begin(Macro):
class DocumentClass(Macro):
class PackageReference(Macro):
class UsePackage(PackageReference):
class RequirePackage(PackageReference):
    def __init__(self, flap):
    def _execute(self, parser, invocation):
    def __init__(self, flap):
    def execute2(self, parser, invocation):
    def rewrite2(self, parser, invocation):
    def rewrite(self, parser):
    def _capture_arguments(self, parser, invocation):
    def __init__(self, flap):
    def _capture_arguments(self, parser, invocation):
    def execute2(self, parser, invocation):
    def rewrite2(self, parser, invocation):
    def __init__(self, flap):
    def _capture_arguments(self, parser, invocation):
    def execute2(self, parser, invocation):
    def find_class_name(self, parser, invocation):
    def rewrite2(self, parser, invocation):
    def __init__(self, flap, name):
    def _capture_arguments(self, parser, invocation):
    def execute2(self, parser, invocation):
    def rewrite2(self, parser, invocation):
    def __init__(self, flap):
    def __init__(self, flap): class IncludeGraphics(UpdateLink):
    """
    Intercept the `\\includegraphics` directive
    """

    def __init__(self, flap):
        super().__init__(flap, "includegraphics")

    def update_link(self, parser, link, invocation):
        return self._flap.update_link_to_graphic(link, invocation) class IncludeSVG(UpdateLink):
    """
    Intercept the r`\\includesvg` directive
    """

    def __init__(self, flap):
        super().__init__(flap, r"includesvg")

    def update_link(self, parser, link, invocation):
        searched_directories = []
        options = self._extract_options(parser, invocation)
        if self.SVG_PATH in options:
            searched_directories.append(options[self.SVG_PATH])
        return self._flap.update_link_to_graphic(link,
                                                 invocation,
                                                 searched_directories)

    SVG_PATH = "svgpath"

    @staticmethod
    def _extract_options(parser, invocation):
        full_options = parser.evaluate_as_text(
            invocation.argument("options"))
        if not full_options.strip():
            return {}
        result = {}
        options = full_options[1:-1]
        for each_attribute in options.split(","):
            key, value = each_attribute.split("=")
            result[key] = value
        return result class GraphicsPath(Macro):
    """
    Intercept the `\\graphicspath` directive
    """

    def __init__(self, flap):
        super().__init__(flap, r"graphicspath", None, None)

    def _capture_arguments(self, parser, invocation):
        invocation.append_argument(
            "paths", parser.read.group())

    def execute2(self, parser, invocation):
        argument = parser.evaluate_as_text(invocation.argument("paths"))
        paths = list(map(str.strip, argument.split(",")))
        self._flap.record_graphic_path(paths, invocation)

    def rewrite2(self, parser, invocation):
        return invocation.as_tokens class Overpic(Environment):
    """
    Intercept the \begin{overpic} environment
    """

    def __init__(self, flap):
        super().__init__(flap, "overpic")

    def execute2(self, parser, invocation):
        pass

    def rewrite2(self, parser, invocation):
        invocation.append_argument("options", parser.read.options())
        logger.debug("OPTIONS: '%s'",
                     invocation.argument_as_text("options"))
        invocation.append_argument("link", parser.read.one())
        logger.debug("LINK: '%s'",
                     invocation.argument_as_text("link"))
        link = parser.evaluate_as_text(invocation.argument("link"))
        new_link = self._flap.update_link_to_graphic(link, invocation)
        return invocation.substitute("link", parser._create.as_list(
            "{" + new_link + "}")).as_tokens class Bibliography(UpdateLink):
    """
    Intercept the `\bibliography` directive
    """

    def __init__(self, flap):
        super().__init__(flap, "bibliography")

    def update_link(self, parser, link, invocation):
        return self._flap.update_link_to_bibliography(link, invocation) class BibliographyStyle(UpdateLink):
    """
    Intercept the `\bibliographystyle` directive
    """

    def __init__(self, flap):
        super().__init__(flap, "bibliographystyle")

    def update_link(self, parser, link, invocation):
        return self._flap.update_link_to_bibliography_style(link, invocation) class MakeIndex(Macro):
    """
    Intercept 'makeindex' commands. It triggers copying the index style file,
    if they are it can be found locally.
    """

    def __init__(self, flap):
        super().__init__(flap, "makeindex", None, None)

    def _capture_arguments(self, parser, invocation):
        invocation.append_argument("options", parser.read.options())

    def rewrite2(self, parser, invocation):
        style_file = self._fetch_style_file(parser, invocation)
        new_style_file = self._flap.update_link_to_index_style(
            style_file, invocation)
        return invocation.as_text.replace(style_file, new_style_file)

    @staticmethod
    def _fetch_style_file(parser, invocation):
        text = parser.evaluate_as_text(invocation.argument("options"))
        for each in text.strip()[1:-1].split(","):
            _, value = each.split("=")
            options = re.split(r"(-\w\s)", value)
            for index, _ in enumerate(options):
                if "-s" in options[index]:
                    return options[index + 1]
        return None class EndInput(Macro):

    def __init__(self, flap):
        super().__init__(flap, "endinput", None, None)

    def execute2(self, parser, invocation):
        source = invocation.name.location.source
        self._flap.end_of_input(source, invocation)
        parser.flush(source)

    def rewrite2(self, parser, invocation):
        return [] class Include(TexFileInclusion):
    """
    Intercept the `\\include` directive
    """

    def __init__(self, flap):
        super().__init__(flap, "include")

    def execute2(self, parser, invocation):
        self._called = True
        link = parser.evaluate_as_text(invocation.argument("link"))
        if self._flap.shall_include(link):
            tokens = parser._create.as_list(r"\clearpage")
            parser._tokens.push(tokens)
            super().execute2(parser, invocation)

    def rewrite2(self, parser, invocation):
        return [] class IncludeOnly(Macro):
    """
    Intercept includeonly commands
    """

    def __init__(self, flap):
        super().__init__(flap, r"includeonly", None, None)

    def _capture_arguments(self, parser, invocation):
        invocation.append_argument("selection", parser.read.one())

    def execute2(self, parser, invocation):
        pass

    def rewrite2(self, parser, invocation):
        text = parser.evaluate_as_text(invocation.argument("selection"))
        files_to_include = list(map(str.strip, text.split(",")))
        self._flap.include_only(files_to_include, invocation)
        return [] class Input(TexFileInclusion):
    """
    Intercept the `\\input` directive
    """

    def __init__(self, flap):
        super().__init__(flap, "input") class SubFile(TexFileInclusion):

    def __init__(self, flap):
        super().__init__(flap, "subfile") class Verbatim(Environment):
    """
    Intercept the \begin{verbatim} environment
    """

    def __init__(self, flap):
        super().__init__(flap, "verbatim")
        self._content = None

    def execute2(self, parser, invocation):
        self._content = parser.read.until_text(r"\end{verbatim}")

    def rewrite2(self, parser, invocation):
        return parser._create.as_list(r"\begin{verbatim}") + self._content class AddBibResource(UpdateLink):
    """
    Intercept the `\bibliography` directive
    """

    def __init__(self, flap):
        super().__init__(flap, r"addbibresource")

    def update_link(self, parser, link, invocation):
        return self._flap.update_link_to_bibliography(
            link,
            invocation,
            True)
from flap.latex.macros.commons import UserDefinedMacro
from flap.latex.macros.core import Begin, CatCode, Def, DocumentClass, \
    Macro, RequirePackage, UsePackage
from flap.latex.macros.graphics import IncludeGraphics, IncludeSVG, \
    GraphicsPath, Overpic
from flap.latex.macros.bibliography import Bibliography, \
    BibliographyStyle, MakeIndex
from flap.latex.macros.inlining import EndInput, Include, IncludeOnly, \
    Input, SubFile
from flap.latex.macros.listings import Verbatim
from flap.latex.macros.biblatex import AddBibResource
#!/usr/bin/env python

#
# This file is part of Flap.
#
# Flap is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Flap is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Flap.  If not, see <http://www.gnu.org/licenses/>.
#




class MacroFactory:
    """
    Create macros that are associated with a given FLaP backend
    """

    def __init__(self, flap):
        self._flap = flap
        self._macros = [
            Begin(self._flap),
            Bibliography(self._flap),
            BibliographyStyle(self._flap),
            AddBibResource(self._flap),
            CatCode(self._flap),
            Def(self._flap),
            DocumentClass(self._flap),
            EndInput(self._flap),
            GraphicsPath(self._flap),
            IncludeGraphics(self._flap),

            IncludeOnly(self._flap),