Skip to content

Commit 5fb6658

Browse files
add recommended-list to the Go content-tree model
1 parent b68f3da commit 5fb6658

4 files changed

Lines changed: 126 additions & 11 deletions

File tree

content_tree.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const (
4545
PullquoteType = "pullquote"
4646
ImageSetType = "image-set"
4747
RecommendedType = "recommended"
48+
RecommendedListType = "recommended-list"
4849
TweetType = "tweet"
4950
FlourishType = "flourish"
5051
BigNumberType = "big-number"
@@ -394,6 +395,7 @@ type BodyBlock struct {
394395
*Table
395396
*Text
396397
*Recommended
398+
*RecommendedList
397399
*Tweet
398400
*Video
399401
*YoutubeVideo
@@ -449,6 +451,9 @@ func (n *BodyBlock) GetEmbedded() Node {
449451
if n.Recommended != nil {
450452
return n.Recommended
451453
}
454+
if n.RecommendedList != nil {
455+
return n.RecommendedList
456+
}
452457
if n.Tweet != nil {
453458
return n.Tweet
454459
}
@@ -513,6 +518,9 @@ func (n *BodyBlock) GetChildren() []Node {
513518
if n.Recommended != nil {
514519
return n.Recommended.GetChildren()
515520
}
521+
if n.RecommendedList != nil {
522+
return n.RecommendedList.GetChildren()
523+
}
516524
if n.Tweet != nil {
517525
return n.Tweet.GetChildren()
518526
}
@@ -627,6 +635,12 @@ func (n *BodyBlock) UnmarshalJSON(data []byte) error {
627635
return err
628636
}
629637
n.Recommended = &v
638+
case RecommendedListType:
639+
var v RecommendedList
640+
if err := json.Unmarshal(data, &v); err != nil {
641+
return err
642+
}
643+
n.RecommendedList = &v
630644
case TweetType:
631645
var v Tweet
632646
if err := json.Unmarshal(data, &v); err != nil {
@@ -699,6 +713,8 @@ func (n *BodyBlock) MarshalJSON() ([]byte, error) {
699713
return json.Marshal(n.Text)
700714
case n.Recommended != nil:
701715
return json.Marshal(n.Recommended)
716+
case n.RecommendedList != nil:
717+
return json.Marshal(n.RecommendedList)
702718
case n.Tweet != nil:
703719
return json.Marshal(n.Tweet)
704720
case n.Video != nil:
@@ -747,6 +763,8 @@ func makeBodyBlock(n Node) (*BodyBlock, error) {
747763
return &BodyBlock{Text: n.(*Text)}, nil
748764
case RecommendedType:
749765
return &BodyBlock{Recommended: n.(*Recommended)}, nil
766+
case RecommendedListType:
767+
return &BodyBlock{RecommendedList: n.(*RecommendedList)}, nil
750768
case TweetType:
751769
return &BodyBlock{Tweet: n.(*Tweet)}, nil
752770
case VideoType:
@@ -1674,6 +1692,37 @@ func (n *Recommended) GetChildren() []Node {
16741692

16751693
func (n *Recommended) AppendChild(_ Node) error { return ErrCannotHaveChildren }
16761694

1695+
type RecommendedList struct {
1696+
Type string `json:"type"`
1697+
Data interface{} `json:"data,omitempty"`
1698+
Heading string `json:"heading,omitempty"`
1699+
Children []*Recommended `json:"children"`
1700+
}
1701+
1702+
func (n *RecommendedList) GetType() string {
1703+
return n.Type
1704+
}
1705+
1706+
func (n *RecommendedList) GetEmbedded() Node {
1707+
return nil
1708+
}
1709+
1710+
func (n *RecommendedList) GetChildren() []Node {
1711+
result := make([]Node, len(n.Children))
1712+
for i, v := range n.Children {
1713+
result[i] = v
1714+
}
1715+
return result
1716+
}
1717+
1718+
func (n *RecommendedList) AppendChild(child Node) error {
1719+
if child.GetType() != RecommendedType {
1720+
return ErrInvalidChildType
1721+
}
1722+
n.Children = append(n.Children, child.(*Recommended))
1723+
return nil
1724+
}
1725+
16771726
type ScrollyBlock struct {
16781727
Type string `json:"type"`
16791728
Children []*ScrollySection `json:"children"`

libraries/from-bodyxml/go/html_transformers.go

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -310,24 +310,29 @@ var defaultTransformers = map[string]transformer{
310310
Muted: attr(content, "muted") == "true",
311311
}
312312
},
313-
"recommended": func(rl *etree.Element) contenttree.Node {
314-
id := ""
315-
teaser := ""
316-
if link := findChild(rl, "content"); link != nil {
317-
id = attr(link, "id")
318-
teaser = textContent(link)
319-
}
313+
"recommended": transformRecommended,
314+
"recommended-list": func(rl *etree.Element) contenttree.Node {
320315
heading := findChild(rl, "recommended-title")
321-
return &contenttree.Recommended{
322-
Type: contenttree.RecommendedType,
323-
ID: id,
316+
rl.RemoveChild(heading)
317+
ul := findChild(rl, "ul")
318+
var children []*contenttree.Recommended
319+
if ul != nil {
320+
for _, li := range ul.ChildElements() {
321+
if r := transformRecommended(li).(*contenttree.Recommended); r != nil {
322+
children = append(children, r)
323+
}
324+
}
325+
rl.RemoveChild(ul)
326+
}
327+
return &contenttree.RecommendedList{
328+
Type: contenttree.RecommendedListType,
324329
Heading: func() string {
325330
if heading != nil {
326331
return textContent(heading)
327332
}
328333
return ""
329334
}(),
330-
TeaserTitleOverride: teaser,
335+
Children: children,
331336
}
332337
},
333338
"div": func(div *etree.Element) contenttree.Node {
@@ -375,3 +380,24 @@ var defaultTransformers = map[string]transformer{
375380
return newLiftChildrenNode()
376381
},
377382
}
383+
384+
func transformRecommended(rl *etree.Element) contenttree.Node {
385+
id := ""
386+
teaser := ""
387+
if link := findChild(rl, "content"); link != nil {
388+
id = attr(link, "id")
389+
teaser = textContent(link)
390+
}
391+
heading := findChild(rl, "recommended-title")
392+
return &contenttree.Recommended{
393+
Type: contenttree.RecommendedType,
394+
ID: id,
395+
Heading: func() string {
396+
if heading != nil {
397+
return textContent(heading)
398+
}
399+
return ""
400+
}(),
401+
TeaserTitleOverride: teaser,
402+
}
403+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<body><p>Lorell</p><recommended-list><recommended-title>Recommended</recommended-title><ul><li><content id="70981218-05a4-4685-86e0-a19b7cae7ff9" type="http://www.ft.com/ontology/content/Content">The fight for the future of Chile </content></li></ul></recommended-list><p>Ipsum</p></body>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"type": "root",
3+
"body": {
4+
"type": "body",
5+
"children": [
6+
{
7+
"type": "paragraph",
8+
"children": [
9+
{
10+
"type": "text",
11+
"value": "Lorell"
12+
}
13+
]
14+
},
15+
{
16+
"type": "recommended-list",
17+
"heading": "Recommended",
18+
"children": [
19+
{
20+
"type": "recommended",
21+
"heading": "",
22+
"id": "70981218-05a4-4685-86e0-a19b7cae7ff9",
23+
"teaserTitleOverride": "The fight for the future of Chile "
24+
}
25+
]
26+
},
27+
{
28+
"type": "paragraph",
29+
"children": [
30+
{
31+
"type": "text",
32+
"value": "Ipsum"
33+
}
34+
]
35+
}
36+
],
37+
"version": 1
38+
}
39+
}

0 commit comments

Comments
 (0)