Initial release of Link Heading Plugin
Dieser Commit ist enthalten in:
+120
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* DokuWiki Plugin linkheading (Syntax Component)
|
||||
*
|
||||
* Creates clickable headings linking to internal DokuWiki pages.
|
||||
*
|
||||
* @license MIT
|
||||
* @author Jens Mohr
|
||||
*/
|
||||
|
||||
use dokuwiki\Extension\SyntaxPlugin;
|
||||
|
||||
class syntax_plugin_linkheading extends SyntaxPlugin
|
||||
{
|
||||
/**
|
||||
* @return string Syntax mode type
|
||||
*/
|
||||
public function getType()
|
||||
{
|
||||
return 'substition';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string Paragraph type
|
||||
*/
|
||||
public function getPType()
|
||||
{
|
||||
return 'block';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int Sort order
|
||||
*/
|
||||
public function getSort()
|
||||
{
|
||||
return 155;
|
||||
}
|
||||
|
||||
/**
|
||||
* Connect syntax pattern to lexer.
|
||||
*
|
||||
* @param string $mode Parser mode
|
||||
*/
|
||||
public function connectTo($mode)
|
||||
{
|
||||
$this->Lexer->addSpecialPattern(
|
||||
'\{\{linkheading>[^}]+\}\}',
|
||||
$mode,
|
||||
'plugin_linkheading'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse plugin syntax.
|
||||
*
|
||||
* Supported forms:
|
||||
*
|
||||
* {{linkheading>target|Heading}}
|
||||
* {{linkheading>4|target|Heading}}
|
||||
*
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function handle($match, $state, $pos, Doku_Handler $handler)
|
||||
{
|
||||
$content = substr($match, 14, -2);
|
||||
$parts = explode('|', $content);
|
||||
|
||||
if (
|
||||
count($parts) >= 3 &&
|
||||
preg_match('/^[1-6]$/', trim($parts[0]))
|
||||
) {
|
||||
$level = (int) trim(array_shift($parts));
|
||||
$target = trim(array_shift($parts));
|
||||
$title = trim(implode('|', $parts));
|
||||
} else {
|
||||
$level = 2;
|
||||
$target = trim(array_shift($parts));
|
||||
$title = trim(implode('|', $parts));
|
||||
}
|
||||
|
||||
if ($title === '') {
|
||||
$title = $target;
|
||||
}
|
||||
|
||||
return [
|
||||
'level' => $level,
|
||||
'target' => $target,
|
||||
'title' => $title,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Render linked heading.
|
||||
*
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function render($mode, Doku_Renderer $renderer, $data)
|
||||
{
|
||||
$level = max(1, min(6, (int) $data['level']));
|
||||
$target = $data['target'];
|
||||
$title = $data['title'];
|
||||
|
||||
if ($mode === 'xhtml') {
|
||||
$renderer->doc .= '<h' . $level . ' class="linkheading">';
|
||||
$renderer->internallink($target, $title);
|
||||
$renderer->doc .= '</h' . $level . '>';
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($mode === 'metadata') {
|
||||
$renderer->internallink($target, $title);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
In neuem Issue referenzieren
Einen Benutzer sperren