img.py 1.5 KB
Newer Older
M
Mars Liu 已提交
1
import os
F
fix bug  
feilong 已提交
2
import subprocess
M
Mars Liu 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
from sys import path, version
from types import new_class
from .tree import load_json, dump_json


def simple_list_md_load(p):
    with open(p, 'r', encoding='utf-8') as f:
        lines = f.readlines()
        result = []
        for line in lines:
            item = line.strip('\n')
            result.append(item)
        return result


def simple_list_md_dump(p, lines):
    with open(p, 'w', encoding='utf-8') as f:
        f.write('\n'.join(lines))


F
fix bug  
feilong 已提交
23 24 25 26 27 28 29 30 31
def get_repo_name():
    ret = subprocess.Popen([
        "git", "config", "--get", "remote.origin.url"
    ], stdout=subprocess.PIPE)
    lines = list(map(lambda l: l.decode(), ret.stdout.readlines()))
    url = lines[0]
    return url.split('/')[-1].split('.')[0]


M
Mars Liu 已提交
32 33 34 35
class ImgWalker():

    def __init__(self, root) -> None:
        self.root = root
F
fix bug  
feilong 已提交
36
        self.repo_name = get_repo_name()
M
Mars Liu 已提交
37 38 39 40 41 42 43 44 45 46

    def walk(self):
        for base, dirs, files in os.walk(self.root):
            for file in files:
                if file[-3:] == '.md':
                    md_file = os.path.join(base, file)
                    md_lines = simple_list_md_load(md_file)
                    md_new = []
                    for line in md_lines:
                        new_line = line.replace(
F
fix bug  
feilong 已提交
47
                            '![](./', f'![](https://gitcode.net/csdn/{self.repo_name}/-/raw/master/{base}/')
M
Mars Liu 已提交
48 49 50
                        md_new.append(new_line)
                    md_new.append('')
                    simple_list_md_dump(md_file, md_new)