#!/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()
    ->in(__DIR__ . '/../docs/zh-cn')
    ->name('*.md')
    ->files();

foreach ($files as $file) {
    file_put_contents($file, replace(file_get_contents($file)));
}
echo count($files).' markdown files formatted!'.PHP_EOL;

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',
        ],

        'bracket_cjk' => [
            '([' . $cjk . '])([`]+\w(.*?)\w[`]+)([' . $cjk . '，。])',
            '$1 $2 $4',
        ],

        'ans_operator_cjk' => [
            '([\+\-\*\/=&\\|<>])([A-Za-zΑ-Ωα-ω0-9])([' . $cjk . '])',
            '$1 $2 $3',
        ],

        'cjk_ans' => [
            '([' . $cjk . '])([A-Za-zΑ-Ωα-ω0-9@&%\=\$\^\\-\+\\\><])',
            '$1 $2',
        ],

        'ans_cjk' => [
            '([A-Za-zΑ-Ωα-ω0-9~!%&=;\,\.\?\$\^\\-\+\\\<>])([' . $cjk . '])',
            '$1 $2',
        ],
    ];
    $code = [];
    $i = 0;
    $text = preg_replace_callback('/```(\n|.)*?\n```/m', function ($match) use (&$code, &$i) {
        $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;
}