aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/mangoumbrella/goldmark-figure
diff options
context:
space:
mode:
authorMitja Felicijan <mitja.felicijan@gmail.com>2024-10-25 00:47:47 +0200
committerMitja Felicijan <mitja.felicijan@gmail.com>2024-10-25 00:47:47 +0200
commitc6cc0108ca7738023b45e0eeac0fa2390532dd93 (patch)
tree36890e6cd3091bbab8efbe686cc56f467f645bfd /vendor/github.com/mangoumbrella/goldmark-figure
parent0130404a1dc663d4aa68d780c9bcb23a4243e68d (diff)
downloadjbmafp-master.tar.gz
Added vendor lock on depsHEADmaster
Diffstat (limited to 'vendor/github.com/mangoumbrella/goldmark-figure')
-rw-r--r--vendor/github.com/mangoumbrella/goldmark-figure/.gitignore21
-rw-r--r--vendor/github.com/mangoumbrella/goldmark-figure/LICENSE21
-rw-r--r--vendor/github.com/mangoumbrella/goldmark-figure/README.md72
-rw-r--r--vendor/github.com/mangoumbrella/goldmark-figure/ast/ast.go118
-rw-r--r--vendor/github.com/mangoumbrella/goldmark-figure/figure.go36
-rw-r--r--vendor/github.com/mangoumbrella/goldmark-figure/parser/parser.go76
6 files changed, 344 insertions, 0 deletions
diff --git a/vendor/github.com/mangoumbrella/goldmark-figure/.gitignore b/vendor/github.com/mangoumbrella/goldmark-figure/.gitignore
new file mode 100644
index 0000000..3b735ec
--- /dev/null
+++ b/vendor/github.com/mangoumbrella/goldmark-figure/.gitignore
@@ -0,0 +1,21 @@
1# If you prefer the allow list template instead of the deny list, see community template:
2# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
3#
4# Binaries for programs and plugins
5*.exe
6*.exe~
7*.dll
8*.so
9*.dylib
10
11# Test binary, built with `go test -c`
12*.test
13
14# Output of the go coverage tool, specifically when used with LiteIDE
15*.out
16
17# Dependency directories (remove the comment below to include it)
18# vendor/
19
20# Go workspace file
21go.work
diff --git a/vendor/github.com/mangoumbrella/goldmark-figure/LICENSE b/vendor/github.com/mangoumbrella/goldmark-figure/LICENSE
new file mode 100644
index 0000000..8d32f3b
--- /dev/null
+++ b/vendor/github.com/mangoumbrella/goldmark-figure/LICENSE
@@ -0,0 +1,21 @@
1MIT License
2
3Copyright (c) 2023 The goldmark-figure authors
4
5Permission is hereby granted, free of charge, to any person obtaining a copy
6of this software and associated documentation files (the "Software"), to deal
7in the Software without restriction, including without limitation the rights
8to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9copies of the Software, and to permit persons to whom the Software is
10furnished to do so, subject to the following conditions:
11
12The above copyright notice and this permission notice shall be included in all
13copies or substantial portions of the Software.
14
15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21SOFTWARE.
diff --git a/vendor/github.com/mangoumbrella/goldmark-figure/README.md b/vendor/github.com/mangoumbrella/goldmark-figure/README.md
new file mode 100644
index 0000000..22ad7a4
--- /dev/null
+++ b/vendor/github.com/mangoumbrella/goldmark-figure/README.md
@@ -0,0 +1,72 @@
1# goldmark-figure
2
3[![goldmark-figure Go reference](https://pkg.go.dev/badge/github.com/mangoumbrella/goldmark-figure.svg)](https://pkg.go.dev/github.com/mangoumbrella/goldmark-figure)
4[![goldmark-figure test results](https://github.com/mangoumbrella/goldmark-figure/actions/workflows/test.yml/badge.svg?event=push)](https://github.com/mangoumbrella/goldmark-figure/actions/workflows/test.yml/badge.svg?event=push)
5[![goldmark-figure Go report card](https://goreportcard.com/badge/github.com/mangoumbrella/goldmark-figure)](https://goreportcard.com/report/github.com/mangoumbrella/goldmark-figure)
6
7[goldmark-figure](https://github.com/MangoUmbrella/goldmark-figure) is a
8[goldmark](http://github.com/yuin/goldmark)
9extension to parse mardown paragraphs that start with an image into HTML
10`<figure>` elements. One nice thing is it doesn't use any new markdown
11syntaxes.
12
13Example markdown source:
14
15```md
16![Picture of Oscar.](/path/to/cat.jpg)
17Awesome caption about **Oscar** the kitty.
18```
19
20Render result:
21
22```html
23<figure>
24 <img src="/path/to/cat.jpg" alt="Picture of Oscar." />
25 <figcaption>Awesome caption about <strong>Oscar</strong> the kitty.</figcaption>
26</figure>
27```
28
29# Installation
30
31```
32go get github.com/mangoumbrella/goldmark-figure
33```
34
35# Usage
36
37```go
38import (
39 "bytes"
40 "fmt"
41
42 "github.com/mangoumbrella/goldmark-figure"
43 "github.com/yuin/goldmark"
44)
45
46func main() {
47 markdown := goldmark.New(
48 goldmark.WithExtensions(
49 figure.Figure,
50 ),
51 )
52 source := `
53 ![Picture of Oscar.](/path/to/cat.jpg)
54 Awesome caption about **Oscar** the kitty.
55 `
56 var buf bytes.Buffer
57 if err := markdown.Convert([]byte(source), &buf); err != nil {
58 panic(err)
59 }
60 fmt.Print(buf.String())
61}
62```
63
64See [`figure_test.go`](/figure_test.go) for detailed usages.
65
66# LICENSE
67
68MIT
69
70# Authors
71
72Yilei Yang
diff --git a/vendor/github.com/mangoumbrella/goldmark-figure/ast/ast.go b/vendor/github.com/mangoumbrella/goldmark-figure/ast/ast.go
new file mode 100644
index 0000000..e27f2fb
--- /dev/null
+++ b/vendor/github.com/mangoumbrella/goldmark-figure/ast/ast.go
@@ -0,0 +1,118 @@
1// Copyright 2023 The goldmark-figure authors
2//
3// Use of this source code is governed by an MIT-style
4// license that can be found in the LICENSE file or at
5// https://opensource.org/licenses/MIT.
6package ast
7
8import (
9 gast "github.com/yuin/goldmark/ast"
10 "github.com/yuin/goldmark/renderer"
11 "github.com/yuin/goldmark/util"
12)
13
14// KindFigure is a NodeKind of the Figure node.
15var KindFigure = gast.NewNodeKind("Figure")
16
17// A Figure struct represents a table of Markdown(GFM) text.
18type Figure struct {
19 gast.BaseBlock
20}
21
22// Kind implements Node.Kind.
23func (n *Figure) Kind() gast.NodeKind {
24 return KindFigure
25}
26
27// Dump implements Node.Dump
28func (n *Figure) Dump(source []byte, level int) {
29 gast.DumpHelper(n, source, level, nil, func(level int) {
30 })
31}
32
33// NewFigure returns a new Table node.
34func NewFigure() *Figure {
35 return &Figure{}
36}
37
38// KindFigureImage is a NodeKind of the FigureImage node.
39var KindFigureImage = gast.NewNodeKind("FigureImage")
40
41// A FigureImage struct represents a table of Markdown(GFM) text.
42type FigureImage struct {
43 gast.BaseBlock
44}
45
46// Kind implements Node.Kind.
47func (n *FigureImage) Kind() gast.NodeKind {
48 return KindFigureImage
49}
50
51// Dump implements Node.Dump
52func (n *FigureImage) Dump(source []byte, level int) {
53 gast.DumpHelper(n, source, level, nil, func(level int) {
54 })
55}
56
57// NewFigureImage returns a new Table node.
58func NewFigureImage() *FigureImage {
59 return &FigureImage{}
60}
61
62// KindFigureCaption is a NodeKind of the FigureCaption node.
63var KindFigureCaption = gast.NewNodeKind("FigureCaption")
64
65// A FigureCaption struct represents a table of Markdown(GFM) text.
66type FigureCaption struct {
67 gast.BaseBlock
68}
69
70// Kind implements Node.Kind.
71func (n *FigureCaption) Kind() gast.NodeKind {
72 return KindFigureCaption
73}
74
75// Dump implements Node.Dump
76func (n *FigureCaption) Dump(source []byte, level int) {
77 gast.DumpHelper(n, source, level, nil, func(level int) {
78 })
79}
80
81// NewFigureCaption returns a new Table node.
82func NewFigureCaption() *FigureCaption {
83 return &FigureCaption{}
84}
85
86// FigureHTMLRenderer is a renderer.NodeRenderer implementation that
87// renders Figure nodes.
88type FigureHTMLRenderer struct {
89}
90
91// NewFigureHTMLRenderer returns a new FigureHTMLRenderer.
92func NewFigureHTMLRenderer() renderer.NodeRenderer {
93 return &FigureHTMLRenderer{}
94}
95
96// RegisterFuncs implements renderer.NodeRenderer.RegisterFuncs.
97func (r *FigureHTMLRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) {
98 reg.Register(KindFigure, r.renderFigure)
99 reg.Register(KindFigureCaption, r.renderFigureCaption)
100}
101
102func (r *FigureHTMLRenderer) renderFigure(w util.BufWriter, source []byte, n gast.Node, entering bool) (gast.WalkStatus, error) {
103 if entering {
104 _, _ = w.WriteString("<figure>\n")
105 } else {
106 _, _ = w.WriteString("</figure>\n")
107 }
108 return gast.WalkContinue, nil
109}
110
111func (r *FigureHTMLRenderer) renderFigureCaption(w util.BufWriter, source []byte, n gast.Node, entering bool) (gast.WalkStatus, error) {
112 if entering {
113 _, _ = w.WriteString("<figcaption><p>")
114 } else {
115 _, _ = w.WriteString("</p></figcaption>\n")
116 }
117 return gast.WalkContinue, nil
118}
diff --git a/vendor/github.com/mangoumbrella/goldmark-figure/figure.go b/vendor/github.com/mangoumbrella/goldmark-figure/figure.go
new file mode 100644
index 0000000..00ec574
--- /dev/null
+++ b/vendor/github.com/mangoumbrella/goldmark-figure/figure.go
@@ -0,0 +1,36 @@
1// Copyright 2023 The goldmark-figure authors
2//
3// Use of this source code is governed by an MIT-style
4// license that can be found in the LICENSE file or at
5// https://opensource.org/licenses/MIT.
6package figure
7
8import (
9 "github.com/mangoumbrella/goldmark-figure/ast"
10 "github.com/yuin/goldmark"
11 aparser "github.com/yuin/goldmark/parser"
12 "github.com/yuin/goldmark/renderer"
13 "github.com/yuin/goldmark/util"
14
15 fparser "github.com/mangoumbrella/goldmark-figure/parser"
16)
17
18type extension struct {
19}
20
21// Figure is an extension to render <figure> elements.
22var Figure = &extension{}
23
24func (f *extension) Extend(m goldmark.Markdown) {
25 m.Parser().AddOptions(
26 aparser.WithParagraphTransformers(
27 util.Prioritized(fparser.NewFigureParagraphTransformer(), 120),
28 ),
29 aparser.WithASTTransformers(
30 util.Prioritized(fparser.NewFigureASTTransformer(), 0),
31 ),
32 )
33 m.Renderer().AddOptions(renderer.WithNodeRenderers(
34 util.Prioritized(ast.NewFigureHTMLRenderer(), 0),
35 ))
36}
diff --git a/vendor/github.com/mangoumbrella/goldmark-figure/parser/parser.go b/vendor/github.com/mangoumbrella/goldmark-figure/parser/parser.go
new file mode 100644
index 0000000..573ef2b
--- /dev/null
+++ b/vendor/github.com/mangoumbrella/goldmark-figure/parser/parser.go
@@ -0,0 +1,76 @@
1// Copyright 2023 The goldmark-figure authors
2//
3// Use of this source code is governed by an MIT-style
4// license that can be found in the LICENSE file or at
5// https://opensource.org/licenses/MIT.
6package parser
7
8import (
9 "regexp"
10
11 fast "github.com/mangoumbrella/goldmark-figure/ast"
12 gast "github.com/yuin/goldmark/ast"
13 "github.com/yuin/goldmark/parser"
14 "github.com/yuin/goldmark/text"
15)
16
17var imageRegexp = regexp.MustCompile(`^!\[[^[\]]*\](\([^()]*\)|\[[^[\]]*\])\s*$`)
18
19type figureParagraphTransformer struct {
20}
21
22var defaultFigureParagraphTransformer = &figureParagraphTransformer{}
23
24// NewFigureParagraphTransformer returns a new ParagraphTransformer
25// that can transform paragraphs into figures.
26func NewFigureParagraphTransformer() parser.ParagraphTransformer {
27 return defaultFigureParagraphTransformer
28}
29
30func (b *figureParagraphTransformer) Transform(node *gast.Paragraph, reader text.Reader, pc parser.Context) {
31 lines := node.Lines()
32 if lines.Len() < 1 {
33 return
34 }
35 var first_seg = lines.At(0)
36 var first_line_str = first_seg.Value(reader.Source())
37 // Here we simply match by regex.
38 // But this simple regex ignores image descriptions that contain other links.
39 // E.g. ![foo ![bar](/url)](/url2).
40 // See CommonMark spec: https://spec.commonmark.org/0.30/#images.
41 if !imageRegexp.Match(first_line_str) {
42 return
43 }
44 figure := fast.NewFigure()
45 node.Parent().ReplaceChild(node.Parent(), node, figure)
46
47 figureImage := fast.NewFigureImage()
48 figureImage.Lines().Append(lines.At(0))
49 figure.AppendChild(figure, figureImage)
50
51 if lines.Len() >= 2 {
52 figureCaption := fast.NewFigureCaption()
53 for i := 1; i < lines.Len(); i++ {
54 seg := lines.At(i)
55 if i == lines.Len()-1 {
56 // trim last newline(\n)
57 seg.Stop = seg.Stop - 1
58 }
59 figureCaption.Lines().Append(seg)
60 }
61 figure.AppendChild(figure, figureCaption)
62 }
63}
64
65type figureASTTransformer struct {
66}
67
68var defaultFigureASTTransformer = &figureASTTransformer{}
69
70// NewFigureASTTransformer returns a parser.ASTTransformer for tables.
71func NewFigureASTTransformer() parser.ASTTransformer {
72 return defaultFigureASTTransformer
73}
74
75func (a *figureASTTransformer) Transform(node *gast.Document, reader text.Reader, pc parser.Context) {
76}