From bd946c9efc13b13c911ee42fdf7f607a8c734a9d Mon Sep 17 00:00:00 2001 From: yixiu Date: Mon, 8 Jul 2024 14:53:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9B=BE=E7=89=87=E4=B8=8A=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .theme/script.js.temp | 13 +++++++-- generate_index_html.py | 62 ++++++++++++++++++++++++++++++++++++++++++ script.js | 13 +++++++-- 3 files changed, 84 insertions(+), 4 deletions(-) diff --git a/.theme/script.js.temp b/.theme/script.js.temp index 164b361..5696460 100644 --- a/.theme/script.js.temp +++ b/.theme/script.js.temp @@ -44,6 +44,15 @@ function initCollapsible(tableContent, firstDirectory) { console.error('复制失败: ', err); }); } -} -// 默认展开第一个目录并显示其内容 + // 默认展开第一个目录并显示其内容 + if (firstDirectory) { + populateTable(firstDirectory); + var firstCollapsible = document.querySelector('.collapsible[data-path="' + firstDirectory + '"]'); + firstCollapsible.classList.add("active"); + firstCollapsible.nextElementSibling.style.display = "block"; + } +} +document.addEventListener("DOMContentLoaded", function() { + initCollapsible(tableContent, firstDirectory); +}); diff --git a/generate_index_html.py b/generate_index_html.py index 3a7bd17..d4ce5b7 100644 --- a/generate_index_html.py +++ b/generate_index_html.py @@ -6,6 +6,68 @@ TEMPLATE_DIR = '.theme' +def read_template(file_path): + with open(file_path, 'r', encoding='utf-8') as file: + return file.read() + +def generate_index_html(root_dir): + base_url = f"https://github.com/{GITHUB_USERNAME}/{GITHUB_REPOSITORY}/raw/main/" + cdn_url = f"https://cdn.jsdelivr.net/gh/{GITHUB_USERNAME}/{GITHUB_REPOSITORY}@main/" + + html_template = read_template(os.path.join(TEMPLATE_DIR, 'template.html.temp')) + js_template = read_template(os.path.join(TEMPLATE_DIR, 'script.js.temp')) + + directory_html = '' + table_content = {} + + def generate_directory_html(directory, level=0): + content = '' + first_directory = None + for entry in sorted(os.listdir(directory)): + if entry.startswith('.'): + continue + path = os.path.join(directory, entry) + rel_path = os.path.relpath(path, root_dir).replace("\\", "/") + if os.path.isdir(path): + if first_directory is None: + first_directory = rel_path + content += f''' + +
+ {generate_directory_html(path, level + 1)[0]} +
+ ''' + elif entry.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp', '.tiff')): + dir_path = rel_path.rsplit("/", 1)[0] + if dir_path not in table_content: + table_content[dir_path] = [] + https_url = base_url + rel_path + cdn_url_complete = cdn_url + rel_path + table_content[dir_path].append((entry, https_url, cdn_url_complete)) + return content, first_directory + + directory_html, first_directory = generate_directory_html(root_dir) + + html_content = html_template.replace('', directory_html) + html_content = html_content.replace('', str(table_content)) + html_content = html_content.replace('', '"' + first_directory + '"') + + with open(os.path.join(root_dir, 'index.html'), 'w', encoding='utf-8') as f: + f.write(html_content) + + with open(os.path.join(root_dir, 'script.js'), 'w', encoding='utf-8') as f: + f.write(js_template) + +if __name__ == "__main__": + generate_index_html('.') +import os + +# 在这里定义GitHub用户名和仓库名称 +GITHUB_USERNAME = 'yixiu001' +GITHUB_REPOSITORY = 'Figurebed' + +TEMPLATE_DIR = '.theme' + def read_template(file_path): with open(file_path, 'r', encoding='utf-8') as file: return file.read() diff --git a/script.js b/script.js index 164b361..5696460 100644 --- a/script.js +++ b/script.js @@ -44,6 +44,15 @@ function initCollapsible(tableContent, firstDirectory) { console.error('复制失败: ', err); }); } -} -// 默认展开第一个目录并显示其内容 + // 默认展开第一个目录并显示其内容 + if (firstDirectory) { + populateTable(firstDirectory); + var firstCollapsible = document.querySelector('.collapsible[data-path="' + firstDirectory + '"]'); + firstCollapsible.classList.add("active"); + firstCollapsible.nextElementSibling.style.display = "block"; + } +} +document.addEventListener("DOMContentLoaded", function() { + initCollapsible(tableContent, firstDirectory); +});