<commit_before><?php
/**
* @version      $Id$
* @category		Koowa
* @package      Koowa_Template
* @subpackage	Filter
* @copyright    Copyright (C) 2007 - 2010 <NAME>. All rights reserved.
* @license      GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
* @link 		http://www.nooku.org
*/

/**
 * Template filter to parse script tags
 *
 * @author		<NAME> <<EMAIL>>
 * @category	Koowa
 * @package     Koowa_Template
 * @subpackage	Filter
 */
class KTemplateFilterScript extends KTemplateFilterAbstract implements KTemplateFilterWrite
{
	/**
     * Initializes the options for the object
     *
     * Called from {@link __construct()} as a first step of object instantiation.
     *
     * @param   object  An optional KConfig object with configuration options
     * @return void
     */
    protected function _initialize(KConfig $config)
    {
        $config->append(array(
            'priority'   => KCommand::PRIORITY_LOW,
        ));

        parent::_initialize($config);
    }
	
	
	/**
	 * Find any <script src="" /> or <script></script> elements and render them
	 *
	 * <script inline></script> can be used for inline scripts
	 *
	 * @param string Block of text to parse
	 * @return KTemplateFilterLink
	 */
	public function write(&$text)
	{
		//Parse the script information
		$scripts = $this->_parseScripts($text);

		//Prepend the script information
		$text = $scripts.$text;

		return $this;
	}

	/**
	 * Parse the text for script tags
	 *
	 * @param string Block of text to parse
	 * @return string
	 */
	protected function _parseScripts(&$text)
	{
		$scripts = '';

		$matches = array();
		// <script src="" />
		if(preg_match_all('#<script(?!\s+inline\s*)\s+src="([^"]+)"(.*)/>#siU', $text, $matches))
		{
			foreach(array_unique($matches[1]) as $key => $match)
			{
				$attribs = $this->_parseAttributes( $matches[2][$key]);
				$scripts .= $this->_renderScript($match, true, $attribs);
			}

			$text = str_replace($matches[0], '', $text);
		}

		$matches = array();
		// <script></script>
		if(preg_match_all('#<script(?!\s+inline\s*)(.*)>(.*)</script>#siU', $text, $matches))
		{
			foreach($matches[2] as $key => $match)
			{
				$attribs = $this->_parseAttributes( $matches[1][$key]);
				$scripts .= $this->_renderScript($match, false, $attribs);
			}

			$text = str_replace($matches[0], '', $text);
		}

		// get rid of inline and inline="true" in script tags
		$text = preg_replace('#<script\s*(?:inline="true"|inline)\s*#siU', '<script', $text);

		return $scripts;
	}

	/**
	 * Render script information
	 *
	 * @param string	The script information
	 * @param boolean	True, if the script information is a URL.
	 * @param array		Associative array of attributes
	 * @return string
	 */
	protected function _renderScript($script, $link, $attribs = array())
	{
		$attribs = KHelperArray::toString($attribs);

		if(!$link)
		{
			$html  = '<script type="text/javascript" '.$attribs.'>'."\n";
			$html .= trim($script);
			$html .= '</script>'."\n";
		}
		else $html = '<script type="text/javascript" src="'.$script.'" '.$attribs.'></script>'."\n";

		return $html;
	}
}<commit_msg>re #189 : Replace inline with data-inline in KTemplateHelperScript<commit_after><?php
/**
* @version      $Id$
* @category		Koowa
* @package      Koowa_Template
* @subpackage	Filter
* @copyright    Copyright (C) 2007 - 2010 <NAME>. All rights reserved.
* @license      GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
* @link 		http://www.nooku.org
*/

/**
 * Template filter to parse script tags
 *
 * @author		<NAME> <<EMAIL>>
 * @category	Koowa
 * @package     Koowa_Template
 * @subpackage	Filter
 */
class KTemplateFilterScript extends KTemplateFilterAbstract implements KTemplateFilterWrite
{
	/**
     * Initializes the options for the object
     *
     * Called from {@link __construct()} as a first step of object instantiation.
     *
     * @param   object  An optional KConfig object with configuration options
     * @return void
     */
    protected function _initialize(KConfig $config)
    {
        $config->append(array(
            'priority'   => KCommand::PRIORITY_LOW,
        ));

        parent::_initialize($config);
    }
	
	
	/**
	 * Find any <script src="" /> or <script></script> elements and render them
	 *
	 * <script inline></script> can be used for inline scripts
	 *
	 * @param string Block of text to parse
	 * @return KTemplateFilterLink
	 */
	public function write(&$text)
	{
		//Parse the script information
		$scripts = $this->_parseScripts($text);

		//Prepend the script information
		$text = $scripts.$text;

		return $this;
	}

	/**
	 * Parse the text for script tags
	 *
	 * @param string Block of text to parse
	 * @return string
	 */
	protected function _parseScripts(&$text)
	{
		$scripts = '';

		$matches = array();
		// <script src="" />
		if(preg_match_all('#<script(?!\s+data\-inline\s*)\s+src="([^"]+)"(.*)/>#siU', $text, $matches))
		{
			foreach(array_unique($matches[1]) as $key => $match)
			{
				$attribs = $this->_parseAttributes( $matches[2][$key]);
				$scripts .= $this->_renderScript($match, true, $attribs);
			}

			$text = str_replace($matches[0], '', $text);
		}

		$matches = array();
		// <script></script>
		if(preg_match_all('#<script(?!\s+data\-inline\s*)(.*)>(.*)</script>#siU', $text, $matches))
		{
			foreach($matches[2] as $key => $match)
			{
				$attribs = $this->_parseAttributes( $matches[1][$key]);
				$scripts .= $this->_renderScript($match, false, $attribs);
			}

			$text = str_replace($matches[0], '', $text);
		}

		return $scripts;
	}

	/**
	 * Render script information
	 *
	 * @param string	The script information
	 * @param boolean	True, if the script information is a URL.
	 * @param array		Associative array of attributes
	 * @return string
	 */
	protected function _renderScript($script, $link, $attribs = array())
	{
		$attribs = KHelperArray::toString($attribs);

		if(!$link)
		{
			$html  = '<script type="text/javascript" '.$attribs.'>'."\n";
			$html .= trim($script);
			$html .= '</script>'."\n";
		}
		else $html = '<script type="text/javascript" src="'.$script.'" '.$attribs.'></script>'."\n";

		return $html;
	}
}
{'fraction_non_alphanumeric': 0.17520391517128875, 'fraction_numerical': 0.005383360522022838, 'mean_word_length': 3.2814245810055866, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '7142398', 'n_tokens_mistral': 2198, 'n_tokens_neox': 1987, 'n_words': 496}