diff --git a/content_tree.go b/content_tree.go index cda3cd7..1dce4fe 100644 --- a/content_tree.go +++ b/content_tree.go @@ -103,6 +103,9 @@ type Node interface { // GetEmbedded returns the embedded node, if applicable. // It is useful for traversing node structs which embed other node structs. GetEmbedded() Node + // GetString returns text contained in the node, but not the associated children. + // Returns empty string when no text is contained in node. + GetString() string // AppendChild attempts to append a child node, returning an error if not allowed. AppendChild(child Node) error } @@ -139,6 +142,10 @@ type BigNumber struct { Number string `json:"number"` } +func (n *BigNumber) GetString() string { + return fmt.Sprintf("%s %s", n.Number, n.Description) +} + func (n *BigNumber) GetType() string { return n.Type } @@ -159,6 +166,10 @@ type Blockquote struct { Data interface{} `json:"data,omitempty"` } +func (n *Blockquote) GetString() string { + return "" +} + func (n *Blockquote) GetType() string { return n.Type } @@ -194,6 +205,10 @@ type BlockquoteChild struct { *Link } +func (n *BlockquoteChild) GetString() string { + return n.GetEmbedded().GetString() +} + func (n *BlockquoteChild) GetType() string { return BlockquoteChildType } @@ -355,6 +370,10 @@ type Body struct { Version float64 `json:"version,omitempty"` } +func (n *Body) GetString() string { + return "" +} + func (n *Body) GetType() string { return n.Type } @@ -408,6 +427,10 @@ type BodyBlock struct { *InNumbers } +func (n *BodyBlock) GetString() string { + return n.GetEmbedded().GetString() +} + func (n *BodyBlock) GetType() string { return BodyBlockType } @@ -807,6 +830,10 @@ type Break struct { Data interface{} `json:"data,omitempty"` } +func (n *Break) GetString() string { + return "" +} + func (n *Break) GetType() string { return n.Type } @@ -827,6 +854,10 @@ type Emphasis struct { Data interface{} `json:"data,omitempty"` } +func (n *Emphasis) GetString() string { + return "" +} + func (n *Emphasis) GetType() string { return n.Type } @@ -864,6 +895,10 @@ type Flourish struct { FragmentIdentifier string `json:"fragmentIdentifier,omitempty"` } +func (n *Flourish) GetString() string { + return n.Description +} + func (n *Flourish) GetType() string { return n.Type } @@ -901,6 +936,10 @@ type Heading struct { FragmentIdentifier string `json:"fragmentIdentifier,omitempty"` } +func (n *Heading) GetString() string { + return "" +} + func (n *Heading) GetType() string { return n.Type } @@ -933,6 +972,10 @@ type ImageSet struct { FragmentIdentifier string `json:"fragmentIdentifier,omitempty"` } +func (n *ImageSet) GetString() string { + return n.FragmentIdentifier +} + func (n *ImageSet) GetType() string { return n.Type } @@ -955,6 +998,10 @@ type Layout struct { LayoutWidth string `json:"layoutWidth,omitempty"` } +func (n *Layout) GetString() string { + return "" +} + func (n *Layout) GetType() string { return n.Type } @@ -986,6 +1033,10 @@ type LayoutChild struct { *LayoutImage } +func (n *LayoutChild) GetString() string { + return n.GetEmbedded().GetString() +} + func (n *LayoutChild) GetType() string { return LayoutSlotChildType } @@ -1086,6 +1137,10 @@ type LayoutImage struct { Picture *Picture `json:"picture,omitempty"` } +func (n *LayoutImage) GetString() string { + return n.Caption +} + func (n *LayoutImage) GetType() string { return n.Type } @@ -1106,6 +1161,10 @@ type LayoutSlot struct { Data interface{} `json:"data,omitempty"` } +func (n *LayoutSlot) GetString() string { + return "" +} + func (n *LayoutSlot) GetType() string { return n.Type } @@ -1137,6 +1196,10 @@ type LayoutSlotChild struct { *LayoutImage } +func (n *LayoutSlotChild) GetString() string { + return n.GetEmbedded().GetString() +} + func (n *LayoutSlotChild) GetType() string { return LayoutSlotChildType } @@ -1236,6 +1299,10 @@ type Link struct { StyleType string `json:"styleType,omitempty"` } +func (n *Link) GetString() string { + return fmt.Sprintf("%s %s", n.Title, n.URL) +} + func (n *Link) GetType() string { return n.Type } @@ -1268,6 +1335,10 @@ type List struct { Ordered bool `json:"ordered"` } +func (n *List) GetString() string { + return "" +} + func (n *List) GetType() string { return n.Type } @@ -1299,6 +1370,10 @@ type ListItem struct { Data interface{} `json:"data,omitempty"` } +func (n *ListItem) GetString() string { + return "" +} + func (n *ListItem) GetType() string { return n.Type } @@ -1334,6 +1409,10 @@ type ListItemChild struct { *Link } +func (n *ListItemChild) GetString() string { + return n.GetEmbedded().GetString() +} + func (n *ListItemChild) GetType() string { return ListItemChildType } @@ -1497,6 +1576,10 @@ type Paragraph struct { Data interface{} `json:"data,omitempty"` } +func (n *Paragraph) GetString() string { + return "" +} + func (n *Paragraph) GetType() string { return n.Type } @@ -1531,6 +1614,10 @@ type Phrasing struct { *Link } +func (n *Phrasing) GetString() string { + return n.GetEmbedded().GetString() +} + func (n *Phrasing) GetType() string { return PhrasingType } @@ -1676,6 +1763,10 @@ type Pullquote struct { Text string `json:"text"` } +func (n *Pullquote) GetString() string { + return n.Text +} + func (n *Pullquote) GetType() string { return n.Type } @@ -1699,6 +1790,10 @@ type Recommended struct { TeaserTitleOverride string `json:"teaserTitleOverride"` } +func (n *Recommended) GetString() string { + return n.Heading +} + func (n *Recommended) GetType() string { return n.Type } @@ -1720,6 +1815,10 @@ type RecommendedList struct { Children []*Recommended `json:"children"` } +func (n *RecommendedList) GetString() string { + return n.Heading +} + func (n *RecommendedList) GetType() string { return n.Type } @@ -1751,6 +1850,10 @@ type ScrollyBlock struct { Theme string `json:"theme,omitempty"` } +func (n *ScrollyBlock) GetString() string { + return "" +} + func (n *ScrollyBlock) GetType() string { return n.Type } @@ -1781,6 +1884,10 @@ type ScrollyCopy struct { Data interface{} `json:"data,omitempty"` } +func (n *ScrollyCopy) GetString() string { + return "" +} + func (n *ScrollyCopy) GetType() string { return n.Type } @@ -1811,6 +1918,10 @@ type ScrollyCopyChild struct { *ScrollyHeading } +func (n *ScrollyCopyChild) GetString() string { + return n.GetEmbedded().GetString() +} + func (n *ScrollyCopyChild) GetType() string { return ScrollyCopyChildType } @@ -1892,6 +2003,10 @@ type ScrollyHeading struct { Level string `json:"level,omitempty"` } +func (n *ScrollyHeading) GetString() string { + return "" +} + func (n *ScrollyHeading) GetType() string { return n.Type } @@ -1917,6 +2032,10 @@ type ScrollyImage struct { Picture *Picture `json:"picture,omitempty"` } +func (n *ScrollyImage) GetString() string { + return "" +} + func (n *ScrollyImage) GetType() string { return n.Type } @@ -1941,6 +2060,10 @@ type ScrollySection struct { Transition string `json:"transition,omitempty"` } +func (n *ScrollySection) GetString() string { + return "" +} + func (n *ScrollySection) GetType() string { return n.Type } @@ -1971,6 +2094,10 @@ type ScrollySectionChild struct { *ScrollyImage } +func (n *ScrollySectionChild) GetString() string { + return n.GetEmbedded().GetString() +} + func (n *ScrollySectionChild) GetType() string { return ScrollySectionChildType } @@ -2051,6 +2178,10 @@ type Strikethrough struct { Data interface{} `json:"data,omitempty"` } +func (n *Strikethrough) GetString() string { + return "" +} + func (n *Strikethrough) GetType() string { return n.Type } @@ -2082,6 +2213,10 @@ type Strong struct { Data interface{} `json:"data,omitempty"` } +func (n *Strong) GetString() string { + return "" +} + func (n *Strong) GetType() string { return n.Type } @@ -2119,6 +2254,10 @@ type Table struct { Stripes bool `json:"stripes,omitempty"` } +func (n *Table) GetString() string { + return "" +} + func (n *Table) GetType() string { return n.Type } @@ -2150,6 +2289,10 @@ type TableChild struct { *TableFooter } +func (n *TableChild) GetString() string { + return n.GetEmbedded().GetString() +} + func (n *TableChild) GetType() string { return TableChildType } @@ -2246,6 +2389,10 @@ type TableBody struct { Data interface{} `json:"data,omitempty"` } +func (n *TableBody) GetString() string { + return "" +} + func (n *TableBody) GetType() string { return n.Type } @@ -2276,6 +2423,10 @@ type TableCaption struct { Data interface{} `json:"data,omitempty"` } +func (n *TableCaption) GetString() string { + return "" +} + func (n *TableCaption) GetType() string { return n.Type } @@ -2307,6 +2458,10 @@ type TableCell struct { Heading bool `json:"heading,omitempty"` } +func (n *TableCell) GetString() string { + return "" +} + func (n *TableCell) GetType() string { return n.Type } @@ -2337,6 +2492,10 @@ type TableFooter struct { Data interface{} `json:"data,omitempty"` } +func (n *TableFooter) GetString() string { + return "" +} + func (n *TableFooter) GetType() string { return n.Type } @@ -2367,6 +2526,10 @@ type TableRow struct { Data interface{} `json:"data,omitempty"` } +func (n *TableRow) GetString() string { + return "" +} + func (n *TableRow) GetType() string { return n.Type } @@ -2397,6 +2560,10 @@ type Text struct { Value string `json:"value,omitempty"` } +func (n *Text) GetString() string { + return n.Value +} + func (n *Text) GetType() string { return n.Type } @@ -2416,6 +2583,10 @@ type ThematicBreak struct { Data interface{} `json:"data,omitempty"` } +func (n *ThematicBreak) GetString() string { + return "" +} + func (n *ThematicBreak) GetType() string { return n.Type } @@ -2437,6 +2608,10 @@ type Tweet struct { ID string `json:"id,omitempty"` } +func (n *Tweet) GetString() string { + return n.HTML +} + func (n *Tweet) GetType() string { return n.Type } @@ -2457,6 +2632,10 @@ type Video struct { ID string `json:"id"` } +func (n *Video) GetString() string { + return "" +} + func (n *Video) GetType() string { return n.Type } @@ -2477,6 +2656,10 @@ type YoutubeVideo struct { URL string `json:"url,omitempty"` } +func (n *YoutubeVideo) GetString() string { + return n.URL +} + func (n *YoutubeVideo) GetType() string { return n.Type } @@ -2502,6 +2685,10 @@ type CustomCodeComponent struct { VersionRange string `json:"versionRange,omitempty"` } +func (n *CustomCodeComponent) GetString() string { + return "" +} + func (n *CustomCodeComponent) GetType() string { return n.Type } @@ -2526,6 +2713,10 @@ type ClipSet struct { Muted bool `json:"muted,omitempty"` } +func (n *ClipSet) GetString() string { + return "" +} + func (n *ClipSet) GetType() string { return n.Type } @@ -2639,6 +2830,10 @@ type Root struct { Data interface{} `json:"data,omitempty"` } +func (n *Root) GetString() string { + return "" +} + func (n *Root) GetType() string { return n.Type } @@ -2693,6 +2888,10 @@ type Timeline struct { Children []*TimelineEvent `json:"children"` } +func (n *Timeline) GetString() string { + return n.Title +} + func (n *Timeline) GetType() string { return n.Type } @@ -2723,6 +2922,10 @@ type TimelineEvent struct { Children []*TimelineEventChild `json:"children"` } +func (n *TimelineEvent) GetString() string { + return n.Title +} + func (n *TimelineEvent) GetType() string { return n.Type } @@ -2753,6 +2956,10 @@ type TimelineEventChild struct { *ImageSet } +func (n *TimelineEventChild) GetString() string { + return n.GetEmbedded().GetString() +} + func (n *TimelineEventChild) GetType() string { return TimelineEventChildType } @@ -2832,6 +3039,10 @@ type Definition struct { Description string `json:"description"` } +func (d *Definition) GetString() string { + return fmt.Sprintf("%s %s", d.Term, d.Description) +} + func (d *Definition) GetType() string { return d.Type } @@ -2852,6 +3063,10 @@ type InNumbers struct { Children []*Definition `json:"children"` } +func (in *InNumbers) GetString() string { + return in.Title +} + func (in *InNumbers) GetType() string { return in.Type } diff --git a/libraries/from-bodyxml/go/html_transformers.go b/libraries/from-bodyxml/go/html_transformers.go index 85608aa..0efb616 100644 --- a/libraries/from-bodyxml/go/html_transformers.go +++ b/libraries/from-bodyxml/go/html_transformers.go @@ -13,6 +13,7 @@ type unknownNode struct { Class string `json:"class,omitempty"` } +func (n *unknownNode) GetString() string { return "" } func (n *unknownNode) GetType() string { return n.Type } func (n *unknownNode) GetEmbedded() contenttree.Node { return nil } func (n *unknownNode) GetChildren() []contenttree.Node { return nil } @@ -32,10 +33,11 @@ type liftChildrenNode struct { Class string `json:"class,omitempty"` } +func (n *liftChildrenNode) GetString() string { return "" } func (n *liftChildrenNode) GetType() string { return n.Type } func (n *liftChildrenNode) GetEmbedded() contenttree.Node { return nil } func (n *liftChildrenNode) GetChildren() []contenttree.Node { return nil } -func (n *liftChildrenNode) AppendChild(child contenttree.Node) error { +func (n *liftChildrenNode) AppendChild(_ contenttree.Node) error { return contenttree.ErrCannotHaveChildren } diff --git a/libraries/to-string/go/transform.go b/libraries/to-string/go/transform.go index 8dc20b1..969ec6b 100644 --- a/libraries/to-string/go/transform.go +++ b/libraries/to-string/go/transform.go @@ -61,97 +61,15 @@ func transformNode(n contenttree.Node) (string, error) { return "", errors.New("nil node") } - switch n.GetType() { - case contenttree.BodyBlockType: - return transformNode(n.GetEmbedded()) - case contenttree.BlockquoteChildType: - return transformNode(n.GetEmbedded()) - case contenttree.LayoutChildType: - return transformNode(n.GetEmbedded()) - case contenttree.LayoutSlotChildType: - return transformNode(n.GetEmbedded()) - case contenttree.ListItemChildType: - return transformNode(n.GetEmbedded()) - case contenttree.PhrasingType: - return transformNode(n.GetEmbedded()) - case contenttree.ScrollyCopyChildType: - return transformNode(n.GetEmbedded()) - case contenttree.ScrollySectionChildType: - return transformNode(n.GetEmbedded()) - case contenttree.TableChildType: - return transformNode(n.GetEmbedded()) - case contenttree.TimelineEventChildType: - return transformNode(n.GetEmbedded()) - case contenttree.RecommendedType: - return "", nil - case contenttree.RecommendedListType: - return "", nil - case contenttree.TextType: - text, ok := n.(*contenttree.Text) - if !ok { - return "", errors.New("failed to parse node to text") - } - - return text.Value, nil - case contenttree.RootType: - root, ok := n.(*contenttree.Root) - if !ok { - return "", errors.New("failed to parse node to root") - } - - return transformNode(root.Body) - case contenttree.BigNumberType: - bigNumber, ok := n.(*contenttree.BigNumber) - if !ok { - return "", errors.New("failed to parse node to bigNumber") - } - - return fmt.Sprintf("%s %s ", bigNumber.Number, bigNumber.Description), nil - case contenttree.PullquoteType: - pq, ok := n.(*contenttree.Pullquote) - if !ok { - return "", errors.New("failed to parse node to Pullquote") - } - - return fmt.Sprintf("%s ", pq.Text), nil - case contenttree.InNumbersType: - in, ok := n.(*contenttree.InNumbers) - if !ok { - return "", errors.New("failed to parse node to InNumbers") - } - resultChildren, err := transformChildren(in.GetChildren()) + result := n.GetString() + if currentElementChildren := n.GetChildren(); currentElementChildren != nil && len(currentElementChildren) > 0 { + childrenString, err := transformChildren(currentElementChildren) if err != nil { return "", err } - - return fmt.Sprintf("%s %s ", in.Title, resultChildren), nil - case contenttree.DefinitionType: - def, ok := n.(*contenttree.Definition) - if !ok { - return "", errors.New("failed to parse node to Definition") - } - - return fmt.Sprintf("%s %s ", def.Term, def.Description), nil - case contenttree.TimelineEventType: - te, ok := n.(*contenttree.TimelineEvent) - if !ok { - return "", errors.New("failed to parse node to text") - } - - resultChildren, err := transformChildren(te.GetChildren()) - if err != nil { - return "", err - } - - return fmt.Sprintf("%s %s ", te.Title, resultChildren), nil - default: - result, err := transformChildren(n.GetChildren()) - if err != nil { - return "", err - } - - return result, nil + result = fmt.Sprintf("%s %s ", result, childrenString) } + return result, nil } func transformChildren(childrenNodes []contenttree.Node) (string, error) {