ptproc

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2023 License: MIT Imports: 15 Imported by: 0

README

ptproc - Plain Text PROCessor

review-preproc (JA) like text processor.

Install

$ go install github.com/vvakame/ptproc/cmd/ptproc

mapfile directive

mapfile directive embeds specified file.

Hello, world!
mapfile:external.txt
mapfile.end
Good night, world.
Good afternoon, world.
Hello, world!
mapfile:external.txt
Good afternoon, world.
mapfile.end
Good night, world.

maprange directive

maprange directive embeds segment of the specified file.

Hello, world!
maprange:external.txt,targetB
maprange.end
Good night, world.
foo
range:targetA
Good afternoon, world.
range.end
range:targetB
Good evening, world.
range.end
bar
Hello, world!
maprange:external.txt,targetB
Good evening, world.
maprange.end
Good night, world.

examples

$ ptproc ./_misc/testdata/**/testcase/test.md
$ docker run -v "$(pwd):/work" ghcr.io/vvakame/ptproc \
    --logLevel debug \
    --replace \
    --config ./_misc/config/default/testcase/ptproc.yaml \
    --glob "./_misc/testdata/*/*/testcase/test.md"

Documentation

Index

Constants

View Source
const DefaultIndentLevel = 2

Variables

View Source
var DefaultDedentSpaceRegEx = regexp.MustCompile(`^([\s]+)`)
View Source
var DefaultMapfileEndRegEx = regexp.MustCompile(`mapfile.end`)
View Source
var DefaultMapfileStartRegEx = regexp.MustCompile(`mapfile:([^\s]+)`)
View Source
var DefaultMaprangeEndRegEx = regexp.MustCompile(`maprange.end`)
View Source
var DefaultMaprangeStartRegEx = regexp.MustCompile(`maprange:([^\s]+)`)
View Source
var DefaultRangeImportEndRegEx = regexp.MustCompile(`range.end`)
View Source
var DefaultRangeImportStartRegEx = regexp.MustCompile(`range:(?P<Cue>[^\s]+)`)

Functions

This section is empty.

Types

type Config

type Config struct {
	Mapfile  *MapfileDirective  `yaml:"mapfile"`
	Maprange *MaprangeDirective `yaml:"maprange"`
}

func LoadConfig

func LoadConfig(ctx context.Context, filePath string) (_ *Config, err error)

func (*Config) LogValue

func (cfg *Config) LogValue() slog.Value

func (*Config) ToProcessorConfig

func (cfg *Config) ToProcessorConfig(ctx context.Context) (_ *ProcessorConfig, err error)

type DedentRuleConfig

type DedentRuleConfig struct {
	SpaceRegExp *regexp.Regexp
}

type MapFileEndNode

type MapFileEndNode struct {
	Node
}

type MapFileNode

type MapFileNode struct {
	Node
	ImportFile string
}

type MapfileDirective

type MapfileDirective struct {
	StartRegExp          string `yaml:"startRegExp"`
	EndRegExp            string `yaml:"endRegExp"`
	DisableRewriteIndent bool   `yaml:"disableRewriteIndent"`
	IndentWidth          int    `yaml:"indentWidth"`
	DefaultSkip          int    `yaml:"defaultSkip"`
}

func (*MapfileDirective) LogValue

func (d *MapfileDirective) LogValue() slog.Value

type MapfileRuleConfig

type MapfileRuleConfig struct {
	StartRegExp *regexp.Regexp
	EndRegExp   *regexp.Regexp
	DefaultSkip int
	EmbedRules  []Rule
}

type MaprangeDirective

type MaprangeDirective struct {
	StartRegExp          string `yaml:"startRegExp"`
	EndRegExp            string `yaml:"endRegExp"`
	DisableDedent        bool   `yaml:"disableDedent"`
	DisableRewriteIndent bool   `yaml:"disableRewriteIndent"`
	IndentWidth          int    `yaml:"indentWidth"`
	DefaultSkip          int    `yaml:"defaultSkip"`
}

func (*MaprangeDirective) LogValue

func (d *MaprangeDirective) LogValue() slog.Value

type MaprangeRuleConfig

type MaprangeRuleConfig struct {
	StartRegExp *regexp.Regexp
	EndRegExp   *regexp.Regexp
	DefaultSkip int
	EmbedRules  []Rule
}

type Node

type Node interface {
	Text() string
	// contains filtered or unexported methods
}

type Processor

type Processor interface {
	Parse(ctx context.Context, filePath string, r io.Reader) ([]Node, error)
	ProcessFile(ctx context.Context, filePath string) (string, error)
	WithRules(ctx context.Context, rules []Rule) (Processor, error)
}

func NewProcessor

func NewProcessor(cfg *ProcessorConfig) (Processor, error)

type ProcessorConfig

type ProcessorConfig struct {
	OpenFile func(filePath string) (io.Reader, error)
	Rules    []Rule
}

type RangeImportRuleConfig

type RangeImportRuleConfig struct {
	Name        string
	StartRegExp *regexp.Regexp
	EndRegExp   *regexp.Regexp
}

type ReindentRuleConfig

type ReindentRuleConfig struct {
	IndentLevel int
}

type Rule

type Rule interface {
	Apply(ctx context.Context, opts *RuleOptions, nodes []Node) ([]Node, error)
}

func NewDedentRule

func NewDedentRule(cfg *DedentRuleConfig) (Rule, error)

func NewMapfileRule

func NewMapfileRule(cfg *MapfileRuleConfig) (Rule, error)

func NewMaprangeRule

func NewMaprangeRule(cfg *MaprangeRuleConfig) (Rule, error)

func NewRangeImportRule

func NewRangeImportRule(cfg *RangeImportRuleConfig) (Rule, error)

func NewReindentRule

func NewReindentRule(cfg *ReindentRuleConfig) (Rule, error)

type RuleOptions

type RuleOptions struct {
	Processor  Processor
	OpenFile   func(filePath string) (io.Reader, error)
	TargetPath string
}

func (*RuleOptions) FilePath

func (opts *RuleOptions) FilePath(externalFilePath string) string

Directories

Path Synopsis
cmd
internal

Jump to

Keyboard shortcuts

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