Ali Abdi
Ali Abdi

Reputation: 427

Find headings & Manipulate (add content) to the right place in markdown file with Python

I have a markdown file and I want to insert new content to it with some conditions (like sorting).
For example, if the user gives the URL & it was an Video URL (is in pre-defined list of URLs), if the new video's upload date was older than 2022/08/06 (2022/08/04), it should create a new heading level 4 before the heading 2022/08/06 and insert the URL and body (No problem with getting details of video).
How can I do this type of manipulation in Python into the .md file?

# Web Bookmarks

## Videos

### www.youtube.com

<INSERT_NEW_URL_HERE>
#### 2022/08/04
...

#### 2022/08/06

##### URL: 9LPB8X4KB1g

...Body...

#### 2022/08/10

##### URL: wcuG2SsDbnM

...Body...

### www.vimeo.com
...

Upvotes: 0

Views: 1152

Answers (1)

furas
furas

Reputation: 142720

markdown file is text file so you can use string functions or regex (ie. '#### (\d{4}/\d{2}/\d{2})') to search #### date and compare it with new date, and if date > new_date then replace date with new_date + url + body + ### + date (and skip other dates).


Minimal working example:

I uses special construction for/else/break (not if/else) to put text at the end if it couldn't find date > new_date (if it didn't run break inside for-loop)

It may need some changes if you have more complex data. For example it doesn't check if it has to put in ### www.youtube.com or in ### www.vimeo.com. It would need first search part with www.youtube.com or www.vimeo.com

#with open("filename.md") as fh
#    text = fh.read()

text = '''# Web Bookmarks

## Videos

### www.youtube.com

#### 2022/08/06

##### URL: 9LPB8X4KB1g

...Body...

#### 2022/08/10

##### URL: wcuG2SsDbnM

...Body...

### www.vimeo.com

'''

import re

# new data 

#new_date = '2022/09/08'  # it should put at the end
new_date = '2022/08/08'   # it should put before `2022/08/10 (after `2022/08/06`)
#new_date = '2022/08/04'  # it should put before `2022/08/06 (at the beginning)
new_url  = '...New.Url...'
new_body = '...New.Body...'

# create text for replacement

new_information = f'''{new_date}

### URL: {new_url}

{new_body}

'''

# search dates

all_dates = re.findall('#### (\d{4}/\d{2}/\d{2})', text)
print(f'[!] all_dates: {all_dates}')

# check dates 

for date in all_dates:
    if date > new_date:
        print('[!] putting before:', date)
        text = text.replace(date, new_information + '#### ' + date)
        break  # don't check next dates
else:  # special construction `for/else/break` (not `if/else`)
    print('[!] putting at the end')
    if not text.endswith('\n'):
        text += '\n'
    text += '#### ' + new_information

# result

print(text)    

#with open("filename.md", "w") as fh
#    fh.write(text)

Result:

[!] all_dates: ['2022/08/06', '2022/08/10']
[!] putting before: 2022/08/10
# Web Bookmarks

## Videos

### www.youtube.com

#### 2022/08/06

##### URL: 9LPB8X4KB1g

...Body...

#### 2022/08/08

### URL: ...New.Url...

...New.Body...

#### 2022/08/10

##### URL: wcuG2SsDbnM

...Body...

### www.vimeo.com

Upvotes: 2

Related Questions