diff options
| author | Mitja Felicijan <mitja.felicijan@gmail.com> | 2024-10-25 00:47:47 +0200 |
|---|---|---|
| committer | Mitja Felicijan <mitja.felicijan@gmail.com> | 2024-10-25 00:47:47 +0200 |
| commit | c6cc0108ca7738023b45e0eeac0fa2390532dd93 (patch) | |
| tree | 36890e6cd3091bbab8efbe686cc56f467f645bfd /vendor/github.com/mangoumbrella/goldmark-figure | |
| parent | 0130404a1dc663d4aa68d780c9bcb23a4243e68d (diff) | |
| download | jbmafp-master.tar.gz | |
Diffstat (limited to 'vendor/github.com/mangoumbrella/goldmark-figure')
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 | ||
| 21 | go.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 @@ | |||
| 1 | MIT License | ||
| 2 | |||
| 3 | Copyright (c) 2023 The goldmark-figure authors | ||
| 4 | |||
| 5 | Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| 6 | of this software and associated documentation files (the "Software"), to deal | ||
| 7 | in the Software without restriction, including without limitation the rights | ||
| 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| 9 | copies of the Software, and to permit persons to whom the Software is | ||
| 10 | furnished to do so, subject to the following conditions: | ||
| 11 | |||
| 12 | The above copyright notice and this permission notice shall be included in all | ||
| 13 | copies or substantial portions of the Software. | ||
| 14 | |||
| 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| 21 | SOFTWARE. | ||
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 | [](https://pkg.go.dev/github.com/mangoumbrella/goldmark-figure) | ||
| 4 | [](https://github.com/mangoumbrella/goldmark-figure/actions/workflows/test.yml/badge.svg?event=push) | ||
| 5 | [](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) | ||
| 9 | extension 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 | ||
| 11 | syntaxes. | ||
| 12 | |||
| 13 | Example markdown source: | ||
| 14 | |||
| 15 | ```md | ||
| 16 |  | ||
| 17 | Awesome caption about **Oscar** the kitty. | ||
| 18 | ``` | ||
| 19 | |||
| 20 | Render 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 | ``` | ||
| 32 | go get github.com/mangoumbrella/goldmark-figure | ||
| 33 | ``` | ||
| 34 | |||
| 35 | # Usage | ||
| 36 | |||
| 37 | ```go | ||
| 38 | import ( | ||
| 39 | "bytes" | ||
| 40 | "fmt" | ||
| 41 | |||
| 42 | "github.com/mangoumbrella/goldmark-figure" | ||
| 43 | "github.com/yuin/goldmark" | ||
| 44 | ) | ||
| 45 | |||
| 46 | func main() { | ||
| 47 | markdown := goldmark.New( | ||
| 48 | goldmark.WithExtensions( | ||
| 49 | figure.Figure, | ||
| 50 | ), | ||
| 51 | ) | ||
| 52 | source := ` | ||
| 53 |  | ||
| 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 | |||
| 64 | See [`figure_test.go`](/figure_test.go) for detailed usages. | ||
| 65 | |||
| 66 | # LICENSE | ||
| 67 | |||
| 68 | MIT | ||
| 69 | |||
| 70 | # Authors | ||
| 71 | |||
| 72 | Yilei 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. | ||
| 6 | package ast | ||
| 7 | |||
| 8 | import ( | ||
| 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. | ||
| 15 | var KindFigure = gast.NewNodeKind("Figure") | ||
| 16 | |||
| 17 | // A Figure struct represents a table of Markdown(GFM) text. | ||
| 18 | type Figure struct { | ||
| 19 | gast.BaseBlock | ||
| 20 | } | ||
| 21 | |||
| 22 | // Kind implements Node.Kind. | ||
| 23 | func (n *Figure) Kind() gast.NodeKind { | ||
| 24 | return KindFigure | ||
| 25 | } | ||
| 26 | |||
| 27 | // Dump implements Node.Dump | ||
| 28 | func (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. | ||
| 34 | func NewFigure() *Figure { | ||
| 35 | return &Figure{} | ||
| 36 | } | ||
| 37 | |||
| 38 | // KindFigureImage is a NodeKind of the FigureImage node. | ||
| 39 | var KindFigureImage = gast.NewNodeKind("FigureImage") | ||
| 40 | |||
| 41 | // A FigureImage struct represents a table of Markdown(GFM) text. | ||
| 42 | type FigureImage struct { | ||
| 43 | gast.BaseBlock | ||
| 44 | } | ||
| 45 | |||
| 46 | // Kind implements Node.Kind. | ||
| 47 | func (n *FigureImage) Kind() gast.NodeKind { | ||
| 48 | return KindFigureImage | ||
| 49 | } | ||
| 50 | |||
| 51 | // Dump implements Node.Dump | ||
| 52 | func (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. | ||
| 58 | func NewFigureImage() *FigureImage { | ||
| 59 | return &FigureImage{} | ||
| 60 | } | ||
| 61 | |||
| 62 | // KindFigureCaption is a NodeKind of the FigureCaption node. | ||
| 63 | var KindFigureCaption = gast.NewNodeKind("FigureCaption") | ||
| 64 | |||
| 65 | // A FigureCaption struct represents a table of Markdown(GFM) text. | ||
| 66 | type FigureCaption struct { | ||
| 67 | gast.BaseBlock | ||
| 68 | } | ||
| 69 | |||
| 70 | // Kind implements Node.Kind. | ||
| 71 | func (n *FigureCaption) Kind() gast.NodeKind { | ||
| 72 | return KindFigureCaption | ||
| 73 | } | ||
| 74 | |||
| 75 | // Dump implements Node.Dump | ||
| 76 | func (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. | ||
| 82 | func NewFigureCaption() *FigureCaption { | ||
| 83 | return &FigureCaption{} | ||
| 84 | } | ||
| 85 | |||
| 86 | // FigureHTMLRenderer is a renderer.NodeRenderer implementation that | ||
| 87 | // renders Figure nodes. | ||
| 88 | type FigureHTMLRenderer struct { | ||
| 89 | } | ||
| 90 | |||
| 91 | // NewFigureHTMLRenderer returns a new FigureHTMLRenderer. | ||
| 92 | func NewFigureHTMLRenderer() renderer.NodeRenderer { | ||
| 93 | return &FigureHTMLRenderer{} | ||
| 94 | } | ||
| 95 | |||
| 96 | // RegisterFuncs implements renderer.NodeRenderer.RegisterFuncs. | ||
| 97 | func (r *FigureHTMLRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) { | ||
| 98 | reg.Register(KindFigure, r.renderFigure) | ||
| 99 | reg.Register(KindFigureCaption, r.renderFigureCaption) | ||
| 100 | } | ||
| 101 | |||
| 102 | func (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 | |||
| 111 | func (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. | ||
| 6 | package figure | ||
| 7 | |||
| 8 | import ( | ||
| 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 | |||
| 18 | type extension struct { | ||
| 19 | } | ||
| 20 | |||
| 21 | // Figure is an extension to render <figure> elements. | ||
| 22 | var Figure = &extension{} | ||
| 23 | |||
| 24 | func (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. | ||
| 6 | package parser | ||
| 7 | |||
| 8 | import ( | ||
| 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 | |||
| 17 | var imageRegexp = regexp.MustCompile(`^!\[[^[\]]*\](\([^()]*\)|\[[^[\]]*\])\s*$`) | ||
| 18 | |||
| 19 | type figureParagraphTransformer struct { | ||
| 20 | } | ||
| 21 | |||
| 22 | var defaultFigureParagraphTransformer = &figureParagraphTransformer{} | ||
| 23 | |||
| 24 | // NewFigureParagraphTransformer returns a new ParagraphTransformer | ||
| 25 | // that can transform paragraphs into figures. | ||
| 26 | func NewFigureParagraphTransformer() parser.ParagraphTransformer { | ||
| 27 | return defaultFigureParagraphTransformer | ||
| 28 | } | ||
| 29 | |||
| 30 | func (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. ](/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 | |||
| 65 | type figureASTTransformer struct { | ||
| 66 | } | ||
| 67 | |||
| 68 | var defaultFigureASTTransformer = &figureASTTransformer{} | ||
| 69 | |||
| 70 | // NewFigureASTTransformer returns a parser.ASTTransformer for tables. | ||
| 71 | func NewFigureASTTransformer() parser.ASTTransformer { | ||
| 72 | return defaultFigureASTTransformer | ||
| 73 | } | ||
| 74 | |||
| 75 | func (a *figureASTTransformer) Transform(node *gast.Document, reader text.Reader, pc parser.Context) { | ||
| 76 | } | ||
