-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
42 additions
and
7 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
<?php | ||
/** | ||
* External Links v1.1.2 | ||
* External Links v1.1.3 | ||
* | ||
* This plugin adds small icons to external and mailto links, informing | ||
* users the link will take them to a new site or open their email client. | ||
* | ||
* Licensed under MIT, see LICENSE. | ||
* | ||
* @package External Links | ||
* @version 1.1.2 | ||
* @version 1.1.3 | ||
* @link <https://github.com/sommerregen/grav-plugin-archive-plus> | ||
* @author Benjamin Regler <[email protected]> | ||
* @copyright 2015, Benjamin Regler | ||
|
@@ -87,13 +87,29 @@ public function onPageContentProcessed(Event $event) { | |
if ( $config->get('process', TRUE) ) { | ||
$content = $page->getRawContent(); | ||
|
||
/** | ||
* Two Really good resources to handle DOMDocument with HTML(5) | ||
* correctly. | ||
* | ||
* @see http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php | ||
* @see http://stackoverflow.com/questions/7997936/how-do-you-format-dom-structures-in-php | ||
*/ | ||
|
||
// Clear previous errors | ||
if ( libxml_use_internal_errors(TRUE) === TRUE ) { | ||
libxml_clear_errors(); | ||
} | ||
|
||
// Create a DOM parser object | ||
$dom = new \DOMDocument('1.0', 'UTF-8'); | ||
|
||
// Pretty print output | ||
$dom->preserveWhiteSpace = FALSE; | ||
$dom->formatOutput = TRUE; | ||
|
||
// Normalize newlines | ||
$content = preg_replace('~\R~u', "\n", $content); | ||
|
||
// Parse the HTML using UTF-8 | ||
// The @ before the method call suppresses any warnings that | ||
// loadHTML might throw because of invalid HTML in the page. | ||
|
@@ -183,15 +199,28 @@ public function onPageContentProcessed(Event $event) { | |
} | ||
|
||
$content = ''; | ||
// Process HTML from DOM document | ||
// Transform DOM document to valid HTML(5) | ||
$body = $dom->getElementsByTagName('body')->item(0); | ||
foreach ( $body->childNodes as $node ) { | ||
$content .= $dom->saveXML($node); | ||
// Expand empty tags (e.g. <br/> to <br></br>) | ||
if ( ($html = $dom->saveXML($node, LIBXML_NOEMPTYTAG)) !== FALSE ) { | ||
$content .= $html; | ||
} | ||
} | ||
|
||
// Fix formatting for self-closing tags in HTML5 | ||
$content = preg_replace('~<(area|base(?:font)?|br|col|command|embed|frame|hr|img|input|keygen|link|meta|param|source|track|wbr)(.*?)\s*/>~i', '<$1$2 />', $content); | ||
// Fix formatting for self-closing tags in HTML5 and removing | ||
// encapsulated (uncommented) CDATA blocks in <script> and | ||
// <style> tags | ||
$regex = array( | ||
'~' . preg_quote('<![CDATA[', '~') . '~' => '', | ||
'~' . preg_quote(']]>', '~') . '~' => '', | ||
'~></(?:area|base(?:font)?|br|col|command|embed|frame|hr|img|input|keygen|link|meta|param|source|track|wbr)>~' => ' />', | ||
); | ||
|
||
// Make XML HTML5 compliant | ||
$content = preg_replace(array_keys($regex), $regex, $content); | ||
|
||
// Write content back to page | ||
$page->setRawcontent($content); | ||
} | ||
} | ||
|