txtpp

Build Badge Version Badge Docs Badge License Badge Issue Badge

A simple-to-use general purpose preprocessor for text files, written in rust.

You can: - Include another file in the current file, much like C-style #include - Execute a command in the middle of the current file and include the output

You can use txtpp both as a command line tool, or as a library with the rust crate. txtpp is well tested with unit tests and integration tests.

The full API doc is available on docs.rs

Currently in Preview. Not all features are implemented and some examples may not work.

Installation

Install with cargo cargo install txtpp txtpp --help

Examples

Include a file

Say you have 2 files foo.txt.txtpp and bar.txt ``` (foo.txt.txtpp) (bar.txt)

1 |hello 1 |bar 2 |-TXTPP#include bar.txt 2 | 3 |world 4 | ```

Running txtpp foo.txt will produce foo.txt: ``` (foo.txt)

1 |hello 2 |bar 3 |world 4 | `` Ifbar.txt.txtppalso exists, it will be preprocessed first to producebar.txt`, and the result will be used.

Execute a command

Say you have the file foo.txt.txtpp: ``` (foo.txt.txtpp)

1 |hello
2 |-TXTPP#run cat foo.txt.txtpp
3 |world 4 | Running `txtpp foo.txt` will produce `foo.txt`: (foo.txt)

1 |hello 2 |hello 3 |-TXTPP#run cat foo.txt.txtpp 3 |world 4 |world 5 | ```

More examples can be found in the examples directory. These are also used as integration tests.

Table of Contents

Feature Summary

txtpp provides directives that you can use in the .txtpp files. A directive replaces itself with the output of the directive. The directives are all prefixed with TXTPP#: - include - Include the content of another file. - run - Run a command and include the output of the command. - temp - Store text into a temporary file next to the input file. - tag - Hold the output of the next directive until a tag is seen, and replace the tag with the output. - write - Write content to the output file. Can be used for escaping directives.

Directive Overview

Syntax

A directive is a single- or multi-line structure in the source file, that looks like this: ``` {WHITESPACES}{PREFIX1}TXTPP#{DIRECTIVE} {ARG1} {WHITESPACES}{PREFIX2}{ARG2} {WHITESPACES}{PREFIX2}{ARG3} ...

``` Explanation:

  1. First line:
  2. Subsequent lines: Some directives are allowed to have more than one lines (see the specification for what they are)
  3. Ending: if a line does not match the format above, the directive ends. The ending line won't be part of this directive, but can be the start of the next directive.

For example, you can write the directive like ``` // TXTPP#run echo "hello world"

``` which will be treated like a comment in most languages to help with syntax highlighting.

The same example as a block comment /* TXTPP#run echo " hello world " -TXTPP# */ This will execute the command echo "hello world". The - in the last line in front of TXTPP# is needed to indicate that it's the start of a different directive.

Execution

The directives are executed immediately after they are parsed. They may produce an output to be included in the output file and/or have side effects such as creating a temporary file.

If the directive has output (like include and run), it will be formated as: - Every line in the output will be prepended with {WHITESPACES}, so that the indentation is consistent Directive: 1 | // TXTPP#run echo 1; echo 2 Output: 1 | 1 2 | 2 3 | - The line endings will be normalized to be the same as the output file. Whether the last line has a trailing newline is persisted from the output of the command/included file. If the output from the directive doesn't have a newline character in the end, the next line from the source file will be on the same line as the last line of the directive output. Directive: 1 | // TXTPP#run echo -n hello 2 |world 3 | Output: 1 | helloworld 2 |

Note that normally, you will not be able to connect a directive output to the previous line, since directives always start on its own line. However, you can use tag (see below) to achieve this. If there is currently an active tag directive that is listening for output, the output will be sent to the tag instead of the output file, without the indentation. and the directive will produce no output.

Directive Specification

This section contains detailed specification of each directive.

Include Directive

USAGE

This directive is used include the content of another file into the current file.

ARGUMENTS

Single-line only. The argument is FILE_PATH

BEHAVIOR

TXTPP#include foo.txt

Run Directive

USAGE

This directive is used to run a command and include the output of the command into the current file.

ARGUMENTS

Can have more than one line. The arguments are joined with a single space in between to form the COMMAND

BEHAVIOR

TXTPP#run echo "hello world"

CAVEATS

  1. txtpp will not run as a subcommand to avoid processing loops. You shouldn't need to run txtpp inside txtpp anyway.

Empty directive

USAGE

Empty directive has the empty string as the name and does nothing. It can be used to remove lines from the input.

ARGUMENTS

Can have more than one line. All arguments to the empty directive will be ignored.

BEHAVIOR

Nothing

EXAMPLE

For example, you can use it to terminate a block comment javascript function hello() { // GENERATED CODE /* TXTPP#run ./codegen -arg -really_long_arg --really-really-long-option -TXTPP# */ }

If you have to put the end of the block comment in a new line, make sure to format it correctly so it is treated as part of the directive. javascript function hello() { // GENERATED CODE /* TXTPP#run ./codegen -arg -really_long_arg --really-really-long-option -TXTPP# -*/ } In both scenarios, the entire block comment /**/ will be replaced with the output from running ./codegen

Temp Directive

USAGE

This directive is used to create a temporary file.

ARGUMENTS

Must have at least 1 argument. The first argument specifies the FILE_PATH to save the output (relative to the current file). The rest of the arguments are joined by line endings to form the CONTENT, with a trailing line ending.

BEHAVIOR

```javascript function getcities() { // GENERATED return [ // TXTPP#temp printcities.py // import csv // with open("cities.csv") as f: // reader = csv.reader(f) // for (name, population) in reader: // print(f"{{name: "{name}", population: "{population}"}},")

// TXTPP#run python print_cities.py

]; } ```

Tag Directive

USAGE

This directive is used to create a tag to store the next directive's output.

ARGUMENTS

Single-line only. The argument is TAG

BEHAVIOR

EXAMPLE

In this example, we want the output to be exactly as is because of the <pre> tag. The output of the run directive will be put in the <pre> tag. ```html

PRE_CONTENT -->

The following is invalid because the tag is used before the output is stored. html

PRE_CONTENT -->

```

Write Directive

USAGE

This directive writes its arguments to the output file. It can be used to escape other directives.

ARGUMENTS

Can have more than one line. Each argument is one line in the output

BEHAVIOR

1 |-TXTPP#write the line below will be written to the output file as is 2 |-TXTPP#run echo "hello world" 3 |stuff 4 | Output 1 |the line below will be written to the output file as is 2 |TXTPP#run echo "hello world"stuff 3 | (To put stuff on its own line, add an extra argument to the write directive)

Output Specification

This section specifies details of the output of the preprocessor.

Line endings

The output files and temporary output files will have consistent line ending with the input .txtpp files. If the input file has mixed line endings, the output file will have the same line endings as the first line in the input file.

If the input file does not have a line ending, the output file will have the same line ending as the operating system (i.e. \r\n on Windows, \n on Unix).

The output files will have a trailing newline unless --no-trailing-newline is specified. The flag will not affect the temporary output files, however. Whether a temporary file has a trailing newline depends on if the directive has an empty line in the end.