DokuWiki

It's better when it's simple

User Tools

Site Tools


plugin:hiddenheader

This is an old revision of the document!


hiddenheader Plugin

Compatible with DokuWiki

angua, rincewind, weatherwax, binky, ponder stibbons, hrun, detritus

plugin Create invisible section headers. Useful companion to the include plugin.

Last updated on
2013-05-22
Provides
Action

The missing download url means that this extension cannot be installed via the Extension Manager. Please see Publishing a Plugin on dokuwiki.org. Recommended are public repository hosts like GitHub, GitLab or Bitbucket.

This extension has not been updated in over 2 years. It may no longer be maintained or supported and may have compatibility issues.

Tagged with header, include, section, title

This plugin allows you to create a section header which doesn't appear in the page. Why would you ever want to do this? Mainly to work with plugins that affect sections. You can also use it to set the page title without having a visible header, but the meta plugin does that better.

I made this as a companion to the include plugin. Include lets you include a page or a section, but it's missing a feature I consider very useful – the ability to include an excerpt from a large document which is not annotated with section headers.

Installation

I haven't bothered to make this plugin downloadable. Just copy and paste the following into /lib/plugins/hiddenheader/action.php.

lib/plugins/hiddenheader/action.php
<?php
/**
 * hiddenheader Plugin for DokuWiki / action.php
 *
 * @license GPL - http://www.gnu.org/copyleft/gpl.html
 * @author  Eli Fenton
 */
 
if (!defined('DOKU_INC')) {die();}
if (!defined('DOKU_PLUGIN')) {define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');}
require_once DOKU_PLUGIN . 'action.php';
 
class action_plugin_hiddenheader extends DokuWiki_Action_Plugin {
    function getInfo() {return array('author' => 'Eli Fenton', 'name' => 'Hidden Header Plugin', 'url' => 'http://dokuwiki.org/plugin:hiddenheader');}
 
    function register(Doku_Event_Handler $controller) {
        $controller->register_hook('PARSER_WIKITEXT_PREPROCESS', 'AFTER', $this, 'handlePreprocess');
        $controller->register_hook('RENDERER_CONTENT_POSTPROCESS', 'BEFORE', $this, 'handlePostProcess');
        $controller->register_hook('TPL_TOC_RENDER', 'BEFORE', $this, 'handleToc');
    }
 
    function handlePreprocess(&$event, $param) {
        // should accumulate in hash
        if ($this->hidden)
                return;
        preg_match_all('/==+\%hide\s*([^=]+)/', $event->data, $m);
        $this->hidden = $m && count($m[1])>0 ? $m[1] : null;
 
        if ($this->hidden)
            $event->data = preg_replace('/(==+)\%hide/', '$1', $event->data);
    }
 
    function handlePostProcess(&$event, $param) {
        if ($this->hidden)
                foreach ($this->hidden as $h) {
                        $event->data[1] = preg_replace('/<h\d[^>]*>(<a name[^>]*>|)'.trim($h).'(<\/a>|)<\/h\d>/', '$1$2', $event->data[1]);
        }
    }
 
    function handleToc(&$event, $param) {
        if ($this->hidden) {
            $map = array();
            foreach ($this->hidden as $h)
                $map[trim($h)] = 1;
            $newdata = array();
            foreach ($event->data as $d) {
                if (!$map[$d['hid']])
                    $newdata[] = $d;
            }
            // I don't know what's special about the number "2." There must be two hidden elements or something.
            $event->data = count($newdata)<=2 ? array() : $newdata;
        }
    }
 
    var $hidden;
}

Examples/Usage

Once the plugin is installed, you can create section headers with the special tag “%hide” in the title. The reference for the header is whatever comes after %hide. For example:

====%hide myhidden ====

In this case, the header will not show up in your page, but it will still be possible to link to pagename#myhidden (as with the anchor plugin). Additionally, other plugins will recognize this as a section called “myhidden.” For example, using the Include plugin, you can include the contents of this section as follows:

{{section>mypage#myhidden}}

Make sure to end the section, by starting a new section. This can be done with another hidden section, or with an empty section header: ==== ====

Here's a complete example:

This page is called "mypage."
====%hide myhidden====
This text will show up if you use the include plugin on this section -- {{section>mypage#myhidden}}.
==== ====
This text will not show up when using the include plugin.

This plug-in has one more use: Creating a title for a page that should not have a visible section header. For example, if you create the folowing as the first section in the page, wiki links to that page will show up with the header text as the title.

This: ====%hide My Magic Page ====

in a page called somens:mypage causes this: [[somens:mypage]] to show up like this: My Magic Page.

Note that the meta plugin does this invisible-title thing better, but if you're already using this plugin and don't want to install meta, you might as well take advantage of the feature.

Bugs/Issues

Looking at the code, I feel like this is pretty inefficient, since it involves multiple scans through the entire contents of the page. However, I haven't noticed any slowdown. If someone has an idea of a more efficient way to do this, let me know.

When an entire page containing these headers is included with the include plugin, the headers will show up in the table of contents. That will not happen when just the section is included.

In Anguna, hidden headers show up in the table of contents. This took some time to work out in the last version. I'll see what I can do.

Change Log
  • 2013-05-22: Updated for Weatherwax. The new DokuWiki doesn't add anchors to headers unless there's a TOC.
  • 2012-03-29: I just ran into a case where the preprocess command could be called twice by dokuwiki. This might be a bug in Angua. The new code works around this problem.
  • 2011-08-26: Hidden headers are no longer added to the table of contents.
  • 2011-08-29: Updated license to GPL (requested by Anika Henke).

Discussion

Thanks, but I have a little problem (Mar2017, Frusterick). When I link to a hidden section from another page, the hidden headers are shown on the page when I arrive. The only way to hide them again appears to be to edit the page, make an actual change, and save.

plugin/hiddenheader.1490224980.txt.gz · Last modified: 2017-03-23 00:23 by 137.45.240.191

Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
CC Attribution-Share Alike 4.0 International Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki