Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hdq/pysig/torch #43

Merged
merged 1 commit into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ _htmlab
.gop
coverage.txt
index.htm
gop_autogen*.go
# gop_autogen*.go

# Binaries for programs and plugins
*.exe
Expand Down
8 changes: 8 additions & 0 deletions chore/pysigfetch/gop_autogen.go

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

Empty file added chore/pysigfetch/pysigfetch.gop
Empty file.
22 changes: 12 additions & 10 deletions hdq.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
/*
Copyright 2021 The GoPlus Authors (goplus.org)
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.
Copyright 2021 The GoPlus Authors (goplus.org)
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 hdq
Expand Down
32 changes: 22 additions & 10 deletions hdq_helper.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
/*
Copyright 2021 The GoPlus Authors (goplus.org)
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.
Copyright 2021 The GoPlus Authors (goplus.org)
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 hdq
Expand Down Expand Up @@ -176,6 +178,16 @@ func (p NodeSet) Ul() (ret NodeSet) {
return p.dataAtom(atom.Ul)
}

// Dl returns NodeSet which node type is ElementNode and it's element type is `dl`.
func (p NodeSet) Dl() (ret NodeSet) {
return p.dataAtom(atom.Dl)
}

// Dt returns NodeSet which node type is ElementNode and it's element type is `dt`.
func (p NodeSet) Dt() (ret NodeSet) {
return p.dataAtom(atom.Dt)
}

// Span returns NodeSet which node type is ElementNode and it's element type is `span`.
func (p NodeSet) Span() (ret NodeSet) {
return p.dataAtom(atom.Span)
Expand Down
80 changes: 80 additions & 0 deletions hdqtest/hdqtest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
Copyright 2024 The GoPlus Authors (goplus.org)
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 hdqtest

import (
"encoding/json"
"log"
"os"
"path"
"reflect"
"strings"
"testing"

"github.com/goplus/hdq"
)

// func(doc hdq.NodeSet) any
type Converter = any

// FromDir tests all html files in a directory.
func FromDir(t *testing.T, sel, relDir string, conv Converter) {
dir, err := os.Getwd()
if err != nil {
t.Fatal("Getwd failed:", err)
}
dir = path.Join(dir, relDir)
fis, err := os.ReadDir(dir)
if err != nil {
t.Fatal("ReadDir failed:", err)
}
vConv := reflect.ValueOf(conv)
for _, fi := range fis {
name := fi.Name()
if !fi.IsDir() || strings.HasPrefix(name, "_") {
continue
}
t.Run(name, func(t *testing.T) {
testFrom(t, dir+"/"+name, sel, vConv)
})
}
}

func testFrom(t *testing.T, pkgDir, sel string, conv reflect.Value) {
if sel != "" && !strings.Contains(pkgDir, sel) {
return
}
log.Println("Parsing", pkgDir)
in := pkgDir + "/in.html"
out := pkgDir + "/out.json"
b, err := os.ReadFile(out)
if err != nil {
t.Fatal("ReadFile failed:", err)
}
expected := string(b)
ret := ConvFile(in, conv)
retb, _ := json.MarshalIndent(ret, "", "\t")
if v := string(retb); v != expected {
t.Fatalf("\n==> got:\n%s\n==> expected:\n%s\n", v, expected)
}
}

// ConvFile converts a html source to an object.
func ConvFile(in any, conv reflect.Value) any {
doc := reflect.ValueOf(hdq.Source(in))
out := conv.Call([]reflect.Value{doc})
return out[0].Interface()
}
24 changes: 13 additions & 11 deletions html_utils.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
/*
Copyright 2021 The GoPlus Authors (goplus.org)
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.
Copyright 2021 The GoPlus Authors (goplus.org)
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 hdq
Expand Down Expand Up @@ -142,7 +144,7 @@ func (p *textPrinter) printNode(node *html.Node) {
return
}
if node.Type == html.TextNode {
p.printText(strings.Trim(node.Data, " \t\r\n"))
p.printText(strings.Trim(node.Data, " \t\r\n"))
return
}
for child := node.FirstChild; child != nil; child = child.NextSibling {
Expand Down
Loading
Loading