Add initial version

This commit is contained in:
2026-03-29 09:03:15 +09:00
commit ed62db01fd
11 changed files with 181 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/karune

7
LICENSE Normal file
View File

@@ -0,0 +1,7 @@
Copyright (c) 2026 Aki Kareha
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

16
Makefile Normal file
View File

@@ -0,0 +1,16 @@
all: build
build:
go build -o karune ./cmd/karune
clean:
rm -f karune
run:
go run ./cmd/karune
fmt:
go fmt ./...
test:
go test ./...

1
README Normal file
View File

@@ -0,0 +1 @@
karune - An Text-based Web Browser

38
cmd/karune/main.go Normal file
View File

@@ -0,0 +1,38 @@
package main
import (
"fmt"
"log"
"os"
"tea.kareha.org/lab/karune/internal/fetch"
"tea.kareha.org/lab/karune/internal/parser"
"tea.kareha.org/lab/karune/internal/render"
"tea.kareha.org/lab/karune/internal/util"
)
func main() {
if len(os.Args) < 2 {
fmt.Printf("Usage: %s PATH\n", os.Args[0])
return
}
u, err := util.ResolvePath(os.Args[1])
if err != nil {
log.Fatalf("failed to resolve path: %v", err)
}
htmlStr, err := fetch.Get(u)
if err != nil {
log.Fatalf("failed to fetch page: %v", err)
}
doc, err := parser.Parse(htmlStr)
if err != nil {
log.Fatalf("failed to parse page: %v", err)
}
var lines []string
parser.ExtractText(doc, &lines)
render.Print(lines)
}

5
go.mod Normal file
View File

@@ -0,0 +1,5 @@
module tea.kareha.org/lab/karune
go 1.25.0
require golang.org/x/net v0.52.0

2
go.sum Normal file
View File

@@ -0,0 +1,2 @@
golang.org/x/net v0.52.0 h1:He/TN1l0e4mmR3QqHMT2Xab3Aj3L9qjbhRm78/6jrW0=
golang.org/x/net v0.52.0/go.mod h1:R1MAz7uMZxVMualyPXb+VaqGSa3LIaUqk0eEt3w36Sw=

52
internal/fetch/fetch.go Normal file
View File

@@ -0,0 +1,52 @@
package fetch
import (
"fmt"
"io"
"net/http"
"net/url"
"os"
)
func Get(u *url.URL) (string, error) {
switch u.Scheme {
case "http", "https":
return getHTTP(u)
case "file":
return getFile(u)
default:
return "", fmt.Errorf("unsupported scheme: %s", u.Scheme)
}
}
func getHTTP(u *url.URL) (string, error) {
resp, err := http.Get(u.String())
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(body), nil
}
func getFile(u *url.URL) (string, error) {
f, err := os.Open(u.Path)
if err != nil {
return "", err
}
defer f.Close()
body, err := io.ReadAll(f)
if err != nil {
return "", err
}
return string(body), nil
}

23
internal/parser/parser.go Normal file
View File

@@ -0,0 +1,23 @@
package parser
import (
"golang.org/x/net/html"
"strings"
)
func Parse(htmlStr string) (*html.Node, error) {
return html.Parse(strings.NewReader(htmlStr))
}
func ExtractText(n *html.Node, out *[]string) {
if n.Type == html.TextNode {
text := strings.TrimSpace(n.Data)
if text != "" {
*out = append(*out, text)
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
ExtractText(c, out)
}
}

View File

@@ -0,0 +1,9 @@
package render
import "fmt"
func Print(lines []string) {
for _, l := range lines {
fmt.Println(l)
}
}

27
internal/util/util.go Normal file
View File

@@ -0,0 +1,27 @@
package util
import (
"net/url"
"os"
"path/filepath"
"regexp"
)
var schemeRe = regexp.MustCompile("^[a-zA-Z][a-zA-Z0-9+.-]*://")
func ResolvePath(s string) (*url.URL, error) {
if schemeRe.MatchString(s) {
return url.Parse(s)
}
_, err := os.Stat(s)
if err == nil {
absPath, err := filepath.Abs(s)
if err != nil {
panic(err)
}
u := &url.URL{Scheme: "file"}
u.Path = filepath.ToSlash(absPath)
return u, nil
}
return url.Parse("https://" + s)
}