/** * @dataProvider indentProvider */ public function testIndent($name) { $indenter = new \Gajus\Dindent\Indenter(); $input = file_get_contents(__DIR__ . '/sample/input/' . $name . '.html'); $expected_output = file_get_contents(__DIR__ . '/sample/output/' . $name . '.html'); $this->assertSame($expected_output, $indenter->indent($input)); }
/** * Creates a valid html output of the current temporary output content. * * @return string * @since Version 1.0 */ public function getOutput() { $str = $this->output; // remove temp_variables foreach ($this->variable_hashes as $key => $value) { $str = str_replace($value, "", $str); } // escape html characters which are marked $pattern = "/\\§\\{.*\\}\\§/s"; $matches = array(); preg_match_all($pattern, $str, $matches); if (count($matches) < 1) { return; } // if no plugin has to be loaded $matches = $matches[0]; // preg_match_all returns array in array foreach ($matches as $m) { $removed = str_replace("§{", "", $m); $removed = str_replace("}§", "", $removed); $str = str_replace($m, htmlspecialchars($removed), $str); } if (template::$doIndent) { require_once (strpos(BASEDIR, "/admin") !== FALSE ? BASEDIR . "../" : BASEDIR) . "sources/dindent/Indenter.php"; $indenter = new Gajus\Dindent\Indenter(); $str = $indenter->indent($str); } return $str; }
/** * Execute compile command */ protected function execute(InputInterface $input, OutputInterface $output) { //get project name & set source/output dirs $project = $input->getArgument('project'); $this->config = new \Implico\Email\Config($project, $input->getOption('dir')); if ($error = $this->config->getErrors()) { switch ($error) { case 'projectNotFound': $output->writeln('<fg=red>ERROR: project directory not found</fg=red>'); exit(1); break; } } SmartyUtils::init($this->config); //get script name(s) $scripts = $input->getOption('script'); //if script name(s) not passed, set all scripts (exclude dirs) if (!$scripts) { $scripts = array_filter(array_diff(scandir($this->config['scriptsDir']), array('.', '..')), function ($script) { return !is_dir($this->config['scriptsDir'] . $script); }); } //add ".tpl" extension when applicable foreach ($scripts as $i => $script) { if (strpos($script, '.') === false) { $scripts[$i] = $script . '.tpl'; } } //get watch option $watch = $input->getOption('watch'); //get output option $outputMode = $input->getOption('output'); //create & configure Smarty object $smarty = new \Smarty(); $smarty->setCompileDir(IE_SMARTY_COMPILE_DIR); $smarty->addPluginsDir(IE_SMARTY_PLUGINS_DIR); $smarty->addPluginsDir(IE_SMARTY_CUSTOM_PLUGINS_DIR); $smarty->compile_check = false; $smarty->force_compile = true; $smarty->error_reporting = E_ALL; $smarty->registerClass('SmartyUtils', 'Implico\\Email\\Utils\\Smarty'); //set directories $smarty->setTemplateDir(array(0 => $this->config['dir'], 'core' => IE_CORE_DIR, 'layouts' => $this->config['layoutsDir'], 'scripts' => $this->config['scriptsDir'], 'styles' => $this->config['stylesDir'])); //master config file $smarty->configLoad(IE_CORE_DIR . 'config.conf'); //optional master custom config file $customConf = IE_CUSTOM_DIR . 'config.conf'; if (file_exists($customConf)) { $smarty->configLoad($customConf); } //console message for watching if ($watch) { $output->writeln('Watching for changes...'); } //main loop - watch for changes (or execute once if not watching) $compileNo = 1; $compileDirStamp = ''; //dirs to inspect file change $checkDirs = array($this->config['configsDir'], $this->config['configsScriptsDir'], $this->config['layoutsDir'], $this->config['scriptsDir'], $this->config['stylesDir']); //set output mode variables $outputMinified = in_array('m', $outputMode); $outputFormatted = in_array('f', $outputMode); //formatter object $formatter = null; //css inliner object $cssToInlineStyles = new CssToInlineStyles(); while (true) { //compile only if not watching or the dirs filestamp changes if (!$watch || $compileDirStamp != $this->getDirStamp($checkDirs)) { //clear compiled templates $smarty->clearCompiledTemplate(); //Smarty assign project-specific config file path $configFile = $this->config['configsDir'] . 'config.conf'; $loadConfigFile = file_exists($configFile); //set random complile_id (forces Smarty to compile) $smarty->compile_id = uniqid(); //list of compiled scripts $compiledScripts = $scripts; //fetch & save templates foreach ($scripts as $i => $script) { //script name without extension $scriptName = substr($script, 0, strrpos($script, '.')); $smarty->clearConfig(); if ($loadConfigFile) { $smarty->configLoad($configFile); } //set script-specific config file path if exists $configFileScript = $this->config['configsScriptsDir'] . $scriptName . '.conf'; if (file_exists($configFileScript)) { $smarty->configLoad($configFileScript); } //lazy create indenter if ($outputFormatted && !$formatter) { $formatter = new \Gajus\Dindent\Indenter(array('indentation_character' => $smarty->getConfigVars('indentChar'))); } //set encoding $outputEncoding = $smarty->getConfigVars('encoding'); if (!$outputEncoding) { $outputEncoding = 'utf-8'; } $outputEncodingUtf8 = strtoupper($outputEncoding) == 'UTF-8'; try { //get the html $html = $smarty->fetch($this->config['scriptsDir'] . $script); //get inline styles $inlineCss = $smarty->fetch($this->config['stylesDir'] . 'inline.tpl'); if (trim($inlineCss)) { $cssToInlineStyles->setHTML($html); $cssToInlineStyles->setCSS($inlineCss); $html = $cssToInlineStyles->convert(); } //save minified if ($outputMinified) { $htmlSave = $html; if (!$outputEncodingUtf8) { $htmlSave = mb_convert_encoding($htmlSave, $outputEncoding, 'utf-8'); } //max line width = 900 chars $maxPerLine = 750; $endLine = false; $newHtml = ''; for ($i = 0; $i < mb_strlen($htmlSave, $outputEncoding); $i++) { if ($i % $maxPerLine == 0 && $i > 0) { $endLine = true; } $curChar = mb_substr($htmlSave, $i, 1, $outputEncoding); $newHtml .= $curChar; if ($endLine) { if ($curChar == '>') { $newHtml .= PHP_EOL; $endLine = false; } } } $htmlSave = $newHtml; $this->saveOutput($this->config['outputsDir'] . $scriptName . '.min.html', $htmlSave); } //save formatted if ($outputFormatted) { $htmlSave = $formatter->indent($html); if (!$outputEncodingUtf8) { $htmlSave = mb_convert_encoding($htmlSave, $outputEncoding, 'utf-8'); } $this->saveOutput($this->config['outputsDir'] . $scriptName . '.html', $htmlSave, true); } } catch (\Exception $e) { $output->writeln('<fg=red>' . $e->getMessage() . '</fg=red>'); $compiledScripts[$i] .= ' <fg=red>(ERROR)</fg=red>'; } } //console info message $output->writeln(($watch ? '#' . $compileNo++ . ' ' : '') . 'Compiled ' . date('d-m-Y H:i:s') . ' ' . implode(', ', $compiledScripts)); } //break if not watching if (!$watch) { break; } //calculate dirs filestamp to compare $compileDirStamp = $this->getDirStamp($checkDirs); //pause usleep(500000); } }
/** * * @param null $view * @param bool $return * * @return string|void */ public static function render($view = NULL, $return = FALSE) { // Loads the view to parse $view = $view != NULL ? $view : self::$view; log_message('debug', 'Tagmanager::render() : Render of the view : ' . $view); $parsed = Theme::load($view); // We can now check if the file is a PHP one or a FTL one if (substr($parsed, 0, 5) === '<?php') { $parsed = self::$ci->load->view($view, array(), TRUE); } else { $parsed = self::parse($parsed, self::$context); if (User()->logged_in() && Authority::can('access', 'admin') && Settings::get('display_connected_label') == '1') { $injected_html = self::$ci->load->view('core/logged_as_editor', array(), TRUE); $parsed = str_replace('</body>', $injected_html, $parsed); } } // Full page cache ? $page = self::registry('page'); if (isset($page['_cached'])) { /* * Write the full page cache file * */ } $isActiveCompress = config_item('compress_html_output'); $isActiveBeautify = config_item('beautify_html_output'); if ($isActiveCompress || $isActiveBeautify) { $parsed = self::moveScriptsDown($parsed); } if ($isActiveBeautify) { require_once APPPATH . 'third_party/indenter.php'; $indenter = new \Gajus\Dindent\Indenter(); $parsed = $indenter->indent($parsed); } // Returns the result or output it directly if ($return) { return $parsed; } else { self::$ci->output->set_output($parsed); } }
./dindent.php --input="./input.html" --indentation_character="\\t" Indent "input.html" file using tab to indent the markup. ./dindent.php --input="./input.html" --inline="div,p" Indent "input.html" file treating <div> and <p> elements as inline. ./dindent.php --input="./input.html" --block="span,em" Indent "input.html" file treating <span> and <em> elements as block. '; exit; } if (!isset($options['input'])) { error('Missing "input" parameter.'); } else { if (!file_exists($options['input'])) { error('"input" file does not exist.'); } } $indenter = new \Gajus\Dindent\Indenter(isset($options['indentation_character']) ? array('indentation_character' => $options['indentation_character']) : array()); if (isset($options['inline'])) { foreach (explode(',', $options['inline']) as $element_name) { $indenter->setElementType($element_name, \Gajus\Dindent\Indenter::ELEMENT_TYPE_INLINE); } } if (isset($options['block'])) { foreach (explode(',', $options['block']) as $element_name) { $indenter->setElementType($element_name, \Gajus\Dindent\Indenter::ELEMENT_TYPE_BLOCK); } } $output = $indenter->indent(file_get_contents($options['input'])); echo $output;