↑の処理を参考に関数を定義して、GLOBAL_CONTEXTに登録したらいけた。
import pathlib
import shutil
def file_copy(post, target_ext):
src = pathlib.Path(post.source_path).parent
dst = pathlib.Path('output') / post.folder / "."
for s in src.glob(target_ext):
d = dst / s.name
if not d.parent.exists():
makedirs(str(d.parent))
shutil.copyfile(s, d)
return ""
post.tmpl内で以下のように呼び出すとoutputディレクトリにコピーされる。
{% for target in ['*.png', '*.jpg'] %}
{{ file_copy(post, target) }}
{% endfor %}