macro

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2023 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultMacros = []*Macro{
	{
		MacroName: "var",
		MacroFunc: func(args *[]string) string {
			if len(*args) == 2 && VariableStore[(*args)[1]] != "" {
				return VariableStore[(*args)[1]]
			} else if len(*args) >= 3 && (*args)[2] == "=" {
				VariableStore[(*args)[1]] = strings.Join((*args)[3:], " ")
				return ""
			} else if VariableStore[(*args)[1]] == "" {
				return fmt.Sprintf("var %s is not defined", (*args)[1])
			}
			return "Usage: var \\<varname> = \\<value>"
		},
	},
	{
		MacroName: "list",
		MacroFunc: func(args *[]string) string {
			if len(*args) <= 1 {
				return "Usage: list \\<items...>"
			}
			if len(newln_sel.Split(strings.Join((*args)[1:], " "), -1)) > 1 {
				return strings.Join(newln_sel.Split(strings.Join((*args)[1:], " "), -1), "|")
			}
			return strings.Join(whitespace_sel.Split(strings.Join((*args)[1:], " "), -1), "|")
		},
	},
	{
		MacroName: "trim",
		MacroFunc: func(args *[]string) string {
			if len(*args) <= 1 {
				return "Usage: trim \\<from> \\<to> \\<list>"
			}
			from, err := strconv.Atoi((*args)[1])
			if err != nil {
				return fmt.Sprintf("invalid '\\<from>': %s", (*args)[1])
			}
			to, err := strconv.Atoi((*args)[2])
			if err != nil {
				return fmt.Sprintf("invalid '\\<to>': %s", (*args)[2])
			}
			list := strings.Split((*args)[3], "|")
			if from < 0 {
				from = 0
			}
			if to < 0 {
				to = len(list)
			}
			if from > len(list) {
				from = len(list) - 1
			}
			if to > len(list) {
				to = len(list)
			}

			var output []string
			for i, item := range list {
				if i < from || i >= to {
					continue
				}
				output = append(output, item)
			}
			return strings.Join(output, "|")
		},
	},
	{
		MacroName: "foreach",
		MacroFunc: func(args *[]string) string {
			if len(*args) < 5 {
				return "Usage: foreach \\<varname> in \\<list> \\<body>"
			}
			varname := (*args)[1]
			list := (*args)[3]
			body := (*args)[4]
			var output string
			for _, item := range list_del.Split(list, -1) {
				if item == "" {
					continue
				}
				VariableStore[varname] = item
				output += fmt.Sprintf("#!(var %s = %s)", varname, item)

				output += body
			}
			logger.Printf("foreach out: %s\n", output)
			return output
		},
	},
}
View Source
var MAX_EVAL = 2048
View Source
var UnsafeMacros = []*Macro{
	{
		MacroName: "exec",
		MacroFunc: func(args *[]string) string {
			if len(*args) <= 1 {
				return "Usage: exec \\<command>"
			}
			res := exec.RunCommand(strings.Join((*args)[1:], " "))
			logger.Printf("exec: '%s'", *res)
			*res = bytes.TrimSpace(*res)
			return string(*res)
		},
	},
	{
		MacroName: "exec-screenshot",
		MacroFunc: func(args *[]string) string {
			if len(*args) <= 1 {
				return "Usage: exec-screenshot \\<command>"
			}
			res, err := exec.ScreenShotCommands((*args)[1:])
			if err != nil {
				return err.Error()
			}
			data := base64.StdEncoding.EncodeToString(*res)
			logger.Printf("exec-screenshot: '%s'", (*args)[1])
			return fmt.Sprintf("![](data:image/png;base64,%s)", data)
		},
	},
	{
		MacroName: "file-read",
		MacroFunc: func(args *[]string) string {
			if len(*args) <= 1 {
				return "Usage: file-read \\<filename>"
			}
			res, err := os.ReadFile((*args)[1])
			if err != nil {
				return err.Error()
			}
			return string(res)
		},
	},
}
View Source
var VariableStore = map[string]string{}

Functions

func EvalMacro

func EvalMacro(content []byte, macros []*Macro) *string

Types

type Macro

type Macro struct {
	MacroName  string
	IsExternal bool
	MacroFunc  func(*[]string) string
}

type OutputType

type OutputType uint

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

func (Parser) Process

func (p Parser) Process(file *[]byte)

func (*Parser) RegisterMacro

func (p *Parser) RegisterMacro(macro *Macro)

func (*Parser) RegisterMacros

func (p *Parser) RegisterMacros(macros []*Macro)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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