ตัวช่วยสำหรับ Hugo เพื่อจัดเตรียมการประมวลผลไฟล์ล่วงหน้า
จัดเตรียมตัวประมวลผลล่วงหน้าที่ยืดหยุ่นสำหรับ Hugo เนื่องจากเราไม่สามารถรับประเภทไฟล์บางประเภทที่รองรับสำหรับตัวจัดการ/ตัวประมวลผลภายนอกในโค้ดหลักของ Hugo
มีวัตถุประสงค์เพื่อช่วยในการประมวลผลล่วงหน้าใดๆ ที่ต้องการสำหรับการเผยแพร่ไฟล์ เช่น:
ไฟล์การกำหนดค่าใช้เพื่อกำหนดการประมวลผล ตามค่าเริ่มต้น ชื่อไฟล์กำหนดค่าคือ .hugo-preproc.yaml (หรือ .toml หรือ .json )
คุณสามารถระบุไฟล์ปรับแต่งบนบรรทัดคำสั่งด้วยตัวเลือก -c / --config
ดำเนินการคำสั่งและการประมวลผลเกิดขึ้นตามการกำหนดค่า
ไฟล์นี้มีคีย์หลักสองคีย์: git และ processors ดังตัวอย่างนี้:
git :
- path : path/to/repo
processors :
- mode : head | each | all
file : path/to/output/{{ .Commit.Hash }}
template : Entry {{ .<field> }}
exec :
- path : path/to/top/directory
pattern : " *.md "
command : echo {{ . }} คีย์ git เป็นวัตถุอาร์เรย์โดยแต่ละองค์ประกอบอาร์เรย์มีการกำหนดดังนี้:
path - กำหนดเส้นทางไปยัง repo git (ค่าเริ่มต้น: ".")processors - อาร์เรย์ของตัวจัดการบันทึก gitmode - ค่าของ head (เฉพาะ head commit) each (แต่ละรายการบันทึกที่ส่งผ่านโปรเซสเซอร์ ติดต่อกัน) หรือ all (รายการทั้งหมดที่ส่งผ่านโปรเซสเซอร์)file - ไฟล์ที่จะส่งออก; ประมวลผลเป็นแม่แบบtemplate - เทมเพลตที่ใช้ประมวลผลรายการ/รายการบันทึก git แล้วเขียนลงใน file คีย์ exec เป็นอ็อบเจ็กต์อาร์เรย์ โดยแต่ละองค์ประกอบอาร์เรย์กำหนดไว้ดังนี้:
path - เส้นทางระดับบนสุดที่จะถูกเดินและสแกนหาชื่อไฟล์ที่ตรงกันpattern - รูปแบบที่ใช้จับคู่ชื่อไฟล์ขณะเดินเนื้อหา path แบบวนซ้ำcommand - คำสั่งให้รันบนไฟล์ที่ตรงกัน ค่านี้ได้รับการประมวลผลเป็นเทมเพลต Goรายการอาร์เรย์จะถูกดำเนินการตามลำดับตามลำดับที่กำหนดไว้
เรากำลังใช้ Go Templates เพื่อประมวลผล file และคีย์ template ในตัวจัดการ git แต่ละตัว รวมถึงคีย์ command ในออบเจ็กต์ตัว processors แต่ละตัว
นอกเหนือจากฟังก์ชัน Go Template มาตรฐานแล้ว เรายังเพิ่ม:
fields - structs.Namesreplace - strings.Replace{{replace <input> <search> <replace> <n>}}example.drawiocommand : draw.io --export --output {{replace . ".md" ".svg" -1}} --format svg {{.}}exec : draw.io --export --output example.svg --format svg example.drawiosplit - strings.Split{{split <input> <separator>}}trimsuffix - strings.TrimSuffix{{trimsuffix <input> <trim_string>}} นอกจากนี้ เราได้แมปไลบรารีทั้งหมดของฟังก์ชันเทมเพลต Masterminds/sprig แล้ว สำหรับ hugo-preproc รุ่น 1.x เราจะคงฟังก์ชันเทมเพลตแบบกำหนดเองไว้เหมือนเดิม เมื่อเราย้ายไปที่รุ่น 2.x เราจะเลิกใช้ฟังก์ชันแบบกำหนดเองด้านบนแทน sprig
เราจัดเตรียมอินพุตต่อไปนี้สำหรับตัวจัดการที่กำหนดค่าไว้
ตัวจัดการ git
head และ each
. {
Commit {
Hash string // Hash of the commit object.
Author { // Author is the original author of the commit.
Name string // Name of the Author.
Email string // Email address of the Author.
When time . Time // Date/time of the commit.
}
Committer { // Committer is the one performing the commit,
// might be different from Author.
Name string // Name of the Committer.
Email string // Email address of the Committer.
When time . Time // Date/time of the commit.
}
Message string // Message is the commit message, contains arbitrary text.
TreeHash string // TreeHash is the hash of the root tree of the commit.
ParentHashes [] string // ParentHashes are the hashes of the parent commits of the commit.
PGPSignature string // PGPSignature is the PGP signature of the commit.
}
Stats [] string // Array of strings for files changed and their stats.
} all
. {
// Array of Commits
Commits []{
Commit {
Hash string // Hash of the commit object.
Author string // Author is the original author of the commit.
Name string // Name of the Author.
Email string // Email address of the Author.
When time . Time // Date/time of the commit.
}
Committer { // Committer is the one performing the commit,
// might be different from Author.
Name string // Name of the Committer.
Email string // Email address of the Committer.
When time . Time // Date/time of the commit.
}
Message string // Message is the commit message, contains arbitrary text.
TreeHash string // TreeHash is the hash of the root tree of the commit.
ParentHashes [] string // ParentHashes are the hashes of the parent commits of the commit.
PGPSignature string // PGPSignature is the PGP signature of the commit.
}
Stats [] string // Array of strings for files changed and their stats.
}
// Head Commit
Head {
Commit {
Hash string // Hash of the commit object.
Author string // Author is the original author of the commit.
Name string // Name of the Author.
Email string // Email address of the Author.
When time. Time // Date/time of the commit.
}
Committer { // Committer is the one performing the commit,
// might be different from Author.
Name string // Name of the Committer.
Email string // Email address of the Committer.
When time. Time // Date/time of the commit.
}
Message string // Message is the commit message, contains arbitrary text.
TreeHash string // TreeHash is the hash of the root tree of the commit.
ParentHashes [] string // ParentHashes are the hashes of the parent commits of the commit.
PGPSignature string // PGPSignature is the PGP signature of the commit.
}
Stats [] string // Array of strings for files changed and their stats.
}
} ตัวจัดการผู้ exec
. string // String representing the matched filename,
// including its full path (handler top search path + sub-path to file).