mkdoc处理多级目录生成mkdocs.yml

# NOTE: Should execute this script in the my-wiki
import os
import yaml

DOC_PATH = "docs"
SITE_PATH = "site"
NOTE_PATH = "docs/NOTE"
CONFIG_PATH = os.getcwd()
YML_LOCTION = os.path.join(CONFIG_PATH, "mkdocs.yml")

header_config = {
    'site_name': 'My Docs',
    'theme': {
        'name': ('material'),
    }
}

navgation_config = {
    'nav':[]
}
def genereate():
    yield navgation_config['nav']

def create_temp(top_dir):
    for root, dirs, files in os.walk(top_dir):
        return root, dirs, files

def fetchDocList(*k):
    print(k[0])
    if(k[0] is None):
        return

    result = create_temp(k[0])
    if(result is None):
        return

    root, dirs, files = result

    a, = k[1]

    if(len(a) == 0 ):
        a.extend([os.path.join(root, f) for f in files])
        for d in dirs:
            l = {f"{d}": []}
            a.append(l)
            def generator():
                yield a[-1][f"{d}"]
            fetchDocList(os.path.join(root, d), generator())


if(__name__ == '__main__'):
    os.chdir(DOC_PATH)
    fetchDocList(os.path.relpath(NOTE_PATH, DOC_PATH), genereate())
    navgation_config['nav'].insert(0, {'Home': 'index.md'})
    # print(yaml.dump(navgation_config, allow_unicode=True))

    os.chdir(CONFIG_PATH)
    with open (YML_LOCTION, 'w', encoding='utf-8') as f:
        mkdocs_config = OrderedDict(navgation_config)
        mkdocs_config_str = yaml.dump(header_config, allow_unicode=True, default_flow_style=False) + "\n\n\n" + \
                            yaml.dump(navgation_config, allow_unicode=True, default_flow_style=False)
        f.write(mkdocs_config_str)

你可能感兴趣的:(python,python,开发语言)