[emacs-berlin] A riddle to be solved with Emacs
Jack Rusher
jack at rusher.com
Mon Jun 7 15:32:32 UTC 2021
Something like this should do it (assuming you’ve got just the lines in the file):
M-x replace-regex
^(.*),
{ value: \,(downcase \1), label: \1 }
… the important points:
1) set a capture group that excludes the comma for each string in the match
2) use an elisp function in the replacement :)
There’s a video in this tweet showing an example of cleaning up some XML with this technique:
https://twitter.com/jackrusher/status/1306591797506797570
MfG,
J.
On 7 Jun, 2021, at 13:23, jman <emacs-berlin at city17.xyz> wrote:
> ---snip-snip-snip-snip-snip---
> [
> "ONLY CAPITAL CASE",
> "ALSO THIS ONE",
> "HANDS ARE TYPING",
> "NO WEIRD CHARS, ONLY LETTERS, NUMBERS AND PUNCTUATION",
> ...
> ]
> ---snip-snip-snip-snip-snip---
>
> You want to transform it into a JSON where each label goes into a `label`
> field, plus a `value` field with a "slug" of the label. A "slug" is a
> conversion of some text replacing puntuation and whitespaces with
> hyphens, example follows:
>
> ---snip-snip-snip-snip-snip---
> [
> { value: "only-capital-case", label: "ONLY CAPITAL CASE"},
> { value: "also-this-one", label: "ALSO THIS ONE"},
> { value: "hands-are-typing", label: "HANDS ARE TYPING"},
> { value: "only-letters-numbers-and-punctuation", label: "ONLY LETTERS, NUMBERS AND PUNCTUATION"},
> ...
> ]
> ---snip-snip-snip-snip-snip---
>
> I've tried either using the `multiple-cursors` package, I've tried
> learning my first macro, I've tried with `M-x query-replace-regexp`. All
> my attempts have failed. I could not learn how to use these by reading
> the documentation.
>
> My solution: ended up with 10 lines of Python :-/
>
> So, who's up for some Emacs-fu? :-) Thanks!
>
> p.s. I had to set Emacs in `so-long` mode because it struggled with a
> 1000 lines file (I'm on Emacs native-comp).
>
> Best,
> _______________________________________________
> emacs-berlin mailing list
> emacs-berlin at emacs-berlin.org
> https://mailb.org/mailman/listinfo/emacs-berlin
More information about the emacs-berlin
mailing list