fork from prologic/pages-server
This commit is contained in:
parent
9d86fd33c6
commit
839a429820
33 changed files with 865 additions and 2482 deletions
180
gitea/frontmatter.go
Normal file
180
gitea/frontmatter.go
Normal file
|
@ -0,0 +1,180 @@
|
|||
// Package gitea ...
|
||||
// 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.
|
||||
package gitea
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
chromahtml "github.com/alecthomas/chroma/v2/formatters/html"
|
||||
"github.com/yuin/goldmark"
|
||||
highlighting "github.com/yuin/goldmark-highlighting/v2"
|
||||
"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(
|
||||
chromahtml.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
gitea/fs.go
Normal file
93
gitea/fs.go
Normal 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
|
||||
}
|
218
gitea/gitea.go
Normal file
218
gitea/gitea.go
Normal file
|
@ -0,0 +1,218 @@
|
|||
package gitea
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"code.gitea.io/sdk/gitea"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultPagesRef = "gitea-pages"
|
||||
defaultPagesTopic = "gitea-pages"
|
||||
)
|
||||
|
||||
// Client ...
|
||||
type Client struct {
|
||||
cli *gitea.Client
|
||||
url string
|
||||
domain string
|
||||
token string
|
||||
}
|
||||
|
||||
// NewClient ...
|
||||
func NewClient(url, token, domain string) (*Client, error) {
|
||||
cli, err := gitea.NewClient(url, gitea.SetToken(token), gitea.SetGiteaVersion(""))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Client{
|
||||
cli: cli,
|
||||
url: url,
|
||||
domain: domain,
|
||||
token: token,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Open ...
|
||||
func (c *Client) Open(user, path string) (fs.File, error) {
|
||||
log.Printf("user: %s", user)
|
||||
log.Printf("path: %s", path)
|
||||
|
||||
var (
|
||||
repo string
|
||||
filepath string
|
||||
)
|
||||
|
||||
parts := strings.Split(strings.TrimLeft(path, "/"), "/")
|
||||
log.Printf("parts: %d #%v", len(parts), parts)
|
||||
if len(parts) > 1 {
|
||||
repo = parts[0]
|
||||
filepath = strings.Join(parts[1:], "/")
|
||||
} else {
|
||||
repo = fmt.Sprintf("%s.%s", user, c.domain)
|
||||
filepath = path
|
||||
}
|
||||
log.Printf("repo: %s", repo)
|
||||
|
||||
// if path is empty they want to have the index.html
|
||||
if filepath == "" || filepath == "/" {
|
||||
filepath = "index.html"
|
||||
}
|
||||
|
||||
// If it's a dir, give them index.html
|
||||
if strings.HasSuffix(filepath, "/") {
|
||||
filepath += "index.html"
|
||||
}
|
||||
|
||||
allowed := c.allowsPages(user, repo)
|
||||
log.Printf("allowed? %t", allowed)
|
||||
if !allowed && repo != fmt.Sprintf("%s.%s", user, c.domain) {
|
||||
repo = fmt.Sprintf("%s.%s", user, c.domain)
|
||||
if c.allowsPages(user, repo) {
|
||||
filepath = path
|
||||
} else {
|
||||
return nil, fs.ErrNotExist
|
||||
}
|
||||
}
|
||||
|
||||
res, err := c.getRawFileOrLFS(user, repo, filepath, defaultPagesRef)
|
||||
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
|
||||
)
|
||||
|
||||
// TODO: make pr for go-sdk
|
||||
// gitea sdk doesn't support "media" type for lfs/non-lfs
|
||||
giteaURL, err = url.JoinPath(c.url+"/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)
|
||||
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
switch res.StatusCode {
|
||||
case http.StatusNotFound:
|
||||
return nil, fs.ErrNotExist
|
||||
case http.StatusOK:
|
||||
default:
|
||||
return nil, fmt.Errorf("unexpected status code '%d'", res.StatusCode)
|
||||
}
|
||||
|
||||
data, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
var bufPool = sync.Pool{
|
||||
New: func() any {
|
||||
return new(bytes.Buffer)
|
||||
},
|
||||
}
|
||||
|
||||
func handleMD(res []byte) ([]byte, error) {
|
||||
meta, body, err := extractFrontMatter(string(res))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
html, err := markdown([]byte(body))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
title, _ := meta["title"].(string)
|
||||
|
||||
res = append([]byte("<!DOCTYPE html>\n<html>\n<body>\n<h1>"), []byte(title)...)
|
||||
res = append(res, []byte("</h1>")...)
|
||||
res = append(res, html...)
|
||||
res = append(res, []byte("</body></html>")...)
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (c *Client) repoTopics(owner, repo string) ([]string, error) {
|
||||
repos, _, err := c.cli.ListRepoTopics(owner, repo, gitea.ListRepoTopicsOptions{})
|
||||
return repos, err
|
||||
}
|
||||
|
||||
func (c *Client) hasRepoBranch(owner, repo, branch string) bool {
|
||||
b, _, err := c.cli.GetRepoBranch(owner, repo, branch)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return b.Name == branch
|
||||
}
|
||||
|
||||
func (c *Client) allowsPages(owner, repo string) bool {
|
||||
topics, err := c.repoTopics(owner, repo)
|
||||
if err != nil {
|
||||
log.Printf("error finding topics for %s/%s: %s", owner, repo, err)
|
||||
return false
|
||||
}
|
||||
|
||||
for _, topic := range topics {
|
||||
if topic == defaultPagesTopic {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func splitName(name string) (string, string, string) {
|
||||
parts := strings.Split(name, "/")
|
||||
|
||||
// parts contains: ["owner", "repo", "filepath"]
|
||||
switch len(parts) {
|
||||
case 1:
|
||||
return parts[0], "", ""
|
||||
case 2:
|
||||
return parts[0], parts[1], ""
|
||||
default:
|
||||
return parts[0], parts[1], strings.Join(parts[2:], "/")
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue