diff --git a/fetcher/fetch.go b/fetcher/fetch.go index cc2abb6..ad33fb1 100644 --- a/fetcher/fetch.go +++ b/fetcher/fetch.go @@ -21,27 +21,27 @@ import ( "github.com/goplus/hdq" ) -// func(url string, doc hdq.NodeSet) +// func(input any, doc hdq.NodeSet) type Conv = any // ----------------------------------------------------------------------------- // Convert converts a html source to an object. -func Convert(conv reflect.Value, url string, source any) any { +func Convert(conv reflect.Value, input, source any) any { doc := reflect.ValueOf(hdq.Source(source)) - out := conv.Call([]reflect.Value{reflect.ValueOf(url), doc}) + out := conv.Call([]reflect.Value{reflect.ValueOf(input), doc}) return out[0].Interface() } // ----------------------------------------------------------------------------- // New creates a new object from a html source by a registered converter. -func New(pageType string, url string, source any) any { +func New(pageType string, input, source any) any { page, ok := convs[pageType] if !ok { panic("fetcher: unknown pageType - " + pageType) } - return Convert(page.Conv, url, source) + return Convert(page.Conv, input, source) } // FromInput creates a new object from the html source with the specified input. @@ -51,13 +51,13 @@ func FromInput(pageType string, input any) any { panic("fetcher: unknown pageType - " + pageType) } url := page.URL(input) - return Convert(page.Conv, url, url) + return Convert(page.Conv, input, url) } // sitePageType represents a site page type. type sitePageType struct { Conv reflect.Value - URL func(any) string + URL func(input any) string } var ( @@ -65,7 +65,7 @@ var ( ) // Register registers a convType with a convert function. -func Register(pageType string, conv Conv, urlOf func(any) string) { +func Register(pageType string, conv Conv, urlOf func(input any) string) { vConv := reflect.ValueOf(conv) convs[pageType] = sitePageType{vConv, urlOf} } diff --git a/fetcher/torch/_testdata/eye/out.json b/fetcher/torch/_testdata/eye/out.json index d6b977a..d504c75 100644 --- a/fetcher/torch/_testdata/eye/out.json +++ b/fetcher/torch/_testdata/eye/out.json @@ -1,5 +1,5 @@ { - "name": "eye", + "name": "", "doc": "", "sig": "(n, m=None, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor" } \ No newline at end of file diff --git a/fetcher/torch/gop_autogen.go b/fetcher/torch/gop_autogen.go index b361d98..2d7c840 100644 --- a/fetcher/torch/gop_autogen.go +++ b/fetcher/torch/gop_autogen.go @@ -15,56 +15,64 @@ const spaces = " \t\r\n¶" type Result struct { Name string `json:"name"` + Type string `json:"type"` Doc string `json:"doc"` Sig string `json:"sig"` URL string `json:"url,omitempty"` } -//line fetcher/torch/pysig_torch.gop:38:1 +//line fetcher/torch/pysig_torch.gop:39:1 // New creates a new Result from a html document. -func New(url string, doc hdq.NodeSet) Result { -//line fetcher/torch/pysig_torch.gop:40:1 - if doc.Ok() { +func New(input interface{}, doc hdq.NodeSet) Result { //line fetcher/torch/pysig_torch.gop:41:1 - fn := doc.Any().Dl().Class("py function") + name := input.(string) //line fetcher/torch/pysig_torch.gop:42:1 + url := name +//line fetcher/torch/pysig_torch.gop:43:1 + if name != "" { +//line fetcher/torch/pysig_torch.gop:60:1 + url = URL(input) + } +//line fetcher/torch/pysig_torch.gop:46:1 + if doc.Ok() { +//line fetcher/torch/pysig_torch.gop:47:1 + fn := doc.Any().Dl().Class("py function") +//line fetcher/torch/pysig_torch.gop:48:1 decl := func() (_gop_ret string) { -//line fetcher/torch/pysig_torch.gop:42:1 +//line fetcher/torch/pysig_torch.gop:48:1 var _gop_err error -//line fetcher/torch/pysig_torch.gop:42:1 +//line fetcher/torch/pysig_torch.gop:48:1 _gop_ret, _gop_err = fn.FirstElementChild().Dt().Text__0() -//line fetcher/torch/pysig_torch.gop:42:1 +//line fetcher/torch/pysig_torch.gop:48:1 if _gop_err != nil { -//line fetcher/torch/pysig_torch.gop:42:1 - _gop_err = errors.NewFrame(_gop_err, "fn.firstElementChild.dt.text", "fetcher/torch/pysig_torch.gop", 42, "torch.New") -//line fetcher/torch/pysig_torch.gop:42:1 +//line fetcher/torch/pysig_torch.gop:48:1 + _gop_err = errors.NewFrame(_gop_err, "fn.firstElementChild.dt.text", "fetcher/torch/pysig_torch.gop", 48, "torch.New") +//line fetcher/torch/pysig_torch.gop:48:1 panic(_gop_err) } -//line fetcher/torch/pysig_torch.gop:42:1 +//line fetcher/torch/pysig_torch.gop:48:1 return }() -//line fetcher/torch/pysig_torch.gop:43:1 +//line fetcher/torch/pysig_torch.gop:49:1 pos := strings.IndexByte(decl, '(') -//line fetcher/torch/pysig_torch.gop:44:1 +//line fetcher/torch/pysig_torch.gop:50:1 if pos > 0 { -//line fetcher/torch/pysig_torch.gop:45:1 - name := strings.TrimPrefix(decl[:pos], "torch.") -//line fetcher/torch/pysig_torch.gop:46:1 +//line fetcher/torch/pysig_torch.gop:51:1 sig := decl[pos:] -//line fetcher/torch/pysig_torch.gop:47:1 - return Result{strings.TrimSpace(name), "", strings.TrimRight(sig, spaces), url} +//line fetcher/torch/pysig_torch.gop:52:1 + return Result{name, "function", "", strings.TrimRight(sig, spaces), url} } } -//line fetcher/torch/pysig_torch.gop:50:1 - return Result{"", "", "", url} +//line fetcher/torch/pysig_torch.gop:55:1 + return Result{name, "", "", "", url} } -//line fetcher/torch/pysig_torch.gop:53:1 +//line fetcher/torch/pysig_torch.gop:58:1 // URL returns the input URL for the given name. func URL(name interface{}) string { -//line fetcher/torch/pysig_torch.gop:55:1 +//line fetcher/torch/pysig_torch.gop:60:1 return "https://pytorch.org/docs/stable/generated/torch." + name.(string) + ".html" } -//line fetcher/torch/pysig_torch.gop:58:1 +//line fetcher/torch/pysig_torch.gop:63:1 func init() { -//line fetcher/torch/pysig_torch.gop:59:1 +//line fetcher/torch/pysig_torch.gop:64:1 fetcher.Register("torch", New, URL) } diff --git a/fetcher/torch/pysig_torch.gop b/fetcher/torch/pysig_torch.gop index 1dd2cf2..c47f1f5 100644 --- a/fetcher/torch/pysig_torch.gop +++ b/fetcher/torch/pysig_torch.gop @@ -30,24 +30,29 @@ const ( type Result struct { Name string `json:"name"` + Type string `json:"type"` Doc string `json:"doc"` Sig string `json:"sig"` URL string `json:"url,omitempty"` } // New creates a new Result from a html document. -func New(url string, doc hdq.NodeSet) Result { +func New(input any, doc hdq.NodeSet) Result { + name := input.(string) + url := name + if name != "" { + url = URL(input) + } if doc.ok { fn := doc.any.dl.class("py function") decl := fn.firstElementChild.dt.text! pos := strings.indexByte(decl, '(') if pos > 0 { - name := strings.trimPrefix(decl[:pos], "torch.") sig := decl[pos:] - return {strings.trimSpace(name), "", strings.trimRight(sig, spaces), url} + return {name, "function", "", strings.trimRight(sig, spaces), url} } } - return {"", "", "", url} + return {name, "", "", "", url} } // URL returns the input URL for the given name. diff --git a/tutorial/02-GithubRepos/gop_autogen.go b/tutorial/02-GithubRepos/gop_autogen.go index 9ce17f7..da02a36 100644 --- a/tutorial/02-GithubRepos/gop_autogen.go +++ b/tutorial/02-GithubRepos/gop_autogen.go @@ -119,7 +119,7 @@ func newRepo(node hdq.NodeSet) Repo { } //line tutorial/02-GithubRepos/repos.gop:44:1 // New creates a new Result from a html document. -func New(url string, doc hdq.NodeSet) Result { +func New(_ interface{}, doc hdq.NodeSet) Result { //line tutorial/02-GithubRepos/repos.gop:46:1 divRepos := doc.Any().Div().Id("user-repositories-list").One() //line tutorial/02-GithubRepos/repos.gop:47:1 diff --git a/tutorial/02-GithubRepos/repos.gop b/tutorial/02-GithubRepos/repos.gop index 9503ce2..36ace67 100644 --- a/tutorial/02-GithubRepos/repos.gop +++ b/tutorial/02-GithubRepos/repos.gop @@ -42,7 +42,7 @@ type Result struct { } // New creates a new Result from a html document. -func New(url string, doc hdq.NodeSet) Result { +func New(_ any, doc hdq.NodeSet) Result { divRepos := doc.any.div.id("user-repositories-list").one repoList := divRepos.child.ul.one repos := [newRepo(x) for x <- repoList.child.li]