Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

Support asciicast files as new markup (#22448) Support [asciicast files](https://github.com/asciinema/asciinema/blob/develop/doc/asciicast-v2.md) as a new markup via [asciinema-player](https://github.com/asciinema/asciinema-player). For more on asciinema, see the [introduction](https://asciinema.org/). So users can use asciinema recorder to generate an asciicast file (or you can download a sample file from https://asciinema.org/a/335480.cast?dl=1), then upload it to Gitea and play it on Gitea. Snapshots: <details> ## Upload asciicast files <img width="1134" alt="image" src="https://user-images.githubusercontent.com/9418365/212461061-cc2c7181-0e14-4534-af55-1ec60a639fd1.png"> ## Open an asciicast file <img width="1137" alt="image" src="https://user-images.githubusercontent.com/9418365/212461090-a3b5141f-4894-430d-a2b4-ea257801a0ed.png"> ## Play it <img width="1144" alt="image" src="https://user-images.githubusercontent.com/9418365/212461157-4e82db69-0e41-471d-928f-ac1fe0737105.png"> ## Copy contents from the "video" <img width="1145" alt="image" src="https://user-images.githubusercontent.com/9418365/212461286-211612bc-15d6-427a-89a9-6abff5c6a0a5.png"> ## View the source <img width="1140" alt="image" src="https://user-images.githubusercontent.com/9418365/212461187-05473b2d-ba3d-4072-84a6-4aa1e7d82182.png"> </details> Known issue: Don't support the [v1 version asciicast files](https://github.com/asciinema/asciinema/blob/develop/doc/asciicast-v1.md), it's a poorly designed version, it does not specify the file extension and uses `*.json` usually, so it's impossible to recognize the files. Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
1 рік тому
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package asciicast
  4. import (
  5. "fmt"
  6. "io"
  7. "net/url"
  8. "regexp"
  9. "code.gitea.io/gitea/modules/markup"
  10. "code.gitea.io/gitea/modules/setting"
  11. )
  12. func init() {
  13. markup.RegisterRenderer(Renderer{})
  14. }
  15. // Renderer implements markup.Renderer for asciicast files.
  16. // See https://github.com/asciinema/asciinema/blob/develop/doc/asciicast-v2.md
  17. type Renderer struct{}
  18. // Name implements markup.Renderer
  19. func (Renderer) Name() string {
  20. return "asciicast"
  21. }
  22. // Extensions implements markup.Renderer
  23. func (Renderer) Extensions() []string {
  24. return []string{".cast"}
  25. }
  26. const (
  27. playerClassName = "asciinema-player-container"
  28. playerSrcAttr = "data-asciinema-player-src"
  29. )
  30. // SanitizerRules implements markup.Renderer
  31. func (Renderer) SanitizerRules() []setting.MarkupSanitizerRule {
  32. return []setting.MarkupSanitizerRule{
  33. {Element: "div", AllowAttr: "class", Regexp: regexp.MustCompile(playerClassName)},
  34. {Element: "div", AllowAttr: playerSrcAttr},
  35. }
  36. }
  37. // Render implements markup.Renderer
  38. func (Renderer) Render(ctx *markup.RenderContext, _ io.Reader, output io.Writer) error {
  39. rawURL := fmt.Sprintf("%s/%s/%s/raw/%s/%s",
  40. setting.AppSubURL,
  41. url.PathEscape(ctx.Metas["user"]),
  42. url.PathEscape(ctx.Metas["repo"]),
  43. ctx.Metas["BranchNameSubURL"],
  44. url.PathEscape(ctx.RelativePath),
  45. )
  46. _, err := io.WriteString(output, fmt.Sprintf(
  47. `<div class="%s" %s="%s"></div>`,
  48. playerClassName,
  49. playerSrcAttr,
  50. rawURL,
  51. ))
  52. return err
  53. }