| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "strconv" |
| 6 | "strings" |
| 7 | "unicode" |
| 8 | "unicode/utf8" |
| 9 | |
| 10 | "golang.org/x/text/unicode/norm" |
| 11 | |
| 12 | "amadan.net/oficina/mapa/internal/mapa/markup" |
| 13 | "amadan.net/oficina/mapa/internal/mapa/seal" |
| 14 | ) |
| 15 | |
| 16 | // The shared namespace (§3.3, decisions 6 and 7, ruled): one title means |
| 17 | // one team page for everyone, and only team pages hold titles. A title is |
| 18 | // compared after folding, so "Acme", "acme" and "Ácme" are one title. |
| 19 | |
| 20 | // maxTitle is the longest title, in characters. |
| 21 | const maxTitle = 120 |
| 22 | |
| 23 | // Title refusals. Each is a sentence a person can act on. |
| 24 | var ( |
| 25 | ErrTitleEmpty = errors.New("A page needs a title.") |
| 26 | ErrTitleLong = errors.New("That title is too long: keep it under " + |
| 27 | "120 characters.") |
| 28 | ErrTitleChars = errors.New(`A title can't contain [ ] | # or ^: ` + |
| 29 | `they are link syntax.`) |
| 30 | // ErrTitleDate is §3.4's date reservation: a link whose text is an |
| 31 | // ISO date opens the reader's own daily note, so no page may hold |
| 32 | // one as a title or alias. |
| 33 | ErrTitleDate = errors.New("A date can't be a page title: " + |
| 34 | "[[2026-11-12]] always opens your own daily note for that day.") |
| 35 | ErrTitleTaken = errors.New("A page with that title already exists.") |
| 36 | ) |
| 37 | |
| 38 | // Fold is the namespace's equality: Unicode compatibility decomposition, |
| 39 | // accents dropped, lower case, runs of space made one. Links, prints and |
| 40 | // search all compare titles through it. |
| 41 | func Fold(text string) string { |
| 42 | if folded, ok := foldASCII(text); ok { |
| 43 | return folded |
| 44 | } |
| 45 | var out strings.Builder |
| 46 | space := false |
| 47 | for _, char := range norm.NFKD.String(text) { |
| 48 | switch { |
| 49 | case unicode.Is(unicode.Mn, char): |
| 50 | case unicode.IsSpace(char): |
| 51 | space = out.Len() > 0 |
| 52 | default: |
| 53 | if space { |
| 54 | out.WriteByte(' ') |
| 55 | space = false |
| 56 | } |
| 57 | out.WriteRune(unicode.ToLower(char)) |
| 58 | } |
| 59 | } |
| 60 | return out.String() |
| 61 | } |
| 62 | |
| 63 | // CleanTitle is a title as it is stored: trimmed, inner space collapsed, |
| 64 | // and refused if it cannot be a title. |
| 65 | func CleanTitle(raw string) (string, error) { |
| 66 | title := strings.Join(strings.Fields(raw), " ") |
| 67 | switch { |
| 68 | case title == "": |
| 69 | return "", ErrTitleEmpty |
| 70 | case utf8.RuneCountInString(title) > maxTitle: |
| 71 | return "", ErrTitleLong |
| 72 | case strings.ContainsAny(title, "[]|#^"): |
| 73 | return "", ErrTitleChars |
| 74 | case markup.IsDateShaped(Fold(title)): |
| 75 | // Folded first: "2026-11-12" in full-width digits is |
| 76 | // the same title as 2026-11-12 (review M5). |
| 77 | return "", ErrTitleDate |
| 78 | } |
| 79 | return title, nil |
| 80 | } |
| 81 | |
| 82 | // titlePrint is the durable file's handle on a folded title (§3.1). |
| 83 | func (store *Store) titlePrint(title string) (string, error) { |
| 84 | return store.sealer.Print(seal.PrintTitle, []byte(Fold(title))) |
| 85 | } |
| 86 | |
| 87 | // dailyPrint is (owner, date)'s handle: one daily note per person per day. |
| 88 | func (store *Store) dailyPrint(ownerID int64, date string) (string, error) { |
| 89 | return store.sealer.Print(seal.PrintDaily, |
| 90 | []byte(strconv.FormatInt(ownerID, 10)+"\x00"+date)) |
| 91 | } |
| 92 | |
| 93 | // foldASCII is Fold for text that is all ASCII, which most words are, |
| 94 | // without the Unicode decomposition: lower case, runs of space made one. |
| 95 | func foldASCII(text string) (string, bool) { |
| 96 | clean := true |
| 97 | for index := 0; index < len(text); index++ { |
| 98 | char := text[index] |
| 99 | if char >= utf8.RuneSelf { |
| 100 | return "", false |
| 101 | } |
| 102 | if ('A' <= char && char <= 'Z') || isSpaceByte(char) { |
| 103 | clean = false |
| 104 | } |
| 105 | } |
| 106 | if clean { |
| 107 | return text, true |
| 108 | } |
| 109 | fields := strings.Fields(text) |
| 110 | return strings.ToLower(strings.Join(fields, " ")), true |
| 111 | } |
| 112 | |
| 113 | func isSpaceByte(char byte) bool { |
| 114 | return char == ' ' || char == '\t' || char == '\n' || char == '\r' || |
| 115 | char == '\v' || char == '\f' |
| 116 | } |
| 117 | |