<commit_before><?php

namespace Remp\MailerModule\Generators;

use Nette\Application\UI\Form;
use Remp\MailerModule\Api\v1\Handlers\Mailers\PreprocessException;
use Remp\MailerModule\Components\GeneratorWidgets\Widgets\MediaBriefingWidget;
use Remp\MailerModule\Repository\SourceTemplatesRepository;
use Tomaj\NetteApi\Params\InputParam;
use GuzzleHttp\Client;

class MediaBriefingGenerator implements IGenerator
{
    protected $mailSourceTemplateRepository;

    public $onSubmit;

    public function __construct(SourceTemplatesRepository $mailSourceTemplateRepository)
    {
        $this->mailSourceTemplateRepository = $mailSourceTemplateRepository;
    }

    public function apiParams()
    {
        return [
            new InputParam(InputParam::TYPE_POST, 'source_template_id', InputParam::REQUIRED),
            new InputParam(InputParam::TYPE_POST, 'mediabriefing_html', InputParam::REQUIRED),
            new InputParam(InputParam::TYPE_POST, 'url', InputParam::REQUIRED),
            new InputParam(InputParam::TYPE_POST, 'title', InputParam::REQUIRED),
            new InputParam(InputParam::TYPE_POST, 'sub_title', InputParam::REQUIRED),
        ];
    }

    public function getWidgets()
    {
        return [MediaBriefingWidget::class];
    }

