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
132 changes: 115 additions & 17 deletions content_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,20 @@ import (
)

const (
RootType = "root"
BodyType = "body"
TextType = "text"
BreakType = "break"
ThematicBreakType = "thematic-break"
ParagraphType = "paragraph"
HeadingType = "heading"
StrongType = "strong"
EmphasisType = "emphasis"
StrikethroughType = "strikethrough"
LinkType = "link"
FindOutMoreLinkType = "find-out-more-link"
RootType = "root"
BodyType = "body"
TextType = "text"
BreakType = "break"
ThematicBreakType = "thematic-break"
ParagraphType = "paragraph"
HeadingType = "heading"
StrongType = "strong"
EmphasisType = "emphasis"
StrikethroughType = "strikethrough"
LinkType = "link"
FindOutMoreLinkType = "find-out-more-link"
FindOutMoreLinkChildType = "find-out-more-link-child"

ListType = "list"
ListItemType = "list-item"
BlockquoteType = "blockquote"
Expand Down Expand Up @@ -1325,10 +1327,10 @@ func (n *Link) AppendChild(child Node) error {
}

type FindOutMoreLink struct {
Type string `json:"type"`
Children []*Phrasing `json:"children"`
Title string `json:"title"`
URL string `json:"url"`
Type string `json:"type"`
Children []*FindOutMoreLinkChild `json:"children"`
Title string `json:"title"`
URL string `json:"url"`
}

func (n *FindOutMoreLink) GetType() string {
Expand All @@ -1348,14 +1350,110 @@ func (n *FindOutMoreLink) GetChildren() []Node {
}

func (n *FindOutMoreLink) AppendChild(child Node) error {
p, err := makePhrasing(child)
p, err := makeFindOutMoreLinkChild(child)
if err != nil {
return err
}
n.Children = append(n.Children, p)
return nil
}

type FindOutMoreLinkChild struct {
*Text
*Strong
*Emphasis
}

func (n *FindOutMoreLinkChild) GetType() string {
return FindOutMoreLinkChildType
}

func (n *FindOutMoreLinkChild) GetEmbedded() Node {
if n.Text != nil {
return n.Text
}
if n.Strong != nil {
return n.Strong
}
if n.Emphasis != nil {
return n.Emphasis
}
return nil
}

func (n *FindOutMoreLinkChild) GetChildren() []Node {
if n.Text != nil {
return n.Text.GetChildren()
}
if n.Strong != nil {
return n.Strong.GetChildren()
}
if n.Emphasis != nil {
return n.Emphasis.GetChildren()
}
return nil
}

func (n *FindOutMoreLinkChild) AppendChild(_ Node) error { return ErrCannotHaveChildren }

func (n *FindOutMoreLinkChild) UnmarshalJSON(data []byte) error {
var tn typedNode
if err := json.Unmarshal(data, &tn); err != nil {
return err
}

switch tn.Type {
case TextType:
var v Text
if err := json.Unmarshal(data, &v); err != nil {
return err
}
n.Text = &v
case StrongType:
var v Strong
if err := json.Unmarshal(data, &v); err != nil {
return err
}
n.Strong = &v
case EmphasisType:
var v Emphasis
if err := json.Unmarshal(data, &v); err != nil {
return err
}
n.Emphasis = &v
default:
return fmt.Errorf("failed to unmarshal FindOutMoreLinkChild from %s: %w", data, ErrUnmarshalInvalidNode)
}
return nil
}

func (n *FindOutMoreLinkChild) MarshalJSON() ([]byte, error) {
switch {
case n.Text != nil:
return json.Marshal(n.Text)
case n.Strong != nil:
return json.Marshal(n.Strong)
case n.Emphasis != nil:
return json.Marshal(n.Emphasis)
default:
return []byte(`{}`), nil
}
}

// Build a FindOutMoreLinkChild wrapper for paragraph/FindOutMoreLinkChild-bearing parents.
func makeFindOutMoreLinkChild(n Node) (*FindOutMoreLinkChild, error) {
switch n.GetType() {
case TextType:
return &FindOutMoreLinkChild{Text: n.(*Text)}, nil
case StrongType:
return &FindOutMoreLinkChild{Strong: n.(*Strong)}, nil
case EmphasisType:
return &FindOutMoreLinkChild{Emphasis: n.(*Emphasis)}, nil
default:
return nil, ErrInvalidChildType
}
}

type List struct {
Type string `json:"type"`
Children []*ListItem `json:"children"`
Expand Down
2 changes: 1 addition & 1 deletion libraries/from-bodyxml/go/html_transformers.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ var defaultTransformers = map[string]transformer{
Type: contenttree.FindOutMoreLinkType,
Title: attr(a, "title"),
URL: attr(a, "href"),
Children: []*contenttree.Phrasing{},
Children: []*contenttree.FindOutMoreLinkChild{},
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<body><p>Lorel<a href="http://ft.com/content/8c4ad03c-75cf-4af7-bfd3-a8ebf620a458" title="t1">Link</a><a href="http://ft.com/content/8c4ad03c-75cf-4af7-bfd3-a8ebf620a458" title="t2" data-anchor-style="onward-journey">Link with anchor style tag</a>ipsum</p><p><em><a href="http://ft.com/content/8c4ad03c-75cf-4af7-bfd3-a8ebf620a458" title="t3" data-anchor-style="onward-journey">Emphasis link</a></em></p><p><strong><a href="http://ft.com/content/8c4ad03c-75cf-4af7-bfd3-a8ebf620a458" title="t4" data-anchor-style="onward-journey">Strong link</a></strong></p><p><s><a href="http://ft.com/content/8c4ad03c-75cf-4af7-bfd3-a8ebf620a458" title="t5" data-anchor-style="onward-journey">Strike link</a></s></p><ul><li><a href="http://ft.com/content/8c4ad03c-75cf-4af7-bfd3-a8ebf620a458" title="t6" data-anchor-style="onward-journey">List direct link</a></li><li><p>List para <a href="http://ft.com/content/8c4ad03c-75cf-4af7-bfd3-a8ebf620a458" title="t7" data-anchor-style="onward-journey">List para link</a></p></li></ul><blockquote><a href="http://ft.com/content/8c4ad03c-75cf-4af7-bfd3-a8ebf620a458" title="t8" data-anchor-style="onward-journey">Quote direct link</a><p>Quote para <a href="http://ft.com/content/8c4ad03c-75cf-4af7-bfd3-a8ebf620a458" title="t9" data-anchor-style="onward-journey">Quote para link</a></p></blockquote></body>
<body><p>Lorel<a href="http://ft.com/content/8c4ad03c-75cf-4af7-bfd3-a8ebf620a458" title="t1">Link</a><a href="http://ft.com/content/8c4ad03c-75cf-4af7-bfd3-a8ebf620a458" title="t2" data-anchor-style="onward-journey">Link with anchor style tag</a>ipsum</p><p><em><a href="http://ft.com/content/8c4ad03c-75cf-4af7-bfd3-a8ebf620a458" title="t3" data-anchor-style="onward-journey"><em>Emphasis</em> link</a></em></p><p><strong><a href="http://ft.com/content/8c4ad03c-75cf-4af7-bfd3-a8ebf620a458" title="t4" data-anchor-style="onward-journey"><strong>Strong link</strong></a></strong></p><p><s><a href="http://ft.com/content/8c4ad03c-75cf-4af7-bfd3-a8ebf620a458" title="t5" data-anchor-style="onward-journey">Strike link</a></s></p><ul><li><a href="http://ft.com/content/8c4ad03c-75cf-4af7-bfd3-a8ebf620a458" title="t6" data-anchor-style="onward-journey">List direct link</a></li><li><p>List para <a href="http://ft.com/content/8c4ad03c-75cf-4af7-bfd3-a8ebf620a458" title="t7" data-anchor-style="onward-journey">List para link</a></p></li></ul><blockquote><a href="http://ft.com/content/8c4ad03c-75cf-4af7-bfd3-a8ebf620a458" title="t8" data-anchor-style="onward-journey">Quote direct link</a><p>Quote para <a href="http://ft.com/content/8c4ad03c-75cf-4af7-bfd3-a8ebf620a458" title="t9" data-anchor-style="onward-journey">Quote para link</a></p></blockquote></body>
Loading