txtTransformer
This Golang package is a personal microlibrary to help developing scripts that transforms large numbers of text files. It's being built to make programatic changes to thousands of html files.
Include library
To include the libray on your project, first download it:
go get github.com/osvik/txttransformer
And import it in your code:
import (
"github.com/osvik/txttransformer"
)
File functions
Functions to read, write, search and filter files and paths.
FindFiles
Finds all files recursively, ignoring folders.
filesInsideFoo := txttransformer.FindFiles("testdata/foo")
FilterPaths
Filters paths by extension. Use without the dot, like html or txt.
listTxtPaths := txttransformer.FilterPaths(filesInsideFoo, "txt")
ReadListPaths
Reads a list of paths from a txt file. One path per line.
listTxtPaths := txttransformer.ReadListPaths("paths.txt")
PrintListPaths
Prints the list of files.
txttransformer.PrintListPaths(listTxtPaths)
CreatePathFolder
Checks if the folder for the current path exists and creates it if not.
txttransformer.CreatePathFolder("testdata/foo/bar/file.txt")
ReadTextFile
Reads a file into a string.
myTxtFile := txttransformer.ReadTextFile("testdata/file.txt")
WriteTextFile
Writes a string as a file.
txttransformer.WriteTextFile("testdata/foo/bar/file.txt", myTxtFile)
Strings (files as strings)
ReplaceAllText
Replaces text in multiline strings.
changedText := txttransformer.ReplaceAllText(originalText, "is", "é")
ReplaceMatchedText
Replaces text that matches the regular expression by new text.
changedText := txttransformer.ReplaceMatchedText(originalText, `do\!`, "Xooer")
ReplaceMatchedTextFunction
Replaces text caught in regularExp by the result of the function.
changedText := txttransformer.ReplaceMatchedTextFunction( originalText, `do\!$`, func(inputExp string) string {
if (inputExp == "do!") {
return "Work"
}
return "Sleep"
})