Last active
August 29, 2015 14:14
-
-
Save muskie9/1bc96d343de4e4316bb7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once( dirname(dirname(__FILE__)).'/thirdparty/simple_html_dom.php' ); | |
// Transforms the content area of the page | |
// Usage: | |
// $Content.Parse(ContentParser) | |
class ContentParser extends TextParser { | |
public function parse() { | |
$html = str_get_html($this->content); | |
if ( !$html ) { | |
return $this->content; | |
} | |
// Add 'responsive' class to tables | |
$tables = $html->find('table'); | |
foreach($tables as $table ) { | |
$table->class = trim($table->class . ' responsive'); | |
} | |
// Add anchors and back to top links before H3 elements - linked to by sidebar | |
$headings = $html->find('h3'); | |
foreach( $headings as $Pos => $heading ) { | |
$Pos = $Pos + 1; // $Pos is 1-indexed | |
$newOuterText = '<a name="heading-'.$Pos.'"></a>' . $heading->outertext; | |
// If there are 2 or more headings, add back to top before each one (but not the top one) | |
if ( count($headings) >= 2 && $Pos != 1 ) { | |
$newOuterText = '<p class="back-to-top"><a href="#top">Back to top</a></p>' . $newOuterText; | |
} | |
$heading->outertext = $newOuterText; | |
} | |
// IMG elements need a bunch of stuff, can be tricky! | |
$imgs = $html->find('img'); | |
foreach( $imgs as $img ) { | |
$target = $img; | |
$src = '/' . $img->src; | |
$alt = $img->alt; | |
$title = $img->title; | |
$class = ( $img->class == 'right' ? 'span3 build-figure-portrait build-figure-right' : 'build-figure-landscape' ); | |
$orientation = ($img->class == 'right' ? 'Portrait' : 'Landscape' ); | |
$imgAltSpan = $img->parent()->find('span', 0); | |
if ($imgAltSpan) $imgAltText = $imgAltSpan->plaintext; else $imgAltText = $alt; | |
} | |
// Youtube video divs need the 'build-content-video' class added | |
$iframes = $html->find('iframe'); | |
foreach( $iframes as $iframe ) { | |
$parent = $iframe->parent(); | |
if ( $parent->tag == 'div' && strpos($parent->class, 'thumbnail=') !== false ) { | |
$parent->class = trim($parent->class . ' build-content-video'); | |
} | |
} | |
$string = $html->save(); | |
// Add a final back to top link if there are 2 or more headings | |
if ( count($headings) >= 2 ) { | |
$string = $string . '<p class="back-to-top"><a href="#top">Back to top</a></p>'; | |
} | |
return $string; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment