feat: refactor some shit

This commit is contained in:
Max Richter
2025-08-17 00:46:45 +02:00
parent 43644c4f40
commit cc8f967f07
20 changed files with 459 additions and 209 deletions

View File

@@ -3,12 +3,13 @@ package parser
import (
"math"
"git.max-richter.dev/max/marka/parser/blocks"
"github.com/agext/levenshtein"
)
type MatchBlock struct {
Start, End int
Block Block
Block blocks.TemplateBlock
src *string
}
@@ -22,17 +23,17 @@ func (m MatchBlock) GetContent() string {
// MatchBlocksFuzzy finds anchor positions for all BlockMatching blocks using
// Levenshtein distance (tolerant matching), then returns ONLY the BlockData
// segments as gaps between those anchors.
func MatchBlocksFuzzy(markdown string, blocks []Block, maxDist float64) []MatchBlock {
func MatchBlocksFuzzy(markdown string, templateBlocks []blocks.TemplateBlock, maxDist float64) []MatchBlock {
var out []MatchBlock
var lastIndex = 0
for i, b := range blocks {
if b.Type == BlockMatching {
for i, b := range templateBlocks {
if b.Type == blocks.MatchingBlock {
start, end := FuzzyFind(markdown, lastIndex, b.GetContent(), 0.3)
if end != -1 {
if i > 0 {
previousBlock := blocks[i-1]
if previousBlock.Type == BlockData {
previousBlock := templateBlocks[i-1]
if previousBlock.Type == blocks.DataBlock {
out = append(out, MatchBlock{
Start: lastIndex,
End: start,
@@ -47,8 +48,8 @@ func MatchBlocksFuzzy(markdown string, blocks []Block, maxDist float64) []MatchB
}
// Handle the last block
lastBlock := blocks[len(blocks)-1]
if lastBlock.Type == BlockData {
lastBlock := templateBlocks[len(templateBlocks)-1]
if lastBlock.Type == blocks.DataBlock {
out = append(out, MatchBlock{
Start: lastIndex,
End: len(markdown),