Documents from source
A Markdown or LaTeX fence makes a markdown or latex value, with no import: .text is its source and .extension its file extension. Import KScript.Document for the methods below. @document(source) turns a fence into a document, or a string with as: latex for LaTeX source. A plain string defaults to Markdown.
<import package="KScript.Document" />
<fn name="notes" value="```md
# Notes
Ship *soon*.
```" />
<fn name="paper" value="```latex
\section{Draft}
```" />
<echo value="@notes()->convert(to: html)" />
<do>@notes()->save('notes.pdf')</do>
<do>@paper()->replace('Draft', 'Final')->save('paper')</do>import KScript.Document
fn notes() => ```md
# Notes
Ship *soon*.
```
fn paper() => ```latex
\section{Draft}
```
echo @notes()->convert(to: html)
@notes()->save('notes.pdf')
@paper()->replace('Draft', 'Final')->save('paper')Both kinds of fence have ->find(text) for the first source offset, ->replace(search, replacement) for every literal match, and ->insert(content, at?) to insert text or a same-format fence. Offsets are zero-based and an omitted offset appends. These methods return a new value for each edit. ->save(path, as?) writes the source when the path ends in .md or .tex or has no extension.
A Markdown fence is a document already. It has the document's ->convert(to: html), ->convert(to: latex), ->words() and ->pages(), and ->save() renders it as PDF, Word, LaTeX or HTML by the path's extension or as:.
A LaTeX fence is a document too. It has ->convert(to: html), ->convert(to: markdown), ->words() and ->pages(), and ->save('paper.pdf') or ->save('paper.docx') renders it. KScript reads the LaTeX a document part reads: sections, paragraphs, lists, maths, quotes, figures and the usual text commands. A command it doesn't read is an error that names it when the fence is rendered, and ->save('paper.tex') keeps any LaTeX as written for a LaTeX engine such as pdflatex.