Files
ai_academy/internal/sourceloc/text.go
T
runny 7ec21f35b0 chore: 初始化仓库,导入项目基线代码
Academy v0.8.3 完整代码基线:Go 后端(internal/cmd/cli/mcp-server/docreader)、
Vue 前端(frontend)、数据库迁移(migrations)、部署编排(docker/helm/deploy)与文档。
今日开发的培训推送管理改动随后单独提交。
2026-11-16 18:12:23 +08:00

49 lines
1.1 KiB
Go

package sourceloc
import "academy/internal/types"
// TextBlocks splits a plain-text original (Markdown, TXT) that the parser
// passed through unchanged into paragraph blocks whose text locators point at
// the same rune ranges of the original file.
func TextBlocks(content string) []types.SourceBlock {
var blocks []types.SourceBlock
start := -1 // rune offset where the current paragraph starts
pos := 0
blankRun := 0
emit := func(end int) {
if start >= 0 && end > start {
blocks = append(blocks, types.SourceBlock{
Start: start, End: end,
Locator: types.SourceLocator{Type: types.SourceLocatorText, Mapping: "exact", Start: start, End: end},
})
}
start = -1
}
lineHasText := false
lineStart := 0
for _, r := range content {
switch r {
case '\n':
if !lineHasText {
blankRun++
if blankRun == 1 {
emit(lineStart)
}
} else {
blankRun = 0
}
lineHasText = false
lineStart = pos + 1
case ' ', '\t', '\r':
default:
if !lineHasText && start < 0 {
start = lineStart
}
lineHasText = true
}
pos++
}
emit(pos)
return blocks
}