[emacs-berlin] A riddle to be solved with Emacs

jman emacs-berlin at city17.xyz
Mon Jun 7 11:23:09 UTC 2021


Hello everyone,

the other day I've failed to solve a small task consisting of text
search and replace in a file. I'm asking for an opinion on how could I
have solved this using Emacs. This could have been an interesting
question during the meetup :-)

Task: you have a text file of about 1000 lines in this format:

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


More information about the emacs-berlin mailing list