Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migration to native Focus in Kirby v4 - regex to use #75

Open
FynnZW opened this issue Sep 18, 2023 · 8 comments
Open

Migration to native Focus in Kirby v4 - regex to use #75

FynnZW opened this issue Sep 18, 2023 · 8 comments

Comments

@FynnZW
Copy link

FynnZW commented Sep 18, 2023

I don't know if this is the correct place, but I just tried to migrate existing focus points from this plugin to the new native syntax in Kirby 4.
So going from:
Focus: {"x":0.5, "y": 0.22}
to:
Focus: 50% 22%.

Converting from one decimal place to percentage (0.5 = 50%) made this not so easy.
Here's how I did it - might be useful for others.
Done with VSCode search & replace on the content folder (via regex, the .*-button)

  1. Transform X values to two decimal places
    Search:
    Focus: \{"x":(0.\d(?!\d)),"y":(0.\d+)\}
    Replace:
    Focus: {"x":$10,"y":$2}

  2. Transform Y values to two decimal places
    Search:
    Focus: \{"x":(0.\d+),"y":(0.\d(?!\d))\}
    Replace:
    Focus: {"x":$1,"y":$20}

  3. Turn to percentage syntax
    Search:
    Focus: \{"x":\d+\.(\d+?),"y":\d+\.(\d+?)\}
    Replace:
    Focus: $1% $2%

PS: Thanks a lot for this great plugin! While it won't be needed anymore soon, it made working with images in Kirby v3 so much better (I probably wouldn't have used Kirby v3 if this plugin hadn't existed).

@flokosiol
Copy link
Owner

Thank you so much for your kind words! @FynnZW

And also thank you for sharing your solution to migrate to Kirby v4. I'm sure it will help others.

@sciloqi
Copy link

sciloqi commented Nov 9, 2023

Thank you both for your incredible work! Worked absolutely smooth ❤️

@tobimori
Copy link

Maybe make a CLI command that migrates the content?

@bnomei
Copy link

bnomei commented Nov 30, 2023

something like this?

<?php

declare(strict_types=1);

use Kirby\CLI\CLI;
use Kirby\Filesystem\F;
use Kirby\Toolkit\A;
use Kirby\Toolkit\Str;

return [
    // Author: Bnomei
    // WARNING: this is a destructive command. Make a backup first!
    //
    // site/commands/focus-k4.php
    // php vendor/bin/kirby focus-k4 --dry-run
    //
    'description' => 'Migrate from Focus plugin to native K4',
    'args' => [
        'verbose' => [
            'shortPrefix' => 'v',
            'longPrefix' => 'verbose',
            'description' => 'Verbose output',
            'defaultValue' => false,
            'noValue' => true,
        ],
        'dryrun' => [
            'longPrefix' => 'dry-run',
            'description' => 'Dry run',
            'defaultValue' => false,
            'noValue' => true,
        ],
    ],
    'command' => static function (CLI $cli): void {
        // https://github.com/flokosiol/kirby-focus/issues/75
        $files = \Symfony\Component\Finder\Finder::create()
            ->in(kirby()->roots()->content())
            ->name('*.txt')
            ->files();
        $processed = 0;
        $replacements = 0;
        $isDryRun = $cli->arg('dryrun');
        $isVerbose = $cli->arg('verbose');

        foreach ($files as $file) {
            $path = $file->getPathname();
            $relpath = $file->getRelativePathname();
            $content = F::read($path);

            if (!Str::contains($content, 'Focus: ')) {
                if ($isVerbose) {
                    $cli->out("{$relpath}");
                }
                continue;
            }

            $content = preg_replace_callback('/^Focus: {(.*)}$/mi', function ($matches) use ($cli) {
                $json = json_decode('{' . $matches[1] . '}', true);
                if (!is_array($json)) {
                    return $matches[0]; // no change
                }
                $x = A::get($json, 'x', 0);
                $y = A::get($json, 'y', 0);
                $x = number_format($x * 100, 0);
                $y = number_format($y * 100, 0);

                return "Focus: {$x}% {$y}%";
            }, $content, -1, $count);

            if (!$isDryRun) {
                F::write($path, $content);
            }
            if ($count === 0 && $isVerbose) {
                $cli->out("⏩ [0] {$relpath}");
            }
            if ($count > 0) {
                $cli->out("✅ [{$count}] {$relpath}");
            }
            $processed++;
            $replacements += $count;
        }

        $cli->success("files: {$processed}, replacements: {$replacements}");
    }
];

