Skip to content

Instantly share code, notes, and snippets.

@walterdavis
Created April 19, 2012 17:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save walterdavis/2422409 to your computer and use it in GitHub Desktop.
Save walterdavis/2422409 to your computer and use it in GitHub Desktop.
<?php
require_once('simple_html_dom.php');
/**
* utility function to DRY up testing if a variable is usable
*
* @param string $strVariableName
* @return boolean
* @author Walter Lee Davis
*/
function present($strVariableName){
return( isset($strVariableName) && !empty($strVariableName) );
}
/**
* rewrite links to respect current depth of the page in the virtual hierarchy and TLD
*
* @param object $element simple_html_dom tag object
* @global string $prefix difference between synthetic URL and filesystem path
* @global string $here site in use (for left-hand nav links)
* @return void
* @author Walter Lee Davis
*/
function rewrite_links($element){
global $prefix;
global $here;
$sites = array('blog_icon' => 'http://blog.tedgoranson.com/',
'kutachi_icon' => 'http://kutachi.tedgoranson.com/',
'films_icon' => 'http://filmsfolded.tedgoranson.com/',
'geok_icon' => 'http://geokabbalitter.tedgoranson.com/',
'tool_icon' => 'http://tool.tedgoranson.com/',
'app_link' => '/colophon.html'
);
if($element->tag == 'a' && ! preg_match('/^(https?:\/\/|#)/',$element->href)){
$element->href = ($prefix . '/' . $element->href);
}
if($element->tag == 'a' && present($element->id) && array_key_exists($element->id, $sites)){
$element->href = ($element->id == $here) ? '/' : $sites[$element->id];
}
if($element->tag == 'link' && ! preg_match('/^https?:\/\//',$element->href)){
$element->href = ('/' . preg_replace('/\.\.\//', '', $element->href));
}
if($element->tag == 'script' && ! preg_match('/^https?:\/\//',$element->src)){
$element->src = ('/' . preg_replace('/\.\.\//', '', $element->src));
}
if($element->tag == 'img' && ! preg_match('/^https?:\/\//',$element->src)){
$element->src = ('/' . preg_replace('/\.\.\//', '', $element->src));
}
die('Testing');
if($here == 'films_icon' && present($element->id) && $element->id == 'films_stories'){
$element->innertext = ($element->innertext . '
<div id="disqus_thread"></div>
<script type="text/javascript">
(function() {
var dsq = document.createElement(\'script\'); dsq.type = \'text/javascript\'; dsq.async = true;
dsq.src = \'http://filmsfolded.disqus.com/embed.js\';
(document.getElementsByTagName(\'head\')[0] || document.getElementsByTagName(\'body\')[0]).appendChild(dsq);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="http://disqus.com" class="dsq-brlink">blog comments powered by <span class="logo-disqus">Disqus</span></a>');
}
}
$doc = file_get_html($path);
$doc->set_callback('rewrite_links');
print $doc;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment