package docx import ( "archive/zip" "bytes" "encoding/xml" "errors" "io" "slices" "sort" "strconv" "strings" "unicode" "github.com/carlosframework/docs/internal/export" "github.com/carlosframework/docs/internal/fonts" ) // Import reads a .docx into the paragraphs, headings and text runs Docs // can hold, and reports everything it could not bring in and where. // // It is a bounded token walk rather than an unmarshal into structs, // because the text that matters is nested: Google Docs exports a link // as w:p>w:hyperlink>w:r, a table as w:tbl>w:tr>w:tc>w:p, a suggestion // as w:ins or w:del around runs, and some content under w:sdt or // w:smartTag. The reader this replaced bound w:body>w:p>w:r>w:t alone // and dropped all of that without a word, and read as // bold. Every container below is walked for its text; what Docs cannot // represent is flattened or omitted and reported. // // The bounds are this package's, not the XML decoder's: encoding/xml // does not limit depth, and a ZIP header's sizes are claims, so entry // count, the bytes actually decompressed, element depth and the output's // blocks, runs and text are all counted here. Nothing is written to // disk and no relationship is followed, so a link's target, an image's // source or an external template is never even read. Errors carry no // source content. func Import(data []byte) (*Result, error) { z, err := zip.NewReader(bytes.NewReader(data), int64(len(data))) if err != nil { return nil, ErrNotWord } if len(z.File) > maxEntries { return nil, ErrTooLarge } parts := map[string]*zip.File{} for _, f := range z.File { if _, dup := parts[f.Name]; dup { return nil, ErrMalformed } parts[f.Name] = f } doc := parts["word/document.xml"] if doc == nil { return nil, ErrNotWord } budget := maxTotalBytes r := &reader{rep: newReporter()} if f := parts["word/styles.xml"]; f != nil { b, err := readPart(f, &budget) if err != nil { return nil, err } if err := r.readStyles(b); err != nil { return nil, err } } b, err := readPart(doc, &budget) if err != nil { return nil, err } if err := r.readDocument(b); err != nil { return nil, err } // Comments are counted by their references in the text; a comments // part with none (every anchor deleted, say) is still reported. if parts["word/comments.xml"] != nil && r.rep.items[CodeCommentsOmitted] == nil { r.rep.add(CodeCommentsOmitted, 0) } if r.text == 0 { return nil, ErrEmpty } return &Result{Document: r.document(), Report: r.rep.report()}, nil } // The limits. The output's are what internal/core's builder test holds // to its call budget (TestBuildAtTheReadersLimitFitsTheCallTimeout). const ( maxEntries = 1000 maxPartBytes = 32 << 20 maxTotalBytes = 64 << 20 maxDepth = 256 maxBlocks = 5_000 maxRuns = 10_000 maxText = 512 << 10 // maxLocations is how many paragraph numbers one report item keeps; // its Count is still exact. maxLocations = 20 ) // Import's refusals. Each is a whole answer: none of them says anything // about the file's content. var ( // ErrNotWord: not a ZIP, or a ZIP with no word/document.xml. ErrNotWord = errors.New("docx: not a Word document") // ErrMalformed: a part is not well-formed XML, nests past maxDepth, // declares a DTD, or the archive names one entry twice. ErrMalformed = errors.New("docx: the document is damaged") // ErrTooLarge: past one of the limits above. ErrTooLarge = errors.New("docx: the document is too large to import") // ErrEmpty: nothing Docs can hold has any text. ErrEmpty = errors.New("docx: the document has no text to import") ) // Result is what Import read: the document and its report. type Result struct { Document export.Document Report Report } // Report is everything Import could not bring in as it was, in Codes // order. An empty report still never means full fidelity: it means // nothing this reader knows to look for was found. type Report struct { Items []Item `json:"items"` } // Item is one kind of loss: how many times it happened and, where it // has one, the first maxLocations paragraphs (1-based, in the imported // document) it happened at. Closed metadata only: nothing from the file // is ever quoted, not even a face name, which is as arbitrary as text. type Item struct { Code Code `json:"code"` Count int `json:"count"` Paragraphs []int `json:"paragraphs,omitempty"` } // Code names one kind of loss. The set is closed: Codes lists it. type Code string const ( CodeListFlattened Code = "list_flattened" CodeTableFlattened Code = "table_flattened" CodeLinkTextKept Code = "link_text_kept" CodeInsertionKept Code = "insertion_kept" CodeDeletionOmitted Code = "deletion_omitted" CodeTabAsSpace Code = "tab_as_space" CodeLineBreakSplit Code = "line_break_split" CodePageBreakSplit Code = "page_break_split" CodeTitleAsHeading Code = "title_as_heading" CodeSubtitleAsParagraph Code = "subtitle_as_paragraph" CodeSizeDropped Code = "size_dropped" CodeFontSubstituted Code = "font_substituted" CodeFormattingLost Code = "formatting_lost" CodeAlignmentLost Code = "alignment_lost" CodeObjectOmitted Code = "object_omitted" CodeNotesOmitted Code = "notes_omitted" CodeCommentsOmitted Code = "comments_omitted" CodeHeadersOmitted Code = "headers_footers_omitted" ) // Codes is every Code, in the order a report lists them. var Codes = []Code{ CodeTableFlattened, CodeListFlattened, CodeLinkTextKept, CodeInsertionKept, CodeDeletionOmitted, CodeObjectOmitted, CodeNotesOmitted, CodeCommentsOmitted, CodeHeadersOmitted, CodeTitleAsHeading, CodeSubtitleAsParagraph, CodeFontSubstituted, CodeSizeDropped, CodeFormattingLost, CodeAlignmentLost, CodeTabAsSpace, CodeLineBreakSplit, CodePageBreakSplit, } func readPart(f *zip.File, budget *int) ([]byte, error) { rc, err := f.Open() if err != nil { return nil, ErrMalformed } defer rc.Close() limit := min(maxPartBytes, *budget) b, err := io.ReadAll(io.LimitReader(rc, int64(limit)+1)) if err != nil { return nil, ErrMalformed } if len(b) > limit { return nil, ErrTooLarge } *budget -= len(b) return b, nil } // ---- the report type reporter struct { items map[Code]*Item } func newReporter() *reporter { return &reporter{items: map[Code]*Item{}} } // add counts one occurrence of code at paragraph (1-based; 0 for none). func (r *reporter) add(code Code, paragraph int) { it := r.items[code] if it == nil { it = &Item{Code: code} r.items[code] = it } it.Count++ if paragraph > 0 && len(it.Paragraphs) < maxLocations && !slices.Contains(it.Paragraphs, paragraph) { it.Paragraphs = append(it.Paragraphs, paragraph) } } func (r *reporter) report() Report { var out Report for _, c := range Codes { if it := r.items[c]; it != nil { sort.Ints(it.Paragraphs) out.Items = append(out.Items, *it) } } return out } // ---- styles const ( wNS = "http://schemas.openxmlformats.org/wordprocessingml/2006/main" mcNS = "http://schemas.openxmlformats.org/markup-compatibility/2006" ) type style struct { name, basedOn string rp props // the style's own run properties numbered bool aligned bool // centred or right-aligned } // styleKind is what a paragraph style makes of a block. type styleKind struct { heading int title bool subtitle bool } // ---- the walk type block struct { heading int runs []export.Node } // tri is an OOXML toggle as stated somewhere in the cascade: unset, // on, or explicitly off. An explicit off stops inheritance. type tri int8 const ( unset tri = iota on off ) func triOf(b bool) tri { if b { return on } return off } // props are run properties at one level of the cascade: a run's own // w:rPr, a character style, a paragraph style, or the document default. type props struct { bold, italic tri font string // a Word face name, "" for unset size int // half-points, 0 for unset lost bool // underline, strike, colour, highlight, raised, caps rstyle string // a run's character style } // over is p with every property p leaves unset taken from base. Lost // formatting accumulates: a style's colour is as lost as a run's. func (p props) over(base props) props { if p.bold == unset { p.bold = base.bold } if p.italic == unset { p.italic = base.italic } if p.font == "" { p.font = base.font } if p.size == 0 { p.size = base.size } p.lost = p.lost || base.lost return p } type reader struct { dec *xml.Decoder depth int styles map[string]*style // paragraph styles charStyles map[string]*style defaultStyle string base props // w:docDefaults' run properties blocks []block runs, text int rep *reporter tables int headersCounted bool } // next is the one way the walk reads a token, so depth is counted for // every element, skipped ones included. func (r *reader) next() (xml.Token, error) { t, err := r.dec.Token() if err != nil { if errors.Is(err, io.EOF) { return nil, io.EOF } return nil, ErrMalformed } switch t.(type) { case xml.StartElement: r.depth++ if r.depth > maxDepth { return nil, ErrMalformed } case xml.EndElement: r.depth-- case xml.Directive: // A DTD has no place in OOXML, and an entity declaration is the // one way XML grows past the bytes it arrived as. return nil, ErrMalformed } return t, nil } // children calls visit for each child element of the element just // opened and returns at its end. Character data between children is // passed to text, when it is not nil. func (r *reader) children(visit func(xml.StartElement) error, text func([]byte)) error { for { t, err := r.next() if err != nil { if err == io.EOF { return ErrMalformed } return err } switch t := t.(type) { case xml.StartElement: if err := visit(t); err != nil { return err } case xml.EndElement: return nil case xml.CharData: if text != nil { text(t) } } } } // skip consumes the element just opened, counting depth as it goes. func (r *reader) skip() error { return r.children(func(xml.StartElement) error { return r.skip() }, nil) } func isW(e xml.StartElement, local string) bool { return e.Name.Space == wNS && e.Name.Local == local } func attr(e xml.StartElement, local string) (string, bool) { for _, a := range e.Attr { if a.Name.Space == wNS && a.Name.Local == local { return a.Value, true } } return "", false } // toggle reads an OOXML on/off property: present with no w:val, or with // one of the true spellings, is on; "0", "false" and "off" are off. func toggle(e xml.StartElement) bool { v, ok := attr(e, "val") if !ok { return true } switch strings.ToLower(v) { case "0", "false", "off", "none": return false } return true } func newDecoder(b []byte) *xml.Decoder { d := xml.NewDecoder(bytes.NewReader(b)) d.Strict = true return d } // root opens the part's root element, refusing anything before it but // the XML declaration, whitespace and comments. func (r *reader) root(local string) error { for { t, err := r.next() if err != nil { return ErrMalformed } if e, ok := t.(xml.StartElement); ok { if !isW(e, local) { return ErrNotWord } return nil } } } func (r *reader) readStyles(b []byte) error { r.dec = newDecoder(b) r.depth = 0 r.styles = map[string]*style{} r.charStyles = map[string]*style{} if err := r.root("styles"); err != nil { return err } return r.children(func(e xml.StartElement) error { switch { case isW(e, "docDefaults"): return r.children(func(e xml.StartElement) error { if !isW(e, "rPrDefault") { return r.skip() } return r.children(func(e xml.StartElement) error { if !isW(e, "rPr") { return r.skip() } return r.runProps(&r.base) }, nil) }, nil) case isW(e, "style"): kind, _ := attr(e, "type") id, _ := attr(e, "styleId") def, _ := attr(e, "default") s := &style{} err := r.children(func(c xml.StartElement) error { switch { case isW(c, "name"): s.name, _ = attr(c, "val") case isW(c, "basedOn"): s.basedOn, _ = attr(c, "val") case isW(c, "rPr"): return r.runProps(&s.rp) case isW(c, "pPr"): return r.children(func(p xml.StartElement) error { switch { case isW(p, "numPr"): s.numbered = true case isW(p, "jc"): s.aligned = alignedValue(p) } return r.skip() }, nil) } return r.skip() }, nil) if err != nil { return err } switch { case id == "": case kind == "paragraph": r.styles[id] = s if toggle1(def) { r.defaultStyle = id } case kind == "character": r.charStyles[id] = s } return nil } return r.skip() }, nil) } func toggle1(v string) bool { return v == "1" || strings.EqualFold(v, "true") || strings.EqualFold(v, "on") } // chain is a style and its ancestors, nearest first, at most ten deep and // never revisiting one (a basedOn loop is a damaged file, read as far as // it makes sense). func (r *reader) chain(id string) []*style { return chainIn(r.styles, id) } func chainIn(styles map[string]*style, id string) []*style { var out []*style seen := map[string]bool{} for id != "" && !seen[id] && len(out) < 10 { seen[id] = true s := styles[id] if s == nil { break } out = append(out, s) id = s.basedOn } return out } func (r *reader) kind(id string) styleKind { names := []string{strings.ToLower(id)} for _, s := range r.chain(id) { names = append(names, strings.ToLower(strings.TrimSpace(s.name))) } for _, n := range names { compact := strings.ReplaceAll(n, " ", "") if lvl, ok := strings.CutPrefix(compact, "heading"); ok { if n, err := strconv.Atoi(lvl); err == nil && n >= 1 && n <= 6 { return styleKind{heading: n} } } switch compact { case "title": return styleKind{heading: 1, title: true} case "subtitle": return styleKind{subtitle: true} } } return styleKind{} } // inherited is what a run in a paragraph of this style inherits: the // style chain's run properties, nearest first, then the document // default's. func (r *reader) inherited(id string) props { if id == "" { id = r.defaultStyle } var p props for _, s := range r.chain(id) { p = p.over(s.rp) } return p.over(r.base) } // charInherited is a character style chain's run properties. func (r *reader) charInherited(id string) props { var p props for _, s := range chainIn(r.charStyles, id) { p = p.over(s.rp) } return p } func (r *reader) alignedStyle(id string) bool { if id == "" { id = r.defaultStyle } for _, s := range r.chain(id) { if s.aligned { return true } } return false } // alignedValue is whether a w:jc is an alignment Docs does not import: // centred, right, or anything else that is not the start edge or // justification. func alignedValue(e xml.StartElement) bool { switch v, _ := attr(e, "val"); v { case "", "left", "start", "both", "distribute": return false } return true } func (r *reader) numberedStyle(id string) bool { for _, s := range r.chain(id) { if s.numbered { return true } } return false } // ---- document.xml func (r *reader) readDocument(b []byte) error { r.dec = newDecoder(b) r.depth = 0 if err := r.root("document"); err != nil { return err } return r.children(func(e xml.StartElement) error { if isW(e, "body") { return r.blockContent() } return r.skip() }, nil) } // blockContent walks the children of a body, a table cell, or a // block-level content control. func (r *reader) blockContent() error { return r.children(func(e xml.StartElement) error { switch { case isW(e, "p"): return r.paragraph() case isW(e, "tbl"): return r.table() case isW(e, "sdt"): return r.sdt(r.blockContent) case isW(e, "customXml"), isW(e, "ins"), isW(e, "moveTo"): return r.blockContent() case isW(e, "del"), isW(e, "moveFrom"): r.rep.add(CodeDeletionOmitted, len(r.blocks)) return r.skip() case isW(e, "sectPr"): return r.sectPr() case isW(e, "altChunk"): r.rep.add(CodeObjectOmitted, len(r.blocks)) return r.skip() case e.Name.Space == mcNS && e.Name.Local == "AlternateContent": r.rep.add(CodeObjectOmitted, len(r.blocks)) return r.skip() } return r.skip() }, nil) } // sdt walks a content control's w:sdtContent with content, ignoring its // properties. func (r *reader) sdt(content func() error) error { return r.children(func(e xml.StartElement) error { if isW(e, "sdtContent") { return content() } return r.skip() }, nil) } func (r *reader) sectPr() error { return r.children(func(e xml.StartElement) error { if (isW(e, "headerReference") || isW(e, "footerReference")) && !r.headersCounted { r.headersCounted = true r.rep.add(CodeHeadersOmitted, 0) } return r.skip() }, nil) } func (r *reader) table() error { r.tables++ first := len(r.blocks) + 1 err := r.children(func(e xml.StartElement) error { if !isW(e, "tr") { return r.skip() } return r.children(func(e xml.StartElement) error { switch { case isW(e, "tc"): return r.blockContent() case isW(e, "sdt"): return r.sdt(func() error { return r.children(func(e xml.StartElement) error { if isW(e, "tc") { return r.blockContent() } return r.skip() }, nil) }) } return r.skip() }, nil) }, nil) if err != nil { return err } if len(r.blocks) >= first { r.rep.add(CodeTableFlattened, first) } else { r.rep.add(CodeTableFlattened, 0) } return nil } // para is the paragraph being read: its style, and the run properties // its runs inherit. type para struct { style string kind styleKind inh props // what its runs inherit listMarked bool jcSet bool // the paragraph states its own alignment } func (r *reader) newBlock(p *para) error { if len(r.blocks) >= maxBlocks { return ErrTooLarge } r.blocks = append(r.blocks, block{heading: p.kind.heading}) return nil } func (r *reader) here() int { return len(r.blocks) } func (r *reader) paragraph() error { p := ¶{} // The block exists from the paragraph's start, so a report line // about anything inside it can name it; pPr, which always comes // first, then settles what kind of block it is. if err := r.newBlock(p); err != nil { return err } first := true err := r.children(func(e xml.StartElement) error { if first { first = false if isW(e, "pPr") { if err := r.paragraphProps(p); err != nil { return err } r.settle(p) return nil } r.settle(p) } return r.inline(p, e) }, nil) if err != nil { return err } if first { r.settle(p) } return nil } // settle applies the paragraph's style once pPr has been read. func (r *reader) settle(p *para) { p.kind = r.kind(p.style) p.inh = r.inherited(p.style) if p.kind.heading > 0 { // A heading looks like its level in Docs: the heading style's // own size, weight, slope and colour are the level's here, and // the import page says so. Only its face, and what its runs // state themselves, are carried. p.inh = props{font: p.inh.font} } else if !p.jcSet && r.alignedStyle(p.style) { r.rep.add(CodeAlignmentLost, r.here()) } r.blocks[len(r.blocks)-1].heading = p.kind.heading switch { case p.kind.title: r.rep.add(CodeTitleAsHeading, r.here()) case p.kind.subtitle: r.rep.add(CodeSubtitleAsParagraph, r.here()) } if !p.listMarked && r.numberedStyle(p.style) { r.rep.add(CodeListFlattened, r.here()) } } func (r *reader) paragraphProps(p *para) error { return r.children(func(e xml.StartElement) error { switch { case isW(e, "pStyle"): p.style, _ = attr(e, "val") case isW(e, "numPr"): numbered := true err := r.children(func(c xml.StartElement) error { if isW(c, "numId") { if v, _ := attr(c, "val"); v == "0" { numbered = false } } return r.skip() }, nil) if err != nil { return err } if numbered { p.listMarked = true r.rep.add(CodeListFlattened, r.here()) } return nil case isW(e, "jc"): p.jcSet = true if alignedValue(e) { r.rep.add(CodeAlignmentLost, r.here()) } case isW(e, "sectPr"): return r.sectPr() } return r.skip() }, nil) } // inline walks one child of a paragraph or of anything inside one that // holds runs: links, suggestions, content controls, smart tags, fields. func (r *reader) inline(p *para, e xml.StartElement) error { walk := func() error { return r.children(func(c xml.StartElement) error { return r.inline(p, c) }, nil) } switch { case isW(e, "r"): return r.run(p) case isW(e, "hyperlink"): // Only the visible text: r:id and w:anchor are never read, so no // target from the file reaches anything. r.rep.add(CodeLinkTextKept, r.here()) return walk() case isW(e, "ins"), isW(e, "moveTo"): r.rep.add(CodeInsertionKept, r.here()) return walk() case isW(e, "del"), isW(e, "moveFrom"): r.rep.add(CodeDeletionOmitted, r.here()) return r.skip() case isW(e, "smartTag"), isW(e, "customXml"), isW(e, "fldSimple"), isW(e, "dir"), isW(e, "bdo"): return walk() case isW(e, "sdt"): return r.sdt(walk) case e.Name.Space == mcNS && e.Name.Local == "AlternateContent", e.Name.Local == "oMath" || e.Name.Local == "oMathPara": r.rep.add(CodeObjectOmitted, r.here()) return r.skip() } return r.skip() } func (r *reader) run(p *para) error { rp := props{} return r.children(func(e xml.StartElement) error { switch { case isW(e, "rPr"): return r.runProps(&rp) case isW(e, "t"): var b strings.Builder err := r.children(func(xml.StartElement) error { return r.skip() }, func(c []byte) { b.Write(c) }) if err != nil { return err } return r.emit(p, rp, b.String()) case isW(e, "tab"), isW(e, "ptab"): r.rep.add(CodeTabAsSpace, r.here()) if err := r.emit(p, rp, " "); err != nil { return err } case isW(e, "br"), isW(e, "cr"): code := CodeLineBreakSplit if v, _ := attr(e, "type"); v == "page" || v == "column" { code = CodePageBreakSplit } if err := r.newBlock(p); err != nil { return err } r.rep.add(code, r.here()) case isW(e, "noBreakHyphen"): if err := r.emit(p, rp, "‑"); err != nil { return err } case isW(e, "drawing"), isW(e, "pict"), isW(e, "object"), isW(e, "sym"), isW(e, "ruby"), e.Name.Space == mcNS && e.Name.Local == "AlternateContent": r.rep.add(CodeObjectOmitted, r.here()) case isW(e, "footnoteReference"), isW(e, "endnoteReference"): r.rep.add(CodeNotesOmitted, r.here()) case isW(e, "commentReference"): r.rep.add(CodeCommentsOmitted, r.here()) } // delText, instrText, fldChar, softHyphen, lastRenderedPageBreak // and anything unknown carry no visible text of their own. return r.skip() }, nil) } func (r *reader) runProps(p *props) error { return r.children(func(e xml.StartElement) error { switch { case isW(e, "b"): p.bold = triOf(toggle(e)) case isW(e, "i"): p.italic = triOf(toggle(e)) case isW(e, "rStyle"): p.rstyle, _ = attr(e, "val") case isW(e, "rFonts"): if v, ok := attr(e, "ascii"); ok && v != "" { p.font = v } else if v, ok := attr(e, "hAnsi"); ok && v != "" { p.font = v } case isW(e, "sz"): if v, ok := attr(e, "val"); ok { if n, err := strconv.Atoi(v); err == nil && n > 0 { p.size = n } } case isW(e, "u"): if v, _ := attr(e, "val"); v != "none" { p.lost = true } case isW(e, "strike"), isW(e, "dstrike"), isW(e, "caps"), isW(e, "smallCaps"): if toggle(e) { p.lost = true } case isW(e, "color"): if v, _ := attr(e, "val"); !strings.EqualFold(v, "auto") && v != "000000" && v != "" { p.lost = true } case isW(e, "highlight"): if v, _ := attr(e, "val"); v != "none" && v != "" { p.lost = true } case isW(e, "vertAlign"): if v, _ := attr(e, "val"); v == "superscript" || v == "subscript" { p.lost = true } } return r.skip() }, nil) } // clean makes a run's text into what a run can hold: a tab or a line // ending inside w:t becomes a space, and any other control character // goes. func (r *reader) clean(s string) string { if strings.IndexFunc(s, unicode.IsControl) < 0 { return s } var b strings.Builder for _, c := range s { switch { case c == '\t': r.rep.add(CodeTabAsSpace, r.here()) b.WriteByte(' ') case c == '\n' || c == '\r': b.WriteByte(' ') case unicode.IsControl(c): default: b.WriteRune(c) } } return b.String() } func (r *reader) emit(p *para, rp props, s string) error { s = r.clean(s) if s == "" { return nil } r.runs++ r.text += len(s) if r.runs > maxRuns || r.text > maxText { return ErrTooLarge } run := export.Node{Type: "text", Text: s} // The cascade: the run's own properties, its character style, its // paragraph's style, the document default. rp = rp.over(r.charInherited(rp.rstyle)).over(p.inh) face := rp.font if face != "" { if f, ok := fonts.ByDocxName(face); ok { if f != fonts.Default { run.Marks = append(run.Marks, export.Mark{Type: f.Mark()}) } } else { r.rep.add(CodeFontSubstituted, r.here()) } } size := rp.size if size != 0 && size != export.BodySizePt*2 { if mark, ok := sizeMark(size); ok { run.Marks = append(run.Marks, export.Mark{Type: mark}) } else { r.rep.add(CodeSizeDropped, r.here()) } } if rp.bold == on { run.Marks = append(run.Marks, export.Mark{Type: "strong"}) } if rp.italic == on { run.Marks = append(run.Marks, export.Mark{Type: "em"}) } if rp.lost { r.rep.add(CodeFormattingLost, r.here()) } b := &r.blocks[len(r.blocks)-1] b.runs = append(b.runs, run) return nil } // sizeMark is the point-ladder mark for a size in half-points. The ladder // alone: the retired four-step names are never written. func sizeMark(half int) (string, bool) { for _, mark := range export.SizeMarks() { if !strings.HasPrefix(mark, "size_") { continue } if _, err := strconv.Atoi(strings.TrimPrefix(mark, "size_")); err != nil { continue } if pt, ok := export.SizePt(mark); ok && float64(half) == pt*2 { return mark, true } } return "", false } func (r *reader) document() export.Document { doc := export.Document{Root: export.Node{Type: "doc"}} for _, b := range r.blocks { n := export.Node{Type: "paragraph"} if b.heading > 0 { n = export.Node{Type: "heading", Attrs: export.Attrs{Level: b.heading}} } n.Content = b.runs doc.Root.Content = append(doc.Root.Content, n) } return doc }