summaryrefslogtreecommitdiff
path: root/scripts/generate-readme-neovim-keymaps.php
blob: 4dbe1e12bcf0ed805800ea962d6d1b2179ef8adf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php

declare(strict_types=1);

$source = __DIR__ . '/../nvim/.config/nvim/lua/config/keymaps.lua';
$target = __DIR__ . '/../README.md';

if (!file_exists($source)) {
    echo "Source keymaps.lua not found.\n";
    exit(1);
}

if (!file_exists($target)) {
    echo "Target README.md not found.\n";
    exit(1);
}

preg_match_all(
    '/set\((".*?"|{.*?}), "(.*?)".*?desc = "(.*?)"/s',
    file_get_contents($source),
    $matches,
    PREG_SET_ORDER,
);

$table[] = '| Key | Description | Mode |';
$table[] = '| :--- | :--- | :--- |';
$table = array_reduce(
    $matches,
    function (array $carry, array $match): array {
        $key = str_replace(['<', '>'], ["\<", "\>"], $match[2]);
        $description = $match[3];
        $mode = str_replace(['{ ', ' }', '"'], '', $match[1]);

        $carry[] = "| $key | $description | $mode |";

        return $carry;
    },
    $table
);

$readme = file_get_contents($target);
$pattern = '/(<!-- BEGIN-NEOVIM-KEYMAPS -->).*(<!-- END-NEOVIM-KEYMAPS -->)/s';
$replacement = "$1\n" . implode("\n", $table) . "\n$2";
file_put_contents($target, preg_replace($pattern, $replacement, $readme));