Add HTML anchors to a page. Useful when section headers are really long.
Compatible with DokuWiki
Similar to bookmark
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.
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”
<?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
<?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>'; } } ?>
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
In my opinion, it works fine with Anteater
AlekS
Works also with “Angua” — aleks
aleks
2012/01/10 09:48
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
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