KScript docs
EN

The .kscript format

Long strings and blocks of text

End a line inside a string with \ to carry the string on to the next line. The new line and the next line's indentation aren't part of the string, so a long string can follow the code's indentation:

<let name="name" value="'Kyle'" />
<echo>Hello { $name }. KScript reads your whole script, checks it, and only then runs it.</echo>
$name = 'Kyle'
echo "Hello { $name }. KScript reads your whole script, checks it, \
  and only then runs it."

That prints one line. It works in "double-quoted" and 'single-quoted' strings, and keeps any spaces before the \. Write \\ for a backslash of your own at the end of a line of text. kscript fmt uses it to wrap a string that runs past print_width (the wrap_strings rule).

For text that should keep its new lines, such as a query, a template or an equation, use a block fenced with three backticks, as in Markdown:

  <echo>Hi { $name },
your order has shipped.</echo>

  <let name="query">```sql
      SELECT * FROM users
      WHERE active = 1
      ```</let>

  <let name="repo">```&lt;Repo>
      { "name": "kscript", "stars": 12 }
      ```</let>
echo ```
  Hi { $name },
  your order has shipped.
  ```

$query = ```sql
  SELECT * FROM users
  WHERE active = 1
  ```

$repo = ```<Repo>
  { "name": "kscript", "stars": 12 }
  ```
  • The block runs from the opening fence to a line that starts with the same number of backticks. Use four (or more) when the text holds three.
  • The closing fence's indentation is taken off every line, so the block sits at the code's indentation and keeps any indentation inside it. A line indented less than the closing fence is an error. The new lines just inside the fences aren't part of the text.
  • In an expression, an empty header or md or markdown produces a markdown value. latex or tex produces a latex value. Import KScript.Document for document methods, or read the original text with .text.
  • kscript and other file extensions such as ps1 produce a formatted_file with .extension and .text. Use ->save('filename') to write it, or pass a complete filename to choose the exact path.
  • csv produces a csv value: CSV text that is checked where it is written, read with ->cells(). See CSV.
  • text and string produce plain strings. Existing html, xml, css, scss, ks.css, ks.scss, js, javascript, ts, typescript, json, jsonc, sql, yaml and yml fences also produce strings in expressions. A typed component return uses the tag to choose its renderer.
  • VS Code highlights known fence languages in place. A compatible editor formatter can format a fence during Format Document. Use text or string for literal text that should keep its spacing.
  • A type in angle brackets parses the block: ```<json> is the parsed JSON, and ```<Repo> maps it onto a type, with the same checks (and Shape errors) as ->convert(from: json, as: Repo).
  • As an element's text (after echo, Paragraph, Math, ...) a block is the same as "text": { } interpolates where the element interpolates, and Math keeps its braces. No escapes apply inside a block.
  • Code can carry on after the closing fence on the same line: ``` ) or ```->trim().

Edit this page on GitHub