oficina / mapa Public

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

Plain git — no account needed to clone.

Download

Download this file

1package markup
2
3import (
4 "regexp"
5 "strconv"
6 "strings"
7)
8
9// Column layouts (columns and icons spec §1): Pandoc's fenced divs, in
10// the strict form Mapa reads and writes.
11//
12// :::: columns
13//
14// ::: column
15// The left column's blocks.
16//
17// :::
18//
19// ::: column
20// The right column's blocks.
21//
22// :::
23//
24// ::::
25//
26// A layout is recognised only when it is whole (§1.1's six rules).
27// Anything less is today's text, byte for byte and meaning for meaning.
28// Its four kinds of fence line are blocks of their own (§1.4), so the
29// merge, Replace, Settle and block indices keep working on one flat
30// list; the blocks between them know their column.
31
32// The fence kinds.
33const (
34 Columns = "columns"
35 Column = "column"
36 ColumnEnd = "column_end"
37 ColumnsEnd = "columns_end"
38)
39
40// Grammar is the block grammar's version (D17): 2 from the column
41// layout. Every route that posts a block index sends it back.
42const Grammar = 2
43
44var (
45 layoutOpener = regexp.MustCompile(`^(:{4,})[ \t]+columns[ \t]*$`)
46 columnOpener = regexp.MustCompile(`^(:{3,})[ \t]+column[ \t]*$`)
47 fenceLike = regexp.MustCompile(`^[ \t]*:::`)
48)
49
50// FenceLike is the one predicate for a line that looks like a fence:
51// its text, after any spaces and tabs, starts with ":::". The grammar's
52// rule 5, the editor's neutraliser and the importer's all use it.
53func FenceLike(line string) bool { return fenceLike.MatchString(line) }
54
55// IsFence reports whether kind is one of the layout's fence kinds.
56func IsFence(kind string) bool {
57 return kind == Columns || kind == Column || kind == ColumnEnd ||
58 kind == ColumnsEnd
59}
60
61// closes reports whether line is exactly colons colons and blanks.
62func closes(line string, colons int) bool {
63 trimmed := strings.TrimRight(line, " \t")
64 return len(trimmed) == colons && strings.Count(trimmed, ":") == colons
65}
66
67// layout is one recognised layout: its opener and closer lines, and each
68// column's opener and closer lines.
69type layout struct {
70 open, close int
71 columns [][2]int
72}
73
74// layouts finds every whole layout, scanning top to bottom (§1.1, "The
75// scan, exactly"): outside a code fence, at an opener that starts a
76// block, it tries a whole layout; on success the scan resumes after its
77// closer, on failure at the very next line.
78func layouts(lines []string) map[int]layout {
79 found := map[int]layout{}
80 fence := ""
81 for index := 0; index < len(lines); index++ {
82 line := lines[index]
83 if fence != "" {
84 if ClosesFence(line, fence) {
85 fence = ""
86 }
87 continue
88 }
89 if match := fenceLine.FindStringSubmatch(line); match != nil {
90 fence = match[1]
91 continue
92 }
93 if index > 0 && strings.TrimSpace(lines[index-1]) != "" {
94 continue
95 }
96 if whole, ok := readLayout(lines, index); ok {
97 found[index] = whole
98 index = whole.close
99 }
100 }
101 return found
102}
103
104// readLayout reads a whole layout opening at start, by rules 2 to 6.
105func readLayout(lines []string, start int) (layout, bool) {
106 match := layoutOpener.FindStringSubmatch(lines[start])
107 if match == nil {
108 return layout{}, false
109 }
110 outer := len(match[1])
111 whole := layout{open: start}
112 for at := skipBlank(lines, start+1); at < len(lines); {
113 line := lines[at]
114 if closes(line, outer) {
115 ends := at+1 == len(lines) ||
116 strings.TrimSpace(lines[at+1]) == ""
117 count := len(whole.columns)
118 whole.close = at
119 return whole, ends && count >= 2 && count <= 4
120 }
121 inner := columnOpener.FindStringSubmatch(line)
122 if inner == nil || len(inner[1]) >= outer ||
123 len(whole.columns) == 4 {
124 return layout{}, false
125 }
126 end, ok := columnEnd(lines, at+1, len(inner[1]))
127 if !ok {
128 return layout{}, false
129 }
130 whole.columns = append(whole.columns, [2]int{at, end})
131 at = skipBlank(lines, end+1)
132 }
133 return layout{}, false
134}
135
136// columnEnd is the line that closes a column whose fence is colons long,
137// reading from start. Code fences are tracked as Parse tracks them; any
138// other fence-like line, or no closer, is no column (rules 4 and 5).
139func columnEnd(lines []string, start, colons int) (int, bool) {
140 fence := ""
141 for at := start; at < len(lines); at++ {
142 line := lines[at]
143 switch {
144 case fence != "":
145 if ClosesFence(line, fence) {
146 fence = ""
147 }
148 case fenceLine.MatchString(line):
149 fence = fenceLine.FindStringSubmatch(line)[1]
150 case closes(line, colons):
151 return at, true
152 case FenceLike(line):
153 return 0, false
154 }
155 }
156 return 0, false
157}
158
159func skipBlank(lines []string, at int) int {
160 for at < len(lines) && strings.TrimSpace(lines[at]) == "" {
161 at++
162 }
163 return at
164}
165
166// fenceBlock is one fence line as a block.
167func fenceBlock(kind, line string, column, start int) Block {
168 return Block{Kind: kind, Source: line, Column: column, Start: start,
169 End: start + len(line)}
170}
171
172// parseLayout gives a whole layout's blocks: its fences, and each
173// column's lines parsed as a body of their own.
174func parseLayout(lines []string, offsets []int, whole layout) []Block {
175 opener := fenceBlock(Columns, lines[whole.open], 0, offsets[whole.open])
176 opener.Level = len(whole.columns)
177 blocks := []Block{opener}
178 for number, span := range whole.columns {
179 column := number + 1
180 blocks = append(blocks, fenceBlock(Column, lines[span[0]], column,
181 offsets[span[0]]))
182 inside := parseLines(lines, offsets, span[0]+1, span[1], nil)
183 for at := range inside {
184 inside[at].Column = column
185 }
186 blocks = append(blocks, inside...)
187 blocks = append(blocks, fenceBlock(ColumnEnd, lines[span[1]],
188 column, offsets[span[1]]))
189 }
190 return append(blocks, fenceBlock(ColumnsEnd, lines[whole.close], 0,
191 offsets[whole.close]))
192}
193
194// renderLayout draws the layout opening at start (§1.5):
195//
196// <div mapa-columns="2"><div mapa-column>…</div>…</div>
197//
198// each column's blocks drawn as anywhere, and an empty column holding
199// the editor's own empty paragraph. It returns the index after the
200// layout's closer. editor/schema.mjs draws the same; L4 holds them.
201func renderLayout(out *strings.Builder, blocks []Block, start int,
202 resolver Resolver) int {
203 out.WriteString(`<div mapa-columns="` + strconv.Itoa(blocks[start].Level) +
204 `">`)
205 index := start + 1
206 for index < len(blocks) && blocks[index].Kind == Column {
207 end := index + 1
208 for end < len(blocks) && blocks[end].Kind != ColumnEnd {
209 end++
210 }
211 out.WriteString("<div mapa-column>")
212 if end == index+1 {
213 out.WriteString(EmptyParagraph)
214 } else {
215 out.WriteString(string(Render(blocks[index+1:end], resolver)))
216 }
217 out.WriteString("</div>")
218 index = end + 1
219 }
220 out.WriteString("</div>")
221 return index + 1
222}
223