-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from basenana/feature/plugin
feat: subContent plugin
- Loading branch information
Showing
8 changed files
with
224 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
Copyright 2024 Friday Author. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package plugin | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/basenana/friday/pkg/models/doc" | ||
) | ||
|
||
func TestHeaderImgPlugin_Run(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
document *doc.Document | ||
wantErr bool | ||
wantHeaderImg string | ||
}{ | ||
{ | ||
name: "test-relative-address", | ||
document: &doc.Document{ | ||
WebUrl: "https://blog.abc/123", | ||
Content: "<p><img src=\"media/123.png\" alt=\"\" /></p>", | ||
}, | ||
wantErr: false, | ||
wantHeaderImg: "https://blog.abc/media/123.png", | ||
}, | ||
{ | ||
name: "test-normal", | ||
document: &doc.Document{ | ||
WebUrl: "https://blog.abc", | ||
Content: "<p><img src=\"https://def/123.png\" /></p>", | ||
}, | ||
wantErr: false, | ||
wantHeaderImg: "https://def/123.png", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
h := &HeaderImgPlugin{} | ||
if err := h.Run(context.TODO(), tt.document); (err != nil) != tt.wantErr { | ||
t.Errorf("Run() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
if tt.document.HeaderImage != tt.wantHeaderImg { | ||
t.Errorf("Run() got = %v, want %v", tt.document.HeaderImage, tt.wantHeaderImg) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
Copyright 2024 Friday Author. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package plugin | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/PuerkitoBio/goquery" | ||
|
||
"github.com/basenana/friday/pkg/models/doc" | ||
) | ||
|
||
type SubContentPlugin struct { | ||
} | ||
|
||
func (s *SubContentPlugin) Name() string { | ||
return "subContent" | ||
} | ||
|
||
func (s *SubContentPlugin) Run(ctx context.Context, doc *doc.Document) error { | ||
doc.SubContent = GenerateContentSubContent(doc.Content) | ||
return nil | ||
} | ||
|
||
var _ ChainPlugin = &SubContentPlugin{} | ||
|
||
var repeatSpace = regexp.MustCompile(`\s+`) | ||
var htmlCharFilterRegexp = regexp.MustCompile(`</?[!\w:]+((\s+[\w-]+(\s*=\s*(?:\\*".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)/?>`) | ||
|
||
func ContentTrim(contentType, content string) string { | ||
switch contentType { | ||
case "html", "htm", "webarchive", ".webarchive": | ||
content = strings.ReplaceAll(content, "</p>", "</p>\n") | ||
content = strings.ReplaceAll(content, "</P>", "</P>\n") | ||
content = strings.ReplaceAll(content, "</div>", "</div>\n") | ||
content = strings.ReplaceAll(content, "</DIV>", "</DIV>\n") | ||
content = htmlCharFilterRegexp.ReplaceAllString(content, "") | ||
} | ||
content = repeatSpace.ReplaceAllString(content, " ") | ||
return content | ||
} | ||
|
||
func GenerateContentSubContent(content string) string { | ||
if subContent, err := slowPathContentSubContent([]byte(content)); err == nil { | ||
return subContent | ||
} | ||
|
||
content = ContentTrim("html", content) | ||
subContents := strings.Split(content, "\n") | ||
contents := make([]string, 0) | ||
i := 0 | ||
for _, subContent := range subContents { | ||
subContent = strings.TrimSpace(subContent) | ||
if subContent != "" { | ||
contents = append(contents, subContent) | ||
i++ | ||
if i >= 3 { | ||
break | ||
} | ||
} | ||
} | ||
return strings.Join(contents, " ") | ||
} | ||
|
||
func slowPathContentSubContent(content []byte) (string, error) { | ||
query, err := goquery.NewDocumentFromReader(bytes.NewReader(content)) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
contents := make([]string, 0) | ||
query.Find("p").EachWithBreak(func(i int, selection *goquery.Selection) bool { | ||
if len(contents) > 10 { | ||
return false | ||
} | ||
t := strings.TrimSpace(selection.Text()) | ||
if t != "" { | ||
contents = append(contents, strings.ReplaceAll(t, "\n", " ")) | ||
} | ||
return true | ||
}) | ||
|
||
return trimDocumentContent(strings.Join(contents, " "), 400), nil | ||
} | ||
|
||
func trimDocumentContent(str string, m int) string { | ||
str = ContentTrim("html", str) | ||
runes := []rune(str) | ||
if len(runes) > m { | ||
return string(runes[:m]) | ||
} | ||
return str | ||
} | ||
|
||
func init() { | ||
RegisterPlugin(&SubContentPlugin{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters