Initial commit

This commit is contained in:
Wim
2022-10-30 22:46:54 +01:00
parent e6e5634d44
commit 4511c4c017
6 changed files with 1739 additions and 0 deletions

181
pkg/gitea/frontmatter.go Normal file
View File

@ -0,0 +1,181 @@
// Taken from caddy source code (https://github.com/mholt/caddy/)
// Copyright 2015 Matthew Holt and The Caddy Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//nolint:all
package gitea
import (
"bytes"
"encoding/json"
"fmt"
"strings"
"unicode"
"github.com/BurntSushi/toml"
"github.com/alecthomas/chroma/formatters/html"
"github.com/yuin/goldmark"
highlighting "github.com/yuin/goldmark-highlighting"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/parser"
gmhtml "github.com/yuin/goldmark/renderer/html"
"gopkg.in/yaml.v3"
)
func extractFrontMatter(input string) (map[string]any, string, error) {
// get the bounds of the first non-empty line
var firstLineStart, firstLineEnd int
lineEmpty := true
for i, b := range input {
if b == '\n' {
firstLineStart = firstLineEnd
if firstLineStart > 0 {
firstLineStart++ // skip newline character
}
firstLineEnd = i
if !lineEmpty {
break
}
continue
}
lineEmpty = lineEmpty && unicode.IsSpace(b)
}
firstLine := input[firstLineStart:firstLineEnd]
// ensure residue windows carriage return byte is removed
firstLine = strings.TrimSpace(firstLine)
// see what kind of front matter there is, if any
var closingFence []string
var fmParser func([]byte) (map[string]any, error)
for _, fmType := range supportedFrontMatterTypes {
if firstLine == fmType.FenceOpen {
closingFence = fmType.FenceClose
fmParser = fmType.ParseFunc
}
}
if fmParser == nil {
// no recognized front matter; whole document is body
return nil, input, nil
}
// find end of front matter
var fmEndFence string
fmEndFenceStart := -1
for _, fence := range closingFence {
index := strings.Index(input[firstLineEnd:], "\n"+fence)
if index >= 0 {
fmEndFenceStart = index
fmEndFence = fence
break
}
}
if fmEndFenceStart < 0 {
return nil, "", fmt.Errorf("unterminated front matter")
}
fmEndFenceStart += firstLineEnd + 1 // add 1 to account for newline
// extract and parse front matter
frontMatter := input[firstLineEnd:fmEndFenceStart]
fm, err := fmParser([]byte(frontMatter))
if err != nil {
return nil, "", err
}
// the rest is the body
body := input[fmEndFenceStart+len(fmEndFence):]
return fm, body, nil
}
func yamlFrontMatter(input []byte) (map[string]any, error) {
m := make(map[string]any)
err := yaml.Unmarshal(input, &m)
return m, err
}
func tomlFrontMatter(input []byte) (map[string]any, error) {
m := make(map[string]any)
err := toml.Unmarshal(input, &m)
return m, err
}
func jsonFrontMatter(input []byte) (map[string]any, error) {
input = append([]byte{'{'}, input...)
input = append(input, '}')
m := make(map[string]any)
err := json.Unmarshal(input, &m)
return m, err
}
type parsedMarkdownDoc struct {
Meta map[string]any `json:"meta,omitempty"`
Body string `json:"body,omitempty"`
}
type frontMatterType struct {
FenceOpen string
FenceClose []string
ParseFunc func(input []byte) (map[string]any, error)
}
var supportedFrontMatterTypes = []frontMatterType{
{
FenceOpen: "---",
FenceClose: []string{"---", "..."},
ParseFunc: yamlFrontMatter,
},
{
FenceOpen: "+++",
FenceClose: []string{"+++"},
ParseFunc: tomlFrontMatter,
},
{
FenceOpen: "{",
FenceClose: []string{"}"},
ParseFunc: jsonFrontMatter,
},
}
func markdown(input []byte) ([]byte, error) {
md := goldmark.New(
goldmark.WithExtensions(
extension.GFM,
extension.Footnote,
highlighting.NewHighlighting(
highlighting.WithFormatOptions(
html.WithClasses(true),
),
),
),
goldmark.WithParserOptions(
parser.WithAutoHeadingID(),
),
goldmark.WithRendererOptions(
gmhtml.WithUnsafe(), // TODO: this is not awesome, maybe should be configurable?
),
)
buf := bufPool.Get().(*bytes.Buffer)
buf.Reset()
defer bufPool.Put(buf)
if err := md.Convert(input, buf); err != nil {
return input, err
}
return buf.Bytes(), nil
}

93
pkg/gitea/fs.go Normal file
View File

