<?php
namespace library;

use \IFilter;
use \Request;
use \Response;
use \FilterChain;
use \IoC;
use \Texy;

class TexySyntaxFilter implements IFilter {

    /**
     * @param Request request Request object
     * @param Response response Response object
     * @access public
     */
    public function execute( Request $request, Response $response, FilterChain $filterchain ) {
        $texy = IoC::resolve( 'texy' );
        $texy->registerLinePattern(
            function( Texy\LineParser $parser, array $matches, string $name ) {
                [, $mContent, $mMod] = $matches;

                $texy = $parser->getTexy();

                // create element
                $tag = 'a';
                $el = new Texy\HtmlElement($tag);

                // apply modifier
                $mod = new Texy\Modifier($mMod);
                $mod->decorate($texy, $el);

                $el->attrs['href'] = BASE_URL . '/' . $mContent;
                $el->setText($mContent);

                // parse inner content of this element
                $parser->again = true;

                return $el;
            },
            '#(?<!\[\[)\[\[(?!\ |\[\[)(.+)' . Texy\Patterns::MODIFIER . '?(?<!\ |\]\])\]\](?!\]\])()#U',
            'myInlineSyntax2'
        );
        $filterchain->execute( $request, $response );
    }
}
