python - 使用 Pelican,如何在 Python 代码和主题之间设置或访问变量?

标签 python pelican

我需要将原始源文件名(*.md 文件)传递到 sidebar.html 中。我怎样才能做到这一点?

从这个站点( http://pelican.readthedocs.org/en/3.6.3/themes.html ),我了解到一些变量可用,并且 pelicanconf.py 文件中的所有大写字母变量也可用,但我不知道如何获取主题文件中的原始源文件等信息。

最佳答案

我认为可能有一种更简单的方法,但使用 jinja 过滤器对我来说效果很好( http://linkpeek.com/blog/how-to-add-a-custom-jinja-filter-to-pelican.html )

采取的步骤:

预设置

我将原始标记文件的名称设为 YEAR-MONTH-DAY-NAME 格式,以便从页面的 URL 中恢复。

创建过滤器

过滤器给出了url,从url中,我可以恢复原始的源md文件路径。

def tosource(url):
    # example input
    # posts/2014/01/26/python-unittest-structure/index.html
    # posts/2014/01/26/ocaml-vs-java/index.html
    # posts/2014/01/25/why-ocaml-comparison-with-python/index.html

    if url.startswith("posts"):
        (posts, year, month, day, name) = url.split('/')[:-1]
        res = "%s/%s/%s-%s-%s-%s.md" % (year, month, year, month, day, name)
    else:
        res = "/" # implement later
    return res

更新pelicanconf.py

教鹈鹕过滤器的名称和位置。

import sys
sys.path.append('.')

import sourcename
JINJA_FILTERS = {'sourcename':sourcename.tosource}

OPENCONTENT = "open:///pelican/knowledge_blog/content"

正如 http://docs.getpelican.com/en/3.5.0/themes.html#theming-pelican 中所写,conf 文件中的所有大写字母变量都可以在主题文件中访问。

更新sidebar.html

我在 sidebar.html 中添加了一行代码,以使用 Jinja 过滤器获取原始 md 文件路径。

Click to <a href="{{ OPENCONTENT }}/{{ output_file|sourcename }}">Edit</a>

生成 html

运行make html并测试。

enter image description here enter image description here

关于python - 使用 Pelican,如何在 Python 代码和主题之间设置或访问变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32389975/

相关文章:

python - 当另一个对象在其他地方实例化时,如何从另一个对象接收 PyQt 信号?

python - 具有 Azure AD SSO 的 Snowflake python 连接器

python - 使用 Python setup.py 通过 develop 与 install 安装不同的依赖项

python - 图像未复制到 Pelican 中的输出文件夹

hyperlink - 使用 Pelican 链接到页面中的标签

python - 随着我写的帖子越来越多,鹈鹕花费的时间越来越长……它也在重新制作旧帖子吗?

python - 不在 python 中返回正则表达式中的整个模式

python - 使anaconda jupyter笔记本自动保存.py脚本

python - 服务器启动后Django多个动态数据库

blogs - 在 Pelican 中,如何在主页上显示完整的最新文章/帖子?