@ -0,0 +1,93 @@
package gitea
import (
"io"
"io/fs"
"time"
)
type fileInfo struct {
size int64
isdir bool
name string
}
type openFile struct {
content []byte
offset int64
name string
isdir bool
}
func (g fileInfo) Name() string {
return g.name
}
func (g fileInfo) Size() int64 {
return g.size
}
func (g fileInfo) Mode() fs.FileMode {
return 0o444
}
func (g fileInfo) ModTime() time.Time {
return time.Time{}
}
func (g fileInfo) Sys() any {
return nil
}
func (g fileInfo) IsDir() bool {
return g.isdir
}
var _ io.Seeker = (*openFile)(nil)
func (o *openFile) Close() error {
return nil
}
func (o *openFile) Stat() (fs.FileInfo, error) {
return fileInfo{
size: int64(len(o.content)),
isdir: o.isdir,
name: o.name,
}, nil
}
func (o *openFile) Read(b []byte) (int, error) {
if o.offset >= int64(len(o.content)) {
return 0, io.EOF
}
if o.offset < 0 {
return 0, &fs.PathError{Op: "read", Path: o.name, Err: fs.ErrInvalid}
}
n := copy(b, o.content[o.offset:])
o.offset += int64(n)
return n, nil
}
func (o *openFile) Seek(offset int64, whence int) (int64, error) {
switch whence {
case 0:
offset += 0
case 1:
offset += o.offset
case 2:
offset += int64(len(o.content))
}
if offset < 0 || offset > int64(len(o.content)) {
return 0, &fs.PathError{Op: "seek", Path: o.name, Err: fs.ErrInvalid}
}
o.offset = offset
return offset, nil
}

238
pkg/gitea/gitea.go Normal file
View File

@ -0,0 +1,238 @@
package gitea
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/fs"
"net/http"
"net/url"
"strings"
"sync"
"github.com/spf13/viper"
)
type Client struct {
serverURL string
token string
}
type pagesConfig struct {
}
type topicsResponse struct {
Topics []string `json:"topics"`
}
func NewClient(serverURL, token string) *Client {
return &Client{
serverURL: serverURL,
token: token,
}
}
func (c *Client) Open(name, ref string) (fs.File, error) {
if ref == "" {
ref = "gitea-pages"
}
owner, repo, filepath, err := splitName(name)
if err != nil {
return nil, err
}
if !c.allowsPages(owner, repo) {
return nil, fs.ErrNotExist
}
if err := c.readConfig(owner, repo); err != nil {
return nil, err
}
if !validRefs(ref) {
return nil, fs.ErrNotExist
}
res, err := c.getRawFileOrLFS(owner, repo, filepath, ref)
if err != nil {
return nil, err
}
if strings.HasSuffix(filepath, ".md") {
res, err = handleMD(res)
if err != nil {
return nil, err
}
}
return &openFile{
content: res,
name: filepath,
}, nil
}
func (c *Client) getRawFileOrLFS(owner, repo, filepath, ref string) ([]byte, error) {
var (
giteaURL string
err error
)
giteaURL, err = url.JoinPath(c.serverURL+"/api/v1/repos/", owner, repo, "media", filepath)
if err != nil {
return nil, err
}
giteaURL += "?ref=" + url.QueryEscape(ref)
req, err := http.NewRequest(http.MethodGet, giteaURL, nil)
if err != nil {
return nil, err
}
req.Header.Add("Authorization", "token "+c.token)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
switch resp.StatusCode {
case http.StatusNotFound:
return nil, fs.ErrNotExist
case http.StatusOK:
default:
return nil, fmt.Errorf("unexpected status code '%d'", resp.StatusCode)
}
res, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return res, nil
}
var bufPool = sync.Pool{
New: func() any {
return new(bytes.Buffer)
},
}
func handleMD(res []byte) ([]byte, error) {
meta, resbody, err := extractFrontMatter(string(res))
if err != nil {
return nil, err
}
resmd, err := markdown([]byte(resbody))
if err != nil {
return nil, err
}
res = append([]byte("<!DOCTYPE html>\n<html>\n<body>\n<h1>"), []byte(meta["title"].(string))...)
res = append(res, []byte("</h1>")...)
res = append(res, resmd...)
res = append(res, []byte("</body></html>")...)
return res, nil
}
func (c *Client) repoTopics(owner, repo string) ([]string, error) {
var (
giteaURL string
err error
)
giteaURL, err = url.JoinPath(c.serverURL+"/api/v1/repos/", owner, repo, "topics")
if err != nil {
return nil, err
}
req, err := http.NewRequest(http.MethodGet, giteaURL, nil)
if err != nil {
return nil, err
}
req.Header.Add("Authorization", "token "+c.token)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
res, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
defer resp.Body.Close()
t := topicsResponse{}
json.Unmarshal(res, &t)
return t.Topics, nil
}
func (c *Client) allowsPages(owner, repo string) bool {
topics, err := c.repoTopics(owner, repo)
if err != nil {
return false
}
for _, topic := range topics {
if topic == "gitea-pages" {
return true
}
}
return false
}
func (c *Client) readConfig(owner, repo string) error {
cfg, err := c.getRawFileOrLFS(owner, repo, "gitea-pages.toml", "gitea-pages")
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return err
}
viper.SetConfigType("toml")
viper.ReadConfig(bytes.NewBuffer(cfg))
return nil
}
func splitName(name string) (string, string, string, error) {
parts := strings.Split(name, "/")
// parts contains: ["owner", "repo", "filepath"]
// return invalid path if not enough parts
if len(parts) < 3 {
return "", "", "", fs.ErrInvalid
}
owner := parts[0]
repo := parts[1]
filepath := strings.Join(parts[2:], "/")
return owner, repo, filepath, nil
}
func validRefs(ref string) bool {
validrefs := viper.GetStringSlice("allowedrefs")
for _, r := range validrefs {
if r == ref {
return true
}
if r == "*" {
return true
}
}
return false
}