| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "regexp" |
| 7 | "strconv" |
| 8 | "strings" |
| 9 | "unicode/utf8" |
| 10 | |
| 11 | "gorm.io/gorm" |
| 12 | |
| 13 | "amadan.net/oficina/mapa/internal/mapa/markup" |
| 14 | "amadan.net/oficina/mapa/internal/mapa/notionmd" |
| 15 | ) |
| 16 | |
| 17 | // Titles (Notion import spec §9.2, §9.3, D12). A Notion title is made to |
| 18 | // fit Mapa's namespace, deterministically, and never takes a title that |
| 19 | // is held or planned for another page. Every comparison is on Fold()ed |
| 20 | // text, as the store's title print is. |
| 21 | |
| 22 | const ( |
| 23 | rootParent = "Notion" |
| 24 | parentBudget = 40 |
| 25 | untitled = "Untitled" |
| 26 | ) |
| 27 | |
| 28 | var hashDigit = regexp.MustCompile(`#([0-9])`) |
| 29 | |
| 30 | // fitTitle is rules 1 to 3: link syntax replaced, empty filled, length |
| 31 | // cut. Date-shaped titles are the caller's (rule 4). |
| 32 | func fitTitle(raw string) string { |
| 33 | title := strings.NewReplacer("[", "(", "]", ")", "|", "/", "^", ""). |
| 34 | Replace(raw) |
| 35 | title = hashDigit.ReplaceAllString(title, "$1") |
| 36 | title = strings.ReplaceAll(title, "#", " ") |
| 37 | title = strings.Join(strings.Fields(title), " ") |
| 38 | if title == "" { |
| 39 | title = untitled |
| 40 | } |
| 41 | return cutTitle(title, maxTitle) |
| 42 | } |
| 43 | |
| 44 | // cutTitle cuts text to at most limit characters, at the last word |
| 45 | // boundary that leaves room for "…", or at a rune when there is none. |
| 46 | func cutTitle(text string, limit int) string { |
| 47 | if utf8.RuneCountInString(text) <= limit { |
| 48 | return text |
| 49 | } |
| 50 | runes := []rune(text) |
| 51 | kept := string(runes[:limit-1]) |
| 52 | if space := strings.LastIndex(kept, " "); space > 0 { |
| 53 | kept = kept[:space] |
| 54 | } |
| 55 | return strings.TrimSpace(kept) + "…" |
| 56 | } |
| 57 | |
| 58 | // candidateAt is a page's index'th title in order of preference: the |
| 59 | // title, then with its parent's title in brackets, then numbered " 2", |
| 60 | // " 3" and on, without end (D12). A date-shaped title starts with its |
| 61 | // parent (§9.3). No candidate is over 120 characters. |
| 62 | func candidateAt(title, parent string, index int) string { |
| 63 | if !markup.IsDateShaped(Fold(title)) { |
| 64 | if index == 0 { |
| 65 | return cutTitle(title, maxTitle) |
| 66 | } |
| 67 | index-- |
| 68 | } |
| 69 | suffix := " (" + cutTitle(parent, parentBudget) + ")" |
| 70 | if index > 0 { |
| 71 | suffix += " " + strconv.Itoa(index+1) |
| 72 | } |
| 73 | room := maxTitle - utf8.RuneCountInString(suffix) |
| 74 | return cutTitle(title, room) + suffix |
| 75 | } |
| 76 | |
| 77 | // candidates is the first count of candidateAt's titles. |
| 78 | func candidates(title, parent string, count int) []string { |
| 79 | out := make([]string, 0, count) |
| 80 | for index := range count { |
| 81 | out = append(out, candidateAt(title, parent, index)) |
| 82 | } |
| 83 | return out |
| 84 | } |
| 85 | |
| 86 | // planner assigns titles across a run's pages. |
| 87 | type planner struct { |
| 88 | tx *gorm.DB |
| 89 | store *Store |
| 90 | planned map[string]bool |
| 91 | } |
| 92 | |
| 93 | // plan gives every page of the run its title, in survey order. A mapped |
| 94 | // page keeps its Mapa title unless Notion's has changed since the |
| 95 | // import last read it; a kept page keeps it whatever Notion did. |
| 96 | func (job *importJob) plan(ctx context.Context, set *importSet) error { |
| 97 | plan := &planner{tx: job.store.database.WithContext(ctx), |
| 98 | store: job.store, planned: map[string]bool{}} |
| 99 | for _, entry := range set.pages { |
| 100 | if entry.place.mapped(entry.row) && !entry.live { |
| 101 | continue // deleted in Mapa: never written, holds no title |
| 102 | } |
| 103 | if entry.title != "" { |
| 104 | plan.planned[Fold(entry.title)] = true |
| 105 | continue |
| 106 | } |
| 107 | if err := plan.assign(entry, set); err != nil { |
| 108 | return err |
| 109 | } |
| 110 | entry.planned = entry.title |
| 111 | } |
| 112 | return nil |
| 113 | } |
| 114 | |
| 115 | func (plan *planner) assign(entry *importEntry, set *importSet) error { |
| 116 | raw := plainRuns(entry.staged.Title) |
| 117 | if entry.live && (raw == entry.place.Title || !untouched(entry)) { |
| 118 | entry.title = entry.current.Title |
| 119 | plan.planned[Fold(entry.title)] = true |
| 120 | return nil |
| 121 | } |
| 122 | title, err := plan.next(entry, set, fitTitle(raw), 0) |
| 123 | entry.title = title |
| 124 | return err |
| 125 | } |
| 126 | |
| 127 | // untouched is a mapped page whose revision is still the one the import |
| 128 | // last wrote (spec §13.2, D8): any later revision, a rename or "Update |
| 129 | // links" included, keeps Mapa's version whole. |
| 130 | func untouched(entry *importEntry) bool { |
| 131 | return entry.live && entry.revision == entry.place.Revision |
| 132 | } |
| 133 | |
| 134 | // plainRuns is runs' text. |
| 135 | func plainRuns(runs []notionmd.Run) string { |
| 136 | var out strings.Builder |
| 137 | for _, run := range runs { |
| 138 | out.WriteString(run.Text) |
| 139 | } |
| 140 | return out.String() |
| 141 | } |
| 142 | |
| 143 | // next is the first candidate after skip that is free and unplanned. |
| 144 | func (plan *planner) next(entry *importEntry, set *importSet, title string, |
| 145 | skip int) (string, error) { |
| 146 | parent := set.parentTitle(entry) |
| 147 | if parent == "" { |
| 148 | parent = rootParent |
| 149 | } |
| 150 | // Unbounded: a workspace with seventy pages called "Weekly sync" |
| 151 | // under one parent numbers them all (Fable H1). Every number past |
| 152 | // the namespace's size is free, so this ends. |
| 153 | for index := skip; ; index++ { |
| 154 | candidate := candidateAt(title, parent, index) |
| 155 | free, err := plan.free(candidate, entry.place.PageID) |
| 156 | if err != nil || free { |
| 157 | plan.planned[Fold(candidate)] = true |
| 158 | return candidate, err |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | // free is a title the namespace would give this page: valid, not |
| 164 | // planned for another, and held by nobody or by the page itself. |
| 165 | func (plan *planner) free(title string, own int64) (bool, error) { |
| 166 | if _, err := CleanTitle(title); err != nil || |
| 167 | plan.planned[Fold(title)] { |
| 168 | return false, nil |
| 169 | } |
| 170 | fingerprint, err := plan.store.titlePrint(title) |
| 171 | if err != nil { |
| 172 | return false, err |
| 173 | } |
| 174 | var held TitlePrint |
| 175 | err = plan.tx.Where("print = ?", fingerprint).First(&held).Error |
| 176 | if errors.Is(err, gorm.ErrRecordNotFound) { |
| 177 | return true, nil |
| 178 | } |
| 179 | return err == nil && own != 0 && held.PageID == own, err |
| 180 | } |
| 181 | |
| 182 | // parentTitle is the title of the page a page was reached from, when |
| 183 | // that page is imported: planned this run, or mapped and live. |
| 184 | func (set *importSet) parentTitle(entry *importEntry) string { |
| 185 | if entry.staged.Parent == "" { |
| 186 | return "" |
| 187 | } |
| 188 | title, state := set.Page(entry.staged.Parent) |
| 189 | if state != notionmd.Live { |
| 190 | return "" |
| 191 | } |
| 192 | return title |
| 193 | } |
| 194 | |