Skip to content
Snippets Groups Projects
Verified Commit b95159df authored by Volker Schukai's avatar Volker Schukai :alien:
Browse files

feat: add, remove and attributes modification

parent 94e8c090
No related branches found
No related tags found
No related merge requests found
...@@ -2,8 +2,10 @@ package html ...@@ -2,8 +2,10 @@ package html
import ( import (
"encoding/json" "encoding/json"
"github.com/andybalholm/cascadia"
"gitlab.schukai.com/oss/bob/types" "gitlab.schukai.com/oss/bob/types"
"gitlab.schukai.com/oss/libraries/go/markup/html/engine" "gitlab.schukai.com/oss/libraries/go/markup/html/engine"
"golang.org/x/net/html"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"os" "os"
"path" "path"
...@@ -104,5 +106,77 @@ func Generate(data *types.PageData, name string) (string, error) { ...@@ -104,5 +106,77 @@ func Generate(data *types.PageData, name string) (string, error) {
stringWriter := &strings.Builder{} stringWriter := &strings.Builder{}
e.ProcessHtml(stringWriter, strings.NewReader(string(template))) e.ProcessHtml(stringWriter, strings.NewReader(string(template)))
return doModifications(data, stringWriter.String())
}
func doModifications(page *types.PageData, from string) (string, error) {
node, err := html.Parse(strings.NewReader(from))
if err != nil {
return "", err
}
for _, m := range page.Modifications.Remove {
selector, err := cascadia.Parse(m)
if err != nil {
return "", err
}
list := cascadia.QueryAll(node, selector)
for _, n := range list {
if n.Parent != nil {
n.Parent.RemoveChild(n)
}
}
}
for _, m := range page.Modifications.Add {
selector, err := cascadia.Parse(m.Selector)
if err != nil {
return "", err
}
list := cascadia.QueryAll(node, selector)
for _, n := range list {
if n.Parent != nil {
nodes, err := html.ParseFragment(strings.NewReader(m.Html), n)
if err != nil {
return "", err
}
for _, added := range nodes {
n.InsertBefore(added, n.FirstChild)
}
}
}
}
for _, m := range page.Modifications.SetAttribute {
selector, err := cascadia.Parse(m.Selector)
if err != nil {
return "", err
}
list := cascadia.QueryAll(node, selector)
for _, n := range list {
if n.Parent != nil {
attr := n.Attr
attr = append(attr, html.Attribute{
Key: m.Name,
Val: m.Value,
})
n.Attr = attr
}
}
}
stringWriter := &strings.Builder{}
err = html.Render(stringWriter, node)
if err != nil {
return "", err
}
return stringWriter.String(), nil return stringWriter.String(), nil
} }
...@@ -25,6 +25,23 @@ type Translations struct { ...@@ -25,6 +25,23 @@ type Translations struct {
KeyValues map[string]any `yaml:"translations"` KeyValues map[string]any `yaml:"translations"`
} }
type AddModification struct {
Selector string `yaml:"selector"`
Html string `yaml:"html"`
}
type SetAttributeModification struct {
Selector string `yaml:"selector"`
Name string `yaml:"name"`
Value string `yaml:"value"`
}
type Modifications struct {
Remove []string `yaml:"remove"`
Add []AddModification `yaml:"add"`
SetAttribute []SetAttributeModification `yaml:"attributes"`
}
type PageData struct { type PageData struct {
Export string `yaml:"export"` Export string `yaml:"export"`
Lang string `yaml:"lang"` Lang string `yaml:"lang"`
...@@ -34,6 +51,7 @@ type PageData struct { ...@@ -34,6 +51,7 @@ type PageData struct {
Anchors []Anchor `yaml:"anchors"` Anchors []Anchor `yaml:"anchors"`
Text []Text `yaml:"text"` Text []Text `yaml:"text"`
Translations []Translations `yaml:"translations"` Translations []Translations `yaml:"translations"`
Modifications Modifications `yaml:"modifications"`
} }
func NewPageData() *PageData { func NewPageData() *PageData {
......
...@@ -28,15 +28,19 @@ ...@@ -28,15 +28,19 @@
<link rel="icon" href="/favicon.svg" type="image/svg+xml"/> <link rel="icon" href="/favicon.svg" type="image/svg+xml"/>
<link rel="apple-touch-icon" href="/apple-touch-icon.png"/> <link rel="apple-touch-icon" href="/apple-touch-icon.png"/>
<script id="translations" type="application/json" data-monster-role="translation">{"key1":"value1 gfgf66","key2":"value2 gg","key3":{"one":"value3 tgtgt","two":"value4 trzrtz"}}</script> <script id="translations" type="application/json" data-monster-role="translation">{"key1":"value1","key2":"value2","key3":{"one":"value3","two":"value4"}}</script>
<script id="mainscript" src="/script/main22.mjs" type="module"></script> <script id="mainscript" src="/script/main22.mjs" type="module" data-xyz="YES"></script>
</head> </head>
<body> <body>
<a id="test-link-test-html" href="/test.html" title="test-link" hreflang="">test-link</a>
<header> <header>
<div class="gradient"></div>
</header> </header>
<monster-state> <monster-state>
...@@ -45,7 +49,7 @@ ...@@ -45,7 +49,7 @@
PbGkNUOFk6DZqgHCNGg2T4QAQBoIiRSAwBE4VA4FACKgkB5NGReASFZEmxsQ0whPDi9BiACYQAInXhwOUtgCUQoORFCGt/g4QAIQA7" id="tickyesdata-image-gi-4013311193"/> PbGkNUOFk6DZqgHCNGg2T4QAQBoIiRSAwBE4VA4FACKgkB5NGReASFZEmxsQ0whPDi9BiACYQAInXhwOUtgCUQoORFCGt/g4QAIQA7" id="tickyesdata-image-gi-4013311193"/>
<h1 class="deco">Bad xxxx</h1> <h1 class="deco">Bad xxxx</h1>
<p>The request was incorrect, the server could not process it.</p><p> <p>The request was incorrect, the server could not process it.</p><p>
</p><div class="infobox"> </p><div class="infobox"><b><span>GURKE</span></b>
<div> <div>
<ul> <ul>
......
<!DOCTYPE html><html lang="en"><head>
</head>
<body>
<main>
<p>Das ist ein Text<a id="a-html" href="a.html" title="" hreflang="">one-time password</a>.</p>
</main>
</body></html>
\ No newline at end of file
...@@ -52,6 +52,16 @@ test1.html: ...@@ -52,6 +52,16 @@ test1.html:
key3: key3:
one: value3 one: value3
two: value4 two: value4
modifications:
remove:
- .gradient
add:
- selector: .infobox
html: <b><span>GURKE</span></b>
attributes:
- selector: '#mainscript'
name: data-xyz
value: YES
test2.html: test2.html:
export: en/test2.html export: en/test2.html
lang: en lang: en
...@@ -92,6 +102,10 @@ test2.html: ...@@ -92,6 +102,10 @@ test2.html:
- text: 'You can try the following steps:' - text: 'You can try the following steps:'
id: you-can-try-the-foll-3363859033 id: you-can-try-the-foll-3363859033
translations: [] translations: []
modifications:
remove: []
add: []
attributes: []
test3.html: test3.html:
export: en/test3.html export: en/test3.html
lang: en lang: en
...@@ -132,6 +146,10 @@ test3.html: ...@@ -132,6 +146,10 @@ test3.html:
- text: 'You can try the following steps:' - text: 'You can try the following steps:'
id: you-can-try-the-foll-3363859033 id: you-can-try-the-foll-3363859033
translations: [] translations: []
modifications:
remove: []
add: []
attributes: []
test4.html: test4.html:
export: en/test4.html export: en/test4.html
lang: en lang: en
...@@ -151,3 +169,7 @@ test4.html: ...@@ -151,3 +169,7 @@ test4.html:
- text: . - text: .
id: id1000 id: id1000
translations: [] translations: []
modifications:
remove: []
add: []
attributes: []
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment