anchor Plugin

By the same author

Compatible with DokuWiki

  • 2012-01-25 "Angua" yes
  • 2011-05-25 "Rincewind" yes
  • 2010-11-07 "Anteater" yes

plugin Add HTML anchors to a page. Useful when section headers are really long.

Last updated on
2011-07-25
Provides
Syntax

Similar to bookmark

Tagged with anchor, links, sample

Normally, you can only link to a wiki page, or a specific section header. This is impractical when header names are long, or you expect that they might change. This plugin lets you create links to any part of your page.

This plugin is also a useful sample, if you're having trouble figuring out how to create a syntax plugin.

Installation

This plugin is so simple, and I'm so lazy, that I'm just going to include the source code. The following text should be in the file “lib/plugins/anchor/syntax.php”

lib/plugins/anchor/syntax.php
<?php
if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');  
 
class syntax_plugin_anchor extends DokuWiki_Syntax_Plugin {
    function getType() {return 'substition';}
    function getPType() {return 'block';}
    function getSort() {return 167;}
    function getInfo() {return array('author' => 'Eli Fenton', 'name' => 'Anchor Plugin', 'url' => 'http://dokuwiki.org/plugin:anchor');}
    function connectTo($mode){
        $this->Lexer->addSpecialPattern('\{\{anchor:[^}]*\}\}', $mode, 'plugin_anchor');
    }
 
    function handle($match, $state, $pos, &$handler) {
        return explode(':', substr($match, strlen('{{anchor:'), -2));
    }
 
    function render($mode, &$renderer, $data) {
        $renderer->doc .= '<a name="' . $data[0] . '">' . $data[1] . '</a>';
    }
}

The following modified version fixes the escaping vulnerability and also the improper handling of paragraphs. I am no PHP programmer so use at your own risk (but it works for me). Peter B

lib/plugins/anchor/syntaxfixed.php
<?php
if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');  
 
class syntax_plugin_anchor extends DokuWiki_Syntax_Plugin {
    function getType() {return 'substition';}
    function getPType() {return 'normal';}
    function getSort() {return 167;}
    function getInfo() {return array('author' => 'Eli Fenton', 'name' => 'Anchor Plugin', 'url' => 'http://dokuwiki.org/plugin:anchor');}
    function connectTo($mode){
        $this->Lexer->addSpecialPattern('\{\{anchor:[^}]*\}\}', $mode, 'plugin_anchor');
    }
 
    function handle($match, $state, $pos, &$handler) {
        return explode(':', substr($match, strlen('{{anchor:'), -2));
    }
 
    function render($mode, &$renderer, $data) {
        $renderer->doc .= '<a name="' .htmlspecialchars($data[0]) . '">' .htmlspecialchars($data[1]) . '</a>';
    }
}
?>

Examples/Usage

Here's how you create an anchor:

{{anchor:anchor_name:text}}

The anchor_name part is the name of the anchor to use in links. The text part is optional, and, for the most part, not very useful. It lets you wrap the anchor around a specific piece of text.

To link to an anchor from within DokuWiki:

[[namespace:page_name#anchor_name]]

To link to an anchor from outside DokuWiki:

http://server.com/doku/doku.php?id=page_name#anchor_name

Discussion

Compatible with "Anteater"

In my opinion, it works fine with Anteater :-)
AlekS

Compatible with "Angua"

Works also with “Angua” — aleksaleks

2012/01/10 09:48

Possible security breach

It is possible for users to insert arbitrary HTML even if htmlok is off.

{{anchor:name:<script type="text/javascript">alert("Annoy!");</script>}}

2012-03-24

Possible bug

When I add several anchors within a paragraph (for example, each sentence has its own anchor), the paragraph is broken i.e. each anchor starts its own paragraph, which is undesirable.

2012-03-26

plugin/anchor.txt · Last modified: 2012/05/08 12:13 by 62.2.106.238
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 3.0 Unported
Imprint Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki
WikiForumIRCBugsGitXRefTranslate