Python sphinx 如何使Sphinx从单个页面的源文件访问时间生成“上次修改”?
我正在做一个斯芬克斯和ReadTheDocs主题的文档项目。源文件位于gitlab上的git repo中,我使用自动测试来检查文档在每次合并到主分支时是否正确构建。我能够将成功构建的html文件存储在gitlab上,并将它们同步到我的专用服务器。但是,由于文档总是在自动测试期间从头开始构建,因此页面上最后一次修改的标志总是在最后一次构建页面时。我希望避免这种情况,并保持Last Modified标志为true,即我希望它显示源.rst文件的实际上次修改时间。有办法吗 过去我唯一能做到这一点的方法就是使用类似Git属性的东西。阅读本页:Python sphinx 如何使Sphinx从单个页面的源文件访问时间生成“上次修改”?,python-sphinx,Python Sphinx,我正在做一个斯芬克斯和ReadTheDocs主题的文档项目。源文件位于gitlab上的git repo中,我使用自动测试来检查文档在每次合并到主分支时是否正确构建。我能够将成功构建的html文件存储在gitlab上,并将它们同步到我的专用服务器。但是,由于文档总是在自动测试期间从头开始构建,因此页面上最后一次修改的标志总是在最后一次构建页面时。我希望避免这种情况,并保持Last Modified标志为true,即我希望它显示源.rst文件的实际上次修改时间。有办法吗 过去我唯一能做到这一点的方法
特别是看一下标题为“关键字扩展”的部分。过去我唯一能做到这一点的方法就是使用类似Git属性的东西。阅读本页:
特别是看一下标题为“关键字扩展”的部分。根据Kevin Horn的回答,我在构建脚本中添加了以下内容,该脚本在gitlab服务器上自动执行,以实现持续集成:
#!/bin/bash
#=============================================================================
# Automatically add last modified date according to last commit date
#=============================================================================
# first store all the files that are modified;
# don't want them to be added automatically to the commit
# without you noticing!
modfiles=`git ls-files -m`
# Add or update dates to the file
for f in `find . -name "*.rst"`; do
lastline=`tail -n 1 $f`
llstrip="$(echo -e "${lastline}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
commdate=`git log -n 1 --date=short --pretty=format:%cd -- $f`
if [[ "$llstrip" = "*Page last edited"* ]]; then
sed -i "s/\*Page last edited [0-9]*-[0-9]*-[0-9]*\*/\*Page last edited ${commdate}\*/" $f
else
echo "" >> $f
echo "" >> $f
echo '*Page last edited' "$commdate"'*' >> $f
fi;
done
# Now you're done adding dates to the files.
# Quietly add the changed files to the last commit.
# But only do so for files that were included in the commits
# since your branch deviated from the master,
# don't add untracked files without permission!
mybranch=`git rev-parse --abbrev-ref HEAD`
deviat=`git merge-base $mybranch master`
i=0
looping=true
while $looping; do
i=$((i+1))
cmtlist=`git log -n $i --format=%H`
if [[ ${cmtlist} == *${deviat}* ]]; then
looping=false
fi
done
#cmtlist now contains all commit hashes since the branch deviated from the master.
added=false
for f in `git ls-files -m`; do
comm=`git log -n 1 --pretty=format:%H -- $f`
if [[ ${cmtlist} == *${comm}* ]]; then
if [[ ${modfiles} == *${f}* ]]; then
# if changes in file not commited
continue
else
git add $f
added=true
fi
fi
done
if $added; then
git commit --amend --no-edit --quiet
else
echo 'Quickbuild script: nothing to add'
fi
#=============================================================================
# Make clean then rebuild
#=============================================================================
# rm `find . -name "*checkpoint.ipynb"` > /dev/null 2>&1
make clean
make html
exit 0
这将添加或更新上次提交的日期,其中任何.rst文件更改为.rst文件的结尾。所做的更改将悄悄地添加到提交中,前提是由于分支偏离主分支而更改了文件,并且没有任何修改。提交将在gitlab的自动测试期间失败,但这并不重要,因为正确的日期已经在文件中,并且可以使用服务器端的cronjob构建正确的html页面并将其同步到服务器。但是,通过手动运行脚本以快速进行清理和重建,它仍然可以工作
Kevin Horn建议的解决方案的问题在于,它只在本地工作,因为您将git配置设置为在每次提交时自动扩展变量;Hoewer,我必须强制我所有的合作者都做同样的事情,这就是我选择这个选项的原因。根据Kevin Horn的回答,我在构建脚本中添加了以下内容,该脚本将在gitlab服务器上自动执行,以实现持续集成:
#!/bin/bash
#=============================================================================
# Automatically add last modified date according to last commit date
#=============================================================================
# first store all the files that are modified;
# don't want them to be added automatically to the commit
# without you noticing!
modfiles=`git ls-files -m`
# Add or update dates to the file
for f in `find . -name "*.rst"`; do
lastline=`tail -n 1 $f`
llstrip="$(echo -e "${lastline}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
commdate=`git log -n 1 --date=short --pretty=format:%cd -- $f`
if [[ "$llstrip" = "*Page last edited"* ]]; then
sed -i "s/\*Page last edited [0-9]*-[0-9]*-[0-9]*\*/\*Page last edited ${commdate}\*/" $f
else
echo "" >> $f
echo "" >> $f
echo '*Page last edited' "$commdate"'*' >> $f
fi;
done
# Now you're done adding dates to the files.
# Quietly add the changed files to the last commit.
# But only do so for files that were included in the commits
# since your branch deviated from the master,
# don't add untracked files without permission!
mybranch=`git rev-parse --abbrev-ref HEAD`
deviat=`git merge-base $mybranch master`
i=0
looping=true
while $looping; do
i=$((i+1))
cmtlist=`git log -n $i --format=%H`
if [[ ${cmtlist} == *${deviat}* ]]; then
looping=false
fi
done
#cmtlist now contains all commit hashes since the branch deviated from the master.
added=false
for f in `git ls-files -m`; do
comm=`git log -n 1 --pretty=format:%H -- $f`
if [[ ${cmtlist} == *${comm}* ]]; then
if [[ ${modfiles} == *${f}* ]]; then
# if changes in file not commited
continue
else
git add $f
added=true
fi
fi
done
if $added; then
git commit --amend --no-edit --quiet
else
echo 'Quickbuild script: nothing to add'
fi
#=============================================================================
# Make clean then rebuild
#=============================================================================
# rm `find . -name "*checkpoint.ipynb"` > /dev/null 2>&1
make clean
make html
exit 0
这将添加或更新上次提交的日期,其中任何.rst文件更改为.rst文件的结尾。所做的更改将悄悄地添加到提交中,前提是由于分支偏离主分支而更改了文件,并且没有任何修改。提交将在gitlab的自动测试期间失败,但这并不重要,因为正确的日期已经在文件中,并且可以使用服务器端的cronjob构建正确的html页面并将其同步到服务器。但是,通过手动运行脚本以快速进行清理和重建,它仍然可以工作
Kevin Horn建议的解决方案的问题在于,它只在本地工作,因为您将git配置设置为在每次提交时自动扩展变量;Hoewer,我必须强制我所有的合作者做完全相同的事情,这就是我选择这个选项的原因。您可以使用rst的Date指令 在rst文件的顶部或底部,包含以下文本
.. |date| date::
Last Updated on |date|
在最终生成中,仅当源rst已更新时,才会更新此日期
参考:您可以使用rst的Date指令 在rst文件的顶部或底部,包含以下文本
.. |date| date::
Last Updated on |date|
在最终生成中,仅当源rst已更新时,才会更新此日期
参考资料:我知道我有点晚了,但我刚刚创建了一个Sphinx扩展,它从Git获得了上次修改的时间:
我知道我有点晚了,但我刚刚创建了一个Sphinx扩展,它从Git获得了上次修改的时间:
请注意,这将插入文档生成时的日期(可能是也可能不是上次编辑的日期)。请注意,这将插入文档生成时的日期(可能是也可能不是上次编辑的日期)。