mirror of
https://github.com/SphericalKat/medium.rip.git
synced 2026-10-21 01:34:22 +00:00
feat: add algorithm to parse ranges with markup
Signed-off-by: Sphericalkat <amolele@gmail.com>
This commit is contained in:
1 parent
848cd514d0
commit
4b8607b12a
16 files changed
+814
-13
No files matched your search
@@ -0,0 +1,86 @@
|
||||
package converters
|
||||
|
||||
import (
|
||||
"sort"
|
||||
|
||||
"github.com/medium.rip/pkg/entities"
|
||||
)
|
||||
|
||||
type RangeWithMarkup struct {
|
||||
Range []int
|
||||
Markups []entities.Markup
|
||||
}
|
||||
|
||||
func unique(intSlice []int) []int {
|
||||
keys := make(map[int]bool)
|
||||
list := []int{}
|
||||
for _, entry := range intSlice {
|
||||
if _, value := keys[entry]; !value {
|
||||
keys[entry] = true
|
||||
list = append(list, entry)
|
||||
}
|
||||
}
|
||||
return list
|
||||
}
|
||||
|
||||
func ranges(text string, markups []entities.Markup) []RangeWithMarkup {
|
||||
ranges := make([]RangeWithMarkup, 0)
|
||||
|
||||
// first, get all the borders of the markups
|
||||
markupBoundaries := make([]int, 0)
|
||||
for _, m := range markups {
|
||||
markupBoundaries = append(markupBoundaries, []int{int(m.Start), int(m.End)}...)
|
||||
}
|
||||
|
||||
// include the start and end indexes of the text
|
||||
markupBoundaries = append([]int{0}, markupBoundaries...)
|
||||
markupBoundaries = append(markupBoundaries, len(text))
|
||||
|
||||
// remove duplicates
|
||||
markupBoundaries = unique(markupBoundaries)
|
||||
|
||||
// sort slice
|
||||
sort.Slice(markupBoundaries, func(i, j int) bool {
|
||||
return markupBoundaries[i] < markupBoundaries[j]
|
||||
})
|
||||
|
||||
// attach markup to every range
|
||||
for i := 0; i < len(markupBoundaries)-1; i++ {
|
||||
start := markupBoundaries[i]
|
||||
end := markupBoundaries[i+1]
|
||||
|
||||
// check if this markup is covered by the range
|
||||
coveredMarkups := make([]entities.Markup, 0)
|
||||
for _, m := range markups {
|
||||
if (int(m.Start) >= start && int(m.Start) < end) || (int(m.End - 1) >= start && int(m.End - 1) < end) {
|
||||
coveredMarkups = append(coveredMarkups, m)
|
||||
}
|
||||
}
|
||||
|
||||
// append the range
|
||||
ranges = append(ranges, RangeWithMarkup{
|
||||
Range: []int{start, end},
|
||||
Markups: coveredMarkups,
|
||||
})
|
||||
}
|
||||
|
||||
return ranges
|
||||
}
|
||||
|
||||
func Convert(text string, markups []entities.Markup) {
|
||||
// for _, m := range markups {
|
||||
// switch m.Type {
|
||||
// case "A":
|
||||
// if m.Href != nil {
|
||||
|
||||
// } else if {
|
||||
// m.UserID != nil {
|
||||
|
||||
// }
|
||||
// }
|
||||
// case "CODE":
|
||||
// case "EM":
|
||||
// case "STRONG":
|
||||
// }
|
||||
// }
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package converters
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/medium.rip/pkg/entities"
|
||||
)
|
||||
|
||||
func TestRanges(t *testing.T) {
|
||||
ranges := ranges("strong and emphasized only", []entities.Markup{
|
||||
{
|
||||
Type: "STRONG",
|
||||
Start: 0,
|
||||
End: 10,
|
||||
},
|
||||
{
|
||||
Type: "EM",
|
||||
Start: 7,
|
||||
End: 21,
|
||||
},
|
||||
})
|
||||
|
||||
if len(ranges) != 4 {
|
||||
t.Errorf("Expected 4 ranges, got %d", len(ranges))
|
||||
}
|
||||
|
||||
if ranges[0].Range[0] != 0 || ranges[0].Range[1] != 7 {
|
||||
t.Errorf("Expected range to be [0, 7], got %v", ranges[0].Range)
|
||||
}
|
||||
|
||||
if ranges[0].Markups[0].Type != "STRONG" {
|
||||
t.Errorf("Expected markup to be STRONG, got %s", ranges[0].Markups[0].Type)
|
||||
}
|
||||
|
||||
if ranges[1].Range[0] != 7 || ranges[1].Range[1] != 10 {
|
||||
t.Errorf("Expected range to be [7, 10], got %v", ranges[1].Range)
|
||||
}
|
||||
|
||||
if ranges[1].Markups[0].Type != "STRONG" {
|
||||
t.Errorf("Expected markup to be STRONG, got %s", ranges[1].Markups[0].Type)
|
||||
}
|
||||
|
||||
if ranges[2].Range[0] != 10 || ranges[2].Range[1] != 21 {
|
||||
t.Errorf("Expected range to be [10, 21], got %v", ranges[2].Range)
|
||||
}
|
||||
|
||||
if ranges[2].Markups[0].Type != "EM" {
|
||||
t.Errorf("Expected markup to be EM, got %s", ranges[2].Markups[0].Type)
|
||||
}
|
||||
|
||||
if ranges[3].Range[0] != 21 || ranges[3].Range[1] != 26 {
|
||||
t.Errorf("Expected range to be [21, 26], got %v", ranges[3].Range)
|
||||
}
|
||||
|
||||
if len(ranges[3].Markups) != 0 {
|
||||
t.Errorf("Expected markup to be empty, got %v", ranges[3].Markups)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user