    public function process($values)
    {
        $sourceTemplate = $this->mailSourceTemplateRepository->find($values->source_template_id);
        $html = '';

        $post = $values->mediabriefing_html;

        list($captionTemplate, $liTemplate, $hrTemplate) = $this->getTemplates();

        // remove grayboxes
        $post = preg_replace('/\[greybox\].*?\[\/greybox\]/is', '', $post);

        // replace captions
        $post = preg_replace('/\[caption.*?\].*?src="(.*?)".*?\/>(.*?)\[\/caption\]/is', $captionTemplate, $post);

        // replace hrefs
        $post = preg_replace('/<a.*?href="(.*?)".*?>(.*?)<\/a>/is', '<a href="$1" title="$2" style="color:#181818;padding:0;margin:0;Margin:0;line-height:1.3;color:#F26755;text-decoration:none;">$2</a>', $post);

        // replace h2
        $post = preg_replace('/<h2.*?>(.*?)<\/h2>/is', '<h2 style="color:#181818;padding:0;margin:0;Margin:0;line-height:1.3;font-weight:bold;text-align:left;margin-bottom:30px;Margin-bottom:30px;font-size:24px;">$1</h2>' . PHP_EOL, $post);

        // replace images
        $post = preg_replace('/<img.*?src="(.*?)".*?\/>/is', '<img src="$1" alt="" style="outline:none;text-decoration:none;-ms-interpolation-mode:bicubic;width:auto;max-width:100%;clear:both;display:block;margin-bottom:20px;">', $post);

        // replace ul & /ul
        $post = str_replace('<ul>', '<table style="border-spacing:0;border-collapse:collapse;vertical-align:top;color:#181818;padding:0;margin:0;Margin:0;line-height:1.3;text-align:left;font-family:\'Helvetica Neue\', Helvetica, Arial;width:100%;">
    <tbody>', $post);
        $post = str_replace('</ul>', '</tbody></table>' . PHP_EOL, $post);

        // replace li
        $post = preg_replace('/<li>(.*?)<\/li>/is', $liTemplate, $post);
        // hr
        $post = preg_replace('(<hr>|<hr />)', $hrTemplate, $post);

        // loop through lines and wrap or parse individually
        foreach (preg_split("#\n\s*\n#Uis", $post) as $line) {
            if (empty($line)) {
                continue;
            }

            // parse embedd links
            if (preg_match('/^\s*(http|https)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?\s*$/i', $line)) {
                $html .= $this->parseEmbedd(trim($line));
            // dont wrap h2, img, table in paragraphs
            } else if (preg_match('/^\s*?<table/is', $line) || preg_match('/^\s*?<img.*?>\s*?$/i', $line) || preg_match('/^\s*?<h2.*?\/h2>\s*?$/mi', $line) || preg_match('/^\s*?<h3.*?\/h3>\s*?$/mi', $line)) {
                $html .= $line;

            // wrap text in paragraphs
            } else {
                $html .= '<p style="margin:0 0 0 26px;Margin:0 0 0 26px;color:#181818;padding:0;margin:0;Margin:0;line-height:1.3;font-size:18px;line-height:1.6;margin-bottom:26px;Margin-bottom:26px;line-height:160%;text-align:left;font-weight:normal;word-wrap:break-word;-webkit-hyphens:auto;-moz-hyphens:auto;hyphens:auto;border-collapse:collapse !important;">';
                $html .= $line;
                $html .= '</p>';
            }
        }

        $loader = new \Twig_Loader_Array([
            'html_template' => $sourceTemplate->content_html,
            'text_template' => $sourceTemplate->content_text,
        ]);
        $twig = new \Twig_Environment($loader);

        $params = [
            'title' => $values->title,
            'sub_title' => $values->sub_title,
            'url' => $values->url,
            'html' => $html,
            'text' => strip_tags($html),
        ];

        $output = [];
        $output['htmlContent'] = $twig->render('html_template', $params);
        $output['textContent'] = $twig->render('text_template', $params);
        return $output;
    }<commit_msg>replace em tags + refactor wrap lines<commit_after><?php

namespace Remp\MailerModule\Generators;

use Nette\Application\UI\Form;
use Remp\MailerModule\Api\v1\Handlers\Mailers\PreprocessException;
use Remp\MailerModule\Components\GeneratorWidgets\Widgets\MediaBriefingWidget;
use Remp\MailerModule\Repository\SourceTemplatesRepository;
use Tomaj\NetteApi\Params\InputParam;
use GuzzleHttp\Client;

class MediaBriefingGenerator implements IGenerator
{
    protected $mailSourceTemplateRepository;

    public $onSubmit;

    public function __construct(SourceTemplatesRepository $mailSourceTemplateRepository)
    {
        $this->mailSourceTemplateRepository = $mailSourceTemplateRepository;
    }

    public function apiParams()
    {
        return [
            new InputParam(InputParam::TYPE_POST, 'source_template_id', InputParam::REQUIRED),
            new InputParam(InputParam::TYPE_POST, 'mediabriefing_html', InputParam::REQUIRED),
            new InputParam(InputParam::TYPE_POST, 'url', InputParam::REQUIRED),
            new InputParam(InputParam::TYPE_POST, 'title', InputParam::REQUIRED),
            new InputParam(InputParam::TYPE_POST, 'sub_title', InputParam::REQUIRED),
        ];
    }

    public function getWidgets()
    {
        return [MediaBriefingWidget::class];
    }

    public function process($values)
    {
        $sourceTemplate = $this->mailSourceTemplateRepository->find($values->source_template_id);
        $html = '';

        $post = $values->mediabriefing_html;

        list($captionTemplate, $liTemplate, $hrTemplate) = $this->getTemplates();

        // remove grayboxes
        $post = preg_replace('/\[greybox\].*?\[\/greybox\]/is', '', $post);

        // replace em-s
        $post = preg_replace('/<em.*?>(.*?)<\/em>/is', '<i>$1</i>', $post);

        // replace captions
        $post = preg_replace('/\[caption.*?\].*?src="(.*?)".*?\/>(.*?)\[\/caption\]/is', $captionTemplate, $post);

        // replace hrefs
        $post = preg_replace('/<a.*?href="(.*?)".*?>(.*?)<\/a>/is', '<a href="$1" title="$2" style="color:#181818;padding:0;margin:0;Margin:0;line-height:1.3;color:#F26755;text-decoration:none;">$2</a>', $post);

        // replace h2
        $post = preg_replace('/<h2.*?>(.*?)<\/h2>/is', '<h2 style="color:#181818;padding:0;margin:0;Margin:0;line-height:1.3;font-weight:bold;text-align:left;margin-bottom:30px;Margin-bottom:30px;font-size:24px;">$1</h2>' . PHP_EOL, $post);

        // replace images
        $post = preg_replace('/<img.*?src="(.*?)".*?\/>/is', '<img src="$1" alt="" style="outline:none;text-decoration:none;-ms-interpolation-mode:bicubic;width:auto;max-width:100%;clear:both;display:block;margin-bottom:20px;">', $post);

        // replace ul & /ul
        $post = str_replace('<ul>', '<table style="border-spacing:0;border-collapse:collapse;vertical-align:top;color:#181818;padding:0;margin:0;Margin:0;line-height:1.3;text-align:left;font-family:\'Helvetica Neue\', Helvetica, Arial;width:100%;">
    <tbody>', $post);
        $post = str_replace('</ul>', '</tbody></table>' . PHP_EOL, $post);

        // replace li
        $post = preg_replace('/<li>(.*?)<\/li>/is', $liTemplate, $post);
        // hr
        $post = preg_replace('(<hr>|<hr />)', $hrTemplate, $post);

        // loop through lines and wrap or parse individually
        foreach (preg_split("#\n\s*\n#Uis", $post) as $line) {
            if (empty($line)) {
                continue;
            }

            // parse embedd links
            if (preg_match('/^\s*(http|https)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?\s*$/i', $line)) {
                $html .= $this->parseEmbedd(trim($line));

            // wrap line in paragraphs <p> if:
            } else if (
                // not in table
                preg_match('/^\s*?<table/is', $line) ||
                // skip lines where is only image tag
                preg_match('/^\s*?<img.*?>\s*?$/i', $line) ||
                // skip lines where is only h2/h3 tag
                preg_match('/^\s*?<(h2|h3).*?\/(h2|h3)>\s*?$/mi', $line)
            ) {
                $html .= $line;

            // wrap text in paragraphs
            } else {
                $html .= '<p style="margin:0 0 0 26px;Margin:0 0 0 26px;color:#181818;padding:0;margin:0;Margin:0;line-height:1.3;font-size:18px;line-height:1.6;margin-bottom:26px;Margin-bottom:26px;line-height:160%;text-align:left;font-weight:normal;word-wrap:break-word;-webkit-hyphens:auto;-moz-hyphens:auto;hyphens:auto;border-collapse:collapse !important;">';
                $html .= $line;
                $html .= '</p>';
            }
        }

        $loader = new \Twig_Loader_Array([
            'html_template' => $sourceTemplate->content_html,
            'text_template' => $sourceTemplate->content_text,
        ]);
        $twig = new \Twig_Environment($loader);

        $params = [
            'title' => $values->title,
            'sub_title' => $values->sub_title,
            'url' => $values->url,
            'html' => $html,
            'text' => strip_tags($html),
        ];

        $output = [];
        $output['htmlContent'] = $twig->render('html_template', $params);
        $output['textContent'] = $twig->render('text_template', $params);
        return $output;
    }
{'fraction_non_alphanumeric': 0.19384675299386622, 'fraction_numerical': 0.020640638691461394, 'mean_word_length': 2.8515185601799775, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 101, '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': '3745938', 'n_tokens_mistral': 3509, 'n_tokens_neox': 3345, 'n_words': 479}