*/
class syntax_plugin_doi_doi extends \dokuwiki\Extension\SyntaxPlugin
{
/** @inheritDoc */
public function getType()
{
return 'substition';
}
/** @inheritDoc */
public function getPType()
{
return 'normal';
}
/** @inheritDoc */
public function getSort()
{
return 155;
}
/** @inheritDoc */
public function connectTo($mode)
{
$this->Lexer->addSpecialPattern('\[\[doi>[^\]]+\]\]', $mode, 'plugin_doi_doi');
}
/** @inheritDoc */
public function handle($match, $state, $pos, Doku_Handler $handler)
{
$match = substr($match, 2, -2);
list(, $id) = sexplode('>', $match, 2);
list($id, $title) = sexplode('|', $id, 2);
return ['id' => $id, 'title' => $title];
}
/** @inheritDoc */
public function render($mode, Doku_Renderer $renderer, $data)
{
$resolver = $this->getResolver();
$data['id'] = $resolver->cleanID($data['id']);
try {
$publication = $resolver->getData($data['id']);
} catch (Exception $e) {
msg(hsc($e->getMessage()), -1);
$url = $resolver->getFallbackURL($data['id']);
$title = empty($data['title']) ? $data['id'] : $data['title'];
$renderer->externallink($url, $title);
return true;
}
// overwritten title?
if (!empty($data['title'])) $publication['title'] = $data['title'];
if ($mode === 'xhtml') {
/** @var Doku_Renderer_xhtml $renderer */
$this->renderXHTML($publication, $renderer);
} else {
$this->renderAny($publication, $renderer);
}
return true;
}
/**
* @return AbstractResolver
*/
protected function getResolver()
{
return new DoiResolver();
}
/**
* Render the given data on the XHTML renderer
*
* Adds various classes to the output for CSS styling
*
* @param array $data
* @param Doku_Renderer_xhtml $renderer
* @return void
*/
protected function renderXHTML($data, $renderer)
{
$renderer->doc .= '
';
$renderer->externallink($data['url'], $data['title']);
if ($data['published']) {
$renderer->doc .= '
(' . hsc($data['published']) . ')';
}
$renderer->doc .= '
';
if ($data['authors']) {
$authors = array_map(function ($author) {
return '' . hsc($author) . '';
}, $data['authors']);
$renderer->doc .= '' . join(', ', $authors) . '';
}
if ($data['journal']) {
$journal = $data['journal'];
$journal .= ' ' . join('/', array_filter([$data['volume'] ?? null, $data['issue'] ?? null]));
$journal = '' . hsc($journal) . '';
if ($data['page']) {
$journal .= ' p' . hsc($data['page']) . '';
}
$renderer->doc .= ' ' . $journal . '';
}
$renderer->doc .= '
';
$renderer->doc .= '
';
if ($data['publisher']) {
$renderer->doc .= '' . hsc($data['publisher']) . '';
}
$renderer->doc .= ' ' . $data['idtype'] . ':' . hsc($data['id']) . '
';
$renderer->doc .= '
';
$renderer->doc .= '
';
}
/**
* Render the given data on any renderer
*
* Uses renderer methods only
*
* @param array $data
* @param Doku_Renderer $renderer
* @return void
*/
protected function renderAny($data, $renderer)
{
$renderer->p_open();
$renderer->externallink($data['url'], $data['title']);
if ($data['published']) {
$renderer->cdata(' (' . hsc($data['published']) . ')');
}
$renderer->linebreak();
if ($data['authors']) {
$len = count($data['authors']);
for ($i = 0; $i < $len; $i++) {
$renderer->strong_open();
$renderer->cdata($data['authors'][$i]);
$renderer->strong_close();
if ($i < $len - 1) {
$renderer->cdata(', ');
}
}
if ($data['journal']) {
$journal = $data['journal'];
$journal .= ' ' . join('/', array_filter([$data['volume'] ?? null, $data['issue'] ?? null]));
$renderer->cdata(' ' . $journal);
}
if ($data['page']) {
$renderer->cdata(' ');
$renderer->emphasis_open();
$renderer->cdata('p' . $data['page']);
$renderer->emphasis_close();
}
}
$renderer->linebreak();
if ($data['publisher']) {
$renderer->cdata($data['publisher']);
$renderer->cdata(' ');
}
$renderer->monospace_open();
$renderer->cdata($data['idtype'] . ':' . hsc($data['id']));
$renderer->monospace_close();
$renderer->p_close();
}
}