!config
# Arara, the cool TeX automation tool
# Copyright (c) 2023, Island of TeX
# All rights reserved.
#
# This rule is part of arara.
identifier: detex
name: DeTeX
authors:
- Island of TeX
commands:
- name: The DeTeX program
  command: >
    @{
        c = getCommand('detex', references, mode, follow, math, spaces,
                       words, environments, reference.fileName);
        if (output.isEmpty()) {
          return c;
        }
        else {
            p = unsafelyExecuteSystemCommand(c);
            if (p.first == 0) {
                return writeToFile(toFile(output[0]), p.second, false);
            }
            else {
                return false;
            }
        }
    }
arguments:
- identifier: references
  flag: >
    @{
        return isTrue(parameters.references, '-c');
    }
- identifier: mode
  flag: >
    @{
        modes = [ 'latex' : '-l', 'tex' : '-t' ];
        if (modes.keySet().contains(parameters.mode)) {
            return modes.get(parameters.mode);
        }
        else {
            throwError('I was expecting either the TeX or LaTeX mode.');
        }
    }
- identifier: follow
  flag: >
    @{
        return isFalse(parameters.follow, '-n');
    }
- identifier: math
  flag: >
    @{
        return isFalse(parameters.math, '-r');
    }
- identifier: spaces
  flag: >
    @{
        return isTrue(parameters.spaces, '-s');
    }
- identifier: words
  flag: >
    @{
        return isTrue(parameters.words, '-w');
    }
- identifier: environments
  flag: >
    @{
        if (!isList(parameters.environments)) {
            throwError('I was expecting a list of environments.');
        }
        else {
            return [ '-e', String.join(',', parameters.environments) ];
        }
    }
- identifier: output
  flag: >
    @{
        return parameters.output;
    }
