diff options
Diffstat (limited to 'vendor/github.com/mangoumbrella')
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 @@ +# If you prefer the allow list template instead of the deny list, see community template: +# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore +# +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ + +# Go workspace file +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 @@ +MIT License + +Copyright (c) 2023 The goldmark-figure authors + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +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 @@ +# goldmark-figure + +[](https://pkg.go.dev/github.com/mangoumbrella/goldmark-figure) +[](https://github.com/mangoumbrella/goldmark-figure/actions/workflows/test.yml/badge.svg?event=push) +[](https://goreportcard.com/report/github.com/mangoumbrella/goldmark-figure) + +[goldmark-figure](https://github.com/MangoUmbrella/goldmark-figure) is a +[goldmark](http://github.com/yuin/goldmark) +extension to parse mardown paragraphs that start with an image into HTML +`<figure>` elements. One nice thing is it doesn't use any new markdown +syntaxes. + +Example markdown source: + +```md + +Awesome caption about **Oscar** the kitty. +``` + +Render result: + +```html +<figure> + <img src="/path/to/cat.jpg" alt="Picture of Oscar." /> + <figcaption>Awesome caption about <strong>Oscar</strong> the kitty.</figcaption> +</figure> +``` + +# Installation + +``` +go get github.com/mangoumbrella/goldmark-figure +``` + +# Usage + +```go +import ( + "bytes" + "fmt" + + "github.com/mangoumbrella/goldmark-figure" + "github.com/yuin/goldmark" +) + +func main() { + markdown := goldmark.New( + goldmark.WithExtensions( + figure.Figure, + ), + ) + source := ` +  + Awesome caption about **Oscar** the kitty. + ` + var buf bytes.Buffer + if err := markdown.Convert([]byte(source), &buf); err != nil { + panic(err) + } + fmt.Print(buf.String()) +} +``` + +See [`figure_test.go`](/figure_test.go) for detailed usages. + +# LICENSE + +MIT + +# Authors + +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 @@ +// Copyright 2023 The goldmark-figure authors +// +// Use of this source code is governed by an MIT-style +// license that can be found in the LICENSE file or at +// https://opensource.org/licenses/MIT. +package ast + +import ( + gast "github.com/yuin/goldmark/ast" + "github.com/yuin/goldmark/renderer" + "github.com/yuin/goldmark/util" +) + +// KindFigure is a NodeKind of the Figure node. +var KindFigure = gast.NewNodeKind("Figure") + +// A Figure struct represents a table of Markdown(GFM) text. +type Figure struct { + gast.BaseBlock +} + +// Kind implements Node.Kind. +func (n *Figure) Kind() gast.NodeKind { + return KindFigure +} + +// Dump implements Node.Dump +func (n *Figure) Dump(source []byte, level int) { + gast.DumpHelper(n, source, level, nil, func(level int) { + }) +} + +// NewFigure returns a new Table node. +func NewFigure() *Figure { + return &Figure{} +} + +// KindFigureImage is a NodeKind of the FigureImage node. +var KindFigureImage = gast.NewNodeKind("FigureImage") + +// A FigureImage struct represents a table of Markdown(GFM) text. +type FigureImage struct { + gast.BaseBlock +} + +// Kind implements Node.Kind. +func (n *FigureImage) Kind() gast.NodeKind { + return KindFigureImage +} + +// Dump implements Node.Dump +func (n *FigureImage) Dump(source []byte, level int) { + gast.DumpHelper(n, source, level, nil, func(level int) { + }) +} + +// NewFigureImage returns a new Table node. +func NewFigureImage() *FigureImage { + return &FigureImage{} +} + +// KindFigureCaption is a NodeKind of the FigureCaption node. +var KindFigureCaption = gast.NewNodeKind("FigureCaption") + +// A FigureCaption struct represents a table of Markdown(GFM) text. +type FigureCaption struct { + gast.BaseBlock +} + +// Kind implements Node.Kind. +func (n *FigureCaption) Kind() gast.NodeKind { + return KindFigureCaption +} + +// Dump implements Node.Dump +func (n *FigureCaption) Dump(source []byte, level int) { + gast.DumpHelper(n, source, level, nil, func(level int) { + }) +} + +// NewFigureCaption returns a new Table node. +func NewFigureCaption() *FigureCaption { + return &FigureCaption{} +} + +// FigureHTMLRenderer is a renderer.NodeRenderer implementation that +// renders Figure nodes. +type FigureHTMLRenderer struct { +} + +// NewFigureHTMLRenderer returns a new FigureHTMLRenderer. +func NewFigureHTMLRenderer() renderer.NodeRenderer { + return &FigureHTMLRenderer{} +} + +// RegisterFuncs implements renderer.NodeRenderer.RegisterFuncs. +func (r *FigureHTMLRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) { + reg.Register(KindFigure, r.renderFigure) + reg.Register(KindFigureCaption, r.renderFigureCaption) +} + +func (r *FigureHTMLRenderer) renderFigure(w util.BufWriter, source []byte, n gast.Node, entering bool) (gast.WalkStatus, error) { + if entering { + _, _ = w.WriteString("<figure>\n") + } else { + _, _ = w.WriteString("</figure>\n") + } + return gast.WalkContinue, nil +} + +func (r *FigureHTMLRenderer) renderFigureCaption(w util.BufWriter, source []byte, n gast.Node, entering bool) (gast.WalkStatus, error) { + if entering { + _, _ = w.WriteString("<figcaption><p>") + } else { + _, _ = w.WriteString("</p></figcaption>\n") + } + return gast.WalkContinue, nil +} 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 @@ +// Copyright 2023 The goldmark-figure authors +// +// Use of this source code is governed by an MIT-style +// license that can be found in the LICENSE file or at +// https://opensource.org/licenses/MIT. +package figure + +import ( + "github.com/mangoumbrella/goldmark-figure/ast" + "github.com/yuin/goldmark" + aparser "github.com/yuin/goldmark/parser" + "github.com/yuin/goldmark/renderer" + "github.com/yuin/goldmark/util" + + fparser "github.com/mangoumbrella/goldmark-figure/parser" +) + +type extension struct { +} + +// Figure is an extension to render <figure> elements. +var Figure = &extension{} + +func (f *extension) Extend(m goldmark.Markdown) { + m.Parser().AddOptions( + aparser.WithParagraphTransformers( + util.Prioritized(fparser.NewFigureParagraphTransformer(), 120), + ), + aparser.WithASTTransformers( + util.Prioritized(fparser.NewFigureASTTransformer(), 0), + ), + ) + m.Renderer().AddOptions(renderer.WithNodeRenderers( + util.Prioritized(ast.NewFigureHTMLRenderer(), 0), + )) +} 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 @@ +// Copyright 2023 The goldmark-figure authors +// +// Use of this source code is governed by an MIT-style +// license that can be found in the LICENSE file or at +// https://opensource.org/licenses/MIT. +package parser + +import ( + "regexp" + + fast "github.com/mangoumbrella/goldmark-figure/ast" + gast "github.com/yuin/goldmark/ast" + "github.com/yuin/goldmark/parser" + "github.com/yuin/goldmark/text" +) + +var imageRegexp = regexp.MustCompile(`^!\[[^[\]]*\](\([^()]*\)|\[[^[\]]*\])\s*$`) + +type figureParagraphTransformer struct { +} + +var defaultFigureParagraphTransformer = &figureParagraphTransformer{} + +// NewFigureParagraphTransformer returns a new ParagraphTransformer +// that can transform paragraphs into figures. +func NewFigureParagraphTransformer() parser.ParagraphTransformer { + return defaultFigureParagraphTransformer +} + +func (b *figureParagraphTransformer) Transform(node *gast.Paragraph, reader text.Reader, pc parser.Context) { + lines := node.Lines() + if lines.Len() < 1 { + return + } + var first_seg = lines.At(0) + var first_line_str = first_seg.Value(reader.Source()) + // Here we simply match by regex. + // But this simple regex ignores image descriptions that contain other links. + // E.g. ](/url2). + // See CommonMark spec: https://spec.commonmark.org/0.30/#images. + if !imageRegexp.Match(first_line_str) { + return + } + figure := fast.NewFigure() + node.Parent().ReplaceChild(node.Parent(), node, figure) + + figureImage := fast.NewFigureImage() + figureImage.Lines().Append(lines.At(0)) + figure.AppendChild(figure, figureImage) + + if lines.Len() >= 2 { + figureCaption := fast.NewFigureCaption() + for i := 1; i < lines.Len(); i++ { + seg := lines.At(i) + if i == lines.Len()-1 { + // trim last newline(\n) + seg.Stop = seg.Stop - 1 + } + figureCaption.Lines().Append(seg) + } + figure.AppendChild(figure, figureCaption) + } +} + +type figureASTTransformer struct { +} + +var defaultFigureASTTransformer = &figureASTTransformer{} + +// NewFigureASTTransformer returns a parser.ASTTransformer for tables. +func NewFigureASTTransformer() parser.ASTTransformer { + return defaultFigureASTTransformer +} + +func (a *figureASTTransformer) Transform(node *gast.Document, reader text.Reader, pc parser.Context) { +} |
