0

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
...
Ali Abdi
  • 408
  • 7
  • 21
  • I don't understand what you want to do - but this can be very complex task and I think there is NO special module to manipulate .md and you will have to write all code on your own. I would be simpler to keep all information from `.md` as dictionary and add new data to dictionary and later regenerate new `.md` – furas Aug 06 '22 at 21:51
  • @furas Thank you, yes I found that it would be nearly impossible to do without a database-like tool. I should store them as columns so they can be sorted, then re-write or add them to the file. Just wondering if it can be done with some if statements, like checking the regex and if it was False, place the line above the heading, or if it was True, place the line above the next heading! – Ali Abdi Aug 07 '22 at 14:40
  • well, if you sure you have newer date then you could use `regex` to find first ### (\d{4}/\d{2}/\d{2})` and replace it with `new information` + `### 2022/08/06`, and later write it back. But if you would have data with `2022/08/08` you would like to put it between `2022/08/06` and `2022/08/10` then it would need to compare regex with `2022/08/08` (even as string) and search first date which is bigger than `2022/08/08` and replace this line ``### 2022/08/10` with `new information` + `### 2022/08/10` – furas Aug 07 '22 at 15:28

1 Answers1

2

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

furas
  • 134,197
  • 12
  • 106
  • 148
  • Thank you so much! The way it works is interesting! I was looking for the actual insertion to the line, but it works perfectly fine... Can you please explain how the `replace` function works here? – Ali Abdi Aug 12 '22 at 16:57
  • 1
    `.replace()` is standard function for strings - it searchs `date` and put in `new_information + '#### ' + date` in its place. Because I put `+ date` so it puts back original date. Without `+ date` it would remove this date. – furas Aug 14 '22 at 17:48