My Problem is to add to every single block of code a language in my markdown files.
I've hundreds of files in nested directories.
The files have this form:
```language
a
```Normal text```
b
```Normal text```
c
```Normal text```language
d
```
and the output for each of these shoud be:
```ios
a
```Normal text```ios
b
```Normal text```ios
c
```Normal text```ios
d
```
(In this case I needed ios lang from a custom lexer I made)
I'm using debian 11 and trying with sed
and I found that this regex
(```).*(\n.*)((\n.*)*?)\n```
could help find the blocks but can't find how to use it.
I can use python for more complex regex and behaviour.
My Solution
WARNING!! If you have impared triple-backtick, this code will have unwanted results! always backup your files before!
- bash
find
all files with absolute path (for some reason I don't like relative paths, and my laziness told me not to write a recursive python search :D)
-exec
python script with 2 arguments (filename and a second parameter to append a string to original file and keep it, having new one with original filename)
The regex inside the python script I came up with to "add" (I actually replace the whole..) the "ios" text for code block is:
(```).*(\n.*)((\n.*)*?)\n```
replace with
\1ios\2\3\n```
I really couldn't transform this for sed
import re
import sys, getopt
from shutil import movedef main(argv):inputfile = ''outputfile = ''try:opts, args = getopt.getopt(argv,"hi:a:",["ifile=","afile="])except getopt.GetoptError:print ('pyre.py -i <inputfile> -a <append_string>')sys.exit(2)for opt, arg in opts:if opt == '-h':print ('pyre.py -i <inputfile> -a <append_string>')sys.exit()elif opt in ("-i", "--ifile"):inputfile = argelif opt in ("-a", "--afile"):outputfile = inputfile + argmagic(inputfile, outputfile)def magic(inputfile, outputfile):regex = r"(```).*(\n.*)((\n.*)*?)\n```"subst = r"\1ios\2\3\n```"move(inputfile, outputfile)open(inputfile, 'w', encoding="utf-8").write(re.sub(regex, subst, open(outputfile, 'r', encoding="utf-8").read(), 0, re.MULTILINE))#print(f"{inputfile} DONE")if __name__ == "__main__":main(sys.argv[1:])
and the actully find
find ~+ -name '*.md' -exec python pyre.py -i \{\} -a .new.md \;
Hope this will help someone with my same issue.