ruby 在Jekyll post_write上调用python插件

hrysbysz  于 2023-06-22  发布在  Ruby
关注(0)|答案(1)|浏览(124)

这感觉像是一个Jekyll问题,但可能只是一个Ruby问题。

1后台

  • hooks.feature提供了一些有用的Jekyll::Hooks.register用法示例,但旨在挂钩到jekyll build
  • 我使用Python脚本(_plugins/compile_tags.py)从我的文章的YAML front matter tags变量生成我的标签页面。
  • 我用bundle exec jekyll serve查看我的网站。
  • 为了测试,我在_config.yml中有verbose: true

2 wish:在post_write上调用python

Ray Fong主持了一个Jekyll博客,她在其中解释了她如何自动调用python标签页面创建脚本-Automated Jekyll blog tags

Jekyll::Hooks.register :posts, :post_write do
  system("python _plugins/compile_tags.py")
  • 它确实在这里_plugins/compile_tags.rb我假设当她在本地服务她的站点时,这个Ruby代码调用她的Python脚本来根据需要重新创建标记页面。

3问题

假设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
  • 还是没有消息

4我的问题

如何在jekyll serve挂接post_write期间调用Python脚本?

5 edit:hook部分工作

Ray Fong鼓励我做一些测试,所以我说jekyll new minimal_test_site,并在那里做了_plugins/test.rb,包含来自hooks.feature的第一个示例,并按预期触发了钩子,(我得到了包含文本“mytinypage”的_site/foo.html)。所以回到我的网站,在我的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"

并且,“Lo!”我的钩子被调用,但不幸的是,它导致了一个无休止的重新生成标签的循环,即使我将_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

也许有人知道为什么。。

w80xi6nr

w80xi6nr1#

当你想在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)

相关问题