MarkdownRel.py 4.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
"""
本文件用于处理markdown文件的移动问题,尤其是对于相对路径的移动有一定的自动化处理能力。
思路是:如果有md文件需要移动/复制的话,
- 检索md文件中的绝对链接和相对链接;
- 将这些相对链接的文件放在新的目标位置;
    - 如果目标位置已经有同名文件+同文件大小,跳过;否则提醒是跳过、保留且修改还是怎样。
"""
import re
import os
import shutil
11
import urllib.parse
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85


def find_links(filepath: str):
    """
    查找md文件中的超链接,并返回绝对链接和相对链接(列表)。
    其中的相对路径开头没有斜杠,所以要手动补充斜杠获取绝对路径。

    :param filepath: 文件完整路径

    :return: Tuple(res_abs,res_rel): 绝对链接(列表),相对链接(列表)

    """

    pattern1 = re.compile(r'<img src=".+"?')
    pattern11 = re.compile(r'src=".+?"')
    pattern2 = re.compile(r'\[.*]\(.+\)?')
    pattern21 = re.compile(r'\(.+?\)+?')

    try:
        with open(filepath, 'r', encoding='utf-8') as f:
            data = f.readlines()
    except UnicodeDecodeError:
        with open(filepath, 'r', encoding='gbk') as f:
            data = f.readlines()
    except Exception as e:
        print(e)
    finally:
        pass

    lst1 = []
    lst2 = []
    for i in data:
        for j in pattern1.findall(i):
            lst1 += pattern11.findall(j)
        for j in pattern2.findall(i):
            lst2 += pattern21.findall(j)
    res = []
    for i in lst1:
        res.append(i[5:-1])
    for i in lst2:
        res.append(i[1:-1])

    res_abs = []
    res_rel = []
    pattern_head = re.compile(r'^(.+?)(/+?)')

    for p in res:
        p = str(p).replace('\\', '/')
        try:
            if str(p).find('/') >= 0:
                if str(pattern_head.findall(p)[0]).find(':') >= 0:
                    res_abs.append(p)
                else:
                    res_rel.append(p)
            else:
                res_rel.append(p)
        except Exception as e:
            print(p)
            print(e)

    return res_abs, res_rel


def copy_md_linked_files(file_path_old: str, new_path, mode='copy'):
    """
    :param file_path_old: md文件所在完整路径(包括文件名)
    :param new_path: md文件目标位置(文件夹)
    :param mode: 移动或者复制
    :return:
    """
    lst_links_abs, lst_links_rel = find_links(file_path_old)
    fpath, fname = os.path.split(file_path_old)

    for p in lst_links_rel:
86 87 88
        # URL解码
        p = urllib.parse.unquote(p)
        #
89 90 91 92 93 94 95 96 97 98
        old_pth = fpath.replace('\\', '/') + '/' + p  # 原位置
        print(old_pth)
        tar_pth = new_path.replace('\\', '/')
        if tar_pth.endswith('/'):
            tar_pth = tar_pth + p
        else:
            tar_pth = tar_pth + '/' + p
        print(tar_pth)
        tarp, tarfn = os.path.split(tar_pth)
        try:
99 100 101
            if not os.path.exists(tarp):
                os.makedirs(tarp)

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
            shutil.copyfile(old_pth, tar_pth)  # 目前,附件永远是复制,因为移动可能存在其他问题

        except Exception as e:
            print(e)


def copy_md(file_path_old: str, file_path_new: str, mode='copy'):
    """
    复制md文件到指定位置。

    :param file_path_old:
    :param file_path_new: 目标位置(文件夹 + 文件名)
    :param mode: 移动或者复制
    :return: 无
    """
117 118

    # 相对路径附件,复制到目标位置
119 120
    new_path, new_filename = os.path.split(file_path_new)
    copy_md_linked_files(file_path_old, new_path)
121
    #
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
    if len(new_filename) == 0:
        fpath, fname = os.path.split(file_path_old)
        new_full_path = new_path + '/' + fname
    else:
        new_full_path = file_path_new

    try:
        if mode == 'copy':
            shutil.copyfile(file_path_old, new_full_path)
        elif mode == 'move':
            shutil.move(file_path_old, new_full_path)
    except Exception as e:
        print('复制文件出错:')
        print(e)


138 139 140 141 142 143 144
def mv_md(file_path_old: str, file_path_new: str):
    """
    移动文件
    """
    copy_md(file_path_old, file_path_new, mode='move')


145 146 147 148 149 150 151 152 153
if __name__ == '__main__':
    file_old = r"D:\MaJian\Desktop\@短文剪辑\Python 图像库 PIL 的类 Image 及其方法介绍_leemboy 的博客 - CSDN 博客_pil.md"
    file_new = r'd:/to_delete/a/b/c/d\\e/PIL.md'
    # pth_abs, pth_rel = find_links(fip)
    # print(pth_abs)
    # print(pth_rel)
    #
    # copy_md_linked_files(fip, 'd:/to_delete')
    copy_md(file_old, file_new)