<commit_before><?php
namespace Morpho\Web\View;

use function Morpho\Base\filter;
use function Morpho\Cli\{cmd};
use Morpho\Fs\Directory;
use Morpho\Fs\File;
use Morpho\Fs\Path;
use Morpho\Di\ServiceManager;

class HtmlParserPost extends HtmlParser {
    protected $scripts = [];

    protected $forceCompileTs;

    protected $nodeBinDirPath;

    protected $tsOptions;

    private $scriptIndex = 0;

    public function __construct(ServiceManager $serviceManager, bool $forceCompileTs, string $nodeBinDirPath, array $tsOptions) {
        parent::__construct($serviceManager);
        $this->forceCompileTs = $forceCompileTs;
        $this->nodeBinDirPath = $nodeBinDirPath;
        $this->tsOptions = $tsOptions;
    }

    protected function containerTypeScript($tag) {
        $inDirPath = str_replace('\\', '/', dirname($this->filePath));
        $cacheDirPath = $this->serviceManager->getSiteManager()->getCurrentSite()->getCacheDirPath();
        $scriptTag = [];
        if (isset($tag['index'])) {
            $scriptTag['index'] = $tag['index'];
        }
        $filesToCompile = [];
        $compile = false;
        $baseModuleDirPath = $this->serviceManager->get('moduleFs')->getBaseModuleDirPath();
        foreach (array_map('trim', explode(',', $tag['src'])) as $fileName) {
            $inFilePath = $inDirPath . '/' . Path::newExt(basename($fileName), 'ts');
            if (!is_file($inFilePath)) {
                throw new \RuntimeException("The '$inFilePath' does not exist");
            }
            $inFileChangedTime = filemtime($inFilePath);
            $outDirPath = $cacheDirPath . '/' . Path::toRelative($baseModuleDirPath, $inFilePath);
            $outFilePath = $outDirPath . '/' . $inFileChangedTime . '.js';
            if ($this->forceCompileTs || !is_file($outFilePath)) {
                Directory::recreate($outDirPath);
                $compile = true;
            }
            $filesToCompile[] = [$inFilePath, $outFilePath];
        }

        $text = [];
        $removeRefs = function ($line) {
            return substr($line, 0, 3) !== '///';
        };
        foreach ($filesToCompile as $inOutFilePaths) {
            if ($compile) {
                $this->runTsc($inOutFilePaths[0], $inOutFilePaths[1]);
            }
            $text[] = implode("\n", filter($removeRefs, File::readLines($inOutFilePaths[1])));
            //$text[] = file_get_contents($inOutFilePaths[1]);
        }
        $scriptTag['_text'] = implode("\n", $text) . $tag['_text'];
        $scriptTag['_tagName'] = 'script';
        return $this->containerScript($scriptTag);
    }

    protected function runTsc(string $inFilePath, string $outFilePath) {
        $options = array_merge($this->tsOptions, ['--out ' . escapeshellarg($outFilePath)]);
        // Note: node and tsc must be in $PATH.
        cmd("PATH=\$PATH:{$this->nodeBinDirPath} tsc " . implode(' ', $options) . ' ' . escapeshellarg($inFilePath));
    }

    protected function containerBody($tag) {
        $childScriptsHtml = $this->renderScripts();
        $tag['_text'] = $this->filter($tag['_text'])
            . $this->renderScripts()
            . "\n" . $childScriptsHtml;
        return $tag;
    }

    protected function containerScript($tag) {<commit_msg>#101. Disabled ability to run .ts files for the '<typeScript>' tags<commit_after><?php
namespace Morpho\Web\View;

use function Morpho\Base\filter;
use function Morpho\Cli\{cmd};
use Morpho\Fs\Directory;
use Morpho\Fs\File;
use Morpho\Fs\Path;
use Morpho\Di\ServiceManager;

class HtmlParserPost extends HtmlParser {
    protected $scripts = [];

    protected $forceCompileTs;

    protected $nodeBinDirPath;

    protected $tsOptions;

    private $scriptIndex = 0;

    public function __construct(ServiceManager $serviceManager, bool $forceCompileTs, string $nodeBinDirPath, array $tsOptions) {
        parent::__construct($serviceManager);
        $this->forceCompileTs = $forceCompileTs;
        $this->nodeBinDirPath = $nodeBinDirPath;
        $this->tsOptions = $tsOptions;
    }
/*
    protected function containerTypeScript($tag) {
        $inDirPath = str_replace('\\', '/', dirname($this->filePath));
        $cacheDirPath = $this->serviceManager->getSiteManager()->getCurrentSite()->getCacheDirPath();
        $scriptTag = [];
        if (isset($tag['index'])) {
            $scriptTag['index'] = $tag['index'];
        }
        $filesToCompile = [];
        $compile = false;
        $baseModuleDirPath = $this->serviceManager->get('moduleFs')->getBaseModuleDirPath();
        foreach (array_map('trim', explode(',', $tag['src'])) as $fileName) {
            $inFilePath = $inDirPath . '/' . Path::newExt(basename($fileName), 'ts');
            if (!is_file($inFilePath)) {
                throw new \RuntimeException("The '$inFilePath' does not exist");
            }
            $inFileChangedTime = filemtime($inFilePath);
            $outDirPath = $cacheDirPath . '/' . Path::toRelative($baseModuleDirPath, $inFilePath);
            $outFilePath = $outDirPath . '/' . $inFileChangedTime . '.js';
            if ($this->forceCompileTs || !is_file($outFilePath)) {
                Directory::recreate($outDirPath);
                $compile = true;
            }
            $filesToCompile[] = [$inFilePath, $outFilePath];
        }

        $text = [];
        $removeRefs = function ($line) {
            return substr($line, 0, 3) !== '///';
        };
        foreach ($filesToCompile as $inOutFilePaths) {
            if ($compile) {
                $this->runTsc($inOutFilePaths[0], $inOutFilePaths[1]);
            }
            $text[] = implode("\n", filter($removeRefs, File::readLines($inOutFilePaths[1])));
            //$text[] = file_get_contents($inOutFilePaths[1]);
        }
        $scriptTag['_text'] = implode("\n", $text) . $tag['_text'];
        $scriptTag['_tagName'] = 'script';
        return $this->containerScript($scriptTag);
    }

    protected function runTsc(string $inFilePath, string $outFilePath) {
        $options = array_merge($this->tsOptions, ['--out ' . escapeshellarg($outFilePath)]);
        // Note: node and tsc must be in $PATH.
        cmd("PATH=\$PATH:{$this->nodeBinDirPath} tsc " . implode(' ', $options) . ' ' . escapeshellarg($inFilePath));
    }
*/
    protected function containerBody($tag) {
        $childScriptsHtml = $this->renderScripts();
        $tag['_text'] = $this->filter($tag['_text'])
            . $this->renderScripts()
            . "\n" . $childScriptsHtml;
        return $tag;
    }

    protected function containerScript($tag) {
{'fraction_non_alphanumeric': 0.16119720449711333, 'fraction_numerical': 0.002582801580066849, 'mean_word_length': 2.6879551820728294, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 42, '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': '4539419', 'n_tokens_mistral': 1992, 'n_tokens_neox': 1907, 'n_words': 356}