Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
livelycode36 committed Aug 8, 2024
1 parent 5c30022 commit fd13d3e
Showing 1 changed file with 28 additions and 42 deletions.
70 changes: 28 additions & 42 deletions lib/TemplateParser.ahk
Original file line number Diff line number Diff line change
@@ -1,54 +1,40 @@
#Requires AutoHotkey v2.0

/**
* 使用指定的分隔符将字符串分成子字符串数组.并将分隔符补回去
* 例如:{image}{enter}title:{title},以{image}分割,转为数组 => ["{image}","{enter}title:{title}"]
* 使用指定的分隔符将字符串分成子字符串数组
* 例如:{image}123123{image}{image}456456{image},以{image}分割,转为数组 => ["{image}", "123123", "{image}", "{image}", "456456", "{image}"]
*
* @param template 用户模板
* @param identifier 分隔符
*/
TemplateConvertedToTemplates(template, identifier){
if (template == identifier){
templates := [identifier]
return templates
} else {
templates := StrSplit(template, identifier)

For index, value in templates{
; 当{image}在开头 及 末尾时,该项为null,所以它(null等于{images})本身就是{images},不需要补{images}
if (value == ""){
continue
}
TemplateConvertedToTemplates(template, delimiter) {
result := []
searchIndex := 1

; 修正:在模板的最后一项为【正常数据】的情况:
; 最后一项不需要补{image},所以跳过
if (index == templates.Length && value != ""){
continue
}
while (true) {
delimiterIndex := InStr(template, delimiter, false, searchIndex)
; 情况2: 搜索结束
if (delimiterIndex = 0)
break
; 情况1: 搜索到 {image}本身
if (delimiterIndex == searchIndex) {
result.Push(delimiter)
} else {
result.Push(SubStr(template, searchIndex, delimiterIndex - searchIndex))
result.Push(delimiter)
}

; 修正:在模板的最后一项为{image}的情况:
; 因为是以{image}分割,所以最后一项为{image}时,值为null,当最后一项为{image}时,它的上一项也会补为{image},所以 跳过 最后一项的上一项补{image}
if ((index == templates.Length - 1) && (templates[templates.Length] == "")){
continue
}

; 将非{image}的项后,加上{image}。因为是以{image}分割,所以给个数组项的后一项,都是{image},将{imgae}给它补回去
if (value != identifier){
templates.InsertAt(index + 1, identifier)
}
}
; 移动游标
searchIndex := delimiterIndex + StrLen(delimiter)
}

; 修正:当{image}在开头 及 末尾时,该项为null
For index, value in templates{
if (value == "" && index == 1){
templates[1] := identifier
}
return result
}

if (value == "" && index == templates.Length){
templates[templates.Length] := identifier
}
}
; testString := "{image}123123{image}{image}456456{image}"
; delimiterString := "{image}"
; result := TemplateConvertedToTemplates(testString, delimiterString)

return templates
}
}
; ; 打印结果
; for index, item in result
; MsgBox("Item " . index . ": " . item)

0 comments on commit fd13d3e

Please sign in to comment.