Skip to content

MkDocs使用技巧集合

展示访问量

利用overrides覆盖默认内容,相关文件放在custom_dir目录中。

theme:
  name: material
  custom_dir: overrides

Overriding partials

custom_dir的partials目录中放置需要替换的文件content.html

{#-
  This file was automatically generated - do not edit
-#}
{% if "tags" in config.plugins %}
  {% include "partials/tags.html" %}
{% endif %}
{% include "partials/actions.html" %}
{% if not "\x3ch1" in page.content %}
  <h1>{{ page.title | d(config.site_name, true)}}</h1>
{% endif %}
{{ page.content }}
<font size="2" color="grey">本文阅读量&nbsp;<span id="busuanzi_value_page_pv"></span>&nbsp;</font>
<br>
<font size="2" color="grey">本站总访问量&nbsp;<span id="busuanzi_value_site_pv"></span>&nbsp;</font>
{% include "partials/source-file.html" %}
{% include "partials/feedback.html" %}
{% include "partials/comments.html" %}

Overriding blocks

Note

更推荐使用该方案

自动部署

检测脚本,ChatGPT生成

import subprocess
import os
import hashlib
import time

# Git仓库地址
repo_url = 'http://git.iomics.pro/yhfu/IData_alpha.git'
# 本地克隆路径
local_repo_path = '/data/bio_wiki/build_spcae/bio_wiki'
# 部署脚本路径
deploy_script = '/data/bio_wiki/deploy.sh'
# 之前的提交哈希值
last_commit_hash_file = '/data/bio_wiki/last_commit_hash.txt'

# 获取当前 Git 仓库的最新提交哈希值
def get_git_commit_hash():
    try:
        result = subprocess.run(['git', 'log', '-1', '--format=%H'], cwd=local_repo_path, capture_output=True, text=True, check=True)
        return result.stdout.strip()
    except subprocess.CalledProcessError as e:
        print(f"Git 操作失败: {e}")
        return None

# 克隆或拉取最新代码
def update_local_repo():
    if not os.path.exists(local_repo_path):
        # 如果本地仓库不存在,克隆仓库
        try:
            subprocess.run(['git', 'clone', repo_url, local_repo_path], check=True)
        except subprocess.CalledProcessError as e:
            print(f"克隆失败: {e}")
    else:
        # 如果仓库已经存在,拉取最新的代码
        try:
            subprocess.run(['git', 'pull'], cwd=local_repo_path, check=True)
        except subprocess.CalledProcessError as e:
            print(f"拉取更新失败: {e}")

# 执行部署脚本
def deploy():
    try:
        subprocess.run(['bash', deploy_script], check=True)
    except subprocess.CalledProcessError as e:
        print(f"执行部署脚本失败: {e}")

# 检查是否有更新并执行部署
def check_for_updates():
    # 获取本地最新提交哈希值
    current_commit_hash = get_git_commit_hash()
    if current_commit_hash is None:
        print("无法获取当前提交哈希值,跳过检查。")
        return

    # 获取存储的上次提交哈希值
    if os.path.exists(last_commit_hash_file):
        with open(last_commit_hash_file, 'r') as f:
            last_commit_hash = f.read().strip()
    else:
        last_commit_hash = None

    # 如果当前提交与上次提交不同,则表示有更新
    if current_commit_hash != last_commit_hash:
        print("发现更新,正在执行部署脚本...")
        deploy()
        # 更新提交哈希值
        with open(last_commit_hash_file, 'w') as f:
            f.write(current_commit_hash)
    else:
        print("没有检测到更新。")

# 主函数
def main():
    update_local_repo()
    check_for_updates()

if __name__ == '__main__':
    main()

配置计划任务

*/5 * * * * python3 /data/bio_wiki/auto_deploy.py

本文阅读量  次
本站总访问量  次
Authors: Wind