2023-11-15 06:47:20 -08:00
# @thefarce/loom
2023-03-31 07:03:45 -07:00
2023-11-15 06:47:20 -08:00
## What is this?
2023-03-31 07:03:45 -07:00
2023-11-15 06:47:20 -08:00
Loom is a tool for weaving form data with content data in nodejs. It does
this by using elements of the Unified.js toolkit for parsing text into
abstract syntax trees (AST).
2023-03-31 07:03:45 -07:00
2023-11-15 06:47:20 -08:00
The fundamental idea of Loom is that presentation and content are orthogonal
dimensions of data. A communication has values in these two dimensions
integrated into a coherent whole.
2023-03-31 07:03:45 -07:00
2023-11-15 06:47:20 -08:00
## When should I use this?
2023-03-31 07:03:45 -07:00
2023-11-15 06:47:20 -08:00
When you want to blend form (such as HTML) with content (such as JSON) into
a single document. Unlike other systems, Loom does not require writing to
or reading from files.
2023-03-31 07:03:45 -07:00
2023-11-15 06:47:20 -08:00
## Installation
2023-03-31 07:03:45 -07:00
2023-11-15 06:47:20 -08:00
`$` `npm install @thefarce/loom`
2023-03-31 07:03:45 -07:00
2023-11-15 06:47:20 -08:00
## Usage Example
2023-03-31 07:03:45 -07:00
2023-11-15 06:47:20 -08:00
For more thorough usage examples, see the `examples` directory and refer to
2023-11-30 07:30:50 -08:00
the [transformers documentation ](./docs/usage.md ) and the [interpolation
documentation](./docs/interpolation.md).
2023-03-31 07:03:45 -07:00
2023-11-30 07:30:50 -08:00
### Simple Example
In this simple example, we will make two changes. First, we will replace
the `div` tag in our html fragment with a `span` tag. Second, we will
replace the `name` variable in our template with the value `Othello` .
2023-03-31 07:03:45 -07:00
2023-11-30 07:30:50 -08:00
```js
import { interpolateTree, transform } from '@thefarce/loom'
import { astToHtml, htmlToAst } from '@thefarce/loom-html5'
// Get our HTML fragment.
const ast = htmlToAst('< p > Hello, < div > {name}< / div > !< / p > ', { fragment: true });
// Now we have an abstract syntax tree (AST) representation of our HTML
// fragment. First, let's transform the div to a span.
let updated = transform(
// our initial syntax tree
ast,
// A transformer that checks if the node is an element and has a tag name
// of "div". If so, we just change the tag name to "span". It is
// important that we return the updated node (or a new one).
(node) => {
if (node.type === 'element' & & node.tagName === 'div') {
node.tagName = 'span';
return node;
}
}
);
2023-11-16 11:37:55 -08:00
2023-11-30 07:30:50 -08:00
// Now let's interpolate the value `name` . There is another major mechanism
// for providing interpolation values; see the for more detailed information.
let interpolated = interpolateTree(
updated,
{ name: 'Othello' }
2023-11-15 06:47:20 -08:00
);
2023-11-16 11:37:55 -08:00
2023-11-30 07:30:50 -08:00
console.log(astToHtml(interpolated));
2023-03-31 07:03:45 -07:00
```
2023-11-15 06:47:20 -08:00
Running this script produces the following output:
2023-03-31 07:03:45 -07:00
2023-11-15 06:47:20 -08:00
```html
< p > Hello, Othello!< / p >
2023-03-31 07:03:45 -07:00
```
2023-11-15 06:47:20 -08:00
## API
2023-03-31 07:03:45 -07:00
2023-11-30 07:30:50 -08:00
### Value Interpolation
2023-03-31 07:03:45 -07:00
2023-11-30 07:30:50 -08:00
Interpolation is the process of executing code within the AST's values.
This uses a syntax identical to JavaScript's "backtick" interpolation. For
example, in the following script, JavaScript will replace `{name}` with the
value `Othello` :
2023-03-31 07:03:45 -07:00
2023-11-30 07:30:50 -08:00
See the [interpolation documentation ](./docs/interpolation.md ) for more
detailed information.
```js
const name = 'Othello';
console.log(`Hello, ${name}!`);
```
2023-11-15 11:52:05 -08:00
2023-11-30 07:30:50 -08:00
This interpolation uses variables in scope to expand values. Loom
interpolation works the same way: any string[^1] in the AST will be replaced
with the value in the data.
[^1]: There are a few exceptions. The `type` , `data` , and `position` values of a node will not be interpolated. You may interpolate them manually, of course.
### AST Transformation
Transformation is the process of making systematic changes to an AST. This
is accomplished by creating "transformer functions" (or "transformers") that
apply changes to a node based on its content.
See the [transformations documentation ](./docs/transformations.md ) for more
detailed information.
2023-03-31 07:03:45 -07:00
2023-11-15 11:52:05 -08:00
## Glossary
### ast
2023-03-31 07:03:45 -07:00
2023-11-30 07:30:50 -08:00
The AST to be transformed. This can be any AST that conforms to the
Unified.js specification.
2023-03-31 07:03:45 -07:00
2023-11-15 11:52:05 -08:00
## Contributing to Loom
2023-04-02 16:49:38 -07:00
2023-11-30 07:30:50 -08:00
Contributions to Loom are welcome.
### Community Guidelines
Contribute good code with full tests and docs, then submit a pull request.
I don't care about any of your other behaviors.
2023-04-02 16:49:38 -07:00
2023-11-15 06:47:20 -08:00
### VIM
2023-03-31 07:03:45 -07:00
2023-11-30 07:30:50 -08:00
This project contains a `.vimrc` file to help with development and
maintainance. See that file for more information and instructions for use.
2023-03-31 07:03:45 -07:00