From 15214b7c98cbe77bddb2a52e0849abdfa7953747 Mon Sep 17 00:00:00 2001 From: tjp Date: Tue, 9 Jan 2024 08:26:52 -0700 Subject: "outline" command --- actions.go | 38 +++++++++++++++++++++++++++++++++++++- command.go | 6 ++++++ help.go | 15 +++++++++++---- 3 files changed, 54 insertions(+), 5 deletions(-) diff --git a/actions.go b/actions.go index 55ec352..28713ee 100644 --- a/actions.go +++ b/actions.go @@ -15,6 +15,7 @@ import ( "tildegit.org/tjp/sliderule" "tildegit.org/tjp/sliderule/gemini" + "tildegit.org/tjp/sliderule/gemini/gemtext" "tildegit.org/tjp/sliderule/gopher" ) @@ -31,6 +32,7 @@ var ( ErrSaveNeedsFilename = errors.New("save requires a filename argument") ErrInvalidMarkArgs = errors.New("mark what?") ErrInvalidTourArgs = errors.New("tour what?") + ErrOnlyTextGemini = errors.New("that is only supported for text/gemini pages") ) func About(_ *BrowserState) error { @@ -111,7 +113,7 @@ func Reload(state *BrowserState, conf *Config) error { urlStr, _ = gopherURL(state.Url) } - var client = sliderule.NewClient(tlsConfig(state)) + client := sliderule.NewClient(tlsConfig(state)) var response *sliderule.Response var err error @@ -468,6 +470,40 @@ func Print(state *BrowserState) error { return print(state) } +func Outline(state *BrowserState, conf *Config) error { + if state.Body == nil { + return ErrMustBeOnAPage + } + + if state.DocType != "text/gemini" { + return ErrOnlyTextGemini + } + + gemdoc, err := gemtext.Parse(bytes.NewBuffer(state.Body)) + if err != nil { + return err + } + + b := &bytes.Buffer{} + for _, line := range gemdoc { + switch line.Type() { + case gemtext.LineTypeHeading3, gemtext.LineTypeHeading2, gemtext.LineTypeHeading1: + if _, err := b.Write(line.Raw()); err != nil { + return err + } + } + } + + formatted, _, err := parseGemtextDoc(b.Bytes(), conf.SoftWrap) + if err != nil { + return err + } + + state.Modal = []byte(formatted) + + return Print(state) +} + func Links(state *BrowserState, conf *Config) error { if state.Links == nil { return ErrMustBeOnAPage diff --git a/command.go b/command.go index 758b764..734aa6e 100644 --- a/command.go +++ b/command.go @@ -85,6 +85,10 @@ func ParseCommand(line string) (*Command, error) { if strings.HasPrefix("next", cmd) { return &Command{Name: "next"}, nil } + case 'o': + if strings.HasPrefix("outline", cmd) { + return &Command{Name: "outline"}, nil + } case 'p': if strings.HasPrefix("print", cmd) { return &Command{Name: "print"}, nil @@ -394,6 +398,8 @@ func RunCommand(conf *Config, cmd *Command, state *BrowserState) error { return Go(state, cmd.Args[0], conf) case "help": return Help(state, cmd.Args[0]) + case "outline": + return Outline(state, conf) case "pipe": return Pipe(state, cmd.Args[0]) case "print": diff --git a/help.go b/help.go index 6e50ff7..3d5cd88 100644 --- a/help.go +++ b/help.go @@ -8,7 +8,7 @@ import ( var ErrNotAHelpTopic = errors.New("don't recognize that help topic") -func Help(state *BrowserState, topic string) error { +func Help(_ *BrowserState, topic string) error { if topic == "" { topic = "topics" } @@ -42,10 +42,11 @@ root Root up back forward next previous reload print pipe -help links history +about help +go history save +links outline tour mark identity -go save -about quit +quit Any command may be written as any prefix sufficiently long to be unique. So for instance, "reload" can be specified by just typing "re". This is @@ -277,6 +278,12 @@ starting points. l[inks] ------- Shortens the current page by only displaying the links in it. +`[1:], + + "outline": ` +o[utline] +--------- +Shortens the current page by online displaying the headers in it. `[1:], "history": ` -- cgit v1.2.3