oficina / mapa Public

Clone
git clone https://amadan.net/oficina/mapa

Plain git — no account needed to clone.

Download

Download this file

1package notionmd
2
3import "strings"
4
5// Helpers that cut and join a line's parts at byte offsets of its
6// written text. Only literal parts are ever cut: a generated part is
7// syntax, and is kept whole.
8
9// edgeSpace is Go's ASCII \s, which Mapa's emphasis patterns test: a
10// delimiter needs a non-space beside it.
11const edgeSpace = "\t\n\f\r "
12
13// wrap puts parts in delim, with the whitespace at their edges outside
14// it. Parts that are only whitespace are left as they are.
15func wrap(parts []part, delim string) []part {
16 text := line(parts).text()
17 lead := len(text) - len(strings.TrimLeft(text, edgeSpace))
18 trail := len(text) - len(strings.TrimRight(text, edgeSpace))
19 if lead == len(text) {
20 return parts
21 }
22 before, rest := cut(parts, lead)
23 middle, after := cut(rest, len(text)-lead-trail)
24 out := append(before, fixed(delim))
25 out = append(out, middle...)
26 out = append(out, fixed(delim))
27 return append(out, after...)
28}
29
30// cut splits parts at offset, cutting a literal part in two where the
31// offset falls inside it.
32func cut(parts []part, offset int) ([]part, []part) {
33 var left []part
34 for index, piece := range parts {
35 width := len(piece.source())
36 switch {
37 case offset <= 0:
38 return left, append([]part{}, parts[index:]...)
39 case offset >= width:
40 left = append(left, piece)
41 offset -= width
42 continue
43 }
44 if piece.kind != literal {
45 return left, append([]part{}, parts[index:]...)
46 }
47 head, tail := piece, piece
48 head.text, tail.text = piece.text[:offset], piece.text[offset:]
49 rest := append([]part{tail}, parts[index+1:]...)
50 return append(left, head), rest
51 }
52 return left, nil
53}
54
55// trimTrailing drops whitespace from the end of a line's last literal
56// parts.
57func trimTrailing(each line) line {
58 out := append(line{}, each...)
59 for len(out) > 0 && out[len(out)-1].kind == literal {
60 last := &out[len(out)-1]
61 last.text = strings.TrimRight(last.text, " \t")
62 if last.text != "" {
63 break
64 }
65 out = out[:len(out)-1]
66 }
67 return out
68}
69
70// trimLeading drops whitespace from the start of a line's first literal
71// parts: a list item's and a heading's marker take every space after
72// them.
73func trimLeading(each line) line {
74 out := append(line{}, each...)
75 for len(out) > 0 && out[0].kind == literal {
76 out[0].text = strings.TrimLeft(out[0].text, " \t")
77 if out[0].text != "" {
78 break
79 }
80 out = out[1:]
81 }
82 return out
83}
84
85// mergeLiterals joins adjacent literal parts, so a reading that forms
86// across two runs is seen in one string (§11.4: `[[Acme` in a bold run
87// then `]]` in the next).
88func mergeLiterals(each line) line {
89 var out line
90 for _, piece := range each {
91 if last := len(out) - 1; last >= 0 && piece.kind == literal &&
92 out[last].kind == literal {
93 out[last].text += piece.text
94 continue
95 }
96 out = append(out, piece)
97 }
98 return out
99}
100
101// insert puts text at offset of the line's written text: inside the
102// literal part that holds it, or as a literal of its own at a seam
103// before a generated part.
104func insert(each line, offset int, text string) line {
105 left, right := cut(each, offset)
106 if last := len(left) - 1; last >= 0 && left[last].kind == literal {
107 left[last].text += text
108 return append(left, right...)
109 }
110 if len(right) > 0 && right[0].kind == literal {
111 right[0].text = text + right[0].text
112 return append(left, right...)
113 }
114 out := append(left, lit(text))
115 return append(out, right...)
116}
117
118// joinLines makes one line of several, a space between each: a heading
119// is one line, and so is a table cell.
120func joinLines(lines []line) line {
121 var out line
122 for index, each := range lines {
123 if index > 0 {
124 out = append(out, lit(" "))
125 }
126 out = append(out, each...)
127 }
128 return out
129}
130
131// prefixed puts parts before a line's own.
132func prefixed(each line, before ...part) line {
133 return append(append(line{}, before...), each...)
134}
135