Skip to content

Commit

Permalink
WIP Convert Spotlight PA embeds to native
Browse files Browse the repository at this point in the history
  • Loading branch information
earthboundkid committed May 23, 2024
1 parent c8f585d commit a714004
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 4 deletions.
22 changes: 21 additions & 1 deletion internal/iterx/iterx.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
// Package iterx has iteration utilities.
package iterx

import "iter"
import (
"cmp"
"iter"
"slices"
)

// Filter returns a sequence of matching items.
func Filter[T any](seq iter.Seq[T], match func(T) bool) iter.Seq[T] {
Expand Down Expand Up @@ -32,3 +36,19 @@ func First[T any](seq iter.Seq[T]) (v T) {
}
return
}

func Keys[M ~map[K]V, K comparable, V any](m M) iter.Seq[K] {
return func(yield func(K) bool) {
for k := range m {
if !yield(k) {
return
}
}
}
}

func Sorted[T cmp.Ordered](seq iter.Seq[T]) []T {
s := Collect(seq)
slices.Sort(s)
return s
}
47 changes: 45 additions & 2 deletions pkg/almanack/service-gdocs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"errors"
"fmt"
"net/http"
"net/url"
"slices"
"strconv"
"strings"
Expand All @@ -21,6 +22,7 @@ import (
"github.com/spotlightpa/almanack/internal/blocko"
"github.com/spotlightpa/almanack/internal/db"
"github.com/spotlightpa/almanack/internal/gdocs"
"github.com/spotlightpa/almanack/internal/iterx"
"github.com/spotlightpa/almanack/internal/must"
"github.com/spotlightpa/almanack/internal/stringx"
"github.com/spotlightpa/almanack/internal/xhtml"
Expand Down Expand Up @@ -234,7 +236,7 @@ func (svc Services) ProcessGDocsDoc(ctx context.Context, dbDoc db.GDocsDoc) (err
blocko.RemoveMarks(docHTML)

// Warn about fake headings
for n := range xhtml.All(docHTML) {
for n := range xhtml.Children(docHTML) {
// <p> with only b/i/strong/em for a child
if n.DataAtom != atom.P {
continue
Expand Down Expand Up @@ -306,6 +308,46 @@ func removeTail(n *html.Node) {
}
}

func replaceSpotlightEmbeds(s string) string {
n, err := html.Parse(strings.NewReader(s))
if err != nil {
// l := almlog.FromContext(ctx)
// l.WarnContext(ctx, "invalid HTML in embed", "html", s)
return s
}
// data-spl-embed-version="1"
div := xhtml.Select(n, func(n *html.Node) bool {
return n.DataAtom == atom.Div && xhtml.Attr(n, "data-spl-embed-version") == "1"
})
if div == nil {
return s
}
netloc := xhtml.Attr(div, "data-spl-src")
u, err := url.Parse(netloc)
if err != nil {
// l := almlog.FromContext(ctx)
// l.WarnContext(ctx, "invalid URL in embed", "html", s, "url", netloc)
return s
}
tag := strings.Trim(u.Path, "/")
q := u.Query()
var buf strings.Builder
buf.WriteString("{{<")
buf.WriteString(tag)
for _, k := range iterx.Sorted(iterx.Keys(q)) {
vv := q[k]
for _, v := range vv {
buf.WriteString(" ")
buf.WriteString(k)
buf.WriteString("=\"")
buf.WriteString(html.EscapeString(v))
buf.WriteString("\"")
}
}
buf.WriteString(">}}\n")
return buf.String()
}

func (svc Services) replaceImageEmbed(
ctx context.Context,
tbl *html.Node,
Expand Down Expand Up @@ -592,9 +634,10 @@ func fixMarkdownPlaceholders(rawHTML *html.Node) {
case db.PartnerRawEmbedTag, db.PartnerTextTag:
dataEl.Parent.RemoveChild(dataEl)
case db.RawEmbedTag, db.SpotlightRawEmbedOrTextTag:
data := replaceSpotlightEmbeds(embed.Value.(string))
xhtml.ReplaceWith(dataEl, &html.Node{
Type: html.RawNode,
Data: embed.Value.(string),
Data: data,
})
case db.ToCEmbedTag:
container := xhtml.New("div")
Expand Down
29 changes: 28 additions & 1 deletion pkg/almanack/service-gdocs_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package almanack

import (
"context"
"testing"

"github.com/carlmjohnson/be"
"github.com/spotlightpa/almanack/pkg/almlog"
)

func TestImageCAS(t *testing.T) {
Expand All @@ -15,7 +17,6 @@ func TestImageCAS(t *testing.T) {
{"Hello, World!", "image/jpeg", "cas/cpme-4zc8-f4m3-gcdp.jpeg"},
}
for _, tc := range cases {
tc := tc
t.Run("", func(t *testing.T) {
got := makeCASaddress([]byte(tc.body), tc.ct)
be.Equal(t, tc.want, got)
Expand All @@ -30,3 +31,29 @@ func TestImageCAS(t *testing.T) {
}
}
}

func TestReplaceSpotlightEmbeds(t *testing.T) {
cases := []struct {
in, want string
}{
{"", ""},
{"Hello, World!", "Hello, World!"},
{
`<script src="https://www.spotlightpa.org/embed.js" async></script><div data-spl-embed-version="1" data-spl-src="https://www.spotlightpa.org/embeds/cta/"></div>`,
"",
},
{`
<script src="https://www.spotlightpa.org/embed.js" async></script>
<div data-spl-embed-version="1" data-spl-src="https://www.spotlightpa.org/embeds/cta/"></div>
`,
`
`},
}
ctx := context.Background()
almlog.UseDevLogger()
for _, tc := range cases {
t := be.Relaxed(t)
be.Equal(t, tc.want, replaceSpotlightEmbeds(ctx, tc.in))
}
}

0 comments on commit a714004

Please sign in to comment.