Skip to content

Commit

Permalink
Add pre.erb docs to the readme
Browse files Browse the repository at this point in the history
  • Loading branch information
schneems committed Dec 11, 2024
1 parent 2b669f5 commit dcff128
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## HEAD

- Add: Rundoc command `pre.erb` command used for dynamically templating any command using ERB syntax. ()

## 4.0.0

- Add: Rundoc command `background.stdin_write` to send a string to a backtround process' STDIN. This allows driving REPL interfaces (https://github.com/zombocom/rundoc/pull/79)
Expand Down
62 changes: 62 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ This will generate a project folder with your project in it, and a markdown `REA
- Execute Bash Commands
- [$](#shell-commands)
- [fail.$](#shell-commands)
- Dynamic command templating
- [pre.erb](#pre.erb)
- Printing
- [print.text](#print)
- [print.erb](#print)
Expand Down Expand Up @@ -257,6 +259,66 @@ These custom commands are kept to a minimum, and for the most part behave as you

Running shell commands like this can be very powerful, you'll likely want more control of how you manipulate files in your project. To do this you can use the `file.` namespace:

## Dynamic command templating

Meta commands that produce no output but instead allow for generating commands via dynamic templates.

Current Commands:

- `pre.erb`

### pre.erb

Template other commands using `pre.erb` in front of them. This will process the command using [ERB](https://rubyapi.org/3.3/o/erb), Ruby's built in templating library, then run the resulting command.

For example:

```
:::>> pre.erb $ echo "The answer to everything is <%= 6*7 %>"
```

When this runs, it will first replace the template with the result of the ERB. It would be the same as this:

```
:::>> $ echo "The answer to everything is 42"
```

The binding (variable and method scope) for `pre.erb` is shared across all executions and the default `print.erb` command. That means you can use it to persist data or logic and re-use it:

```ruby
:::-- print.erb <%
# Won't be rendered because it's using `--` visibility
def lol
"haha"
end

user = "Schneems"
%>
```

```
:::>> pre.erb $ echo <%= user %> said <%= lol() %> | tr '[:lower:]' '[:upper:]'
```

When run this would produce:

```
$ echo Schneems said haha | tr '[:lower:]' '[:upper:]'
SCHNEEMS SAID HAHA
```

Multi line commands are also supported

```
:::>> pre.erb file.write "lol.txt"
Super secret key:
<%= "#{key}" %>
```

The only thing to watch out for is if the resulting template contains a `:::>>` (or similar) rundoc marker at the beginning of the line, Rundoc will think it is a new command rather than a part of `pre.erb` template.

The visibility of the `pre.erb` is forwarded to whatever command is run.

## Print

Current commands:
Expand Down

0 comments on commit dcff128

Please sign in to comment.