parser

package
v0.0.0-...-d1707c3 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2022 License: MIT Imports: 12 Imported by: 0

README

Trinkets-Spinal parser package

This package is about parsing code blocks from text files.

Example:

``` some code ```

Documentation

Overview

File code.go contains helpers to parse, or generate code for different languages.

File parser.go contains high level functions for: parsing a source file or a folder with source files, and converting a source file to scripts, or a folder with source files to scripts.

Index

Constants

This section is empty.

Variables

View Source
var CodeBlocks = map[string]CodeType{
	"js":  {"Javascript", "node", "//"},
	"mjs": {"Javascript", "node", "//"},
	"py":  {"Python", "python3", "#"},
	"sh":  {"Bash", "bash", "#"},
	"zsh": {"ZSH", "zsh", "#"},
}

All known code block types

Functions

func ConvertFolder

func ConvertFolder(dir string) (map[string]StringToString, map[string]CodeFile, error)

ConvertFolder finds all candidate code-files from a folder, and generates source code. The original text files are not changed.

func ParseBlocks

func ParseBlocks(body string) map[string]string

ParseBlocks extracts all code blocks from text The result will be in the form: {language => content}

Types

type CodeFile

type CodeFile struct {
	FrontMatter
	Path   string
	Ctime  time.Time
	Mtime  time.Time
	Blocks map[string]string
}

func ParseFile

func ParseFile(fname string) CodeFile

ParseFile accepts a candidate code-file and returns a structure. The structure contains ID, Path, Creation time and a list of blocks of code. If the file can't be accessed or parsed, the returned structure will be incomplete.

func ParseFolder

func ParseFolder(dir string, checkInvalid bool) ([]CodeFile, error)

ParseFolder finds all candidate code-files from a folder, and extracts useful info about them.

func (*CodeFile) IsValid

func (self *CodeFile) IsValid() bool

IsValid makes a validation check for ID and Path

type CodeType

type CodeType struct {
	Name       string
	Executable string
	Comment    string
}

type FrontMatter

type FrontMatter struct {
	Enabled    bool     `yaml:"spinal" json:"spinal"`
	ID         string   `yaml:"id"  json:"id"`
	Db         bool     `yaml:"db,omitempty"  json:"db,omitempty"`
	Log        bool     `yaml:"log,omitempty" json:"log,omitempty"`
	Cwd        string   `yaml:"cwd,omitempty" json:"cwd,omitempty"`
	Env        []string `yaml:"env,omitempty" json:"env,omitempty"`
	DelayStart uint     `yaml:"delayStart,omitempty" json:"delayStart,omitempty"`
	RetryTimes uint     `yaml:"retryTimes,omitempty" json:"retryTimes,omitempty"`
	Meta       MetaData `yaml:"meta" json:"meta"`
}

type MetaData

type MetaData interface{}

MetaData is a general object

type StringToString

type StringToString map[string]string

StringToString is a helper map

func ConvertFile

func ConvertFile(codFile CodeFile, force bool) (StringToString, error)

ConvertFile generates 1 or more code files, from one code file. Force=true will convert a file without checking the header.

Jump to

Keyboard shortcuts

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