sophuwu.site > manhttpd
idk lol
sophuwu sophie@skisiel.com
Thu, 07 Mar 2024 14:58:35 +0100
commit

58a56f0454fd00833cdaf9d373f2d83c6a9f0f44

parent

bbd83dea25c12a344ed3467eb0826305a9bcba23

6 files changed, 89 insertions(+), 145 deletions(-)

jump to
M dark_theme.cssdark_theme.css

@@ -1,49 +1,61 @@

* { - border-radius: 5px; color: #c7c7c7; - margin: 5px 0; - padding: 0; } - -dd { - margin: 0 0 0 20px; +i, em { + color: #7f7f7f; } - -dt { - margin: 10px 0 0 10px; +dt, b, strong { + color: #f0f0f0; } - hr { color: #363636; margin-top: 10px; } - .flexy { display: flex; flex-direction: row; justify-content: flex-start; } - body, html { background-color: #1f1e23; color: #c7c7c7; - padding: 0 5px; +} +h1, h2, h3, h4, h5, h6 { + color: #8d53a6; +} +h1 { + font-size: 20px; +} +h2 { + font-size: 16px; } - +h3 { + font-size: 14px; +} +.section { + padding: 2px 10px; +} +p, dt { + margin: 2px 10px; +} +a { + color: #ff7597; + text-decoration: none; +} +a:hover { + text-decoration: underline; +} input, textarea { background-color: #343434; border: 1px solid #c7c7c7; padding: 4px; + border-radius: 5px; } input[type="text"], textarea { height: inherit; width: 300px; -} - -label { - margin: 0 10px; - padding: 0; + margin: 2px 10px; } input[type="checkbox"] {

@@ -55,27 +67,5 @@ input[type="submit"] {

background-color: #343434; font-weight: bold; cursor: pointer; - margin: 0 10px; -} - -h1, h2, h3, h4, h5, h6 { - color: #8d53a6; - padding: 5px 0; -} - -a { - color: #ff7597; - text-decoration: none; -} - -a:hover { - text-decoration: underline; -} - -b { - color: #f0f0f0; -} - -i { - color: #7f7f7f; + margin: 0; }
M go.modgo.mod

@@ -16,6 +16,8 @@ github.com/hashicorp/errwrap v1.0.0 // indirect

github.com/hashicorp/go-multierror v1.1.1 // indirect github.com/klauspost/compress v1.15.9 // indirect github.com/klauspost/pgzip v1.2.5 // indirect + github.com/mandelsoft/filepath v0.0.0-20240223090642-3e2777258aa3 // indirect + github.com/mandelsoft/vfs v0.4.3 // indirect github.com/nwaples/rardecode/v2 v2.0.0-beta.2 // indirect github.com/pierrec/lz4/v4 v4.1.15 // indirect github.com/therootcompany/xz v1.0.1 // indirect
M go.sumgo.sum

@@ -89,6 +89,10 @@ github.com/klauspost/pgzip v1.2.5/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs=

github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/mandelsoft/filepath v0.0.0-20240223090642-3e2777258aa3 h1:oo9nIgnyiBgYPbcZslRT4y29siuL5EoNJ/t1tr0xEVQ= +github.com/mandelsoft/filepath v0.0.0-20240223090642-3e2777258aa3/go.mod h1:LxhqC7khDoRENwooP6f/vWvia9ivj6TqLYrR39zqkN0= +github.com/mandelsoft/vfs v0.4.3 h1:2UMrxQkMXkcHyuqSFhgFDupQ1fmqpKLZuu04DOHx1PA= +github.com/mandelsoft/vfs v0.4.3/go.mod h1:zmbhx2ueQc96buqNXg2S88McBMm2mNFNeyGSpSebrHw= github.com/mholt/archiver/v4 v4.0.0-alpha.8 h1:tRGQuDVPh66WCOelqe6LIGh0gwmfwxUrSSDunscGsRM= github.com/mholt/archiver/v4 v4.0.0-alpha.8/go.mod h1:5f7FUYGXdJWUjESffJaYR4R60VhnHxb2X3T1teMyv5A= github.com/nwaples/rardecode/v2 v2.0.0-beta.2 h1:e3mzJFJs4k83GXBEiTaQ5HgSc/kOK8q0rDaRO0MPaOk=
M index.htmlindex.html

@@ -1,10 +1,10 @@

-<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> -<HTML> -<HEAD> -<TITLE>Man Pages Index</TITLE> -<link rel="stylesheet" href="/style.css"> -</HEAD> -<BODY> +<html> +<head> +<meta charset="utf-8"> +<title>Web Manpages</title> +<link rel="stylesheet" type="text/css" href="/style.css"> +</head> +<body> <H1>Man Pages Index</H1> <HR> <H2>Open Page:</H2>

@@ -30,5 +30,5 @@ </div>

</FORM> <HR> <a href="//{{ host }}">{{ host }}</a><br> -</BODY> -</HTML>+</body> +</html>
M main.gomain.go

@@ -9,6 +9,7 @@ "log"

"net/http" "os" "os/exec" + "regexp" "strings" )

@@ -34,7 +35,7 @@ }

func cmdout(s string) string { ss := strings.Split(s, " ") - b, e := exec.Command("/usr/bin/"+ss[0], ss[1:]...).Output() + b, e := exec.Command(ss[0], ss[1:]...).Output() if e != nil { log.Fatal("Fatal: unable to get " + ss[0]) }

@@ -42,15 +43,15 @@ return strings.TrimSpace(string(b))

} func init() { - CFG.MANPATH = cmdout("manpath -g") + CFG.MANPATH = cmdout("manpath") CFG.Hostname = cmdout("hostname") + CFG.Pandoc = cmdout("which pandoc") CFG.ListenAddr = os.Getenv("ListenAddr") CFG.ListenPort = os.Getenv("ListenPort") if CFG.ListenPort == "" { CFG.ListenPort = "8082" } - b, _ := exec.Command("which", "pandoc").Output() - CFG.Pandoc = strings.TrimSpace(string(b)) + css = append(css, font...) }

@@ -74,11 +75,25 @@ http.ListenAndServe(CFG.ListenAddr+":"+CFG.ListenPort, nil)

} +const htmlHeader = `<!DOCTYPE html> +<html> +<head> +<meta charset="utf-8"> +<title>{{ title }}</title> +<link rel="stylesheet" type="text/css" href="/style.css"> +</head> +<body> +` + +func HtmlHeader(body *string, title string) { + *body = strings.ReplaceAll(htmlHeader, "{{ title }}", strings.ToUpper(title)) + *body + "</body></html>" + *body = strings.Replace(*body, "<h1>NAME</h1>", "<h1>"+strings.ToUpper(title)+"</h1>", 1) +} + type ManPage struct { - Section string + Section int Name string Path string - WhatIs string } func readCompressed(fh *os.File, buff *bytes.Buffer) error {

@@ -106,21 +121,8 @@ _, err = buff.ReadFrom(fh)

return buff.String(), err } -func runM2h(input string, host string) (string, error) { - var inbuff bytes.Buffer - inbuff.WriteString(input) - cmd := exec.Command("manweb-conv", "-H", host, "-M", "/", "-") - cmd.Stdin = &inbuff - b, err := cmd.Output() - return string(b), err - -} - func pandocConvert(input string) (string, error) { - if CFG.Pandoc == "" { - return "", fmt.Errorf("pandoc not found, required for syntax conversion") - } - cmd := exec.Command(CFG.Pandoc, "-st", "man", "-f", "man") + cmd := exec.Command(CFG.Pandoc, "--section-divs", "-t", "html4", "-f", "man") cmd.Env = append(cmd.Env, os.Environ()...) cmd.Stdin = strings.NewReader(input) b, err := cmd.Output()

@@ -137,20 +139,11 @@ fh, err = ReadFh(m.Path)

if err != nil { return err } - b, err = runM2h(fh, r.Host) + b, err = pandocConvert(fh) if err != nil { - return fmt.Errorf("page not found") - } - if strings.Contains(b, "<TITLE>Invalid Man Page</TITLE>") { - fh, err = pandocConvert(fh) - if err != nil { - return err - } - b, err = runM2h(fh, r.Host) - if err != nil { - return err - } + return err } + HtmlHeader(&b, m.Name) w.Header().Set("Content-Type", "text/html; charset=utf-8") w.WriteHeader(http.StatusOK) fmt.Fprint(w, b)

@@ -159,11 +152,10 @@ }

