Skip to content

Commit

Permalink
Support HTML attributes in remote assets (#409)
Browse files Browse the repository at this point in the history
* Support HTML attributes in remote assets

* Fix CSS attributes

* Adding coveralls

* Small fix for coveralls

* Help coveralls
  • Loading branch information
gauntface authored Jun 17, 2023
1 parent 379ffbf commit 5199f5c
Show file tree
Hide file tree
Showing 45 changed files with 369 additions and 685 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ on:
branches:
- main
pull_request:
branches:
- main

name: Test
jobs:
Expand All @@ -19,3 +21,9 @@ jobs:
run: npm install
- name: Run Test
run: make test
- name: Coveralls GitHub Action
uses: coverallsapp/github-action@v2
with:
format: golang
file: coverage/cover.out
github-token: ${{ secrets.GITHUB_TOKEN }}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ You will need to install golang to use this tool. Learn more at [go.dev](https:/
Once go is installed, you can install the `htmlassets` tool with:

```bash
go install github.com/gauntface/go-html-asset-manager/v4/cmds/htmlassets@latest
go install github.com/gauntface/go-html-asset-manager/v5/cmds/htmlassets@latest
```

If you'd like to generate image sizes, you can install the `genimgs` tool with:

```bash
go install github.com/gauntface/go-html-asset-manager/v4/cmds/genimgs@latest
go install github.com/gauntface/go-html-asset-manager/v5/cmds/genimgs@latest
```

## Usage
Expand Down
2 changes: 1 addition & 1 deletion assets/assetid/assetid.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"path/filepath"
"strings"

"github.com/gauntface/go-html-asset-manager/v4/assets"
"github.com/gauntface/go-html-asset-manager/v5/assets"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion assets/assetid/assetid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"errors"
"testing"

"github.com/gauntface/go-html-asset-manager/v4/assets"
"github.com/gauntface/go-html-asset-manager/v5/assets"
"github.com/google/go-cmp/cmp"
)

Expand Down
39 changes: 27 additions & 12 deletions assets/assetmanager/assetmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ import (
"sort"
"strings"

"github.com/gauntface/go-html-asset-manager/v4/assets"
"github.com/gauntface/go-html-asset-manager/v4/assets/assetid"
"github.com/gauntface/go-html-asset-manager/v4/utils/files"
"github.com/gauntface/go-html-asset-manager/v4/utils/stringui"
"github.com/gauntface/go-html-asset-manager/v5/assets"
"github.com/gauntface/go-html-asset-manager/v5/assets/assetid"
"github.com/gauntface/go-html-asset-manager/v5/utils/files"
"github.com/gauntface/go-html-asset-manager/v5/utils/stringui"
"golang.org/x/net/html"
)

var (
Expand Down Expand Up @@ -306,7 +307,10 @@ func (l *LocalAsset) URL() (string, error) {
}

return path.Join("/", relPath), nil
}

func (l *LocalAsset) Attributes() []html.Attribute {
return nil
}

func (l *LocalAsset) IsLocal() bool {
Expand All @@ -325,16 +329,18 @@ func (l *LocalAsset) String() string {
}

type RemoteAsset struct {
id string
url string
assetType assets.Type
id string
url string
attributes []html.Attribute
assetType assets.Type
}

func NewRemoteAsset(ID, url string, ty assets.Type) *RemoteAsset {
func NewRemoteAsset(ID, src string, attributes []html.Attribute, ty assets.Type) *RemoteAsset {
return &RemoteAsset{
id: ID,
url: url,
assetType: ty,
id: ID,
url: src,
attributes: attributes,
assetType: ty,
}
}

Expand All @@ -354,6 +360,10 @@ func (r *RemoteAsset) URL() (string, error) {
return r.url, nil
}

func (r *RemoteAsset) Attributes() []html.Attribute {
return r.attributes
}

func (r *RemoteAsset) Contents() (string, error) {
return "", fmt.Errorf("%w for %q", errNoContents, r.url)
}
Expand All @@ -363,7 +373,11 @@ func (r *RemoteAsset) IsLocal() bool {
}

func (r *RemoteAsset) String() string {
return fmt.Sprintf("<Remote Asset: %q>", r.url)
attrs := []string{}
for _, a := range r.attributes {
attrs = append(attrs, fmt.Sprintf("%v=%q", a.Key, a.Val))
}
return fmt.Sprintf("<Remote Asset: %q Attributes: [%v]>", r.url, strings.Join(attrs, ", "))
}

func (r *RemoteAsset) Debug(d string) bool {
Expand Down Expand Up @@ -397,6 +411,7 @@ type Asset interface {
Media() string
URL() (string, error)
Contents() (string, error)
Attributes() []html.Attribute
IsLocal() bool
String() string
Debug(d string) bool
Expand Down
36 changes: 29 additions & 7 deletions assets/assetmanager/assetmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ import (
"reflect"
"testing"

"github.com/gauntface/go-html-asset-manager/v4/assets"
"github.com/gauntface/go-html-asset-manager/v4/assets/assetid"
"github.com/gauntface/go-html-asset-manager/v5/assets"
"github.com/gauntface/go-html-asset-manager/v5/assets/assetid"
"github.com/google/go-cmp/cmp"
"golang.org/x/net/html"
)

var errInjected = errors.New("injected error")
Expand Down Expand Up @@ -1096,25 +1097,46 @@ func TestNewRemoteAsset(t *testing.T) {
description string
id string
url string
attributes []html.Attribute
assetType assets.Type
want *RemoteAsset
}{
{
description: "return new remote asset",
id: "example-id",
url: "http://example.com/example.css",
assetType: assets.InlineCSS,
attributes: []html.Attribute{
{
Key: "example",
Val: "test",
},
{
Key: "example-2",
Val: "another test",
},
},
assetType: assets.InlineCSS,
want: &RemoteAsset{
id: "example-id",
url: "http://example.com/example.css",
id: "example-id",
url: "http://example.com/example.css",
attributes: []html.Attribute{
{
Key: "example",
Val: "test",
},
{
Key: "example-2",
Val: "another test",
},
},
assetType: assets.InlineCSS,
},
},
}

for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
got := NewRemoteAsset(tt.id, tt.url, tt.assetType)
got := NewRemoteAsset(tt.id, tt.url, tt.attributes, tt.assetType)
opts := []cmp.Option{
cmp.AllowUnexported(RemoteAsset{}),
}
Expand Down Expand Up @@ -1180,7 +1202,7 @@ func TestRemoteAsset_String(t *testing.T) {
asset: &RemoteAsset{
url: "http://example.com/url.html",
},
want: `<Remote Asset: "http://example.com/url.html">`,
want: `<Remote Asset: "http://example.com/url.html" Attributes: []>`,
},
}

Expand Down
11 changes: 9 additions & 2 deletions assets/assetstubs/assetsstubs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ package assetstubs
import (
"testing"

"github.com/gauntface/go-html-asset-manager/v4/assets"
"github.com/gauntface/go-html-asset-manager/v4/assets/assetmanager"
"github.com/gauntface/go-html-asset-manager/v5/assets"
"github.com/gauntface/go-html-asset-manager/v5/assets/assetmanager"
"golang.org/x/net/html"
)

type Manager struct {
Expand Down Expand Up @@ -64,6 +65,8 @@ type Asset struct {
URLReturn string
URLError error

AttributesReturn []html.Attribute

ContentsReturn string
ContentsError error

Expand Down Expand Up @@ -92,6 +95,10 @@ func (a *Asset) URL() (string, error) {
return a.URLReturn, a.URLError
}

func (a *Asset) Attributes() []html.Attribute {
return a.AttributesReturn
}

func (a *Asset) Contents() (string, error) {
return a.ContentsReturn, a.ContentsError
}
Expand Down
4 changes: 2 additions & 2 deletions assets/genimgs/genimgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
"github.com/aws/aws-sdk-go-v2/service/s3"
awstypes "github.com/aws/aws-sdk-go-v2/service/s3/types"
"github.com/disintegration/imaging"
"github.com/gauntface/go-html-asset-manager/v4/utils/config"
"github.com/gauntface/go-html-asset-manager/v4/utils/files"
"github.com/gauntface/go-html-asset-manager/v5/utils/config"
"github.com/gauntface/go-html-asset-manager/v5/utils/files"
)

var (
Expand Down
10 changes: 5 additions & 5 deletions cmds/genimgs/genimgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ import (
awstypes "github.com/aws/aws-sdk-go-v2/service/s3/types"
"github.com/chai2010/webp"
"github.com/disintegration/imaging"
"github.com/gauntface/go-html-asset-manager/v4/assets"
"github.com/gauntface/go-html-asset-manager/v4/assets/assetmanager"
"github.com/gauntface/go-html-asset-manager/v4/utils/config"
"github.com/gauntface/go-html-asset-manager/v4/utils/files"
"github.com/gauntface/go-html-asset-manager/v4/utils/sets"
"github.com/gauntface/go-html-asset-manager/v5/assets"
"github.com/gauntface/go-html-asset-manager/v5/assets/assetmanager"
"github.com/gauntface/go-html-asset-manager/v5/utils/config"
"github.com/gauntface/go-html-asset-manager/v5/utils/files"
"github.com/gauntface/go-html-asset-manager/v5/utils/sets"
"github.com/mitchellh/go-homedir"
"github.com/schollz/progressbar/v3"
)
Expand Down
42 changes: 21 additions & 21 deletions cmds/htmlassets/htmlassets.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,27 @@ import (

awsconfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/gauntface/go-html-asset-manager/v4/assets"
"github.com/gauntface/go-html-asset-manager/v4/assets/assetmanager"
"github.com/gauntface/go-html-asset-manager/v4/manipulations"
"github.com/gauntface/go-html-asset-manager/v4/manipulations/asyncsrc"
"github.com/gauntface/go-html-asset-manager/v4/manipulations/iframedefaultsize"
"github.com/gauntface/go-html-asset-manager/v4/manipulations/imgsize"
"github.com/gauntface/go-html-asset-manager/v4/manipulations/imgtopicture"
"github.com/gauntface/go-html-asset-manager/v4/manipulations/injectassets"
"github.com/gauntface/go-html-asset-manager/v4/manipulations/lazyload"
"github.com/gauntface/go-html-asset-manager/v4/manipulations/opengraphimg"
"github.com/gauntface/go-html-asset-manager/v4/manipulations/ratiowrapper"
"github.com/gauntface/go-html-asset-manager/v4/manipulations/stripassets"
"github.com/gauntface/go-html-asset-manager/v4/manipulations/vimeoclean"
"github.com/gauntface/go-html-asset-manager/v4/manipulations/youtubeclean"
"github.com/gauntface/go-html-asset-manager/v4/preprocessors"
"github.com/gauntface/go-html-asset-manager/v4/preprocessors/hamassets"
"github.com/gauntface/go-html-asset-manager/v4/preprocessors/jsonassets"
"github.com/gauntface/go-html-asset-manager/v4/preprocessors/revisionassets"
"github.com/gauntface/go-html-asset-manager/v4/utils/config"
"github.com/gauntface/go-html-asset-manager/v4/utils/html/htmlencoding"
"github.com/gauntface/go-html-asset-manager/v4/utils/vimeoapi"
"github.com/gauntface/go-html-asset-manager/v5/assets"
"github.com/gauntface/go-html-asset-manager/v5/assets/assetmanager"
"github.com/gauntface/go-html-asset-manager/v5/manipulations"
"github.com/gauntface/go-html-asset-manager/v5/manipulations/asyncsrc"
"github.com/gauntface/go-html-asset-manager/v5/manipulations/iframedefaultsize"
"github.com/gauntface/go-html-asset-manager/v5/manipulations/imgsize"
"github.com/gauntface/go-html-asset-manager/v5/manipulations/imgtopicture"
"github.com/gauntface/go-html-asset-manager/v5/manipulations/injectassets"
"github.com/gauntface/go-html-asset-manager/v5/manipulations/lazyload"
"github.com/gauntface/go-html-asset-manager/v5/manipulations/opengraphimg"
"github.com/gauntface/go-html-asset-manager/v5/manipulations/ratiowrapper"
"github.com/gauntface/go-html-asset-manager/v5/manipulations/stripassets"
"github.com/gauntface/go-html-asset-manager/v5/manipulations/vimeoclean"
"github.com/gauntface/go-html-asset-manager/v5/manipulations/youtubeclean"
"github.com/gauntface/go-html-asset-manager/v5/preprocessors"
"github.com/gauntface/go-html-asset-manager/v5/preprocessors/hamassets"
"github.com/gauntface/go-html-asset-manager/v5/preprocessors/jsonassets"
"github.com/gauntface/go-html-asset-manager/v5/preprocessors/revisionassets"
"github.com/gauntface/go-html-asset-manager/v5/utils/config"
"github.com/gauntface/go-html-asset-manager/v5/utils/html/htmlencoding"
"github.com/gauntface/go-html-asset-manager/v5/utils/vimeoapi"
"github.com/mitchellh/go-homedir"
"github.com/schollz/progressbar/v3"
"golang.org/x/net/html"
Expand Down
16 changes: 8 additions & 8 deletions cmds/htmlassets/htmlassets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ import (
"strings"
"testing"

"github.com/gauntface/go-html-asset-manager/v4/assets"
"github.com/gauntface/go-html-asset-manager/v4/assets/assetmanager"
"github.com/gauntface/go-html-asset-manager/v4/assets/assetstubs"
"github.com/gauntface/go-html-asset-manager/v4/manipulations"
"github.com/gauntface/go-html-asset-manager/v4/preprocessors"
"github.com/gauntface/go-html-asset-manager/v4/utils/config"
"github.com/gauntface/go-html-asset-manager/v5/assets"
"github.com/gauntface/go-html-asset-manager/v5/assets/assetmanager"
"github.com/gauntface/go-html-asset-manager/v5/assets/assetstubs"
"github.com/gauntface/go-html-asset-manager/v5/manipulations"
"github.com/gauntface/go-html-asset-manager/v5/preprocessors"
"github.com/gauntface/go-html-asset-manager/v5/utils/config"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/mitchellh/go-homedir"
Expand Down Expand Up @@ -324,9 +324,9 @@ func Test_manipulations(t *testing.T) {
description: "return errors if processing file fails",
manager: &assetstubs.Manager{
WithTypeReturn: map[assets.Type][]assetmanager.Asset{
assets.HTML: []assetmanager.Asset{
assets.HTML: {
assetstubs.MustNewLocalAsset(t, "/example/", "/example/example-1.html"),
assetmanager.NewRemoteAsset("example", "http://example.com/123", assets.Unknown),
assetmanager.NewRemoteAsset("example", "http://example.com/123", []html.Attribute{}, assets.Unknown),
},
},
},
Expand Down
4 changes: 2 additions & 2 deletions embedassets/assets/js/components/n-ham-c-lite-vi-async.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5199f5c

Please sign in to comment.