Reverse characters within selected paragraphs

There are many online tools available (like Online Text Tools with the "line-by-line mode" checked), but I'm looking for a KM solution...

I want to reverse the text in every paragraph while keeping all of the paragraphs in the original order.

Example text:

123 456.
789 ABC.

Desired output:

.654 321
.CBA 987

p.s. This need to work for all text -- even non-ASCII characters.

:ekil gnihtemos spahreP

Reversal of selected lines.kmmacros (19.6 KB)

JS Source
(() => {
    'use strict';

    // main :: IO ()
    const main = () =>
        unlines(lines(
            Application('Keyboard Maestro Engine')
            .getvariable('paras')
        ).map(reverse));


    // GENERIC FUNCTIONS ----------------------------
    // https://github.com/RobTrew/prelude-jxa

    // lines :: String -> [String]
    const lines = s =>
        // A list of strings derived from a single
        // newline-delimited string.
        0 < s.length ? (
            s.split(/[\r\n]/)
        ) : [];

    // reverse :: [a] -> [a]
    const reverse = xs =>
        'string' !== typeof xs ? (
            xs.slice(0).reverse()
        ) : xs.split('').reverse().join('');

    // unlines :: [String] -> String
    const unlines = xs =>
        // A single string formed by the intercalation
        // of a list of strings with the newline character.
        xs.join('\n');

    // MAIN ---
    return main();
})();
3 Likes

I have no idea how it works, but it works. Thanks!

Most of nature is like that, I find ¯\_(ツ)_/¯