Skip to content

Commit

Permalink
Better output mechanisms for URL Type
Browse files Browse the repository at this point in the history
  • Loading branch information
splitbrain committed Jun 28, 2021
1 parent dca2922 commit c7273e8
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
38 changes: 38 additions & 0 deletions _test/Type_Url.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,22 @@ public function validateSuccessProvider() {
);
}

/**
* Provide data to test autoshortening feature
*
* @return array
*/
public function generateAutoTitleProvider() {
return [
['https://foobar.com', 'foobar.com'],
['https://foobar.com/', 'foobar.com'],
['https://www.foobar.com/', 'foobar.com'],
['https://www.foobar.com/test', 'foobar.com/…'],
['https://www.foobar.com/?test', 'foobar.com/…'],
['https://www.foobar.com/#hash', 'foobar.com/…'],
];
}

/**
* @expectedException \dokuwiki\plugin\struct\meta\ValidationException
* @dataProvider validateFailProvider
Expand All @@ -67,4 +83,26 @@ public function test_validate_success($value, $prefix, $postfix, $autoscheme) {
$url->validate($value);
$this->assertTrue(true); // we simply check that no exceptions are thrown
}

/**
* @dataProvider generateAutoTitleProvider
*/
public function test_generateAutoTitle($input, $title) {
$url = new Url(['autoshorten' => true]);
$result = $this->callInaccessibleMethod($url, 'generateTitle', [$input]);
$this->assertSame($title, $result);

$url = new Url(['autoshorten' => false]);
$result = $this->callInaccessibleMethod($url, 'generateTitle', [$input]);
$this->assertSame($input, $result);
}

public function test_generateFixedTitle() {
$input = 'https://www.foobar.com/long';
$title = 'oink';

$url = new Url(['fixedtitle' => $title]);
$result = $this->callInaccessibleMethod($url, 'generateTitle', [$input]);
$this->assertSame($title, $result);
}
}
32 changes: 31 additions & 1 deletion types/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class Url extends Text
'autoscheme' => 'https',
'prefix' => '',
'postfix' => '',
'fixedtitle' => '',
'autoshorten' => true,
);

/**
Expand Down Expand Up @@ -43,10 +45,38 @@ public function validate($rawvalue)
public function renderValue($value, \Doku_Renderer $R, $mode)
{
$url = $this->buildURL($value);
$R->externallink($url);
$title = $this->generateTitle($url);
$R->externallink($url, $title);
return true;
}

/**
* Make a label for the link
*
* @param $url
* @return string
*/
protected function generateTitle($url) {
if($this->config['fixedtitle']) return $this->config['fixedtitle'];
if(!$this->config['autoshorten']) return $url;

$parsed = parse_url($url);

$title = $parsed['host'];
$title = preg_replace('/^www\./i', '', $title);
if(isset($parsed['path']) && $parsed['path'] === '/') {
unset($parsed['path']);
}
if(
isset($parsed['path']) ||
isset($parsed['query']) ||
isset($parsed['fragment'])
) {
$title .= '/…';
}
return $title;
}

/**
* Creates the full URL and applies the autoscheme if needed
*
Expand Down

0 comments on commit c7273e8

Please sign in to comment.