// SPDX-License-Identifier: AGPL-3.0-or-later package convert import ( "fmt" "regexp" "strconv" "strings" "golang.org/x/net/html" ) // Side-by-side columns (Packages/editor/src/columns.ts): a "columns" row of // two to four "column" nodes, each with a width in percent or none for an // equal share. Exports to Markdown, Word and PDF put the columns one after // another; HTML keeps them side by side. const maxColumns = 4 // newColumns is a row of columns around these bodies. Empty bodies are left // out; fewer than two left are returned as plain blocks, and anything past // the fourth joins the fourth. func newColumns(bodies [][]Node, widths []float64) []Node { type col struct { body []Node width float64 } var cols []col for i, b := range bodies { if len(b) == 0 { continue } w := 0.0 if i < len(widths) { w = widths[i] } cols = append(cols, col{b, w}) } if len(cols) < 2 { var out []Node for _, c := range cols { out = append(out, c.body...) } return out } if len(cols) > maxColumns { last := &cols[maxColumns-1] for _, c := range cols[maxColumns:] { last.body = append(last.body, c.body...) last.width += c.width } cols = cols[:maxColumns] } // Widths count only when every column has one; they are scaled to 100. total := 0.0 for _, c := range cols { if c.width <= 0 { total = 0 break } total += c.width } row := Node{Type: "columns"} for _, c := range cols { n := Node{Type: "column", Attrs: map[string]any{"width": nil}, Content: c.body} if total > 0 { n.Attrs["width"] = float64(int(c.width*1000/total+0.5)) / 10 } row.Content = append(row.Content, n) } return []Node{row} } // columnWidth is a column's width in percent, 0 when it has none. func columnWidth(n Node) float64 { switch v := n.Attrs["width"].(type) { case float64: if v > 0 && v <= 100 { return v } case string: if f, err := strconv.ParseFloat(v, 64); err == nil && f > 0 && f <= 100 { return f } } return 0 } // writeHTMLColumns writes a row as a flex box that docwell's own import // and editor read back. func writeHTMLColumns(b *strings.Builder, n Node, o Options) { b.WriteString("
\n") for _, c := range n.Content { grow := 1.0 attr := "" if w := columnWidth(c); w > 0 { grow = w attr = fmt.Sprintf(" data-width=\"%s\"", formatNumber(w)) } fmt.Fprintf(b, "
\n", attr, formatNumber(grow)) for _, child := range c.Content { writeHTML(b, child, o) } b.WriteString("
\n") } b.WriteString("
\n") } var cssWidth = regexp.MustCompile(`(?i)(?:^|;)\s*(?:width|flex-basis)\s*:\s*([0-9.]+)%`) // styleWidth is a percentage width from an element's style, 0 without one. func styleWidth(n *html.Node) float64 { m := cssWidth.FindStringSubmatch(attrOf(n, "style")) if m == nil { return 0 } f, _ := strconv.ParseFloat(m[1], 64) return f } // confluenceCellWidth is the share Confluence's page layouts give a cell: // sidebars are narrow, the rest share what is left. func confluenceCellWidth(cell *html.Node) float64 { if hasClass(cell, "aside") || hasClass(cell, "sidebar") { return 30 } return 0 } // readColumns reads a row of columns from HTML: Docwell's own // (data-type="columns"), Notion's (div.column-list > div.column) and // Confluence's page layouts (div.columnLayout > div.cell). ok is false for // any other element. func (r *reader) readColumns(n *html.Node) (out []Node, ok bool) { var isCol func(*html.Node) bool width := styleWidth switch { case attrOf(n, "data-type") == "columns": isCol = func(c *html.Node) bool { return attrOf(c, "data-type") == "column" } width = func(c *html.Node) float64 { if f, err := strconv.ParseFloat(attrOf(c, "data-width"), 64); err == nil { return f } return 0 } case hasClass(n, "column-list"): isCol = func(c *html.Node) bool { return hasClass(c, "column") } case hasClass(n, "columnLayout"): isCol = func(c *html.Node) bool { return hasClass(c, "cell") } width = confluenceCellWidth default: return nil, false } var bodies [][]Node var widths []float64 for c := n.FirstChild; c != nil; c = c.NextSibling { if c.Type != html.ElementNode || !isCol(c) { continue } bodies = append(bodies, r.blocks(c)) widths = append(widths, width(c)) } // Sidebars have a width and the other cells none: they share the rest. if hasClass(n, "columnLayout") { fixed, open := 0.0, 0 for _, w := range widths { if w > 0 { fixed += w } else { open++ } } if fixed > 0 && open > 0 { for i, w := range widths { if w == 0 { widths[i] = max(100-fixed, 10*float64(open)) / float64(open) } } } } return newColumns(bodies, widths), true }