Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion N2O.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ def debug_print(msg):
regexForbitCharacter = compile("[<>?:/\|*\"]")

for line in NotionPathRaw:
ObsidianPathRaw.append(regexUID.sub("", line))
pathLine = regexUID.sub("", line)
pathLine = N2Omodule.remove_all_characters_except_alpha_numeric(pathLine)
pathLine = N2Omodule.str_space_utf8_replace(pathLine)
ObsidianPathRaw.append(pathLine)


### PATHS IN PROPER OS FORM BY PATHLIB ###
Expand Down Expand Up @@ -120,6 +123,8 @@ def debug_print(msg):
## Make temp destination file path
new_file_name = mdContent[0].replace('# ', '') + '.md'
new_file_name = regexForbitCharacter.sub("", new_file_name)
new_file_name = N2Omodule.str_space_utf8_replace(new_file_name)
new_file_name = N2Omodule.remove_all_characters_except_alpha_numeric(new_file_name)
newfilepath = tempPath / path.dirname(ObsidianPaths[n]) / new_file_name

# Check if file exists, append if true
Expand Down
33 changes: 26 additions & 7 deletions N2Omodule.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,20 @@
from re import compile, search
from csv import DictReader
from pathlib import Path

import re

def remove_all_characters_except_alpha_numeric(string):
# Only keep:
# Letters a-z, A-Z
# Numbers (0-9)
# Dots (\.)
# Back Slashes (\/)
# Forward Slashes (\\)
# Non white space (\s)
# ^ means other than the provided patterns
# need to be substituted by an empty string
string = re.sub('[ ](?=[ ])|[^-_,A-Za-z0-9 \.\/\\\]+', '', string)
return string

def str_slash_char_remove(string):

Expand All @@ -30,11 +43,12 @@ def str_forbid_char_remove(string):
return string


# convert %20 to ' '
# convert %20 or ' ' to '-'
def str_space_utf8_replace(string):

regex_utf8_space = compile("%20")
string = regex_utf8_space.sub(' ', string)
string = regex_utf8_space.sub('-', string)
string = string.replace(' ', '-')

return string

Expand Down Expand Up @@ -184,7 +198,7 @@ def embedded_link_convert(line):

# folder style links
#regexPath = compile("^\[(.+)\]\(([^\(]*)(?:\.md|\.csv)\)$") # Overlap incase multiple links in same line
#regexRelativePathImage = compile("(?:\.png|\.jpg|\.gif|\.bmp|\.jpeg|\.svg)")
regexRelativePathImage = compile("(?:\.png|\.jpg|\.gif|\.bmp|\.jpeg|\.svg)")

regexPath = compile("!\[(.*?)\]\((.*?)\)")
regex20 = compile("%20")
Expand All @@ -197,7 +211,7 @@ def embedded_link_convert(line):
# modify paths into local links. just remove UID and convert spaces
Title = pathMatch.group(1)
relativePath = pathMatch.group(2)
#is_image = regexRelativePathImage.search(relativePath)
is_image = regexRelativePathImage.search(relativePath)

regexSpecialUtf8 = compile("%([A-F0-9][A-F0-9])%([A-F0-9][A-F0-9])%([A-F0-9][A-F0-9])")
regexutf8 = compile("%([A-F0-9][A-F0-9])%([A-F0-9][A-F0-9])")
Expand All @@ -206,7 +220,8 @@ def embedded_link_convert(line):
relativePath = str_forbid_char_remove(relativePath)
relativePath = regexUID.sub("", relativePath)
relativePath = str_space_utf8_replace(relativePath)

relativePath = remove_all_characters_except_alpha_numeric(relativePath)

utf8_match = regexutf8.search(relativePath)
while utf8_match:
is_special_utf8 = False
Expand Down Expand Up @@ -236,7 +251,10 @@ def embedded_link_convert(line):
else:
relativePath = regexutf8.sub(unicode_str, relativePath, 1)

line, num_matchs = regexPath.subn("[["+relativePath+"]]", line)
if is_image:
line, num_matchs = regexPath.subn("!["+relativePath+"](./"+relativePath+")", line)
else:
line, num_matchs = regexPath.subn("[["+relativePath+"]]", line)

if num_matchs > 1:
print(f"Warning: {line} replaced {num_matchs} matchs!!")
Expand Down Expand Up @@ -287,6 +305,7 @@ def internal_link_convert(line):
title = str_space_utf8_replace(title)
title = str_forbid_char_remove(title)
title = str_slash_char_remove(title)
title = remove_all_characters_except_alpha_numeric(title)

if title != markdownLinkMatch.group(1):
print(line)
Expand Down