Skip to content

Commit

Permalink
change syntax to double curly
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianschubek committed Mar 16, 2023
1 parent d87e1b2 commit 27a8edf
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 17 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ Variables can be declared on CLI execution using `key=value` pairs or just a `ke
Statements ending in `s` (e.g. `ifeqs`) can set a variable when their statement evaluates to `true`. (see [reference](#commands-reference))

#### Printing
Outputting a variable can be done using `${<value>}$` (curly braces) where `<value>` is any value defined [above](#values).
Outputting a variable can be done using `${{<value>}}$` (curly braces) where `<value>` is any value defined [above](#values).

> *Note:* Since version 0.3.0+ the syntax for printing variables has changed from `${<value>}$` to `${{<value>}}$` to avoid some common conflicts.
## Examples

Expand All @@ -137,18 +139,18 @@ foobar
$[if `1 + 2 == 4`]$
a
$[ifeq foo bar]$
b ${foo}$
b ${{foo}}$
$[else]$
c
$[end]$
${`0.1 + 0.2`}$
${{`0.1 + 0.2`}}$
${file:incl.txt}$
${{file:incl.txt}}$
${url:https://httpbin.org/uuid}$
${{url:https://example.com}}$
${env:HOME}$
${{env:HOME}}$
```
*and incl.txt:* `This was included from incl.txt`

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "utpp",
"version": "0.2.1",
"version": "0.3.0",
"description": "Universal Text Pre-Processor",
"main": "bin/index.js",
"bin": "bin/npx.js",
Expand Down
24 changes: 20 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ process.emit = function (name, data: any, ...args) {
describe: "disables inclusion from urls",
type: "boolean",
})
.option("env", {
type: "boolean",
default: true,
hidden: true,
})
.option("no-env", {
describe: "disables inclusion of environment variables",
type: "boolean",
})
.option("template", {
type: "boolean",
default: true,
Expand Down Expand Up @@ -136,6 +145,12 @@ process.emit = function (name, data: any, ...args) {
if (arg.startsWith("env:")) {
// is env variable
const envVar = await parseArg(arg.slice(4));

if (!argv.env) {
console.log(chalk.red(`Unable to import env variable '${envVar}' because option '--no-env' is set`));
stop(1);
}

if (process.env[envVar] === undefined) {
err(chalk.red(`Environment variable '${envVar}' does not exist.`));
return stop(1);
Expand Down Expand Up @@ -525,16 +540,16 @@ process.emit = function (name, data: any, ...args) {
}

let processVars = 0;
// replace variables ${}$ with their values
// replace variables with their values
if (argv.vars) {
// save all variables matches in array
const variables: string[] = [];
for (const _m of data.matchAll(/\$\{(.*?)\}\$/g)) variables.push(_m[0].trim());
for (const _m of data.matchAll(/\$\{\{(.*?)\}\}\$/g)) variables.push(_m[0].trim());
vlog(variables);

// replace all variables in raw data with placeholder
data = data.replaceAll(
/\$\{(.*?)\}\$/g,
/\$\{\{(.*?)\}\}\$/g,
(() => {
let number = 0;
return () => {
Expand All @@ -546,12 +561,13 @@ process.emit = function (name, data: any, ...args) {

// foreach variable match: parse
for (let i = 0; i < variables.length; i++) {
variables[i] = await parseArg(variables[i].slice(2, -2), true);
variables[i] = await parseArg(variables[i].slice(3, -3).trim(), true);
}

// replace all placeholders with variable values
data = data.replaceAll(/\$\%\$(.*?)\$\%\$/g, (match) => {
// $%$var:123$%$ => 123
if (!match.startsWith("$%$var:")) return match;
return variables[+match.slice(7, -3)];
});
}
Expand Down
12 changes: 6 additions & 6 deletions tests/example.txt
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
Hello ${`2+2 + 9.5`}$.
Hello ${{`2+2 + 9.5`}}$.

foobar
$[if `1 + 2 == 4`]$
a
$[ifeq foo bar]$
b ${foo}$
b ${{foo}}$
$[else]$
c
$[end]$

${`0.1 + 0.2`}$
${{ `0.1 + 0.2` }}$

${file:incl.txt}$
${{file:incl.txt}}$

${url:https://example.com}$
${{url:https://example.com}}$

${env:HOME}$
${{env:HOME}}$

0 comments on commit 27a8edf

Please sign in to comment.