|
| 1 | +package tocontenttree |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + |
| 6 | + "github.com/beevik/etree" |
| 7 | +) |
| 8 | + |
| 9 | +type layoutwidth string |
| 10 | + |
| 11 | +func toValidLayoutWidth(w string) layoutwidth { |
| 12 | + switch w { |
| 13 | + case "auto", "in-line", "inset-left", "inset-right", |
| 14 | + "full-bleed", "full-grid", "mid-grid", "full-width": |
| 15 | + return layoutwidth(w) |
| 16 | + default: |
| 17 | + return "full-width" |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +func toValidClipLayoutWidth(w string) layoutwidth { |
| 22 | + switch w { |
| 23 | + case "in-line", "full-grid", "mid-grid": |
| 24 | + return layoutwidth(w) |
| 25 | + default: |
| 26 | + return "in-line" |
| 27 | + } |
| 28 | + |
| 29 | +} |
| 30 | + |
| 31 | +func findChild(el *etree.Element, tag string) *etree.Element { |
| 32 | + for _, ch := range el.ChildElements() { |
| 33 | + if ch.Tag == tag { |
| 34 | + return ch |
| 35 | + } |
| 36 | + if found := findChild(ch, tag); found != nil { |
| 37 | + return found |
| 38 | + } |
| 39 | + } |
| 40 | + return nil |
| 41 | +} |
| 42 | + |
| 43 | +func textContent(el *etree.Element) string { |
| 44 | + var b strings.Builder |
| 45 | + for _, tok := range el.Child { |
| 46 | + switch t := tok.(type) { |
| 47 | + case *etree.CharData: |
| 48 | + b.WriteString(t.Data) |
| 49 | + case *etree.Element: |
| 50 | + b.WriteString(textContent(t)) |
| 51 | + } |
| 52 | + } |
| 53 | + return b.String() |
| 54 | +} |
| 55 | + |
| 56 | +func flattenedChildren(el *etree.Element) []etree.Token { |
| 57 | + out := make([]etree.Token, 0, len(el.Child)) |
| 58 | + for _, tok := range el.Child { |
| 59 | + if d, ok := tok.(*etree.Element); ok && d.Tag == "div" { |
| 60 | + out = append(out, d.Child...) |
| 61 | + } else { |
| 62 | + out = append(out, tok) |
| 63 | + } |
| 64 | + } |
| 65 | + return out |
| 66 | +} |
| 67 | + |
| 68 | +func valueOr(v, fallback string) string { |
| 69 | + if v != "" { |
| 70 | + return v |
| 71 | + } |
| 72 | + return fallback |
| 73 | +} |
| 74 | + |
| 75 | +func attr(el *etree.Element, name string) string { |
| 76 | + return el.SelectAttrValue(name, "") |
| 77 | +} |
| 78 | + |
| 79 | +var contentTypeTemplates = map[string]string{ |
| 80 | + "http://www.ft.com/ontology/content/Article": "/content/{{id}}", |
| 81 | + "http://www.ft.com/ontology/content/ImageSet": "/content/{{id}}", |
| 82 | + "http://www.ft.com/ontology/content/ClipSet": "/content/{{id}}", |
| 83 | + "http://www.ft.com/ontology/content/CustomCodeComponent": "/content/{{id}}", |
| 84 | + "http://www.ft.com/ontology/content/MediaResource": "/content/{{id}}", |
| 85 | + "http://www.ft.com/ontology/content/Video": "/content/{{id}}", |
| 86 | + "http://www.ft.com/ontology/company/PublicCompany": "/organisations/{{id}}", |
| 87 | + "http://www.ft.com/ontology/content/ContentPackage": "/content/{{id}}", |
| 88 | + "http://www.ft.com/ontology/content/Content": "/content/{{id}}", |
| 89 | + "http://www.ft.com/ontology/content/Image": "/content/{{id}}", |
| 90 | + "http://www.ft.com/ontology/content/DynamicContent": "/content/{{id}}", |
| 91 | + "http://www.ft.com/ontology/content/Graphic": "/content/{{id}}", |
| 92 | + "http://www.ft.com/ontology/content/Audio": "/content/{{id}}", |
| 93 | + "http://www.ft.com/ontology/company/Organisation": "/organisations/{{id}}", |
| 94 | +} |
| 95 | + |
| 96 | +func generateUrl(t, id string) string { |
| 97 | + const host = "http://api.ft.com" |
| 98 | + template, ok := contentTypeTemplates[t] |
| 99 | + if !ok { |
| 100 | + return "" |
| 101 | + } |
| 102 | + path := strings.Replace(template, "{{id}}", id, 1) |
| 103 | + return host + path |
| 104 | +} |
0 commit comments