gpt4 book ai didi

ruby - 使用 Liquid 获取、操作和排序哈希对象值

转载 作者:数据小太阳 更新时间:2023-10-29 08:59:56 25 4
gpt4 key购买 nike

我正在为部署在 Github Pages 上的 Jekyll 站点制作词汇表模板。
条目是从 _data/glossary.yml 文件中提取的。
我希望模板按字母顺序排列条目,而不考虑 glossary.yml 中数据的顺序。

使用 {% assign glossary = site.data.glossary | sort 'term' %} 确实会返回一个按字母顺序排序的对象,我可以使用 for 循环对其进行迭代。
但是,sort 过滤器区分大小写 - 小写条目排在所有大写或大写术语之后。

Liquid 4.0.0 添加了一个 sort_natural 过滤器来执行我想要的操作,但是 Github Pages 目前运行的是 3.0.6,所以我需要一个解决方法。

我的问题是我怎样才能:

  1. 在 Liquid 模板中获取 site.data.glossary?
  2. 操作每个条目的第一个映射的字符串值?
    • (即使用 capitalize 字符串过滤器去除大写/小写差异)
  3. 使用本地字符串过滤值对整个 map 进行排序?
  4. 奖励:如果我仍然可以使用保留原始大小写的源字符串值,以便在生成的 html 中最终显示。

例如,给定以下 data/glossary.yml:

- term: apricot
loc: plastic

- term: Apple
loc: basket

- term: Banana
loc: basket

- term: bowtie
loc: closet

- term: Cat
loc: outside

如何创建一个本地 Liquid 对象变量来排序和显示以下内容?:

  • 苹果
    • 篮子
  • 杏子
    • 塑料
  • 香蕉
    • 篮子
  • 领结
    • 壁橱
    • 外面

最佳答案

唯一的方法是使用将实现 liquid 4 的过滤器插件 natural_sort .

一些剪切和过去之后你有 _plugins/natural_sort_filter.rb :

module Jekyll
module SortNatural
# Sort elements of an array ignoring case if strings
# provide optional property with which to sort an array of hashes or drops
def sort_natural(input, property = nil)
ary = InputIterator.new(input)

if property.nil?
ary.sort { |a, b| a.casecmp(b) }
elsif ary.empty? # The next two cases assume a non-empty array.
[]
elsif ary.first.respond_to?(:[]) && !ary.first[property].nil?
ary.sort { |a, b| a[property].casecmp(b[property]) }
end
end

class InputIterator
include Enumerable

def initialize(input)
@input = if input.is_a?(Array)
input.flatten
elsif input.is_a?(Hash)
[input]
elsif input.is_a?(Enumerable)
input
else
Array(input)
end
end

def join(glue)
to_a.join(glue)
end

def concat(args)
to_a.concat(args)
end

def reverse
reverse_each.to_a
end

def uniq(&block)
to_a.uniq(&block)
end

def compact
to_a.compact
end

def empty?
@input.each { return false }
true
end

def each
@input.each do |e|
yield(e.respond_to?(:to_liquid) ? e.to_liquid : e)
end
end
end
end
end
Liquid::Template.register_filter(Jekyll::SortNatural)

这个新过滤器可以像这样使用:

{% assign glossary = site.data.glossary | sort_natural: 'term' %}
<ul>
{% for item in glossary %}
<li>{{ item.term }} - {{ item.loc }}</li>
{% endfor %}
</ul>

关于ruby - 使用 Liquid 获取、操作和排序哈希对象值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38288517/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com