Skip to content

Commit

Permalink
Less code
Browse files Browse the repository at this point in the history
Signed-off-by: Miek Gieben <[email protected]>
  • Loading branch information
miekg committed Nov 16, 2024
1 parent 5933a5f commit 181dde3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 75 deletions.
29 changes: 0 additions & 29 deletions render/text/center.go

This file was deleted.

22 changes: 5 additions & 17 deletions render/text/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,11 @@ import (
mtext "github.com/mmarkdown/mmark/v2/internal/text"
)

func noopHeadingTransferFunc(data []byte) []byte { return data }

func (r *Renderer) outOneOf(w io.Writer, outFirst bool, first, second string) {
}

func (r *Renderer) outPrefix(w io.Writer) { r.out(w, r.prefix.flatten()); r.suppress = false }
func (r *Renderer) endline(w io.Writer) { r.outs(w, "\n"); r.suppress = false }

func (r *Renderer) outs(w io.Writer, s string) {
w.Write(r.headingTransformFunc([]byte(s)))
r.suppress = false
}

func (r *Renderer) out(w io.Writer, d []byte) {
w.Write(r.headingTransformFunc(d))
r.suppress = false
}
func (r *Renderer) outOneOf(w io.Writer, outFirst bool, first, second string) {}
func (r *Renderer) outPrefix(w io.Writer) { r.out(w, r.prefix.flatten()); r.suppress = false }
func (r *Renderer) endline(w io.Writer) { r.outs(w, "\n"); r.suppress = false }
func (r *Renderer) outs(w io.Writer, s string) { w.Write([]byte(s)); r.suppress = false }
func (r *Renderer) out(w io.Writer, d []byte) { w.Write(d); r.suppress = false }

func (r *Renderer) newline(w io.Writer) {
if r.suppress {
Expand Down
36 changes: 7 additions & 29 deletions render/text/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ type RendererOptions struct {
type Renderer struct {
opts RendererOptions

headingTransformFunc func([]byte) []byte // How do display heading, noop if nothing needs doing

// TODO(miek): paraStart should probably be a stack, aside in para in aside, etc.
paraStart int
headingStart int
Expand Down Expand Up @@ -68,13 +66,12 @@ func NewRenderer(opts RendererOptions) *Renderer {
opts.TextWidth = 80
}
r := &Renderer{
opts: opts,
prefix: &prefixStack{p: [][]byte{}},
deferredFootBuf: &bytes.Buffer{},
deferredFootID: make(map[string]struct{}),
deferredLinkBuf: &bytes.Buffer{},
deferredLinkID: make(map[string]struct{}),
headingTransformFunc: noopHeadingTransferFunc,
opts: opts,
prefix: &prefixStack{p: [][]byte{}},
deferredFootBuf: &bytes.Buffer{},
deferredFootID: make(map[string]struct{}),
deferredLinkBuf: &bytes.Buffer{},
deferredLinkID: make(map[string]struct{}),
}
r.push(Space(0)) // default indent for all text, except heading.
return r
Expand All @@ -87,30 +84,11 @@ func (r *Renderer) hardBreak(w io.Writer, node *ast.Hardbreak) {

func (r *Renderer) heading(w io.Writer, node *ast.Heading, entering bool) {
if entering {
switch node.Level {
case 1:
r.headingTransformFunc = func(data []byte) []byte {
x := r.centerText(bytes.ToUpper(data))
return x
}
case 2:
r.headingTransformFunc = func(data []byte) []byte {
x := r.centerText(data)
return x
}
default:
r.headingTransformFunc = noopHeadingTransferFunc
}
return
}

r.headingTransformFunc = noopHeadingTransferFunc
r.newline(w)
r.newline(w)
if node.Level == 1 {
r.newline(w)
}
return
r.outs(w, "\n")
}

func (r *Renderer) horizontalRule(w io.Writer, node *ast.HorizontalRule) {
Expand Down

0 comments on commit 181dde3

Please sign in to comment.