package main import ( "encoding/json" "fmt" "html" "regexp" "strings" ) const ( maxPostingChars = 40_000 minPostingChars = 400 // less visible text than this is a shell page, not a posting ) var ( reJSONLD = regexp.MustCompile(`(?is)]*type\s*=\s*["']?application/ld\+json["']?[^>]*>(.*?)`) reTitle = regexp.MustCompile(`(?is)]*>(.*?)`) reComment = regexp.MustCompile(`(?s)`) reBlockTag = regexp.MustCompile(`(?i)]*>`) reAnyTag = regexp.MustCompile(`(?s)<[^>]*>`) reSpaces = regexp.MustCompile(`[ \t\r\f\v\x{00a0}]+`) reNewlines = regexp.MustCompile(`\n[ \n]*\n`) // Elements whose content is never posting text. reDropped = func() []*regexp.Regexp { var out []*regexp.Regexp for _, tag := range []string{"script", "style", "noscript", "svg", "head", "template", "iframe", "nav", "form"} { out = append(out, regexp.MustCompile(`(?is)<`+tag+`\b.*?`)) } return out }() ) // htmlToText flattens HTML to readable plain text. It is a deliberately // simple regexp pass: the reader is a language model, not a browser. func htmlToText(s string) string { s = reComment.ReplaceAllString(s, " ") for _, re := range reDropped { s = re.ReplaceAllString(s, " ") } s = reBlockTag.ReplaceAllString(s, "\n") s = reAnyTag.ReplaceAllString(s, " ") s = html.UnescapeString(s) s = reSpaces.ReplaceAllString(s, " ") lines := strings.Split(s, "\n") for i, l := range lines { lines[i] = strings.TrimSpace(l) } s = reNewlines.ReplaceAllString(strings.Join(lines, "\n"), "\n\n") return strings.TrimSpace(s) } // findJobPosting walks decoded JSON-LD and returns the first schema.org // JobPosting object, wherever it is nested (top level, array, @graph). func findJobPosting(v any) map[string]any { switch t := v.(type) { case []any: for _, e := range t { if m := findJobPosting(e); m != nil { return m } } case map[string]any: switch typ := t["@type"].(type) { case string: if typ == "JobPosting" { return t } case []any: for _, e := range typ { if e == "JobPosting" { return t } } } if g, ok := t["@graph"]; ok { return findJobPosting(g) } } return nil } // str renders a JSON-LD value compactly: strings as they are, objects by // their name/address fields, anything else as JSON. func str(v any) string { switch t := v.(type) { case nil: return "" case string: return strings.TrimSpace(t) case []any: var parts []string for _, e := range t { if s := str(e); s != "" { parts = append(parts, s) } } return strings.Join(parts, "; ") case map[string]any: if a, ok := t["address"]; ok { return str(a) } var parts []string for _, k := range []string{"name", "streetAddress", "addressLocality", "addressRegion", "addressCountry"} { if s := str(t[k]); s != "" { parts = append(parts, s) } } if len(parts) > 0 { return strings.Join(parts, ", ") } } b, _ := json.Marshal(v) return string(b) } // structuredPosting renders the page's JobPosting JSON-LD, if it has one. // Most job sites embed it for search engines, and it is far cleaner than the // visible page. func structuredPosting(page string) string { for _, m := range reJSONLD.FindAllStringSubmatch(page, -1) { var v any if json.Unmarshal([]byte(strings.TrimSpace(m[1])), &v) != nil { continue } jp := findJobPosting(v) if jp == nil { continue } var b strings.Builder for _, f := range []struct{ label, key string }{ {"Title", "title"}, {"Company", "hiringOrganization"}, {"Location", "jobLocation"}, {"Location type", "jobLocationType"}, {"Applicants must be in", "applicantLocationRequirements"}, {"Employment type", "employmentType"}, {"Salary", "baseSalary"}, {"Posted", "datePosted"}, {"Valid through", "validThrough"}, } { if s := str(jp[f.key]); s != "" { fmt.Fprintf(&b, "%s: %s\n", f.label, s) } } if d := htmlToText(html.UnescapeString(str(jp["description"]))); d != "" { fmt.Fprintf(&b, "\nDescription:\n%s\n", d) } return b.String() } return "" } func truncate(s string, n int) string { r := []rune(s) if len(r) <= n { return s } return string(r[:n]) + "\n[truncated]" } // extractPosting turns a fetched page into the text given to Claude. ok is // false when the page holds no usable posting text. func extractPosting(page string) (text string, ok bool) { // Plain text responses (and pasted text) pass straight through. if !strings.Contains(page, "<") { t := strings.TrimSpace(page) return truncate(t, maxPostingChars), len(t) >= minPostingChars } var b strings.Builder if m := reTitle.FindStringSubmatch(page); m != nil { fmt.Fprintf(&b, "Page title: %s\n\n", htmlToText(m[1])) } structured := structuredPosting(page) body := htmlToText(page) if structured != "" { b.WriteString("Structured data from the page:\n" + structured + "\n") // The description is already there; keep less of the noisy page text. body = truncate(body, 12_000) } if body != "" { b.WriteString("Visible page text:\n" + body + "\n") } ok = len(structured) >= minPostingChars || len(body) >= minPostingChars return truncate(b.String(), maxPostingChars), ok }