2021-11-13 21:57:51 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @brief improve, a plugin for Dotclear 2
|
|
|
|
*
|
|
|
|
* @package Dotclear
|
|
|
|
* @subpackage Plugin
|
|
|
|
*
|
|
|
|
* @author Jean-Christian Denis and contributors
|
|
|
|
*
|
|
|
|
* @copyright Jean-Christian Denis
|
|
|
|
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
|
|
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2022-12-21 14:41:27 +00:00
|
|
|
namespace Dotclear\Plugin\improve\Module;
|
2021-11-13 21:57:51 +00:00
|
|
|
|
|
|
|
/* improve */
|
2022-12-21 14:41:27 +00:00
|
|
|
use Dotclear\Plugin\improve\Action;
|
|
|
|
use Dotclear\Plugin\improve\Core;
|
2021-11-13 21:57:51 +00:00
|
|
|
|
|
|
|
/* clearbricks */
|
|
|
|
use form;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Improve action module new line
|
|
|
|
*/
|
2022-12-21 14:41:27 +00:00
|
|
|
class newline extends Action
|
2021-11-13 21:57:51 +00:00
|
|
|
{
|
|
|
|
protected function init(): bool
|
|
|
|
{
|
|
|
|
$this->setProperties([
|
|
|
|
'id' => 'newline',
|
|
|
|
'name' => __('Newlines'),
|
|
|
|
'description' => __('Replace bad and repetitive and empty newline by single newline in files'),
|
|
|
|
'priority' => 840,
|
|
|
|
'configurator' => true,
|
|
|
|
'types' => ['plugin', 'theme'],
|
|
|
|
]);
|
|
|
|
/*
|
2022-11-12 21:58:44 +00:00
|
|
|
$ext = @unserialize(dcCore::app()->blog->settings->improve->newline_extensions);
|
2021-11-13 21:57:51 +00:00
|
|
|
$ext = Improve::cleanExtensions($ext);
|
|
|
|
if (!empty($ext)) {
|
|
|
|
$this->extensions = $ext;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isConfigured(): bool
|
|
|
|
{
|
|
|
|
return !empty($this->getSetting('extensions'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function configure($url): ?string
|
|
|
|
{
|
|
|
|
if (!empty($_POST['save']) && !empty($_POST['newline_extensions'])) {
|
|
|
|
$this->setSettings(
|
|
|
|
'extensions',
|
2022-12-21 14:41:27 +00:00
|
|
|
Core::cleanExtensions($_POST['newline_extensions'])
|
2021-11-13 21:57:51 +00:00
|
|
|
);
|
|
|
|
$this->redirect($url);
|
|
|
|
}
|
|
|
|
|
|
|
|
$ext = $this->getSetting('extensions');
|
|
|
|
if (!is_array($ext)) {
|
|
|
|
$ext = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
'<p><label class="classic" for="newline_extensions">' .
|
|
|
|
__('List of files extension to work on:') . '<br />' .
|
|
|
|
form::field('newline_extensions', 65, 255, implode(',', $ext)) .
|
|
|
|
'</label></p><p class="form-note">' .
|
|
|
|
__('Use comma separated list of extensions without dot, recommand "php,js,xml,txt,md".') .
|
|
|
|
'</p>';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function readFile(string &$content): ?bool
|
|
|
|
{
|
|
|
|
$ext = $this->getSetting('extensions');
|
|
|
|
if (!is_array($ext) || !in_array($this->path_extension, $ext)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$clean = (string) preg_replace(
|
|
|
|
'/(\n\s+\n)/',
|
|
|
|
"\n\n",
|
|
|
|
(string) preg_replace(
|
|
|
|
'/(\n\n+)/',
|
|
|
|
"\n\n",
|
|
|
|
(string) str_replace(
|
|
|
|
["\r\n", "\r"],
|
|
|
|
"\n",
|
|
|
|
$content
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
if ($content != $clean) {
|
|
|
|
$this->setSuccess(__('Replace bad new lines'));
|
|
|
|
$content = $clean;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|