hugo preproc
v1.2.3
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
key 是一個陣列對象,每個陣列元素定義如下:
path
- 定義 git 儲存庫的路徑(預設值:“.”)processors
- git 日誌處理程序陣列。mode
- head
(僅頭提交)、 each
(連續通過處理器的每個日誌條目)或all
(透過處理器的所有條目)的值。file
- 要輸出的檔案;作為模板處理。template
- 透過該範本處理 git 日誌條目,然後將其寫入file
。 exec
鍵是數組對象,每個數組元素定義如下:
path
- 將遍歷並掃描符合檔案名稱的頂級路徑。pattern
- 用於在遞歸遍歷path
內容時匹配檔案名稱的模式。command
- 在符合檔案上執行的命令;該值作為 Go 模板進行處理。數組條目將按照它們定義的順序串行執行。
我們使用 Go 範本來處理每個git
處理程序中的file
和template
鍵,以及每個processors
物件中的command
鍵。
除了標準的 Go Template 函數之外,我們還加入了:
fields
- structs.Names
replace
- strings.Replace
替換{{replace <input> <search> <replace> <n>}}
example.drawio
command
: draw.io --export --output {{replace . ".md" ".svg" -1}} --format svg {{.}}
exec
的範本輸出: draw.io --export --output example.svg --format svg example.drawio
split
- 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).