adding language to markdown codeblock in bulk

2024/9/20 15:18:25

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.

Answer

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.

https://en.xdnf.cn/q/119484.html

Related Q&A

Cant randomize list with classes inside of it Python 2.7.4

I am new to coding and I need some help. Im trying to randomize these rooms or scenes in a text adventure but whenever I try to randomize it they dont even show up when I run it! Here is the script:fro…

calculate the queue for orders based on creation and delivery date, by product group

I have a Pandas dataframe containing records for a lot of orders, one recorde for each order. Each record has order_id, category_id, created_at and picked_at. I need to calculate queue length for each …

Python print with string invalid syntax

I have a rock, paper, scissors code Ive been working on lately (yes, I am a total noob at coding), and I get an Invalid Syntax error with this specific line:print(The magical 8ball reads "Your for…

How to load images and text labels for CNN regression from different folders

I have two folders, X_train and Y_train. X_train is images, Y_train is vector and .txt files. I try to train CNN for regression. I could not figure out how to take data and train the network. When i us…

How to calculate number of dates within a year of a date in pandas

I have the following dataframe and I need to calculate the amount of ER visit Dates with a score of 1 that are one year after the PheneDate for that pheneDate for a given subject. So basically phenevi…

Remove substring from string if substring in list in data frame column

I have the following data frame df1string lists 0 i have a dog [fox, dog, cat] 1 there is a cat [dog, house, car] 2 hello everyone [hi, hello, everyone] 3 …

how to save data in the db django model?

Good day, I cant really understand what Im doing wrong in here. I was using this function base view to store my scrap data in the database with the django model, but now its not saving any more. I cant…

Move existing jointplot legend

I tried answers from a previous question to no avail in Matplotlib 1.5.1. I have a seaborn figure:import seaborn as sns %matplotlib inline import matplotlib.pyplot as plt import numpy as np tips = sns.…

timezone conversion of a large list of timestamps from an excel file with python

I have an excel file named "hello.xlsx". There is a column of timestamps that has a lot of rows (more than 80,000 rows for now). The file basically looks like this:03/29/2018 19:24:5003/29/20…

N_gram frequency python NTLK

I want to write a function that returns the frequency of each element in the n-gram of a given text. Help please. I did this code fo counting frequency of 2-gramcode:from nltk import FreqDistfrom nltk.…