examples 为输入输出交替
(session *grequests.Session, problemURL string)
| 90 | |
| 91 | // examples 为输入输出交替 |
| 92 | func parseCodeAndExamples(session *grequests.Session, problemURL string) (code string, examples []string, err error) { |
| 93 | resp, err := session.Get(problemURL, nil) |
| 94 | if err != nil { |
| 95 | return |
| 96 | } |
| 97 | if !resp.Ok { |
| 98 | return "", nil, fmt.Errorf("%d", resp.StatusCode) |
| 99 | } |
| 100 | |
| 101 | root, err := html.Parse(resp) |
| 102 | if err != nil { |
| 103 | return |
| 104 | } |
| 105 | |
| 106 | var f func(o *html.Node) |
| 107 | f = func(o *html.Node) { |
| 108 | if o.DataAtom == atom.Textarea { |
| 109 | for _, attribute := range o.Attr { |
| 110 | if attribute.Val == "input" || attribute.Val == "output" { |
| 111 | examples = append(examples, strings.TrimSpace(o.FirstChild.Data)) |
| 112 | break |
| 113 | } |
| 114 | if attribute.Val == "goTpl" { |
| 115 | code = o.FirstChild.Data |
| 116 | break |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | for c := o.FirstChild; c != nil; c = c.NextSibling { |
| 121 | f(c) |
| 122 | } |
| 123 | } |
| 124 | f(root) |
| 125 | return |
| 126 | } |
| 127 | |
| 128 | func genMainFileContent(code, funcComment string) string { |
| 129 | code = strings.TrimSpace(code) |
no test coverage detected