!config
# Arara, the cool TeX automation tool
# Copyright (c) 2023, Island of TeX
# All rights reserved.
#
# This rule is part of arara.
identifier: knitr
name: knitr
authors:
- Adam Liter
- Island of TeX
commands:
- name: knitr
  command: >
    @{
        elements = [];
        if (!tangle.isEmpty()) elements.add('tangle = ' + tangle[0]);
        if (!quiet.isEmpty()) elements.add('quiet = ' + quiet[0]);
        out = output.isEmpty() ? 'NULL' : '"' + output[0] + '"';
        elements.add('envir = ' + envir[0]);
        elements.add('encoding = ' + encoding[0]);
        return getCommand('Rscript', '-e',
               'library(knitr); knit("' + reference.fileName + '", ' +
               'output = ' + out + ', ' + String.join(', ', elements) + ')');
    }
arguments:
- identifier: output
  flag: >
    @{
        return parameters.output;
    }
- identifier: tangle
  flag: >
    @{
        if(isTrue(parameters.tangle) && !parameters.containsKey('output')) {
            throwError('If you want to tangle the document, specify an output file.');
        }
        else {
            isTrue(parameters.tangle, 'TRUE', 'FALSE')
        }
    }
- identifier: quiet
  flag: >
    @{
        isTrue(parameters.quiet, 'TRUE', 'FALSE')
    }
- identifier: envir
  flag: >
    @{
        return parameters.envir;
    }
  default: 'parent.frame()'
- identifier: encoding
  flag: >
    @{
        return parameters.encoding;
    }
  default: 'getOption("encoding")'
