integrate new Grok based xref into the syntax

This commit is contained in:
Andreas Gohr
2022-01-27 23:54:28 +01:00
parent d4a11906f9
commit 4463a3c415
11 changed files with 58 additions and 76 deletions

View File

@ -26,6 +26,8 @@ class Grok
*/
public function getSearchUrl()
{
if($this->def === '' && $this->path === '') return $this->baseUrl;
$url = $this->baseUrl . '/search?';
$param = [
'project' => 'dokuwiki',
@ -60,6 +62,8 @@ class Grok
*/
public function getResultCount()
{
if($this->def === '' && $this->path === '') return 0;
$http = new DokuHTTPClient();
$http->timeout = 5;
$json = $http->get($this->getAPIUrl());

View File

@ -42,4 +42,11 @@ class GrokTest extends DokuWikiTest
$grok = new Grok($reference);
$this->assertEquals(1, $grok->getResultCount(), $grok->getSearchUrl());
}
public function testEmptyData()
{
$grok = new Grok('', 'https://testurl/');
$this->assertEquals('https://testurl', $grok->getSearchUrl());
$this->assertSame(0, $grok->getResultCount());
}
}

9
conf/default.php Normal file
View File

@ -0,0 +1,9 @@
<?php
/**
* Default settings for the xref plugin
*
* @author Andreas Gohr <andi@splitbrain.org>
*/
$conf['grokbaseurl'] = 'https://codesearch.dokuwiki.org';

View File

@ -1,5 +1,3 @@
<?php
$meta['web'] = array('string');
$meta['dir'] = array('string');
$meta['grokbaseurl'] = array('string');

View File

@ -2,4 +2,5 @@
$lang['unknown'] = 'unknown cross reference';
$lang['view'] = 'view cross reference for %s';
$lang['search'] = 'search cross reference for %s';

View File

@ -1,5 +1,4 @@
<?php
$lang['dir'] = 'Specify the directory where the PHPXref files of your project are located here.';
$lang['web'] = "Specify the root URL of your PHPXref documents here.";
$lang['grokbaseurl'] = 'URL to the grok install';

View File

@ -1,11 +0,0 @@
a.xref_plugin {
padding-left: 18px;
background: transparent url(xref.png) left top no-repeat;
}
a.xref_plugin_err {
color: __missing__ !important;
text-decoration: none !important;
border-bottom: dashed 1px __missing__ !important;
}

8
style.less Normal file
View File

@ -0,0 +1,8 @@
.dokuwiki a.plugin_xref {
background-image: url(xref.svg);
}
.dokuwiki a.plugin_xref_err {
border-bottom: dashed 1px __missing__ !important;
}

View File

@ -45,16 +45,12 @@ class syntax_plugin_xref extends DokuWiki_Syntax_Plugin
{
$match = trim(substr($match, 7, -2));
list($link, $name) = explode('|', $match, 2);
list($link, $anchor) = explode('#', $link, 2);
if (!$name) $name = $link;
if ($anchor) $anchor = "#" . $anchor;
list($reference, $name) = explode('|', $match, 2);
list($reference, $anchor) = explode('#', $reference, 2);
if (!$name) $name = $reference;
if ($anchor) $reference = "#" . $anchor;
$first = 0;
if ($link[0] == '$') $first = 4;
$found = $this->find($link, $first);
return array($link, $found, $name, $anchor);
return array($reference, $name);
}
/** @inheritdoc */
@ -63,63 +59,33 @@ class syntax_plugin_xref extends DokuWiki_Syntax_Plugin
global $conf;
if ($format != 'xhtml') return false;
//prepare for formating
$link['target'] = $conf['target']['extern'];
$link['style'] = '';
$link['pre'] = '';
$link['suf'] = '';
$link['more'] = '';
$link['class'] = 'xref_plugin';
$link['name'] = hsc($data[2]);
list($reference, $name) = $data;
$grok = new \dokuwiki\plugin\xref\Grok($reference, $this->getConf('grokbaseurl'));
$count = $grok->getResultCount();
if (!$data[1]) {
$link['url'] = $this->web;
$link = [
'target' => $conf['target']['extern'],
'style' => '',
'pre' => '',
'suf' => '',
'more' => '',
'class' => 'interwiki plugin_xref',
'name' => hsc($name),
'url' => $grok->getSearchUrl(),
'title' => sprintf($this->getLang('view'), hsc($reference)),
];
if ($count === false || $count === 0) {
$link['title'] = $this->getLang('unknown');
$link['class'] .= ' xref_plugin_err';
} else {
$link['url'] = $this->web . '/' . $data[1] . hsc($data[3]);
$link['title'] = sprintf($this->getLang('view'), hsc($data[0]));
$link['class'] .= ' plugin_xref_err';
}
if ($count > 1) {
$link['title'] = sprintf($this->getLang('search'), hsc($reference));
}
$R->doc .= $R->_formatLink($link);
return true;
}
/**
* Try to find the given name in the xref directory
*
* @param int $first - defines which type should be searched first for the name
*/
protected function find($name, $first = 0)
{
$paths = array(
0 => '_functions',
1 => '_classes',
2 => '_constants',
3 => '_tables',
4 => '_variables',
);
$clean = preg_replace('/[^\w\-_]+/', '', $name);
$small = strtolower($clean);
$path = $paths[$first];
unset($paths[$first]);
do {
$check = $path . '/' . $clean . '.html';
if (@file_exists($this->dir . '/' . $check)) return $check;
$check = $path . '/' . $small . '.html';
if (@file_exists($this->dir . '/' . $check)) return $check;
$path = array_shift($paths);
} while ($path);
// still here? might be a file reference
$clean = preg_replace('/\.\.+/', '.', $name);
if (@file_exists($this->dir . '/' . $clean . '.html')) {
return $clean . '.html';
}
return '';
}
}

BIN
xref.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 481 B

1
xref.svg Normal file
View File

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path fill="#a3b8cb" d="M15.5,12C18,12 20,14 20,16.5C20,17.38 19.75,18.21 19.31,18.9L22.39,22L21,23.39L17.88,20.32C17.19,20.75 16.37,21 15.5,21C13,21 11,19 11,16.5C11,14 13,12 15.5,12M15.5,14A2.5,2.5 0 0,0 13,16.5A2.5,2.5 0 0,0 15.5,19A2.5,2.5 0 0,0 18,16.5A2.5,2.5 0 0,0 15.5,14M5,3H19C20.11,3 21,3.89 21,5V13.03C20.5,12.23 19.81,11.54 19,11V5H5V19H9.5C9.81,19.75 10.26,20.42 10.81,21H5C3.89,21 3,20.11 3,19V5C3,3.89 3.89,3 5,3M7,7H17V9H7V7M7,11H12.03C11.23,11.5 10.54,12.19 10,13H7V11M7,15H9.17C9.06,15.5 9,16 9,16.5V17H7V15Z"/></svg>

After

Width:  |  Height:  |  Size: 597 B