adding language to markdown codeblock in bulk

Question:

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.

Asked By: Raikoug

||

Answers:

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
1ios23n```

I really couldn’t transform this for sed

import re
import sys, getopt
from shutil import move

def 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 = arg
      elif opt in ("-a", "--afile"):
         outputfile = inputfile + arg

   magic(inputfile, outputfile)

def magic(inputfile, outputfile):
    regex = r"(```).*(n.*)((n.*)*?)n```"
    subst = r"1ios23n```"
    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.

Answered By: Raikoug
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.