hyperf/bin/md-format

91 lines
2.3 KiB
Plaintext
Raw Normal View History

2019-11-12 01:22:35 +08:00
#!/usr/bin/env php
<?php
use Symfony\Component\Finder\Finder;
foreach ([__DIR__ . '/../../autoload.php', __DIR__ . '/../vendor/autoload.php', __DIR__ . '/vendor/autoload.php'] as $file) {
if (file_exists($file)) {
require $file;
break;
}
}
$files = Finder::create()
2020-06-10 00:20:28 +08:00
->in(__DIR__ . '/../docs/zh-cn')
2019-11-12 01:22:35 +08:00
->name('*.md')
->files();
foreach ($files as $file) {
file_put_contents($file, replace(file_get_contents($file)));
}
2019-11-12 01:25:32 +08:00
echo count($files).' markdown files formatted!'.PHP_EOL;
2019-11-12 01:22:35 +08:00
function replace($text)
{
$cjk = '' .
'\x{2e80}-\x{2eff}' .
'\x{2f00}-\x{2fdf}' .
'\x{3040}-\x{309f}' .
'\x{30a0}-\x{30ff}' .
'\x{3100}-\x{312f}' .
'\x{3200}-\x{32ff}' .
'\x{3400}-\x{4dbf}' .
'\x{4e00}-\x{9fff}' .
'\x{f900}-\x{faff}';
$patterns = [
'cjk_quote' => [
'([' . $cjk . '])(["\'])',
'$1 $2',
],
'quote_cjk' => [
'(["\'])([' . $cjk . '])',
'$1 $2',
],
'fix_quote' => [
'(["\']+)(\s*)(.+?)(\s*)(["\']+)',
'$1$3$5',
],
'cjk_operator_ans' => [
'([' . $cjk . '])([A-Za-zΑ-Ωα-ω0-9])([\+\-\*\/=&\\|<>])',
'$1 $2 $3',
],
2019-11-12 19:46:54 +08:00
'bracket_cjk' => [
'([' . $cjk . '])([`]+\w(.*?)\w[`]+)([' . $cjk . ',。])',
'$1 $2 $4',
],
2019-11-12 01:25:32 +08:00
2019-11-12 01:22:35 +08:00
'ans_operator_cjk' => [
'([\+\-\*\/=&\\|<>])([A-Za-zΑ-Ωα-ω0-9])([' . $cjk . '])',
'$1 $2 $3',
],
'cjk_ans' => [
'([' . $cjk . '])([A-Za-zΑ-Ωα-ω0-9@&%\=\$\^\\-\+\\\><])',
2019-11-12 01:22:35 +08:00
'$1 $2',
],
'ans_cjk' => [
'([A-Za-zΑ-Ωα-ω0-9~!%&=;\,\.\?\$\^\\-\+\\\<>])([' . $cjk . '])',
2019-11-12 01:22:35 +08:00
'$1 $2',
],
];
$code = [];
$i = 0;
$text = preg_replace_callback('/```(\n|.)*?\n```/m', function ($match) use (&$code, &$i) {
2019-11-12 01:22:35 +08:00
$code[++$i] = $match[0];
return "__REPLACEMARK__{$i}__";
}, $text);
foreach ($patterns as $key => $value) {
$text = preg_replace('/' . $value[0] . '/iu', $value[1], $text);
}
$text = preg_replace_callback('/__REPLACEMARK__(\d+)__/s', function ($match) use ($code) {
return $code[$match[1]];
}, $text);
return $text;
2019-11-12 19:46:54 +08:00
}