Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions content_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ const (
CardType = "card"
CardChildType = "card-child"
InfoBoxType = "info-box"
InfoPairType = "info-pair"

DefinitionType = "definition"
InNumbersType = "in-numbers"
Expand Down Expand Up @@ -411,6 +412,7 @@ type BodyBlock struct {
*InNumbers
*ImagePair
*InfoBox
*InfoPair
}

func (n *BodyBlock) GetType() string {
Expand Down Expand Up @@ -490,6 +492,9 @@ func (n *BodyBlock) GetEmbedded() Node {
if n.InfoBox != nil {
return n.InfoBox
}
if n.InfoPair != nil {
return n.InfoPair
}
return nil
}

Expand Down Expand Up @@ -566,6 +571,9 @@ func (n *BodyBlock) GetChildren() []Node {
if n.InfoBox != nil {
return n.InfoBox.GetChildren()
}
if n.InfoPair != nil {
return n.InfoPair.GetChildren()
}
return nil
}

Expand Down Expand Up @@ -722,6 +730,12 @@ func (n *BodyBlock) UnmarshalJSON(data []byte) error {
return err
}
n.InfoBox = &v
case InfoPairType:
var v InfoPair
if err := json.Unmarshal(data, &v); err != nil {
return err
}
n.InfoPair = &v
default:
return fmt.Errorf("failed to unmarshal BodyBlock from %s: %w", data, ErrUnmarshalInvalidNode)
}
Expand Down Expand Up @@ -778,6 +792,8 @@ func (n *BodyBlock) MarshalJSON() ([]byte, error) {
return json.Marshal(n.ImagePair)
case n.InfoBox != nil:
return json.Marshal(n.InfoBox)
case n.InfoPair != nil:
return json.Marshal(n.InfoPair)
default:
return []byte(`{}`), nil
}
Expand Down Expand Up @@ -834,6 +850,8 @@ func makeBodyBlock(n Node) (*BodyBlock, error) {
return &BodyBlock{ImagePair: n.(*ImagePair)}, nil
case InfoBoxType:
return &BodyBlock{InfoBox: n.(*InfoBox)}, nil
case InfoPairType:
return &BodyBlock{InfoPair: n.(*InfoPair)}, nil
default:
return nil, ErrInvalidChildType
}
Expand Down Expand Up @@ -3103,3 +3121,33 @@ func (n *InfoBox) AppendChild(child Node) error {
n.Children = append(n.Children, child.(*Card))
return nil
}

type InfoPair struct {
Type string `json:"type"`
Title string `json:"title,omitempty"`
Children []*Card `json:"children"`
}

func (n *InfoPair) GetType() string {
return n.Type
}

func (n *InfoPair) GetEmbedded() Node {
return nil
}

func (n *InfoPair) GetChildren() []Node {
result := make([]Node, len(n.Children))
for i, v := range n.Children {
result[i] = v
}
return result
}

func (n *InfoPair) AppendChild(child Node) error {
if child.GetType() != CardType {
return ErrInvalidChildType
}
n.Children = append(n.Children, child.(*Card))
return nil
}
14 changes: 14 additions & 0 deletions libraries/from-bodyxml/go/html_transformers.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,20 @@ var defaultTransformers = map[string]transformer{
Children: []*contenttree.Card{},
}
}
case "info-pair":
{
var title string
if h3Element := findChild(section, "h3"); h3Element != nil {
title = textContent(h3Element)
//extract title but don't treat like a child element
section.RemoveChild(h3Element)
}
return &contenttree.InfoPair{
Type: contenttree.InfoPairType,
Title: title,
Children: []*contenttree.Card{},
}
}
}
return newUnknownNode("", section)
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<body><section data-type="info-pair"><h3>Title</h3><div data-type="card"><h4>Title</h4><p>paragraph</p><ul><li><p>para inside list</p></li></ul><blockquote><p>para inside blockquote</p></blockquote>text<hr></hr><content data-embedded="true" type="http://www.ft.com/ontology/content/ImageSet" id="23925844-4d8d-11ea-0bc6-d44b54b3bebc"></content></div><div data-type="card"><h4>Title</h4><p>paragraph</p></div></section><section data-type="info-pair"><div data-type="card"><content data-embedded="true" type="http://www.ft.com/ontology/content/ImageSet" id="23925844-4d8d-11ea-0bc6-d44b54b3bebc"></content></div><div data-type="card"><h4>Title</h4><p>paragraph</p></div></section></body>
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
{
"type": "root",
"body": {
"type": "body",
"children": [
{
"type": "info-pair",
"title": "Title",
"children": [
{
"type": "card",
"title": "Title",
"children": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "paragraph"
}
]
},
{
"type": "list",
"children": [
{
"type": "list-item",
"children": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "para inside list"
}
]
}
]
}
],
"ordered": false
},
{
"type": "blockquote",
"children": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "para inside blockquote"
}
]
}
]
},
{
"type": "text",
"value": "text"
},
{
"type": "thematic-break"
},
{
"type": "image-set",
"id": "23925844-4d8d-11ea-0bc6-d44b54b3bebc"
}
]
},
{
"type": "card",
"title": "Title",
"children": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "paragraph"
}
]
}
]
}
]
},
{
"type": "info-pair",
"children": [
{
"type": "card",
"children": [
{
"type": "image-set",
"id": "23925844-4d8d-11ea-0bc6-d44b54b3bebc"
}
]
},
{
"type": "card",
"title": "Title",
"children": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "paragraph"
}
]
}
]
}
]
}
],
"version": 1
}
}
Loading