Update parser
This commit is contained in:
@@ -4,7 +4,7 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/runescape-wiki/rsw/internal/extract"
|
||||
"github.com/runescape-wiki/rsw/internal/htmlconv"
|
||||
"github.com/runescape-wiki/rsw/internal/render"
|
||||
"github.com/runescape-wiki/rsw/internal/wiki"
|
||||
"github.com/spf13/cobra"
|
||||
@@ -27,80 +27,54 @@ Examples:
|
||||
title := args[0]
|
||||
client := wiki.NewClient(GameBaseURL())
|
||||
|
||||
page, err := client.GetPage(title)
|
||||
if Raw() {
|
||||
page, err := client.GetPage(title)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to fetch page: %w", err)
|
||||
}
|
||||
fmt.Println(page.Wikitext)
|
||||
return nil
|
||||
}
|
||||
|
||||
page, err := client.GetPageHTML(title)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to fetch page: %w", err)
|
||||
}
|
||||
|
||||
wikitext := page.Wikitext
|
||||
|
||||
if pageSection != "" {
|
||||
idx := wiki.FindSectionIndex(page.Sections, pageSection)
|
||||
if idx == -1 {
|
||||
needle := strings.ToLower(pageSection)
|
||||
// Case-insensitive exact match
|
||||
for _, s := range page.Sections {
|
||||
if strings.ToLower(s.Line) == needle {
|
||||
fmt.Sscanf(s.Index, "%d", &idx)
|
||||
break
|
||||
}
|
||||
}
|
||||
// Case-insensitive prefix match (e.g. "Location" → "Locations")
|
||||
if idx == -1 {
|
||||
for _, s := range page.Sections {
|
||||
if strings.HasPrefix(strings.ToLower(s.Line), needle) {
|
||||
fmt.Sscanf(s.Index, "%d", &idx)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
// Case-insensitive contains match
|
||||
if idx == -1 {
|
||||
for _, s := range page.Sections {
|
||||
if strings.Contains(strings.ToLower(s.Line), needle) {
|
||||
fmt.Sscanf(s.Index, "%d", &idx)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if idx == -1 {
|
||||
body := htmlconv.ExtractSection(page.HTML, pageSection)
|
||||
if body == "" {
|
||||
sections := htmlconv.ListSections(page.HTML)
|
||||
return fmt.Errorf("section %q not found. Available sections: %s",
|
||||
pageSection, listSections(page.Sections))
|
||||
pageSection, formatSectionNames(sections))
|
||||
}
|
||||
sectionPage, err := client.GetPageSection(title, idx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to fetch section: %w", err)
|
||||
}
|
||||
wikitext = sectionPage.Wikitext
|
||||
}
|
||||
|
||||
if Raw() {
|
||||
fmt.Println(wikitext)
|
||||
md := render.New()
|
||||
md.H1(page.Title)
|
||||
md.Line(body)
|
||||
fmt.Print(md.String())
|
||||
return nil
|
||||
}
|
||||
|
||||
md := render.New()
|
||||
md.H1(page.Title)
|
||||
|
||||
if pageSection == "" && len(page.Sections) > 0 {
|
||||
sections := htmlconv.ListSections(page.HTML)
|
||||
if len(sections) > 0 {
|
||||
md.H2("Sections")
|
||||
for _, s := range page.Sections {
|
||||
for _, s := range sections {
|
||||
indent := ""
|
||||
if s.Level == "3" {
|
||||
if s.Level == 3 {
|
||||
indent = " "
|
||||
} else if s.Level == "4" {
|
||||
} else if s.Level >= 4 {
|
||||
indent = " "
|
||||
}
|
||||
md.Line(fmt.Sprintf("%s- %s", indent, s.Line))
|
||||
md.Line(fmt.Sprintf("%s- %s", indent, s.Name))
|
||||
}
|
||||
md.Newline()
|
||||
md.HR()
|
||||
}
|
||||
|
||||
plain := extract.ExtractPlainText(wikitext)
|
||||
md.P(plain)
|
||||
|
||||
md.Line(htmlconv.Convert(page.HTML))
|
||||
fmt.Print(md.String())
|
||||
return nil
|
||||
},
|
||||
@@ -110,10 +84,10 @@ Examples:
|
||||
return cmd
|
||||
}
|
||||
|
||||
func listSections(sections []wiki.Section) string {
|
||||
func formatSectionNames(sections []htmlconv.SectionInfo) string {
|
||||
names := make([]string, len(sections))
|
||||
for i, s := range sections {
|
||||
names[i] = s.Line
|
||||
names[i] = s.Name
|
||||
}
|
||||
return strings.Join(names, ", ")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user