Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 4 additions & 45 deletions pkg/document/sdt.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,38 +190,11 @@ func (sdt *SDT) AddTOCEntry(text string, level int, pageNum int, entryID string)
Runs: []Run{},
}

// 创建内嵌的SDT用于占位符文本
placeholderSDT := &SDT{
Properties: &SDTProperties{
RunPr: &RunProperties{
FontFamily: &FontFamily{ASCII: "Calibri"},
FontSize: &FontSize{Val: "22"},
},
ID: &SDTID{Val: entryID},
Placeholder: &SDTPlaceholder{
DocPart: &DocPart{Val: generatePlaceholderGUID(level)},
},
Color: &SDTColor{Val: "509DF3"},
},
EndPr: &SDTEndPr{
RunPr: &RunProperties{
FontFamily: &FontFamily{ASCII: "Calibri"},
FontSize: &FontSize{Val: "22"},
},
},
Content: &SDTContent{
Elements: []interface{}{
Run{
Text: Text{Content: text},
},
},
},
// 创建标题文本、制表符和页码的Run
textRun := Run{
Text: Text{Content: text},
}

// 将占位符SDT添加到段落中
sdt.Content.Elements = append(sdt.Content.Elements, placeholderSDT)

// 创建包含制表符和页码的文本Run
tabRun := Run{
Text: Text{Content: "\t"},
}
Expand All @@ -230,26 +203,12 @@ func (sdt *SDT) AddTOCEntry(text string, level int, pageNum int, entryID string)
Text: Text{Content: fmt.Sprintf("%d", pageNum)},
}

entryPara.Runs = append(entryPara.Runs, tabRun, pageRun)
entryPara.Runs = append(entryPara.Runs, textRun, tabRun, pageRun)

// 添加段落到SDT内容中
sdt.Content.Elements = append(sdt.Content.Elements, entryPara)
}

// generatePlaceholderGUID 生成占位符GUID
func generatePlaceholderGUID(level int) string {
guids := map[int]string{
1: "{b5fdec38-8301-4b26-9716-d8b31c00c718}",
2: "{a500490c-aaae-4252-8340-aa59729b9870}",
3: "{d7310822-77d9-4e43-95e1-4649f1e215b3}",
}

if guid, exists := guids[level]; exists {
return guid
}
return "{b5fdec38-8301-4b26-9716-d8b31c00c718}" // 默认使用1级
}

// FinalizeTOCSDT 完成目录SDT构建
func (sdt *SDT) FinalizeTOCSDT() {
// 添加书签结束 - 使用已有的BookmarkEnd类型
Expand Down
18 changes: 8 additions & 10 deletions pkg/document/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,12 +556,6 @@ func (te *TemplateEngine) renderLoopsNested(content string, lists map[string][]i
// 渲染循环
if listData, exists := lists[listVar]; exists {
for i, item := range listData {
// 创建循环上下文变量
loopContent := strings.ReplaceAll(blockContent, "{{this}}", te.interfaceToString(item))
loopContent = strings.ReplaceAll(loopContent, "{{@index}}", strconv.Itoa(i))
loopContent = strings.ReplaceAll(loopContent, "{{@first}}", strconv.FormatBool(i == 0))
loopContent = strings.ReplaceAll(loopContent, "{{@last}}", strconv.FormatBool(i == len(listData)-1))

// 如果item是map,处理属性访问
if itemMap, ok := item.(map[string]interface{}); ok {
// 首先处理嵌套的循环(在替换变量之前)
Expand All @@ -576,23 +570,27 @@ func (te *TemplateEngine) renderLoopsNested(content string, lists map[string][]i

// 如果有嵌套列表,递归处理嵌套循环
if len(nestedLists) > 0 {
loopContent = te.renderLoopsNested(loopContent, nestedLists, depth+1)
blockContent = te.renderLoopsNested(blockContent, nestedLists, depth+1)
}

// 然后替换普通变量
for key, value := range itemMap {
placeholder := fmt.Sprintf("{{%s}}", key)
// 只替换非列表类型的值
if _, isList := value.([]interface{}); !isList {
loopContent = strings.ReplaceAll(loopContent, placeholder, te.interfaceToString(value))
blockContent = strings.ReplaceAll(blockContent, placeholder, te.interfaceToString(value))
}
}

// 处理循环内部的条件语句
loopContent = te.renderLoopConditionals(loopContent, itemMap)
blockContent = te.renderLoopConditionals(blockContent, itemMap)
}

result.WriteString(loopContent)
blockContent = strings.ReplaceAll(blockContent, "{{this}}", te.interfaceToString(item))
blockContent = strings.ReplaceAll(blockContent, "{{@index}}", strconv.Itoa(i))
blockContent = strings.ReplaceAll(blockContent, "{{@first}}", strconv.FormatBool(i == 0))
blockContent = strings.ReplaceAll(blockContent, "{{@last}}", strconv.FormatBool(i == len(listData)-1))
result.WriteString(blockContent)
}
}

Expand Down