Skip to content

Commit

Permalink
feat: extractHeader
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfogre committed Jan 14, 2023
1 parent a76bfed commit 9530aae
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
20 changes: 18 additions & 2 deletions modules/markup/asciicast/asciicast.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
package asciicast

import (
"bufio"
"fmt"
"io"
"net/url"
"regexp"

"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/setting"
)
Expand Down Expand Up @@ -40,12 +42,20 @@ const (
func (Renderer) SanitizerRules() []setting.MarkupSanitizerRule {
return []setting.MarkupSanitizerRule{
{Element: "div", AllowAttr: "class", Regexp: regexp.MustCompile(playerClassName)},
{Element: "div", AllowAttr: "style"},
{Element: "div", AllowAttr: playerSrcAttr},
}
}

// Render implements markup.Renderer
func (Renderer) Render(ctx *markup.RenderContext, _ io.Reader, output io.Writer) error {
func (Renderer) Render(ctx *markup.RenderContext, input io.Reader, output io.Writer) error {
var h *header
if firstLine, err := bufio.NewReader(input).ReadBytes('\n'); err != nil {
return err
} else if h, err = extractHeader(firstLine); err != nil {
log.Warn("extract header from %s: %v", ctx.RelativePath, err)
}

rawURL := fmt.Sprintf("%s/%s/%s/raw/%s/%s",
setting.AppSubURL,
url.PathEscape(ctx.Metas["user"]),
Expand All @@ -54,9 +64,15 @@ func (Renderer) Render(ctx *markup.RenderContext, _ io.Reader, output io.Writer)
url.PathEscape(ctx.RelativePath),
)

style := ""
if h != nil {
style = fmt.Sprintf("aspect-ratio: %d / %d", h.Width, h.Height)
}

_, err := io.WriteString(output, fmt.Sprintf(
`<div class="%s" %s="%s"></div>`,
`<div class="%s" style="%s" %s="%s"></div>`,
playerClassName,
style,
playerSrcAttr,
rawURL,
))
Expand Down
43 changes: 43 additions & 0 deletions modules/markup/asciicast/header.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package asciicast

import (
"fmt"

"code.gitea.io/gitea/modules/json"
)

type header struct {
// Required header attributes:
Version int `json:"version"`
Width int `json:"width"`
Height int `json:"height"`

// Optional header attributes:
Timestamp int `json:"timestamp"`
Duration float64 `json:"duration"`
IdleTimeLimit float64 `json:"idle_time_limit"`
Command string `json:"command"`
Title string `json:"title"`
Env map[string]string `json:"env"`
Theme string `json:"theme"`
}

func extractHeader(data []byte) (*header, error) {
h := &header{}
if err := json.Unmarshal(data, h); err != nil {
return nil, fmt.Errorf("json unmarshal: %w", err)
}
if h.Version != 2 {
return nil, fmt.Errorf("unknown version: %v", h.Version)
}
if h.Width <= 0 {
return nil, fmt.Errorf("invalid width: %v", h.Width)
}
if h.Height <= 0 {
return nil, fmt.Errorf("invalid height: %v", h.Height)
}
return h, nil
}
1 change: 0 additions & 1 deletion web_src/less/_asciicast.less
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

.asciinema-player-container {
width: 100%;
height: 600px;
}

.asciinema-terminal {
Expand Down

0 comments on commit 9530aae

Please sign in to comment.