From ede2ec8039ee9845d9efc91c7064c57721c8c3ab Mon Sep 17 00:00:00 2001 From: Jean-Christian Denis Date: Sat, 11 Sep 2021 17:33:24 +0200 Subject: [PATCH] add Github README shields.io badges tool. --- _admin.php | 1 + _prepend.php | 1 + inc/lib.improve.action.gitshields.php | 143 ++++++++++++++++++++++++++ 3 files changed, 145 insertions(+) create mode 100644 inc/lib.improve.action.gitshields.php diff --git a/_admin.php b/_admin.php index 8c2929e..e0aae8f 100644 --- a/_admin.php +++ b/_admin.php @@ -20,6 +20,7 @@ $core->addBehavior('improveAddAction', ['ImproveActionEndoffile', 'create']); //$core->addBehavior('improveAddAction', ['ImproveActionLicense', 'create']); $core->addBehavior('improveAddAction', ['ImproveActionNewline', 'create']); $core->addBehavior('improveAddAction', ['ImproveActionPhpheader', 'create']); +$core->addBehavior('improveAddAction', ['ImproveActionGitshields', 'create']); $core->addBehavior('improveAddAction', ['ImproveActionTab', 'create']); $core->addBehavior('improveAddAction', ['ImproveActionZip', 'create']); diff --git a/_prepend.php b/_prepend.php index a7bbe5b..be80fe9 100644 --- a/_prepend.php +++ b/_prepend.php @@ -24,6 +24,7 @@ $__autoload['ImproveActionEndoffile'] = $d . 'lib.improve.action.php'; //$__autoload['ImproveActionLicense'] = $d . 'lib.improve.action.license.php'; $__autoload['ImproveActionNewline'] = $d . 'lib.improve.action.php'; $__autoload['ImproveActionPhpheader'] = $d . 'lib.improve.action.phpheader.php'; +$__autoload['ImproveActionGitshields'] = $d . 'lib.improve.action.gitshields.php'; $__autoload['ImproveActionTab'] = $d . 'lib.improve.action.php'; $__autoload['ImproveActionZip'] = $d . 'lib.improve.action.zip.php'; $__autoload['ImproveZipFileZip'] = $d . 'lib.improve.action.zip.php'; \ No newline at end of file diff --git a/inc/lib.improve.action.gitshields.php b/inc/lib.improve.action.gitshields.php new file mode 100644 index 0000000..7651f6c --- /dev/null +++ b/inc/lib.improve.action.gitshields.php @@ -0,0 +1,143 @@ + '/\[!\[Release(.*)LICENSE\)/ms', + 'target' => '/^([^\n]+)[\r\n|\n]{1,}/ms' + ]; + protected $bloc_content = [ + '[![Release](https://img.shields.io/github/v/release/%username%/%module%)](https://github.com/%username%/%module%/releases)', + '[![Date](https://img.shields.io/github/release-date/%username%/%module%)](https://github.com/%username%/%module%/releases)', + '[![Issue](https://img.shields.io/github/issues/%username%/%module%)](https://github.com/%username%/%module%/issues)', + '[![Dotclear](https://img.shields.io/badge/dotclear-v%dotclear%-blue.svg)](https://fr.dotclear.org/download)', + '[![License](https://img.shields.io/github/license/%username%/%module%)](https://github.com/%username%/%module%/blob/master/LICENSE)' + ]; + + protected function init(): bool + { + $this->setProperties([ + 'id' => 'gitshields', + 'name' => __('Add shields badges'), + 'desc' => __('Add and maintain shields.io badges to the REDAME.md file'), + 'priority' => 380, + 'config' => true, + 'types' => ['plugin', 'theme'] + ]); + + return true; + } + + public function isConfigured(): bool + { + return !empty($this->getPreference('username')); + } + + public function configure($url): ?string + { + if (!empty($_POST['save']) && !empty($_POST['username'])) { + $this->setPreferences('username', (string) $_POST['username']); + $this->redirect($url); + } + + return ' +

' . + form::field('username', 60, 100, $this->getPreference('username')) . ' +

' . __('Used in your Github URL: http://github.com/username/module_id.') . '

'; + } + + public function openModule(string $module_type, array $module_info): ?bool + { + $this->type = $module_type; + $this->module = $module_info; + $this->replaceInfo(); + + return null; + } + + public function readFile($path, $extension, &$content): ?bool + { + if ($this->stop_scan || !preg_match('/(.*?)README\.md$/i', $path)) { + return null; + } + + $clean = $this->deleteShieldsBloc($content); + $content = $this->writeShieldsBloc($clean); + $this->stop_scan = true; + + return true; + } + + private function replaceInfo() + { + $bloc = $this->bloc_content; + + if (empty($bloc)) { + self::notice(__('bloc is empty'), false); + + return null; + } + + $username = $this->getPreference('username'); + $module = $this->module['id']; + $dotclear = $this->getDotclearVersion(); + + foreach($bloc as $k => $v) { + $bloc[$k] = trim(str_replace( + ['%username%', '%module%', '%dotclear%', "\r\n", "\n"], + [$username, $module, $dotclear, '', ''], + $v + )); + } + $this->bloc = $bloc; + } + + private function getDotclearVersion() + { + $version = null; + $module = $this->module; + if (!empty($module['requires']) && is_array($module['requires'])) { + foreach ($module['requires'] as $req) { + if (!is_array($req)) { + $req = [$req]; + } + if ($req[0] == 'core') { + $version = $req[1]; + break; + } + } + } + return $version ?: $this->core->getVersion('core'); + } + + private function writeShieldsBloc($content) + { + return preg_replace( + $this->bloc_pattern['target'], + '$1' . "\n\n" . trim(implode("\n", $this->bloc)) . "\n\n", + $content, + 1 + ); + } + + private function deleteShieldsBloc($content) + { + return preg_replace( + $this->bloc_pattern['remove'], + "\n\n", + $content + ); + } +} \ No newline at end of file