|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @file plugins/generic/inlineHtmlGalley/InlineHtmlGalleyPlugin.inc.php |
| 5 | + * |
| 6 | + * Copyright (c) University of Pittsburgh |
| 7 | + * Copyright (c) 2014-2020 Simon Fraser University |
| 8 | + * Copyright (c) 2003-2020 John Willinsky |
| 9 | + * Distributed under the GNU GPL v2 or later. For full terms see the file docs/COPYING. |
| 10 | + * |
| 11 | + * @class InlineHtmlGalleyPlugin |
| 12 | + * @ingroup plugins_generic_inlineHtmlGalley |
| 13 | + * |
| 14 | + * @brief Class for InlineHtmlGalley plugin |
| 15 | + */ |
| 16 | + |
| 17 | +import('plugins.generic.htmlArticleGalley.HtmlArticleGalleyPlugin'); |
| 18 | + |
| 19 | +class InlineHtmlGalleyPlugin extends HtmlArticleGalleyPlugin { |
| 20 | + /** |
| 21 | + * @see Plugin::register() |
| 22 | + */ |
| 23 | + function register($category, $path, $mainContextId = null) { |
| 24 | + if (!parent::register($category, $path, $mainContextId)) return false; |
| 25 | + return true; |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Get the display name of this plugin. |
| 30 | + * @return String |
| 31 | + */ |
| 32 | + function getDisplayName() { |
| 33 | + return __('plugins.generic.inlineHtmlGalley.displayName'); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Get a description of the plugin. |
| 38 | + */ |
| 39 | + function getDescription() { |
| 40 | + return __('plugins.generic.inlineHtmlGalley.description'); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Present the article wrapper page. |
| 45 | + * @param string $hookName |
| 46 | + * @param array $args |
| 47 | + */ |
| 48 | + function articleViewCallback($hookName, $args) { |
| 49 | + $request =& $args[0]; |
| 50 | + $issue =& $args[1]; |
| 51 | + $galley =& $args[2]; |
| 52 | + $article =& $args[3]; |
| 53 | + |
| 54 | + if ($galley && $galley->getFileType() == 'text/html') { |
| 55 | + $templateMgr = TemplateManager::getManager($request); |
| 56 | + $templateMgr->assign(array( |
| 57 | + 'issue' => $issue, |
| 58 | + 'article' => $article, |
| 59 | + 'galley' => $galley, |
| 60 | + )); |
| 61 | + $inlineHtmlGalley = $this->_getHTMLContents($request, $galley); |
| 62 | + $inlineHtmlGalleyBody = $this->_extractBodyContents($inlineHtmlGalley); |
| 63 | + $templateMgr->assign('inlineHtmlGalley', $inlineHtmlGalleyBody); |
| 64 | + $templateMgr->display($this->getTemplateResource('displayInline.tpl')); |
| 65 | + |
| 66 | + return true; |
| 67 | + } |
| 68 | + |
| 69 | + return false; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Return string containing the contents of the HTML body |
| 74 | + * @param $html string |
| 75 | + * @return string |
| 76 | + */ |
| 77 | + function _extractBodyContents($html) { |
| 78 | + $bodyContent = ''; |
| 79 | + try { |
| 80 | + $errorsEnabled = libxml_use_internal_errors(); |
| 81 | + libxml_use_internal_errors(true); |
| 82 | + $dom = DOMDocument::loadHTML($html); |
| 83 | + $tags = $dom->getElementsByTagName('body'); |
| 84 | + foreach ($tags as $body) { |
| 85 | + foreach ($body->childNodes as $child) { |
| 86 | + $bodyContent .= $dom->saveHTML($child); |
| 87 | + } |
| 88 | + last; |
| 89 | + } |
| 90 | + libxml_use_internal_errors($errorsEnabled); |
| 91 | + } catch (Exception $e) { |
| 92 | + $html = preg_replace('/.*<body[^>]*>/isA', '', $html); |
| 93 | + $html = preg_replace('/<\/body\s*>.*$/isD', '', $html); |
| 94 | + $bodyContent = $html; |
| 95 | + } |
| 96 | + return $bodyContent; |
| 97 | + } |
| 98 | +} |
0 commit comments