Skip to content

Commit 9530aae

Browse files
committed
feat: extractHeader
1 parent a76bfed commit 9530aae

File tree

3 files changed

+61
-3
lines changed

3 files changed

+61
-3
lines changed

modules/markup/asciicast/asciicast.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
package asciicast
55

66
import (
7+
"bufio"
78
"fmt"
89
"io"
910
"net/url"
1011
"regexp"
1112

13+
"code.gitea.io/gitea/modules/log"
1214
"code.gitea.io/gitea/modules/markup"
1315
"code.gitea.io/gitea/modules/setting"
1416
)
@@ -40,12 +42,20 @@ const (
4042
func (Renderer) SanitizerRules() []setting.MarkupSanitizerRule {
4143
return []setting.MarkupSanitizerRule{
4244
{Element: "div", AllowAttr: "class", Regexp: regexp.MustCompile(playerClassName)},
45+
{Element: "div", AllowAttr: "style"},
4346
{Element: "div", AllowAttr: playerSrcAttr},
4447
}
4548
}
4649

4750
// Render implements markup.Renderer
48-
func (Renderer) Render(ctx *markup.RenderContext, _ io.Reader, output io.Writer) error {
51+
func (Renderer) Render(ctx *markup.RenderContext, input io.Reader, output io.Writer) error {
52+
var h *header
53+
if firstLine, err := bufio.NewReader(input).ReadBytes('\n'); err != nil {
54+
return err
55+
} else if h, err = extractHeader(firstLine); err != nil {
56+
log.Warn("extract header from %s: %v", ctx.RelativePath, err)
57+
}
58+
4959
rawURL := fmt.Sprintf("%s/%s/%s/raw/%s/%s",
5060
setting.AppSubURL,
5161
url.PathEscape(ctx.Metas["user"]),
@@ -54,9 +64,15 @@ func (Renderer) Render(ctx *markup.RenderContext, _ io.Reader, output io.Writer)
5464
url.PathEscape(ctx.RelativePath),
5565
)
5666

67+
style := ""
68+
if h != nil {
69+
style = fmt.Sprintf("aspect-ratio: %d / %d", h.Width, h.Height)
70+
}
71+
5772
_, err := io.WriteString(output, fmt.Sprintf(
58-
`<div class="%s" %s="%s"></div>`,
73+
`<div class="%s" style="%s" %s="%s"></div>`,
5974
playerClassName,
75+
style,
6076
playerSrcAttr,
6177
rawURL,
6278
))

modules/markup/asciicast/header.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2023 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package asciicast
5+
6+
import (
7+
"fmt"
8+
9+
"code.gitea.io/gitea/modules/json"
10+
)
11+
12+
type header struct {
13+
// Required header attributes:
14+
Version int `json:"version"`
15+
Width int `json:"width"`
16+
Height int `json:"height"`
17+
18+
// Optional header attributes:
19+
Timestamp int `json:"timestamp"`
20+
Duration float64 `json:"duration"`
21+
IdleTimeLimit float64 `json:"idle_time_limit"`
22+
Command string `json:"command"`
23+
Title string `json:"title"`
24+
Env map[string]string `json:"env"`
25+
Theme string `json:"theme"`
26+
}
27+
28+
func extractHeader(data []byte) (*header, error) {
29+
h := &header{}
30+
if err := json.Unmarshal(data, h); err != nil {
31+
return nil, fmt.Errorf("json unmarshal: %w", err)
32+
}
33+
if h.Version != 2 {
34+
return nil, fmt.Errorf("unknown version: %v", h.Version)
35+
}
36+
if h.Width <= 0 {
37+
return nil, fmt.Errorf("invalid width: %v", h.Width)
38+
}
39+
if h.Height <= 0 {
40+
return nil, fmt.Errorf("invalid height: %v", h.Height)
41+
}
42+
return h, nil
43+
}

web_src/less/_asciicast.less

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
.asciinema-player-container {
44
width: 100%;
5-
height: 600px;
65
}
76

87
.asciinema-terminal {

0 commit comments

Comments
 (0)