Add a way to support any platform for the "Edit On" feature, fixes #413

This commit is contained in:
Stéphane Goetz 2016-09-30 22:24:33 +02:00
bovenliggende 5af481f1ab
commit 4cdb7f6fd1
6 gewijzigde bestanden met toevoegingen van 152 en 47 verwijderingen

Bestand weergeven

@ -15,3 +15,21 @@ Daux.io will handle the rest
}
}
```
## Edit on other VCS
While GitHub is the most popular, it isn't the only, collaborative VCS out there.
As long as you can refer your files by a URL, you can create an edit link for your VCS with the following configuration:
```json
{
"html": {
"edit_on": {
"name": "Bitbucket",
"basepath": "https://bitbucket.org/onigoetz/daux.io/src/master/docs"
}
}
}
```

39
libs/BaseConfig.php Normal file
Bestand weergeven

@ -0,0 +1,39 @@
<?php namespace Todaymade\Daux;
use ArrayObject;
class BaseConfig extends ArrayObject
{
/**
* Merge an array into the object
*
* @param array $newValues
* @param bool $override
*/
public function merge($newValues, $override = true)
{
foreach ($newValues as $key => $value) {
// If the key doesn't exist yet,
// we can simply set it.
if (!array_key_exists($key, $this)) {
$this[$key] = $value;
continue;
}
// We already know this value exists
// so if we're in conservative mode
// we can skip this key
if ($override === false) {
continue;
}
// Merge the values only if
// both values are arrays
if (is_array($this[$key]) && is_array($value)) {
$this[$key] = array_replace_recursive($this[$key], $value);
} else {
$this[$key] = $value;
}
}
}
}

Bestand weergeven

@ -1,53 +1,10 @@
<?php namespace Todaymade\Daux;
use ArrayObject;
use Todaymade\Daux\Tree\Content;
use Todaymade\Daux\Format\HTML\Config as HTMLConfig;
class Config extends ArrayObject
class Config extends BaseConfig
{
/**
* Merge an array into the object
*
* @param array $newValues
* @param bool $override
*/
public function merge($newValues, $override = true)
{
foreach ($newValues as $key => $value) {
// If the key doesn't exist yet,
// we can simply set it.
if (!array_key_exists($key, $this)) {
$this[$key] = $value;
continue;
}
// We already know this value exists
// so if we're in conservative mode
// we can skip this key
if ($override === false) {
continue;
}
// Merge the values only if
// both values are arrays
if (is_array($this[$key]) && is_array($value)) {
$this[$key] = array_replace_recursive($this[$key], $value);
} else {
$this[$key] = $value;
}
}
}
/**
* Merge an array into the object, ignore already added keys.
*
* @param $newValues
*/
public function conservativeMerge($newValues)
{
$this->merge($newValues, false);
}
public function getCurrentPage()
{
return $this['current_page'];
@ -153,4 +110,9 @@ class Config extends ArrayObject
{
return $this['confluence'];
}
public function getHTML()
{
return new HTMLConfig($this['html']);
}
}

Bestand weergeven

@ -0,0 +1,36 @@
<?php namespace Todaymade\Daux\Format\HTML;
use Todaymade\Daux\BaseConfig;
class Config extends BaseConfig
{
private function prepareGithubUrl($url)
{
$url = str_replace('http://', 'https://', $url);
return [
'name' => 'GitHub',
'basepath' => (strpos($url, 'https://github.com/') === 0 ? '' : 'https://github.com/') . trim($url, '/')
];
}
function getEditOn()
{
if (array_key_exists('edit_on', $this)) {
if (is_string($this['edit_on'])) {
return $this->prepareGithubUrl($this['edit_on']);
} else {
$this['edit_on']['basepath'] = rtrim($this['edit_on']['basepath'], '/');
return $this['edit_on'];
}
}
if (array_key_exists('edit_on_github', $this)) {
return $this->prepareGithubUrl($this['edit_on_github']);
}
return null;
}
}

Bestand weergeven

@ -8,9 +8,11 @@
<?= date("l, F j, Y g:i A", $page['modified_time']); ?>
</span>
<?php } ?>
<?php if (array_key_exists('edit_on_github', $params['html']) && $params['html']['edit_on_github']) { ?>
<?php
$edit_on = $params->getHTML()->getEditOn();
if ($edit_on) { ?>
<span style="float: right; font-size: 10px; color: gray;">
<a href="https://github.com/<?= $params['html']['edit_on_github'] ?>/<?= $page['relative_path'] ?>" target="_blank">Edit on GitHub</a>
<a href="<?= $edit_on['basepath'] ?>/<?= $page['relative_path'] ?>" target="_blank">Edit on <?= $edit_on['name'] ?></a>
</span>
<?php } ?>
</div>

Bestand weergeven

@ -0,0 +1,48 @@
<?php namespace Todaymade\Daux\Format\HTML;
use Todaymade\Daux\Config as MainConfig;
class ConfigTest extends \PHPUnit_Framework_TestCase
{
function testHTMLConfigCreation() {
$config = new MainConfig(['html' => ['edit_on' => 'test']]);
$this->assertInstanceOf(Config::class, $config->getHTML());
$this->assertEquals('test', $config->getHTML()['edit_on']);
}
public function providerEditOn()
{
$github_result = ['name' => 'GitHub', 'basepath' => 'https://github.com/justinwalsh/daux.io/blob/master/docs'];
return [
[[], null],
[['edit_on_github' => 'justinwalsh/daux.io/blob/master/docs'], $github_result],
// Allow formatting in many ways
[['edit_on_github' => 'justinwalsh/daux.io/blob/master/docs/'], $github_result],
[['edit_on_github' => '/justinwalsh/daux.io/blob/master/docs'], $github_result],
[['edit_on_github' => 'https://github.com/justinwalsh/daux.io/blob/master/docs/'], $github_result],
[['edit_on_github' => 'http://github.com/justinwalsh/daux.io/blob/master/docs/'], $github_result],
// Fallback if a string is provided to 'edit_on'
[['edit_on' => 'justinwalsh/daux.io/blob/master/docs'], $github_result],
// Support any provider
[
['edit_on' => ['name' => 'Bitbucket', 'basepath' => 'https://bitbucket.org/onigoetz/daux.io/src/master/docs/']],
['name' => 'Bitbucket', 'basepath' => 'https://bitbucket.org/onigoetz/daux.io/src/master/docs/']
]
];
}
/**
* @dataProvider providerEditOn
*/
public function testEditOn($value, $expected)
{
$config = new Config($value);
$this->assertEquals($expected, $config->getEditOn());
}
}