@FynnZW
Copy link
Author

FynnZW commented Dec 15, 2023

I just tried it out and it worked perfectly!
Maybe this can be included as an cli command for this plugin?

@flokosiol
Copy link
Owner

I startet to integrate the command into the plugin structure, but I faced one problem. I got an error, because \Symfony\Component\Finder\Finder could not be found. After adding it to the composer.json file of the plugin everything went smooth, but not everyone uses Composer. What will happen to those folks? Anyone else running into this issue? Should be Composer setup mandatory if you want to use the command? What do you think?

And of course … THANK YOU @bnomei for taking the time to write the command and for sharing it! Very much appreciated!

And secondly, sorry for the late response 🙂

@tobimori
Copy link

In general, Kirby plugins upload the vendor folder to git for people that don't use composer.

@medienbaecker
Copy link
Contributor

Because I wanted to use it as a global command I replaced the Symfony stuff with a native RecursiveIteratorIterator:

<?php

declare(strict_types=1);

use Kirby\CLI\CLI;
use Kirby\Toolkit\A;
use Kirby\Toolkit\Str;

return [
    // Author: Bnomei
    // WARNING: this is a destructive command. Make a backup first!
    //
    // site/commands/focus-k4.php
    // php vendor/bin/kirby focus-k4 --dry-run
    //
    'description' => 'Migrate from Focus plugin to native K4',
    'args' => [
        'verbose' => [
            'shortPrefix' => 'v',
            'longPrefix' => 'verbose',
            'description' => 'Verbose output',
            'defaultValue' => false,
            'noValue' => true,
        ],
        'dryrun' => [
            'longPrefix' => 'dry-run',
            'description' => 'Dry run',
            'defaultValue' => false,
            'noValue' => true,
        ],
    ],
    'command' => static function (CLI $cli): void {
        // https://github.com/flokosiol/kirby-focus/issues/75
        $directory = kirby()->roots()->content();
        $processed = 0;
        $replacements = 0;
        $isDryRun = $cli->arg('dryrun');
        $isVerbose = $cli->arg('verbose');

        $filenames = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory), RecursiveIteratorIterator::SELF_FIRST);

        foreach ($filenames as $filename) {
            $filepath = $filename->getPathname();
            $relpath = str_replace($directory . '/', '', $filepath);
            if ($filename->isDir() || pathinfo($filepath, PATHINFO_EXTENSION) !== 'txt') {
                continue;
            }
            $content = file_get_contents($filepath);

            if (!Str::contains($content, 'Focus: ')) {
                if ($isVerbose) {
                    $cli->out("{$relpath}");
                }
                continue;
            }

            $content = preg_replace_callback('/^Focus: {(.*)}$/mi', function ($matches) use ($cli) {
                $json = json_decode('{' . $matches[1] . '}', true);
                if (!is_array($json)) {
                    return $matches[0]; // no change
                }
                $x = A::get($json, 'x', 0);
                $y = A::get($json, 'y', 0);
                $x = number_format($x * 100, 0);
                $y = number_format($y * 100, 0);

                return "Focus: {$x}% {$y}%";
            }, $content, -1, $count);

            if (!$isDryRun) {
                file_put_contents($filepath, $content);
            }
            if ($count === 0 && $isVerbose) {
                $cli->out("⏩ [0] {$relpath}");
            }
            if ($count > 0) {
                $cli->out("✅ [{$count}] {$relpath}");
            }
            $processed++;
            $replacements += $count;
        }

        $cli->success("files processed: {$processed}, replacements: {$replacements}");
    }
];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants