这感觉像是一个
Jekyll
问题,但结果可能只是一个Ruby
问题...
Jekyll::Hooks.register
用法的有用示例,但旨在挂钩 jekyll build
。tags
变量生成我的标签页面。bundle exec jekyll serve
在本地查看我的网站。_config.yml 中有
verbose: true
Ray Fong 主持了 Jekyll 博客,其中解释了如何自动调用 Python 标签页创建脚本 - 自动化 Jekyll 博客标签:
Jekyll::Hooks.register :posts, :post_write do
system("python _plugins/compile_tags.py")
- 事实上它就在这里,_plugins/compile_tags.rb。 我假设当她在本地为她的网站提供服务时,这个
Ruby
代码会调用她的 Python
脚本来根据需要重新创建标签页面。
假设 Ray 的 Ruby 插件如我想象的那样工作,我自己尝试过,但没有成功 - 它从不调用我的 python 脚本。我尝试以各种方式调整它,包括像这样(_plugins/compile_tags.rb),
Jekyll::Hooks.register :posts, :post_write do |post|
puts 'post_write was triggered in _plugins/compile_tags.rb'
exec("python _plugins/compile_tags.py")
end
- 仍然没有运气 - 也没有消息...
如何在
Python
挂机 jekyll serve
期间调用我的 post_write
脚本?
Ray Fong 鼓励我做一些测试,所以我说
jekyll new minimal_test_site
,并在那里制作了 _plugins/test.rb
,其中包含来自 hooks.feature 的第一个示例,并且钩子按预期触发,(我得到 _site/foo.html
包含文本“mytinypage”)。所以回到我的网站,在我的 Gemfile
中我恢复了这个:
# Jekyll:
gem "github-pages", group: :jekyll_plugins # bundle update github-pages
gem "webrick", "~> 1.7" # instead of using Ruby v2.7.4,
# and still required even though GitHub Pages now uses jekyll 3.9.3...
对此:
# Jekyll:
gem "jekyll", "~> 4.3.2"
还有“瞧!”我的钩子被调用,但不幸的是,它会导致重新生成标签的无休止循环,即使我将
_plugins/compile_tags.rb
更改为 Jekyll
钩子 after_reset
:
Jekyll::Hooks.register :site, :after_reset do
puts '- infinite loop of tag generation...'
system("python _plugins/compile_tags.py")
end
也许有人知道为什么......
当你想在 ruby 脚本中轻松执行 shell 或 python 脚本(本例)时,你可以这样做
`/pathto/pyhton your_python_script.py`
# or
%x(/pathto/pyhton your_python_script.py)
#or
system(/pathto/pyhton your_python_script.py)
在 shell 中使用此命令替换“pathto”
which python
/Users/myusername/opt/anaconda3/bin/python # on my laptop return this
所以参考你的问题,这就是解决方案:
`/Users/myusername/opt/anaconda3/bin/python _plugins/compile_tags.py`
# or
%x(/Users/myusername/opt/anaconda3/bin/python _plugins/compile_tags.py)
# or
system(/Users/myusername/opt/anaconda3/bin/python _plugins/compile_tags.py)
在尝试解决同样的问题时发现了这篇文章,尝试以这种方式运行外部脚本时永无休止的重建。
最后,我只是在脚本开始处写下了一个时间戳,并与每次运行的当前时间进行比较。然后脚本退出,如果在过去 10 秒内运行,则不进行任何文件系统更改(甚至不更新时间戳)。
有点“忽悠”,但它有效:-)