Lexer->addSpecialPattern($pattern, $mode, PluginUtility::getModeForComponent($this->getPluginComponent())); } /** * * The handle function goal is to parse the matched syntax through the pattern function * and to return the result for use in the renderer * This result is always cached until the page is modified. * @param string $match * @param int $state * @param int $pos * @param Doku_Handler $handler * @return array|bool * @see DokuWiki_Syntax_Plugin::handle() * */ function handle($match, $state, $pos, Doku_Handler $handler) { $attributes = PluginUtility::getTagAttributes($match); return array($attributes); } /** * Render the output * @param string $format * @param Doku_Renderer $renderer * @param array $data - what the function handle() return'ed * @return boolean - rendered correctly? (however, returned value is not used at the moment) * @see DokuWiki_Syntax_Plugin::render() * */ function render($format, Doku_Renderer $renderer, $data) { switch ($format) { case 'xhtml': list($attributes) = $data; /** @var Doku_Renderer_xhtml $renderer */ /** * Disqus configuration * https://help.disqus.com/en/articles/1717084-javascript-configuration-variables */ $default = PluginUtility::getTagAttributes($this->getConf(self::CONF_DEFAULT_ATTRIBUTES)); $attributes = PluginUtility::mergeAttributes($attributes, $default); $forumShortName = $attributes[self::ATTRIBUTE_SHORTNAME]; if (empty($forumShortName)) { LogUtility::msg("The disqus forum shortName should not be empty", LogUtility::LVL_MSG_ERROR, self::TAG); return false; } $forumShortName = hsc($forumShortName); $disqusIdentifier = MetadataUtility::getMeta(self::META_DISQUS_IDENTIFIER); if (empty($disqusIdentifier)) { $disqusIdentifier = $attributes[self::ATTRIBUTE_IDENTIFIER]; if (empty($disqusIdentifier)) { $disqusIdentifier = PluginUtility::getPageId(); } $canonical = MetadataUtility::getMeta(Page::CANONICAL_PROPERTY); if (!empty($canonical)) { $disqusIdentifier = $canonical; } MetadataUtility::setMeta(self::META_DISQUS_IDENTIFIER, $disqusIdentifier); } $disqusConfig = "this.page.identifier = \"$disqusIdentifier\";"; $url = $attributes[self::ATTRIBUTE_URL]; if (empty($url)) { if (!empty($canonical)) { $url = Page::getUrl($canonical); } } if (!empty($url)) { $disqusConfig .= "this.page.url = $url;"; } $title = $attributes[self::ATTRIBUTE_TITLE]; if (empty($title)) { $title = action_plugin_combo_metatitle::getTitle(); if (!empty($title)){ $disqusConfig .= "this.page.title = $title;"; } } $category = $attributes[self::ATTRIBUTE_CATEGORY]; if (empty($category)){ $disqusConfig .= "this.page.category_id = $category;"; } /** * The javascript */ $renderer->doc .= << // Configuration // The disqus_config should be a var to give it the global scope // Otherwise, disqus will see no config // noinspection ES6ConvertVarToLetConst var disqus_config = function () { $disqusConfig }; // Embed the library (function() { const d = document, s = d.createElement('script'); s.src = 'https://$forumShortName.disqus.com/embed.js'; s.setAttribute('data-timestamp', (+new Date()).toString()); (d.head || d.body).appendChild(s); })(); EOD; // The tag $renderer->doc .= '
'; return true; break; case 'metadata': } return false; } }