Help language development. Donate to The Perl Foundation

Text::CodeProcessing cpan:ANTONOV last updated on 2022-04-27
# Raku Text::CodeProcessing

[![Build Status](](
[![License: Artistic-2.0](](

## In brief

The main goal of this package is to facilitate 
[Literate Programming](
with Raku.

The package has functions and a script for the evaluations of
code chunks in documents of different types (like 
[Org Mode](, 


## Installation

Package installations from both sources use [zef installer](
(which should be bundled with the "standard" [Rakudo]( installation file.)

To install the package from [Raku Modules / PAUSE](
use the shell command:

zef install Text::CodeProcessing

To install the package from the GitHub repository use the shell command:

zef install


## Usage

### Main function

The package provides the function `FileCodeChunksEvaluation` for the 
evaluation of code chunks in files. The first argument is a file name string:

FileCodeChunksEvaluation( $fileName, ... )
Here are the (optional) parameters:

- `Str :$outputFileName` : output file name
- `Str :$evalOutputPrompt = 'AUTO'` : code chunk output prompt

- `Str :$evalErrorPrompt = 'AUTO'` : code chunk error prompt

- `Bool :$noteOutputFileName = False` : whether to print out the name of the new file

- `Bool :$promptPerLine = True` : whether to put prompt to each output or error line or just the first one

When the prompt arguments are given the value `'AUTO'` then the actual prompt values are selected according to the file type:

- Markdown : `evalOutputPrompt = '# '`, `evalErrorPrompt = '#ERROR: '`

- Org-mode : `evalOutputPrompt = ': '`, `evalErrorPrompt = ':ERROR: '`

- Pod6 : `evalOutputPrompt = '# '`, `evalErrorPrompt = '#ERROR: '`

### Scripts

The [directory "./examples"](./examples) has a script files, 
[`file-code-chunks-eval`](bin/file-code-chunks-eval) and
that can be used from the command line. 

Here are script invocation examples for the code chunks evaluation in a file named "":


file-code-chunks-eval file-code-chunks-eval.raku --evalOutputPrompt="## OUTPUT :: " --evalErrorPrompt="## ERROR :: "

Here is a script invocation example for code extraction from code chunks in a file named "":


If no output file name is specified then the script
makes a new file in the same directory with the string
"_woven" ("_tangled") inserted into the input file name.


## Implementation notes

The implementation uses a greatly reduced version of the class
of Raku Jupyter kernel package/repository [BD1].
(See the class [REPLSandbox](./lib/Text/CodeProcessing/REPLSandbox.rakumod).)

Just using 
(as in [SO1]) did not provide state persistence between code chunks evaluations.
For example, creating and assigning variables or loading packages in the first code chunk
did not make those variables and packages "available" in the subsequent code chunks.

That problem is resolved by setting up a separate Raku REPL (sandbox) object. 



The following TODO items are ordered by priority, the most important are on top. 
- [X] Provide a function that works on strings.
  (By refactoring the main function `FileCodeChunksEvaluation`.)
- [ ] Add unit tests for:

  - [X] Code chunks evaluation
  - [X] Persistence of REPL state(s)
  - [ ] REPL availability
  - [X] File code chunks evaluation 

  - [X] File code extraction from chunks 

- [X] Implement handling of code chunk parameters.

- [ ] Implement data arguments for code chunks.
      (As in [Babel org-mode](

- [ ] Implement evaluation of Raku code chunks in Mathematica notebooks.

- [ ] Make the functionalities to work with languages other than Raku.
  - This is both difficult and low priority.
- [X] Comprehensive help for the CLI functions. 


## References

[BD1] Brian Duggan et al.,

[DS1] Daniel Sockwell,
["Weaving Raku: semiliterate programming in a beautiful language"](,

[SO1] Suman Khanal et al.,
["Capture and execute multiline code and incorporate result in raku"](,
[Stack Overflow](