oficina / docs Public

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

Plain git — no account needed to clone.

Download

Download this file

1// Package export turns a document's structure into the formats a
2// person can take away with them: Markdown today, PDF alongside it
3// (pdf.go), DOCX when §11.2's import work makes a round trip worth
4// having.
5//
6// The structure arrives as ProseMirror-shaped JSON from the Rust core
7// (core/src/fragment.rs's fragment_json, read through
8// internal/core's Handle.JSON) — not as the HTML the read view
9// renders, because "where does this list end" and "is this run bold"
10// are questions the markup answers only to a parser, and Go's standard
11// library has none.
12package export
13
14import (
15 "encoding/json"
16 "fmt"
17)
18
19// Node is one node of the document tree: an element with children, or
20// a text run with marks. It is the JSON the core emits, decoded — a
21// deliberately loose mirror rather than a validated schema, because
22// the core carries unknown nodes and unknown marks through on purpose
23// (a newer editor build must not lose text through an older
24// exporter), and a decoder that rejected them would undo that.
25type Node struct {
26 Type string `json:"type"`
27 Attrs Attrs `json:"attrs"`
28 Content []Node `json:"content"`
29 Text string `json:"text"`
30 Marks []Mark `json:"marks"`
31}
32
33// Attrs is the small, closed set of node attributes the core emits.
34//
35// Indent is a level, not a measurement: the core emits 1–8 and both
36// renderers turn that into half an inch apiece. Keeping it a level here
37// rather than a width in points is what lets the screen and the PDF
38// disagree about the unit without disagreeing about the document —
39// views/show.html works in inches and this exporter in points, and
40// neither has to know the other's.
41type Attrs struct {
42 Level int `json:"level"`
43 Align string `json:"align"`
44 Indent int `json:"indent"`
45}
46
47// Mark is one mark on a text run. Only the name matters — every mark
48// this build knows is attribute-free by design; see the MARKS table in
49// core/src/fragment.rs for why.
50type Mark struct {
51 Type string `json:"type"`
52}
53
54// Has reports whether a run carries a named mark.
55func (n Node) Has(mark string) bool {
56 for _, m := range n.Marks {
57 if m.Type == mark {
58 return true
59 }
60 }
61 return false
62}
63
64// IsText reports whether this node is a text run rather than an
65// element. Checking the type rather than "does it have children" —
66// an empty paragraph has no children either, and is not text.
67func (n Node) IsText() bool { return n.Type == "text" }
68
69// Document is a whole document: its title (from the database row, not
70// from the CRDT — the title is not part of the content) and the root
71// node the core rendered.
72type Document struct {
73 Title string
74 Root Node
75}
76
77// Parse decodes the core's JSON into a Document.
78func Parse(title, structureJSON string) (Document, error) {
79 var root Node
80 if err := json.Unmarshal([]byte(structureJSON), &root); err != nil {
81 return Document{}, fmt.Errorf("export: decode document structure: %w", err)
82 }
83 if root.Type != "doc" {
84 return Document{}, fmt.Errorf("export: document structure has root type %q, want \"doc\"", root.Type)
85 }
86 return Document{Title: title, Root: root}, nil
87}
88
89// blockKind classifies a node for the writers below. Both Markdown and
90// PDF ask the same questions of the tree, and asking them in one place
91// is what keeps the two exports describing the same document.
92type blockKind int
93
94const (
95 blockUnknown blockKind = iota
96 blockParagraph
97 blockHeading
98 blockBulletList
99 blockOrderedList
100 blockListItem
101 blockQuote
102 blockCode
103)
104
105func kindOf(n Node) blockKind {
106 switch n.Type {
107 case "paragraph":
108 return blockParagraph
109 case "heading":
110 return blockHeading
111 case "bullet_list":
112 return blockBulletList
113 case "ordered_list":
114 return blockOrderedList
115 case "list_item":
116 return blockListItem
117 case "blockquote":
118 return blockQuote
119 case "code_block":
120 return blockCode
121 }
122 return blockUnknown
123}
124
125// headingLevel clamps to 1–6 a second time. The core already clamps
126// (fragment.rs), so this can only fire on a level the core did not
127// produce — but the two are separate binaries' worth of trust apart
128// once the JSON has crossed the ABI, and a "#######" in a Markdown
129// file is not a heading at all.
130// The two alignments that are not the default. core/src/fragment.rs's
131// ALIGNMENTS table is where this vocabulary is defined; these are the
132// same two values, spelled the same way, because they arrive from it
133// over the wire.
134const (
135 alignCentre = "center"
136 alignRight = "right"
137)
138
139// alignOf is the allowlist a second time, on this side of the ABI, and
140// it is exact — no case folding, no trimming, no synonyms.
141//
142// It exists for the same reason headingLevel clamps a level the core
143// has already clamped: by the time the JSON reaches this package it has
144// crossed a binary boundary, and this package's own decoder is
145// deliberately loose (Node carries unknown types and unknown marks
146// through, so a newer editor cannot lose a reader's text). Loose about
147// what it CARRIES, strict about what it ACTS on: an alignment this
148// build does not know lays out as left rather than as a guess, and
149// "CENTER" is not "center" — a normalisation here would be this
150// package quietly deciding a question the schema and the core have both
151// already answered, and would disagree with the read view, which drops
152// it. A document must not paginate one way on screen and another in its
153// own export.
154//
155// Empty means left, which is both "no attribute" and "align=left" —
156// the default spends nothing, exactly as it spends no class in the read
157// view.
158func alignOf(n Node) string {
159 switch n.Attrs.Align {
160 case alignCentre:
161 return alignCentre
162 case alignRight:
163 return alignRight
164 }
165 return ""
166}
167
168func headingLevel(n Node) int {
169 l := n.Attrs.Level
170 if l < 1 {
171 return 1
172 }
173 if l > 6 {
174 return 6
175 }
176 return l
177}
178