{{- /* 在 Minecraft wiki 上查出某个物品最新版本渲染图的 URL wiki 的物品图片按 <英文名>_JE<版本>[_BE<版本>].png 命名, 同一物品会有多个历史版本, 这里用 allimages API 按前缀列出候选, 再挑版本号最大的一张。 英文名由物品 id 自动推导 (逐词首字母大写), 推不出来的特例写在 data/sfcraft/wiki_aliases.yaml 里。 入参: id 物品 id api wiki api.php 的地址 opts 传给 resources.GetRemote 的选项 返回 dict: url 图片 URL, 没找到时为 "" name 命中的 wiki 英文名 notes 访问失败等诊断信息 (字符串, 无异常时为 "") */ -}} {{- $id := .id -}} {{- $api := .api -}} {{- $opts := .opts -}} {{- $data := (hugo.Data.sfcraft | default dict).wiki_aliases | default dict -}} {{- $aliases := $data.aliases | default dict -}} {{- $connectors := $data.connectors | default slice -}} {{- /* 生成英文名候选 */ -}} {{- $words := slice -}} {{- range split $id "_" -}} {{- if ne . "" -}}{{- $words = $words | append . -}}{{- end -}} {{- end -}} {{- $titleWords := slice -}} {{- $mixedWords := slice -}} {{- range $words -}} {{- $upper := strings.FirstUpper . -}} {{- $titleWords = $titleWords | append $upper -}} {{- $mixedWords = $mixedWords | append (cond (in $connectors .) . $upper) -}} {{- end -}} {{- $variants := slice -}} {{- /* 特例表优先 */ -}} {{- with index $aliases $id -}}{{- $variants = $variants | append . -}}{{- end -}} {{- /* diamond_pickaxe -> Diamond_Pickaxe */ -}} {{- $variants = $variants | append (delimit $titleWords "_") -}} {{- /* flint_and_steel -> Flint_and_Steel */ -}} {{- $variants = $variants | append (delimit $mixedWords "_") -}} {{- /* tnt -> TNT (短且无元音的 id 多为缩写) */ -}} {{- if and (le (len $id) 4) (not (findRE "[aeiou]" $id)) -}} {{- $variants = $variants | append (upper $id) -}} {{- end -}} {{- /* end_rod -> EndRod */ -}} {{- $variants = $variants | append (delimit $titleWords "") -}} {{- $variants = $variants | uniq -}} {{- $bestURL := "" -}} {{- $bestName := "" -}} {{- $bestScore := -1 -}} {{- $notes := slice -}} {{- range $variants -}} {{- $name := . -}} {{- if not $bestURL -}} {{- $query := querify "action" "query" "list" "allimages" "aiprefix" (printf "%s_" $name) "ailimit" 500 "format" "json" -}} {{- $url := printf "%s?%s" $api $query -}} {{- $images := slice -}} {{- with try (resources.GetRemote $url $opts) -}} {{- with .Err -}} {{- $notes = $notes | append (printf "wiki 查询失败 (%s): %s" $name (replaceRE `^.*error calling GetRemote: ` "" (printf "%s" .))) -}} {{- else with .Value -}} {{- $images = (index (. | transform.Unmarshal) "query" "allimages") | default slice -}} {{- else -}} {{- /* API 对合法查询总会返回 200, 拿到 nil 说明请求没真正到达 */ -}} {{- $notes = $notes | append (printf "wiki 查询无响应 (%s)" $name) -}} {{- end -}} {{- end -}} {{- /* 从候选里挑版本最新的一张: 优先 _(item) 物品图标, 再比 JE 版本, 最后比 BE 版本 */ -}} {{- range $images -}} {{- $prefixItem := printf "%s_(item)_JE" $name -}} {{- $prefixPlain := printf "%s_JE" $name -}} {{- $rest := "" -}} {{- $isItem := 0 -}} {{- if hasPrefix .name $prefixItem -}} {{- $rest = strings.TrimPrefix $prefixItem .name -}} {{- $isItem = 1 -}} {{- else if hasPrefix .name $prefixPlain -}} {{- $rest = strings.TrimPrefix $prefixPlain .name -}} {{- end -}} {{- /* $rest 形如 "2_BE2.png" / "1.20.png" */ -}} {{- if and $rest (findRE `^[0-9]+(\.[0-9]+)*(_BE[0-9]+)?\.png$` $rest) -}} {{- $je := int (index (findRE `^[0-9]+` $rest) 0) -}} {{- $be := -1 -}} {{- with findRE `_BE[0-9]+` $rest -}} {{- $be = int (strings.TrimPrefix "_BE" (index . 0)) -}} {{- end -}} {{- $score := add (mul $isItem 1000000) (add (mul $je 1000) (add $be 1)) -}} {{- if gt $score $bestScore -}} {{- $bestScore = $score -}} {{- $bestURL = .url -}} {{- $bestName = .name -}} {{- end -}} {{- end -}} {{- end -}} {{- end -}} {{- end -}} {{- return dict "url" $bestURL "name" $bestName "notes" (delimit $notes "; ") -}}