The pnuts.text module defines functions for the text I/O operations.
printf and sprintf emulate the C functions, printf and sprintf.
sprintf() returns the formatted string. printf() prints the result of sprintf().
sprintf("(%s)(%s)", "foo", "bar") ==> "(foo)(bar)"
readLine() reads one line from input and returns the line as a String.
When only input is specified, it returns a generator object that generates the all lines in turn (see the example below).
If handler is specified, it is called for each line. When EOF is seen, readLines() returns the number of lines that have been processed.
If collection is specified, this function adds all lines to the specified collection and returns the number of lines.
input must be either Reader, URL, File, or String object that represents a file name.
If includeLineSeparator is specified true, the line separator (\r, \n, or \r\n) is kept in the parameter object; by default, the line separator is stripped.
for (line : readLines("template.txt")){
println(line)
}
template() creates a template object which will be passed to formatTemplate() to substitute pattern in text with something. pattern must be a valid regular expression with one group.
This function, first, gets the value associated with a subgroup($1) of the template pattern. Then it performs the following process.
When writer is specified, the formatted text is written to the writer. Otherwise, this function returns the formatted template as a String object.
templ = template("${key} - ${value}", `\$\{([A-Za-z]+)\}`)
formatTemplate(templ, map([["key", "foo"], ["value", "bar"]]))
==> "foo - bar"
applyTemplate() returns a function that prints the formatted template. The resulting function can take zero or one argument (writer). The formatted template is written to the writer if it is specified, or the value of context.getWriter(), otherwise.
applyTemplate() formats template in the same way as formatTemplate().
For example, the following two expressions print the same result.
print(formatTemplate(template, map))
applyTemplate(template, map))()
This function can format templates efficiently even if the template has a nested structure. If a value in map is a function, it is called when the outermost function is called. Unlike formatTemplate(), it does not need temporary buffers allocated for intermediate results.
templ = template("${key} - ${value}", `\$\{([A-Za-z]+)\}`)
tmp = applyTemplate(templ, map([["key", "bar"], ["value", "baz"]]))
result = applyTemplate(templ, map([["key", "foo"], ["value", tmp]]))
result()
=> foo - bar - baz
Here is another example:
templ1 = template("<option value=\"%value%\">%value%</option>\n", `%([A-Za-z0-9_]+)%`)
x = list()
for (i : 0..2){
x.add(applyTemplate(templ1, map([["value", string(i)]])))
}
templ2 = template(`%list%`, `%([A-Za-z0-9_]+)%`)
applyTemplate(templ2, map([["list", x]]))()
=> <option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
textGrab() returns a function that returns the text printed by calling func. The returned function can take the same number of arguments as func.
function hello(who) { print("Hello "); print(who) }
helloText = textGrab(hello)
helloText("Fumi")
==> "Hello Fumi"