func (m *ManPage) FindPath() error { s := m.Name - if m.Section != "" { - s = m.Section + "." + s + if m.Section > 0 { + s += "." + fmt.Sprint(m.Section) } cmd := exec.Command("man", "-w", s) - cmd.Env = append(os.Environ(), "MANPATH="+CFG.MANPATH) b, e := cmd.Output() if e != nil { return fmt.Errorf("page not found")

@@ -172,19 +164,19 @@ m.Path = strings.TrimSpace(string(b))

return nil } -func (m *ManPage) FindHumanInput(s string) error { +var manRegexp = []*regexp.Regexp{regexp.MustCompile(`\.[1-9]$`), regexp.MustCompile(`( )?[(][1-9][)]$`)} + +func (m *ManPage) ParseName(s string) (err error) { s = strings.TrimSpace(s) - s = strings.ReplaceAll(s, "(", "") - s = strings.ReplaceAll(s, ")", "") - if strings.Contains(s, " ") { - arr := strings.SplitN(s, " ", 2) - m.Section, m.Name = arr[1], arr[0] - } else if strings.Contains(s, ".") { - arr := strings.SplitN(s, ".", 2) - m.Section, m.Name = arr[0], arr[1] - } else { - m.Name = s + for i, rx := range manRegexp { + if rx.MatchString(s) { + m.Section = int((s[len(s)-i-1]) - '0') + m.Name = strings.TrimSpace(s[:len(s)-i-2]) + return m.FindPath() + } } + m.Section = 0 + m.Name = s return m.FindPath() }

@@ -195,9 +187,11 @@ search := strings.ReplaceAll(r.Form["search"][0], "\r", "")

args += "\n" + search cmd := exec.Command("apropos", strings.Split(args, "\n")...) cmd.Env = append(cmd.Env, os.Environ()...) - cmd.Env = append(cmd.Env, "MANPATH="+CFG.MANPATH) + // cmd.Env = append(cmd.Env, "MANPATH="+CFG.MANPATH) b, e := cmd.Output() if e != nil { + http.Error(w, "no results", http.StatusNotFound) + return } w.Header().Set("Content-Type", "text/html; charset=utf-8") w.WriteHeader(http.StatusOK)

@@ -220,7 +214,7 @@ }

_ = r.ParseForm() q := r.Form.Get("man") var man ManPage - if err := man.FindHumanInput(q); err != nil { + if err := man.ParseName(q); err != nil { w.Header().Set("Content-Type", "text/html; charset=utf-8") w.WriteHeader(http.StatusOK) w.Write(bytes.ReplaceAll(index, []byte("{{ host }}"), []byte(r.Host)))
D virfs.go

@@ -1,46 +0,0 @@

-package main - -/* -import ( - "fmt" - "github.com/mandelsoft/vfs/pkg/memoryfs" - "github.com/mandelsoft/vfs/pkg/vfs" - "os/exec" - "strings" -) - -func Cmd(s string) []byte { - arg := strings.Split(s, " ") - cmd := exec.Command(arg[0], arg[1:]...) - out, err := cmd.CombinedOutput() - Err(err) - return out -} - -func Err(err error) { - if err != nil { - panic(err) - } -} - -func printDir(dir string) { - fi, err := vfs.ReadDir(fs, dir) - Err(err) - fmt.Println("reading dir: ", dir) - for _, f := range fi { - fmt.Println(f.Name(), f.IsDir(), f.Size()) - } -} - -var fs vfs.FileSystem - -func main() { - fs = memoryfs.New() - wd, err := fs.Getwd() - Err(err) - - printDir(wd) - -} - -*/