对于Pelican,如何设置或访问Python代码和主题之间的变量?

2024-06-16 16:18:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要将原始源文件名(*.md file)传递到边栏.html. 我怎么能做到呢?在

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


Tags: 文件pyorghttp站点文件名htmlreadthedocs
1条回答
网友
1楼 · 发布于 2024-06-16 16:18:02

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

采取的步骤:

预设置

我将原始标记文件的名称设置为要从页面的url中恢复的YEAR-MONTH-DAY-name格式。在

创建过滤器

过滤器被赋予了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

更新鹈鹕形态.py

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

^{pr2}$

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

更新边栏.html

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

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

生成html

运行make html并测试。在

enter image description hereenter image description here

相关问题 更多 >