【问题标题】:Use lowercase month names in Pelican urls在 Pelican url 中使用小写月份名称
【发布时间】:2015-12-13 21:09:34
【问题描述】:

我正在使用静态站点生成器Pelican。我已将 ARTICLE_URL 设置配置为在 url 中包含年份和月份。

例如

ARTICLE_URL = 'posts/{date:%Y}/{date:%b}/{slug}/'

我的帖子网址将类似于

/posts/2015/Dec/my-new-post/

我希望月份是小写的,即

/posts/2015/dec/my-new-post/

有没有简单的方法来实现这一点?

【问题讨论】:

  • .lower() 在字符串之后
  • @depperm 你建议把lower()放在哪里?小写 ARTICLE_URL 不起作用,我想将 Pelican 使用 ARTICLE_URL 生成的字符串小写。
  • 简单的方法?并不真地。它们直接来自日期格式,反过来又来自您的语言环境。最好的办法是编写一个小写插件并将它们设置为override_url/override_save_as

标签: python string-formatting pelican


【解决方案1】:

我遇到了同样的问题,并通过制作一个小的 Pelican plugin 来解决它,它将小写月份名称添加为内容元数据变量。

这是插件:

from pelican import signals

def add_lowercase_month_to_metadata(content):
    if "date" in content.metadata:
        content.metadata["lowercase_month"] = content.metadata["date"].strftime("%B").lower()
        content.metadata["lowercase_month_short"] = content.metadata["date"].strftime("%b").lower()

def register():
    signals.content_object_init.connect(add_lowercase_month_to_metadata)

激活后,您可以在ARTICLE_URLARTICLE_SAVE_AS 中使用新的小写月份名称变量(lowercase_monthlowercase_month_short):

ARTICLE_URL = '/{date:%Y}/{lowercase_month_short}/{date:%d}/{slug}'
ARTICLE_SAVE_AS = '{date:%Y}/{lowercase_month_short}/{date:%d}/{slug}/index.html'

【讨论】:

  • 短短 5 年后,一个答案。谢谢!
【解决方案2】:

尝试改用这个:

ARTICLE_URL = 'posts/{date:%Y}/{date:%b | lower }/{slug}/'

【讨论】:

    猜你喜欢
    • 2016-08-22
    • 1970-01-01
    • 2014-11-13
    • 2017-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多