From 9b6bbd7becbc583ba767b0d03a42e9ea7c97234e Mon Sep 17 00:00:00 2001 From: szktkfm Date: Sun, 10 Mar 2024 16:52:03 +0900 Subject: [PATCH] add code span parser --- README.md | 114 +----------------------------- element.go | 203 ++++++++++++++++------------------------------------- parser.go | 43 +++++++----- 3 files changed, 86 insertions(+), 274 deletions(-) diff --git a/README.md b/README.md index 2f8978b..a1363d8 100644 --- a/README.md +++ b/README.md @@ -1,80 +1,3 @@ -# ๐Ÿฅพ walk - -

-
- walk demo -
-

- -**Walk** โ€” a terminal navigator. - -Why another terminal navigator? I wanted something simple and minimalistic. -Something to help me with faster navigation in the filesystem; a `cd` and `ls` -replacement. So I build **walk**. It allows for quick navigation with fuzzy -searching, `cd` integration is quite simple. And you can open `vim` right from -the walk. That's it. - -## Install - -``` -brew install walk -``` - -``` -pkg_add walk -``` - -``` -pacman -S walk -``` - -``` -go install github.com/antonmedv/walk@latest -``` - -Or download [prebuild binaries](https://github.com/antonmedv/walk/releases). - -Put the next function into the **.bashrc** or a similar config: - - - - - - - - - - - - -
Bash/Zsh Fish PowerShell
- -```bash -function lk { - cd "$(walk "$@")" -} -``` - - - -```fish -function lk - set loc (walk $argv); and cd $loc; -end -``` - - - -```powershell -function lk() { - cd $(walk $args) -} -``` - -
- - -Now use `lk` command to start walking. ## Usage @@ -84,7 +7,7 @@ Now use `lk` command to start walking. | `Enter` | Enter directory | | `Backspace` | Exit directory | | `Space` | Toggle preview | -| `Esc`, `q` | Exit with cd | +| `Esc`, _q_ | Exit with cd | | `Ctrl+c` | Exit without cd | | `/` | Fuzzy search | | `dd` | Delete file or dir | @@ -93,38 +16,3 @@ Now use `lk` command to start walking. The `EDITOR` or `WALK_EDITOR` environment variable used for opening files from the walk. -```bash -export EDITOR=vim -``` - -### Preview mode - -Press `Space` to toggle preview mode. - -Walk Preview Mode - -### Delete file or directory - -Press `dd` to delete file or directory. Press `u` to undo. - -Walk Deletes a File - -### Display icons - -Install [Nerd Fonts](https://www.nerdfonts.com) and add `--icons` flag. - -Walk Icons Support - -### Image preview - -No additional setup is required. - -Walk Image Preview - -## Become a sponsor - -Every line of code in my repositories ๐Ÿ“– signifies my unwavering commitment to open source ๐Ÿ’ก. Your support ๐Ÿค ensures these projects keep thriving, innovating, and benefiting all ๐Ÿ’ผ. If my work has ever resonated ๐ŸŽต or helped you, kindly consider showing love โค๏ธ by sponsoring. [**๐Ÿš€ Sponsor Me Today! ๐Ÿš€**](https://github.com/sponsors/antonmedv) - -## License - -[MIT](LICENSE) \ No newline at end of file diff --git a/element.go b/element.go index 7baebbf..2ae78c4 100644 --- a/element.go +++ b/element.go @@ -1,83 +1,31 @@ package mdtt import ( - "fmt" + "bytes" - east "github.com/yuin/goldmark-emoji/ast" "github.com/yuin/goldmark/ast" astext "github.com/yuin/goldmark/extension/ast" ) type Element struct { Entering string - Exiting string - Renderer func() - Finisher func() + Renderer func(b *bytes.Buffer) + Finisher func(b *bytes.Buffer) } // NewElement returns the appropriate render Element for a given node. func (tr *ModelBuilder) NewElement(node ast.Node, source []byte) Element { - // ctx := tr.context - // fmt.Print(strings.Repeat(" ", ctx.blockStack.Len()), node.Type(), node.Kind()) - // defer fmt.Println() switch node.Kind() { - // // Document - // case ast.KindDocument: - // // Heading - // case ast.KindHeading: - // // Paragraph - // case ast.KindParagraph: - // // Blockquote - // case ast.KindBlockquote: - // // Lists - // case ast.KindList: - - // case ast.KindListItem: - // // Text Elements - // case ast.KindText: - // case ast.KindEmphasis: + // return Element{} - // case astext.KindStrikethrough: - case ast.KindThematicBreak: - // return Element{ - // Entering: "", - // Exiting: "", - // Renderer: &BaseElement{ - // Style: ctx.options.Styles.HorizontalRule, - // }, - // } + // case ast.KindLink: + // return Element{} - // Links - case ast.KindLink: - //TODO - // n := node.(*ast.Link) - // return Element{ - // Renderer: &LinkElement{ - // Text: textFromChildren(node, source), - // BaseURL: ctx.options.BaseURL, - // URL: string(n.Destination), - // }, - // } + // case ast.KindAutoLink: + // return Element{} - case ast.KindAutoLink: - // n := node.(*ast.AutoLink) - // u := string(n.URL(source)) - // label := string(n.Label(source)) - // if n.AutoLinkType == ast.AutoLinkEmail && !strings.HasPrefix(strings.ToLower(u), "mailto:") { - // u = "mailto:" + u - // } - - // return Element{ - // Renderer: &LinkElement{ - // Text: label, - // BaseURL: ctx.options.BaseURL, - // URL: u, - // }, - // } - - // Images case ast.KindImage: // n := node.(*ast.Image) // text := string(n.Text(source)) @@ -88,96 +36,67 @@ func (tr *ModelBuilder) NewElement(node ast.Node, source []byte) Element { // URL: string(n.Destination), // }, // } + return Element{} case ast.KindCodeSpan: - // // n := node.(*ast.CodeSpan) - // e := &BlockElement{ - // Block: &bytes.Buffer{}, - // Style: cascadeStyle(ctx.blockStack.Current().Style, ctx.options.Styles.Code, false), - // } - // return Element{ - // Renderer: e, - // Finisher: e, - // } - - // Tables - case astext.KindTable: - // te := &TableElement{} - // return Element{ - // Entering: "\n", - // Renderer: te, - // Finisher: te, - // } + // n := node.(*ast.CodeSpan) + return Element{ + Renderer: func(b *bytes.Buffer) { + b.WriteString("`") + }, + Finisher: func(b *bytes.Buffer) { + b.WriteString("`") + }, + } case astext.KindTableCell: - // s := "" - // n := node.FirstChild() - // for n != nil { - // switch t := n.(type) { - // case *ast.AutoLink: - // s += string(t.Label(source)) - // default: - // s += string(n.Text(source)) - // } + return Element{ + Renderer: func(b *bytes.Buffer) { + }, + Finisher: func(b *bytes.Buffer) { + }, + } - // n = n.NextSibling() - // } - - // return Element{ - // Renderer: &TableCellElement{ - // Text: s, - // Head: node.Parent().Kind() == astext.KindTableHeader, - // }, - // } - - case astext.KindTableHeader: - // return Element{ - // Finisher: &TableHeadElement{}, - // } case astext.KindTableRow: - // return Element{ - // Finisher: &TableRowElement{}, - // } - - // HTML Elements - case ast.KindHTMLBlock: - // n := node.(*ast.HTMLBlock) - // return Element{ - // Renderer: &BaseElement{ - // Token: ctx.SanitizeHTML(string(n.Text(source)), true), - // Style: ctx.options.Styles.HTMLBlock.StylePrimitive, - // }, - // } - // case ast.KindRawHTML: - // n := node.(*ast.RawHTML) - // return Element{ - // Renderer: &BaseElement{ - // Token: ctx.SanitizeHTML(string(n.Text(source)), true), - // Style: ctx.options.Styles.HTMLSpan.StylePrimitive, - // }, - // } - - // Definition Lists - - // Handled by parents - case astext.KindTaskCheckBox: - // // handled by KindListItem - // return Element{} - case ast.KindTextBlock: - // return Element{} - - case east.KindEmoji: - // n := node.(*east.Emoji) - // return Element{ - // Renderer: &BaseElement{ - // Token: string(n.Value.Unicode), - // }, - // } + return Element{ + Renderer: func(b *bytes.Buffer) { + }, + Finisher: func(b *bytes.Buffer) { + }, + } + + case ast.KindText: + // n := node.(*ast.CodeSpan) + return Element{ + Renderer: func(b *bytes.Buffer) { + b.WriteString(string(node.Text(source))) + }, + Finisher: func(b *bytes.Buffer) { + b.WriteString("") + }, + } + + // + // case astext.KindTaskCheckBox: + // case ast.KindTextBlock: + + // case east.KindEmoji: + // n := node.(*east.Emoji) + // return Element{ + // Renderer: &BaseElement{ + // Token: string(n.Value.Unicode), + // }, + // } + // return Element{} // Unknown case default: - fmt.Println("Warning: unhandled element", node.Kind().String()) - return Element{} + return Element{ + Renderer: func(b *bytes.Buffer) { + }, + Finisher: func(b *bytes.Buffer) { + }, + } } - return Element{} + // return Element{} } diff --git a/parser.go b/parser.go index 6d7df38..834cfb2 100644 --- a/parser.go +++ b/parser.go @@ -87,13 +87,17 @@ type Options struct { // ModelBuilder build tea.Model from markdown type ModelBuilder struct { inTable bool + buf *bytes.Buffer rows []string cols []string } // NewModelBuilder returns a new ANSIRenderer with style and options set. func NewModelBuilder(options Options) *ModelBuilder { - return &ModelBuilder{} + var buf []byte + return &ModelBuilder{ + buf: bytes.NewBuffer(buf), + } } // RegisterFuncs implements NodeRenderer.RegisterFuncs. @@ -149,16 +153,8 @@ func (r *ModelBuilder) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) { } func (r *ModelBuilder) renderNode(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) { - // children get rendered by their parent - // if isChild(node) { - // return ast.WalkContinue, nil - // } - if entering { log.Debugf(">Start %v = %v", node.Kind().String(), string(node.Text(source))) - log.Debugf("\n") - log.Debugf("\n") - // TODO: inline็ณปใฎๅ‡ฆ็†ใ‚’่ฟฝๅŠ ใ™ใ‚‹ if node.Kind() == astext.KindTable { @@ -166,25 +162,34 @@ func (r *ModelBuilder) renderNode(w util.BufWriter, source []byte, node ast.Node } if r.inTable { - if node.Kind() == astext.KindTableCell { - switch node.Parent().Kind() { - case astext.KindTableHeader: - r.cols = append(r.cols, string(node.Text(source))) - case astext.KindTableRow: - r.rows = append(r.rows, string(node.Text(source))) - } - } + e := r.NewElement(node, source) + e.Renderer(r.buf) } } else { log.Debugf("