Help us shape the future of Go! Take the 2026 Go Developer Survey

purification

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2026 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Overview

Package purification applies Legado-compatible reading replacement rules.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Ensure

func Ensure(dataDir string) error

Ensure initializes a separate local rule file without replacing user edits.

func Load

func Load(dataDir string) ([]Rule, []FileInfo, error)

Load reads the current files on every call. It never initializes or rewrites files, so replacing a backup directory also replaces the active definitions. Imported groups run in filename order; each group's order is stable, and the local group is appended last regardless of its individual order values.

Types

type Engine

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

func Compile

func Compile(rules []Rule) (*Engine, error)

Compile validates every rule, including disabled rules, without changing the input. It preserves input order; the loader orders rules within their files.

func (*Engine) Apply

func (e *Engine) Apply(ctx context.Context, target Target, text string) (string, error)

Apply is safe for concurrent use. On any error it returns the original text, never a partially rewritten chapter. No filesystem or network APIs are exposed to replacement scripts. Both Go and JavaScript regexes have bounded matches.

type FileInfo

type FileInfo struct {
	Path         string
	Name         string
	Local        bool
	RuleCount    int
	EnabledCount int
}

FileInfo identifies a rule file that can be edited directly. Local is true only for main.json, which runs after every imported group.

func Import

func Import(ctx context.Context, dataDir, location string) (FileInfo, error)

Import validates a local JSON file or HTTP(S) response before atomically replacing that source's own file. Original JSON bytes, including fields the engine does not recognize, are retained for future edits and round trips.

type Rule

type Rule struct {
	ID                 int64  `json:"id"`
	Name               string `json:"name"`
	Group              string `json:"group,omitempty"`
	Pattern            string `json:"pattern"`
	Replacement        string `json:"replacement"`
	Scope              string `json:"scope,omitempty"`
	ExcludeScope       string `json:"excludeScope,omitempty"`
	ScopeTitle         bool   `json:"scopeTitle"`
	ScopeContent       bool   `json:"scopeContent"`
	IsEnabled          bool   `json:"isEnabled"`
	IsRegex            bool   `json:"isRegex"`
	TimeoutMillisecond int64  `json:"timeoutMillisecond,omitempty"`
	Order              int    `json:"order"`
}

Rule retains the original pattern and replacement for editing and export.

func (*Rule) UnmarshalJSON

func (r *Rule) UnmarshalJSON(data []byte) error

type Target

type Target struct {
	BookName  string
	SourceURL string
	Title     bool
}

Target selects chapter-title or body rules for a book and its source URL.

Jump to

Keyboard shortcuts

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