Skip to main content

Crate corre

Crate corre 

Source
Expand description

Execute shell scripts embedded within text.

§Example

Let’s say you have a blog page in HTML that you want to keep updated with the total number of posts and links to all posts. Let’s call that page archive.html, as seen below.

<!-- archive.html -->
<h1>My Blog Archive</h1>
<p>Contains !((ls pages/*.html | wc -l))! post(s).</p>
<ul>
!((
for PAGE in $(ls pages/*.html)
do
  PAGENAME="$(basename $PAGE .html)"  # name of file minus .html extension
  echo "<li><a href=\"$PAGE\">${PAGENAME//'-'/' '}</a></li>"
done
))!
</ul>

This file can be processed from the command line using this project’s binary:

corre -i archive.html -o www/archive.html

You can also use the run_embedded_scripts(text, opening_tag, closing_tag) function provided by this project’s library:

// Load `archive.html` into the `String` `input_text`
let shouldRecur = false;  // Don't operate on script outputs
let output_text = corre::run_embedded_scripts(input_text, "!((", "))!", shouldRecur)?;
// Save the `String` `output_text` to the file `www/archive.html`

Both will produce the modified text:

<!-- www/archive.html -->
<h1>My Blog Archive</h1>
<p>Contains 3 post(s)</p>
<ul>
<li><a href="pages/Hydroelectric-Dams.html">Hydroelectric Dams</a></li>
<li><a href="pages/The-Finnish-Genitive-Case.html">The Finnish Genitive Case</a></li>
<li><a href="pages/Vultee-XP54-Swoose-Goose.html">Vultee XP54 Swoose Goose</a></li>
</ul>

Functions§

run_embedded_scripts
Runs all scripts that are embedded within the given text. Scripts are identified using the given opening and closing tags. Returns the original text, in which the shell scripts have been replaced with their STDOUT. If recur is true, operates on script outputs as they are generated.