nice clean recipes pear.dunkirk.sh
recipes
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat: pretty format fractions

+50
+50
internal/extract/schema/jsonld.go
··· 3 3 import ( 4 4 "encoding/json" 5 5 "fmt" 6 + "math" 6 7 "regexp" 7 8 "strconv" 8 9 "strings" ··· 370 371 371 372 func parseIngredient(text string) models.Ingredient { 372 373 text = strings.TrimSpace(text) 374 + text = prettifyQuantities(text) 373 375 374 376 if m := ingredientRangeRe.FindStringSubmatch(text); len(m) == 4 { 375 377 return models.Ingredient{RawText: text, Quantity: m[1], Unit: m[2], Name: m[3]} ··· 385 387 return models.Ingredient{RawText: text, Quantity: m[1], Name: m[2]} 386 388 } 387 389 return models.Ingredient{RawText: text} 390 + } 391 + 392 + var decimalRe = regexp.MustCompile(`\b(\d+\.\d+)\b`) 393 + 394 + func prettifyQuantities(text string) string { 395 + return decimalRe.ReplaceAllStringFunc(text, func(match string) string { 396 + f, err := strconv.ParseFloat(match, 64) 397 + if err != nil { 398 + return match 399 + } 400 + if pretty := decimalToFraction(f); pretty != "" { 401 + return pretty 402 + } 403 + return match 404 + }) 405 + } 406 + 407 + func decimalToFraction(f float64) string { 408 + // Common cooking fractions 409 + fracs := map[float64]string{ 410 + 0.125: "⅛", 411 + 0.25: "¼", 412 + 0.333: "⅓", 413 + 0.5: "½", 414 + 0.667: "⅔", 415 + 0.75: "¾", 416 + 1.5: "1½", 417 + 2.5: "2½", 418 + 3.5: "3½", 419 + 4.5: "4½", 420 + 0.1667: "⅙", 421 + } 422 + for k, v := range fracs { 423 + if math.Abs(f-k) < 0.01 { 424 + return v 425 + } 426 + } 427 + // Try whole + fraction 428 + whole := math.Floor(f) 429 + frac := f - whole 430 + if whole > 0 && frac > 0.01 { 431 + for k, v := range fracs { 432 + if k < 1 && math.Abs(frac-k) < 0.01 { 433 + return fmt.Sprintf("%d%s", int(whole), v) 434 + } 435 + } 436 + } 437 + return "" 388 438 } 389 439 390 440 func extractInstructions(m map[string]interface{}) []models.Instruction {