From 4cdb7f6fd189aca721742af36e256ceb7f2aacdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Goetz?= Date: Fri, 30 Sep 2016 22:24:33 +0200 Subject: [PATCH] Add a way to support any platform for the "Edit On" feature, fixes #413 --- docs/01_Features/Edit_on_GitHub_links.md | 18 ++++++++ libs/BaseConfig.php | 39 ++++++++++++++++++ libs/Config.php | 52 ++++-------------------- libs/Format/HTML/Config.php | 36 ++++++++++++++++ templates/content.php | 6 ++- tests/Format/HTML/ConfigTest.php | 48 ++++++++++++++++++++++ 6 files changed, 152 insertions(+), 47 deletions(-) create mode 100644 libs/BaseConfig.php create mode 100644 libs/Format/HTML/Config.php create mode 100644 tests/Format/HTML/ConfigTest.php diff --git a/docs/01_Features/Edit_on_GitHub_links.md b/docs/01_Features/Edit_on_GitHub_links.md index cab0b21..2fa7491 100644 --- a/docs/01_Features/Edit_on_GitHub_links.md +++ b/docs/01_Features/Edit_on_GitHub_links.md @@ -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" + } + } +} +``` diff --git a/libs/BaseConfig.php b/libs/BaseConfig.php new file mode 100644 index 0000000..7a867dc --- /dev/null +++ b/libs/BaseConfig.php @@ -0,0 +1,39 @@ + $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; + } + } + } +} diff --git a/libs/Config.php b/libs/Config.php index 510bb32..6e2b53d 100644 --- a/libs/Config.php +++ b/libs/Config.php @@ -1,53 +1,10 @@ $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']); + } } diff --git a/libs/Format/HTML/Config.php b/libs/Format/HTML/Config.php new file mode 100644 index 0000000..7d139c3 --- /dev/null +++ b/libs/Format/HTML/Config.php @@ -0,0 +1,36 @@ + '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; + } +} diff --git a/templates/content.php b/templates/content.php index b1af9ae..2b2f22e 100644 --- a/templates/content.php +++ b/templates/content.php @@ -8,9 +8,11 @@ - + getHTML()->getEditOn(); + if ($edit_on) { ?> - Edit on GitHub + Edit on diff --git a/tests/Format/HTML/ConfigTest.php b/tests/Format/HTML/ConfigTest.php new file mode 100644 index 0000000..f5f4a50 --- /dev/null +++ b/tests/Format/HTML/ConfigTest.php @@ -0,0 +1,48 @@ + ['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()); + } +}