textutil

package
v0.0.0-...-f5003fd Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 20, 2025 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package textutil provide some extensions text handle util functions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsMatchAll

func IsMatchAll(s string, keywords []string) bool

IsMatchAll keywords in the give text string.

TIP: can use ^ for exclude match.

func ParseInlineINI

func ParseInlineINI(tagVal string, keys ...string) (mp maputil.SMap, err error)

ParseInlineINI parse config string to string-map. it's like INI format contents.

Examples:

eg: "name=val0;shorts=i;required=true;desc=a message"
=>
{name: val0, shorts: i, required: true, desc: a message}

func RenderFile

func RenderFile(filePath string, vars map[string]any) (string, error)

RenderFile render template file with vars

func RenderGoTpl

func RenderGoTpl(input string, data any, optFns ...RenderOptFn) string

RenderGoTpl render input text or template file.

func RenderSMap

func RenderSMap(text string, vars map[string]string, format string) string

RenderSMap by regex replace given tpl vars.

If format is empty, will use {const defaultVarFormat}

func RenderString

func RenderString(input string, data map[string]any) string

RenderString render str template string or file.

func RenderWrite

func RenderWrite(wr io.Writer, s string, vars map[string]any) error

RenderWrite render template string with vars, and write result to writer

func ReplaceVars

func ReplaceVars(text string, vars map[string]any, format string) string

ReplaceVars by regex replace given tpl vars.

If format is empty, will use {const defaultVarFormat}

Types

type FallbackFn

type FallbackFn = func(name string) (val string, ok bool)

FallbackFn type

type LTemplateOptFn

type LTemplateOptFn func(opt *LiteTemplateOpt)

LTemplateOptFn lite template option func

type LiteTemplate

type LiteTemplate struct {
	LiteTemplateOpt
	// contains filtered or unexported fields
}

LiteTemplate implement a simple text template engine.

  • support parse template vars
  • support access multi-level map field. eg: {{ user.name }}
  • support parse default value
  • support parse env vars
  • support custom pipeline func handle. eg: {{ name | upper }} {{ name | def:guest }}

NOTE: not support control flow, eg: if/else/for/with

func NewLiteTemplate

func NewLiteTemplate(opFns ...LTemplateOptFn) *LiteTemplate

NewLiteTemplate instance

func (*LiteTemplate) AddFuncs

func (t *LiteTemplate) AddFuncs(fns map[string]any)

AddFuncs add custom template functions

func (*LiteTemplate) Init

func (t *LiteTemplate) Init()

Init LiteTemplate

func (*LiteTemplate) RenderFile

func (t *LiteTemplate) RenderFile(filePath string, vars map[string]any) (string, error)

RenderFile render template file with vars

func (*LiteTemplate) RenderString

func (t *LiteTemplate) RenderString(s string, vars map[string]any) string

RenderString render template string with vars

func (*LiteTemplate) RenderWrite

func (t *LiteTemplate) RenderWrite(wr io.Writer, s string, vars map[string]any) error

RenderWrite render template string with vars, and write result to writer

type LiteTemplateOpt

type LiteTemplateOpt struct {
	Funcs template.FuncMap

	Left, Right string

	ParseDef bool
	ParseEnv bool
	// contains filtered or unexported fields
}

LiteTemplateOpt template options for LiteTemplate

func (*LiteTemplateOpt) SetVarFmt

func (o *LiteTemplateOpt) SetVarFmt(varFmt string)

SetVarFmt custom sets the variable format in template

type RenderOptFn

type RenderOptFn func(opt *TextRenderOpt)

RenderOptFn render option func

type TextRenderOpt

type TextRenderOpt struct {
	// Output use custom output writer
	Output io.Writer
	// Funcs add custom template functions
	Funcs template.FuncMap
}

TextRenderOpt render text template options

func NewRenderOpt

func NewRenderOpt(optFns []RenderOptFn) *TextRenderOpt

NewRenderOpt create a new render options

type VarReplacer

type VarReplacer struct {
	Left, Right string

	// NotFound hook func. on var-name not found
	NotFound FallbackFn
	// RenderFn custom render func
	RenderFn func(s string, vs map[string]string) string
	// contains filtered or unexported fields
}

VarReplacer struct

func NewFullReplacer

func NewFullReplacer(format string) *VarReplacer

NewFullReplacer instance. will enable parse env and parse default.

Usage:

rpl := NewFullReplacer("{{,}}")

func NewVarReplacer

func NewVarReplacer(format string, opFns ...func(vp *VarReplacer)) *VarReplacer

NewVarReplacer instance.

Usage:

rpl := NewVarReplacer("{{,}}")

func (*VarReplacer) DisableFlatten

func (r *VarReplacer) DisableFlatten() *VarReplacer

DisableFlatten on the input vars map

func (*VarReplacer) Init

func (r *VarReplacer) Init()

Init var replacer

func (*VarReplacer) KeepMissingVars

func (r *VarReplacer) KeepMissingVars() *VarReplacer

KeepMissingVars on the replacement handle

func (*VarReplacer) MissVars

func (r *VarReplacer) MissVars() []string

MissVars list

func (*VarReplacer) OnNotFound

func (r *VarReplacer) OnNotFound(fn FallbackFn) *VarReplacer

OnNotFound var handle

func (*VarReplacer) ParseVars

func (r *VarReplacer) ParseVars(s string) []string

ParseVars the text contents and collect vars

func (*VarReplacer) Render

func (r *VarReplacer) Render(s string, tplVars map[string]any) string

Render any-map vars in the text contents

func (*VarReplacer) RenderSimple

func (r *VarReplacer) RenderSimple(s string, varMap map[string]string) string

RenderSimple string-map vars in the text contents. alias of ReplaceSMap()

func (*VarReplacer) Replace

func (r *VarReplacer) Replace(s string, tplVars map[string]any) string

Replace any-map vars in the text contents

func (*VarReplacer) ReplaceSMap

func (r *VarReplacer) ReplaceSMap(s string, varMap map[string]string) string

ReplaceSMap string-map vars in the text contents

func (*VarReplacer) ResetMissVars

func (r *VarReplacer) ResetMissVars()

ResetMissVars list

func (*VarReplacer) WithFormat

func (r *VarReplacer) WithFormat(format string) *VarReplacer

WithFormat custom var template

func (*VarReplacer) WithParseDefault

func (r *VarReplacer) WithParseDefault() *VarReplacer

WithParseDefault value on the input template contents

func (*VarReplacer) WithParseEnv

func (r *VarReplacer) WithParseEnv() *VarReplacer

WithParseEnv on the input vars value

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL