Skip to content

Commit

Permalink
Added more modern YouTube embedding to XmlLegacy renderer. Externaliz…
Browse files Browse the repository at this point in the history
…ed a few strings. This is not tested very thoroughly.
  • Loading branch information
mermshaus committed May 11, 2012
1 parent c884ec2 commit 54ab4a5
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 52 deletions.
96 changes: 46 additions & 50 deletions src/Kaloa/Renderer/XmlLegacyRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,67 +6,39 @@
use XSLTProcessor;
use Kaloa\Renderer\AbstractRenderer;

/**
*
*/
class XmlLegacyRenderer extends AbstractRenderer
{
/**
* @var type
*/
protected static $myself;

/** @var XSLTProcessor */
/**
* @var XSLTProcessor
*/
protected $xsltProcessor = null;

/**
*
*/
protected function init()
{
self::$myself = $this;

$this->initXsltProcessor();
}

/**
*
*/
protected function initXsltProcessor()
{
$className = __CLASS__;

$xsl = <<<EOT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:php="http://php.net/xsl">
<xsl:output method="xml" encoding="UTF-8" indent="no"/>
<xsl:template match="youtube">
<div class="blog_youtube_container">
<object type="application/x-shockwave-flash"
class="blog_youtube"
data="http://www.youtube.com/v/{@id}"
>
<param name="movie"
value="http://www.youtube.com/v/{@id}&amp;hl=en&amp;fs=0"
/>
</object>
</div>
</xsl:template>
<xsl:template match="code">
<xsl:copy-of select="php:function('$className::highlight', string(.), string(@lang))" />
</xsl:template>
<xsl:template match="img/@src">
<xsl:attribute name="src">
<xsl:copy-of select="php:function('$className::imageUrl', string(.))" />
</xsl:attribute>
</xsl:template>
<xsl:template match="a/@href">
<xsl:attribute name="href">
<xsl:copy-of select="php:function('$className::linkUrl', string(.))" />
</xsl:attribute>
</xsl:template>
<!-- the identity template -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
EOT;
$xsl = file_get_contents(__DIR__ . '/xmllegacy.xsl');

$xsl = str_replace('__CLASS__', __CLASS__, $xsl);

$xslDoc = new DOMDocument();
$xslDoc->loadXML($xsl);
Expand All @@ -78,6 +50,11 @@ class="blog_youtube"
$this->xsltProcessor = $xsltProcessor;
}

/**
*
* @param string $input
* @return string
*/
public function render($input)
{
$xmlCode = '<?xml version="1.0" encoding="utf-8"?>
Expand All @@ -92,9 +69,17 @@ public function render($input)

$tmp = $this->xsltProcessor->transformToDoc($xmldoc);

return substr($tmp->saveXML($tmp->documentElement), 6, -7);
$html = $tmp->saveHTML($tmp->documentElement);
$html = substr($html, 6, -7);

return $html;
}

/**
*
* @param string $path
* @return string
*/
public static function imageUrl($path)
{
if (preg_match('/^https?:\/\//', $path)) {
Expand All @@ -105,6 +90,11 @@ public static function imageUrl($path)
. $path;
}

/**
*
* @param string $path
* @return string
*/
public static function linkUrl($path)
{
if (preg_match('/^(https?:\/\/|mailto:)/', $path)) {
Expand All @@ -120,8 +110,8 @@ public static function linkUrl($path)
*
* @todo OMG this method can't be efficient
*
* @param string $source
* @param string $lang
* @param string $source
* @param string $language
* @return DOMElement?
*/
public static function highlight($source, $language)
Expand Down Expand Up @@ -195,7 +185,13 @@ public static function highlight($source, $language)
# . '</span>';
}

$code[$i] = preg_replace_callback('/>(.*?)</s', function ($matches) { return '>' . str_replace(' ', '&nbsp;<wbr/>', $matches[1]) . '<'; }, $code[$i]);
$code[$i] = preg_replace_callback(
'/>(.*?)</s',
function ($matches)
{
return '>' . str_replace(' ', '&nbsp;<wbr/>', $matches[1]) . '<';
},
$code[$i]);

$parsed_code .= $code[$i] . "\n";
}
Expand Down
42 changes: 42 additions & 0 deletions src/Kaloa/Renderer/xmllegacy.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:php="http://php.net/xsl"
exclude-result-prefixes="php"
>
<xsl:output method="xml" encoding="UTF-8" indent="no"/>

<xsl:template match="youtube">
<div class="videoWrapper">
<iframe src="http://www.youtube.com/embed/{@id}" frameborder="0"></iframe>
</div>
</xsl:template>

<xsl:template match="x">
<p>y</p>
</xsl:template>

<xsl:template match="code">
<xsl:copy-of select="php:function('__CLASS__::highlight', string(.), string(@lang))" />
</xsl:template>

<xsl:template match="img/@src">
<xsl:attribute name="src">
<xsl:copy-of select="php:function('__CLASS__::imageUrl', string(.))" />
</xsl:attribute>
</xsl:template>

<xsl:template match="a/@href">
<xsl:attribute name="href">
<xsl:copy-of select="php:function('__CLASS__::linkUrl', string(.))" />
</xsl:attribute>
</xsl:template>

<!-- the identity template -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>
5 changes: 3 additions & 2 deletions tests/Kaloa/Tests/Renderer/XmlLegacyRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ class XmlLegacyRendererTest extends PHPUnit_Framework_TestCase
public function testIntegrity()
{
// Environment
$contentToRender = file_get_contents(__DIR__ . '/examples/xmllegacy/arrayobject.xml');
#$contentToRender = file_get_contents(__DIR__ . '/examples/xmllegacy/arrayobject.xml');
$contentToRender = file_get_contents(__DIR__ . '/examples/xmllegacy/youtube.xml');
$resourceBasePath = __DIR__ . '/examples/xmllegacy';
$filter = 'xmllegacy';

Expand All @@ -25,6 +26,6 @@ public function testIntegrity()

$output = $renderer->render($contentToRender);

#var_dump($output);
var_dump($output);
}
}
19 changes: 19 additions & 0 deletions tests/Kaloa/Tests/Renderer/examples/xmllegacy/youtube.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<p>Test1</p>

<youtube id="uzQ4f35JMUk" />

<p>Test2</p>

<youtube id="uzQ4f35JMUk" />

<p>Test3</p>

<code lang="php"><![CDATA[
echo "bla";
]]></code>

<p>Test4</p>

<x>x_bla_x</x>

<p>Test5</p>

0 comments on commit 54ab4a5

Please sign in to comment.