Ensure that markdown code formatting is not broken for ResultSender if data itself contains any backticks

This commit is contained in:
agnostic-apollo 2021-07-14 17:39:05 +05:00
parent a189f63604
commit f4e70678b1
2 changed files with 29 additions and 10 deletions

View File

@ -8,6 +8,7 @@ import android.os.Bundle;
import com.termux.shared.R;
import com.termux.shared.data.DataUtils;
import com.termux.shared.markdown.MarkdownUtils;
import com.termux.shared.models.errors.Error;
import com.termux.shared.file.FileUtils;
import com.termux.shared.logger.Logger;
@ -206,7 +207,11 @@ public class ResultSender {
try {
if (DataUtils.isNullOrEmpty(resultConfig.resultFileErrorFormat)) {
error_or_output = String.format(RESULT_SENDER.FORMAT_FAILED_ERR__ERRMSG__STDOUT__STDERR__EXIT_CODE,
resultData.getErrCode(), resultDataErrmsg, resultDataStdout, resultDataStderr, resultDataExitCode);
MarkdownUtils.getMarkdownCodeForString(String.valueOf(resultData.getErrCode()), false),
MarkdownUtils.getMarkdownCodeForString(resultDataErrmsg, true),
MarkdownUtils.getMarkdownCodeForString(resultDataStdout, true),
MarkdownUtils.getMarkdownCodeForString(resultDataStderr, true),
MarkdownUtils.getMarkdownCodeForString(resultDataExitCode, false));
} else {
error_or_output = String.format(resultConfig.resultFileErrorFormat,
resultData.getErrCode(), resultDataErrmsg, resultDataStdout, resultDataStderr, resultDataExitCode);
@ -221,11 +226,17 @@ public class ResultSender {
if (resultDataStderr.isEmpty() && resultDataExitCode.equals("0"))
error_or_output = String.format(RESULT_SENDER.FORMAT_SUCCESS_STDOUT, resultDataStdout);
else if (resultDataStderr.isEmpty())
error_or_output = String.format(RESULT_SENDER.FORMAT_SUCCESS_STDOUT__EXIT_CODE, resultDataStdout, resultDataExitCode);
error_or_output = String.format(RESULT_SENDER.FORMAT_SUCCESS_STDOUT__EXIT_CODE,
resultDataStdout,
MarkdownUtils.getMarkdownCodeForString(resultDataExitCode, false));
else
error_or_output = String.format(RESULT_SENDER.FORMAT_SUCCESS_STDOUT__STDERR__EXIT_CODE, resultDataStdout, resultDataStderr, resultDataExitCode);
error_or_output = String.format(RESULT_SENDER.FORMAT_SUCCESS_STDOUT__STDERR__EXIT_CODE,
MarkdownUtils.getMarkdownCodeForString(resultDataStdout, true),
MarkdownUtils.getMarkdownCodeForString(resultDataStderr, true),
MarkdownUtils.getMarkdownCodeForString(resultDataExitCode, false));
} else {
error_or_output = String.format(resultConfig.resultFileOutputFormat, resultDataStdout, resultDataStderr, resultDataExitCode);
error_or_output = String.format(resultConfig.resultFileOutputFormat,
resultDataStdout, resultDataStderr, resultDataExitCode);
}
} catch (Exception e) {
error = ResultSenderErrno.ERROR_FORMAT_RESULT_OUTPUT_FAILED_WITH_EXCEPTION.getError(e.getMessage());

View File

@ -996,14 +996,18 @@ public final class TermuxConstants {
/** The {@link Formatter} format string for success if `stdout` and `exit_code` need to be written to
* {@link ResultConfig#resultFileBasename} where `stdout` maps to `%1$s` and `exit_code` to `%2$s`.
* This is used when `err` equals {@link Errno#ERRNO_SUCCESS} (-1) and `stderr` is empty
* and `exit_code` does not equal `0` and {@link ResultConfig#resultFileOutputFormat} is not passed. */
public static final String FORMAT_SUCCESS_STDOUT__EXIT_CODE = "%1$s%n%n%n%nexit_code=`%2$s`%n";
* and `exit_code` does not equal `0` and {@link ResultConfig#resultFileOutputFormat} is not passed.
* The exit code will be placed in a markdown inline code. */
public static final String FORMAT_SUCCESS_STDOUT__EXIT_CODE = "%1$s%n%n%n%nexit_code=%2$s%n";
/** The {@link Formatter} format string for success if `stdout`, `stderr` and `exit_code` need to be
* written to {@link ResultConfig#resultFileBasename} where `stdout` maps to `%1$s`, `stderr`
* maps to `%2$s` and `exit_code` to `%3$s`.
* This is used when `err` equals {@link Errno#ERRNO_SUCCESS} (-1) and `stderr` is not empty
* and {@link ResultConfig#resultFileOutputFormat} is not passed. */
public static final String FORMAT_SUCCESS_STDOUT__STDERR__EXIT_CODE = "stdout=%n```%n%1$s%n```%n%n%n%nstderr=%n```%n%2$s%n```%n%n%n%nexit_code=`%3$s`%n";
* and {@link ResultConfig#resultFileOutputFormat} is not passed.
* The stdout and stderr will be placed in a markdown code block. The exit code will be placed
* in a markdown inline code. The surrounding backticks will be 3 more than the consecutive
* backticks in any parameter itself for code blocks. */
public static final String FORMAT_SUCCESS_STDOUT__STDERR__EXIT_CODE = "stdout=%n%1$s%n%n%n%nstderr=%n%2$s%n%n%n%nexit_code=%3$s%n";
/** The {@link Formatter} format string for failure if `err`, `errmsg`(`error`), `stdout`,
* `stderr` and `exit_code` need to be written to {@link ResultConfig#resultFileBasename} where
* `err` maps to `%1$s`, `errmsg` maps to `%2$s`, `stdout` maps
@ -1011,8 +1015,12 @@ public final class TermuxConstants {
* Do not define an argument greater than `5`, like `%6$s` if you change this value since it will
* raise {@link IllegalFormatException}.
* This is used when `err` does not equal {@link Errno#ERRNO_SUCCESS} (-1) and
* {@link ResultConfig#resultFileErrorFormat} is not passed. */
public static final String FORMAT_FAILED_ERR__ERRMSG__STDOUT__STDERR__EXIT_CODE = "err=`%1$s`%n%n%n%nerrmsg=%n```%n%2$s%n```%n%n%n%nstdout=%n```%n%3$s%n```%n%n%n%nstderr=%n```%n%4$s%n```%n%n%n%nexit_code=`%5$s`%n";
* {@link ResultConfig#resultFileErrorFormat} is not passed.
* The errmsg, stdout and stderr will be placed in a markdown code block. The err and exit code
* will be placed in a markdown inline code. The surrounding backticks will be 3 more than
* the consecutive backticks in any parameter itself for code blocks. The stdout, stderr
* and exit code may be empty without any surrounding backticks if not set. */
public static final String FORMAT_FAILED_ERR__ERRMSG__STDOUT__STDERR__EXIT_CODE = "err=%1$s%n%n%n%nerrmsg=%n%2$s%n%n%n%nstdout=%n%3$s%n%n%n%nstderr=%n%4$s%n%n%n%nexit_code=%5$s%n";