KScript docs
EN

formatted_file fields and methods

A field is a plain fact about the value, read with a dot and no brackets: $value.name. A method takes arguments or does work, and is called with ->: $value->name(arguments), or with the value piped in: $value |> name(arguments).

Fields

FieldTypeDescription
.extensionstringThe source file extension without the dot.
.textstringThe source text of the fenced value.

From KScript.Core

#formatted_file->find(text: string) → number

The zero-based source offset of the first literal match, or -1.
ParameterTakes
text requiredstring
Example
<let name="source_file" value="```ps1
  Write-Output 'before'
  ```" />
<echo>{ $source_file->find('before') }</echo>
$source_file = ```ps1
  Write-Output 'before'
  ```
echo $source_file->find('before')

#formatted_file->insert(content, at?: int) → formatted_file

Returns a copy with text or source of the same format inserted at a zero-based offset. With no offset, appends.
ParameterTakes
content requiredany value
at optionalint

Arguments can be named: ->insert(…, at: …).

Example
<let name="source_file" value="```ps1
  Write-Output 'before'
  ```" />
<echo>{ $source_file->insert('# Example ', at: 0).text }</echo>
$source_file = ```ps1
  Write-Output 'before'
  ```
echo $source_file->insert('# Example ', at: 0).text

#formatted_file->replace(search: string, replacement: string) → formatted_file

Returns a copy with every literal match replaced.
ParameterTakes
search requiredstring
replacement requiredstring

Arguments can be named: ->replace(…, replacement: …).

Example
<let name="source_file" value="```ps1
  Write-Output 'before'
  ```" />
<echo>{ $source_file->replace('before', 'after').text }</echo>
$source_file = ```ps1
  Write-Output 'before'
  ```
echo $source_file->replace('before', 'after').text

#formatted_file->save(path: string)

Writes the source to a file. Adds the fence extension when the path has no extension.
ParameterTakes
path requiredstring
Example
<let name="source_file" value="```ps1
  Write-Output 'before'
  ```" />
<do>$source_file->save('example.ps1')</do>
$source_file = ```ps1
  Write-Output 'before'
  ```
$source_file->save('example.ps1')

Methods from any