The crafting shortcode previously depended on scripts/sync-items.py to pre-populate assets/items/ before every build: the script regex-scanned content/ for shortcode usages, listed the sfcraft repo via the GitHub API, downloaded missing vanilla textures from the wiki, and had to be driven through scripts/build.py, scripts/dev.py or a Makefile so that plain `hugo` never ran on its own. Resolution now happens inside the render pipeline, so `hugo` and `hugo server` work directly and nothing has to be kept in sync: - layouts/_partials/sfcraft/item-texture.html resolves an item id to an image Resource, trying assets/items/<id>.png, then the sfcraft repo texture, then the Minecraft wiki. - layouts/_partials/sfcraft/wiki-lookup.html derives the wiki English name from the id, lists candidates via the allimages API and picks the newest JE/BE render, replacing the script's name-variant logic. - Callers go through partialCached keyed on the item id, so each id is resolved once per build no matter how many slots reference it. Because usages are discovered by rendering, the content scanner is gone and the two syntaxes can no longer drift apart from what the scanner understood. Enumerating the sfcraft repo is also unnecessary: a texture is fetched by its raw URL and a 404 simply means "not a custom item". Caching is Hugo's getresource file cache, pinned to maxAge -1, so every URL is downloaded once globally, later builds hit the cache, and offline builds succeed. `hugo --ignoreCache` refreshes upstream changes, which replaces the script's per-build file-size comparison. Error reporting distinguishes cases the script could not tell apart. A 404 from every source means the id is wrong and fails the build (configurable via params.itemTextures.onMissing), while a transport error, rate limit or 5xx only warns and falls back to the `?` placeholder, so a missing network no longer looks like a typo. Also in this change: - wiki name special cases move from a dict in the script to data/sfcraft/wiki_aliases.yaml - textures publish to /sfc/items/<id>.png regardless of source, so switching an item to a hand-placed texture keeps its URL - component CSS moves to assets/css/sfcraft-crafting.css, minified and inlined once per page, instead of a heredoc inside the shortcode - a `.` used as an alias value now means "empty slot", matching what it already meant inside pattern; it previously resolved as an item id and reported a missing texture - assets/items/*.png is no longer gitignored, since that directory now only holds intentional overrides that should be committed Verified against Hugo 0.164.0: custom items resolve from the repo, vanilla items from the wiki, TNT / Flint_and_Steel / Dragon's_Breath exercise the alias and connector rules, hand-placed textures win over both, a typo fails the build, and a cold cache with no network degrades to placeholders while a warm cache builds fully offline. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
88 lines
3.5 KiB
HTML
88 lines
3.5 KiB
HTML
{{- /* SFCraft 合成配方组件
|
||
|
||
两种用法:
|
||
1) 九格直填: a1..c3 为 3x3 合成格, out 为输出格
|
||
2) pattern 别名模式(提供 pattern 时优先):
|
||
pattern="a a a\nb b b\nc c c" 行间用 \n(必须写在一行内), 也可用空格或 ; 分隔, 共 9 个 token
|
||
a="diamond" b="stick" c="" 每个 token 对应一个同名参数, 值为物品 id
|
||
token 也可以直接写物品 id, 如 pattern="diamond stick ."
|
||
"." 表示空格子
|
||
|
||
- 值为物品 id, 留空表示空格子; 名称后可加 ":数量" 显示堆叠数, 例如 a1="stick:2"
|
||
- 贴图由 partials/sfcraft/item-texture.html 自动解析并缓存, 无需手工准备
|
||
- caption 参数可选, 显示在组件下方
|
||
*/ -}}
|
||
|
||
{{- $table := resources.Get "table.png" -}}
|
||
{{- $slots := slice
|
||
(dict "key" "a1" "cls" "grid" "x" "13.75%" "y" "20%")
|
||
(dict "key" "a2" "cls" "grid" "x" "25%" "y" "20%")
|
||
(dict "key" "a3" "cls" "grid" "x" "36.25%" "y" "20%")
|
||
(dict "key" "b1" "cls" "grid" "x" "13.75%" "y" "42.5%")
|
||
(dict "key" "b2" "cls" "grid" "x" "25%" "y" "42.5%")
|
||
(dict "key" "b3" "cls" "grid" "x" "36.25%" "y" "42.5%")
|
||
(dict "key" "c1" "cls" "grid" "x" "13.75%" "y" "65%")
|
||
(dict "key" "c2" "cls" "grid" "x" "25%" "y" "65%")
|
||
(dict "key" "c3" "cls" "grid" "x" "36.25%" "y" "65%")
|
||
(dict "key" "out" "cls" "out" "x" "70%" "y" "37.5%")
|
||
-}}
|
||
|
||
{{- $ctx := . -}}
|
||
{{- $pattern := $ctx.Get "pattern" -}}
|
||
{{- $tokens := slice -}}
|
||
{{- if $pattern -}}
|
||
{{- $flat := replace (replace (replace (replace $pattern "\\n" " ") ";" " ") "\n" " ") "\r" " " -}}
|
||
{{- range split (trim $flat " ") " " -}}
|
||
{{- if ne . "" -}}{{- $tokens = $tokens | append . -}}{{- end -}}
|
||
{{- end -}}
|
||
{{- end -}}
|
||
|
||
{{- /* 每页只输出一次样式 */ -}}
|
||
{{- if not (.Page.Store.Get "sfc_crafting_css") -}}
|
||
{{- .Page.Store.Set "sfc_crafting_css" true -}}
|
||
{{- with resources.Get "css/sfcraft-crafting.css" -}}
|
||
<style>{{ (. | minify).Content | safeCSS }}</style>
|
||
{{- end -}}
|
||
{{- end -}}
|
||
|
||
{{- if and $pattern (ne (len $tokens) 9) -}}
|
||
<div class="sfc-error">
|
||
crafting 的 pattern 需要恰好 9 个 token(3 行 × 3 列),当前是 {{ len $tokens }} 个。
|
||
请检查 pattern="a a a\nb b b\nc c c" 的写法。
|
||
</div>
|
||
{{- else -}}
|
||
<figure class="sfc-crafting" style="background-image: url('{{ $table.RelPermalink }}');">
|
||
{{- range $i, $slot := $slots -}}
|
||
{{- $val := "" -}}
|
||
{{- if eq $slot.key "out" -}}
|
||
{{- $val = trim ($ctx.Get "out") " " -}}
|
||
{{- else if $pattern -}}
|
||
{{- $token := "" -}}
|
||
{{- if lt $i (len $tokens) -}}{{- $token = index $tokens $i -}}{{- end -}}
|
||
{{- if and (ne $token "") (ne $token ".") -}}
|
||
{{- $v := trim ($ctx.Get $token) " " -}}
|
||
{{- if eq $v "" -}}
|
||
{{- if findRE "^[a-z0-9_]+$" $token -}}{{- $v = $token -}}{{- end -}}
|
||
{{- end -}}
|
||
{{- $val = $v -}}
|
||
{{- end -}}
|
||
{{- else -}}
|
||
{{- $val = trim ($ctx.Get $slot.key) " " -}}
|
||
{{- end -}}
|
||
{{- /* "." 表示空格子, 无论它写在 pattern 里还是写成别名的值 */ -}}
|
||
{{- if eq $val "." -}}{{- $val = "" -}}{{- end -}}
|
||
{{- partial "sfcraft/crafting-slot.html"
|
||
(dict
|
||
"val" $val
|
||
"cls" $slot.cls
|
||
"x" $slot.x
|
||
"y" $slot.y
|
||
"page" $ctx.Page
|
||
) -}}
|
||
{{- end -}}
|
||
{{- with $ctx.Get "caption" -}}
|
||
<figcaption class="sfc-caption">{{ . }}</figcaption>
|
||
{{- end -}}
|
||
</figure>
|
||
{{- end -}}
|