Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pretty+markdown: escape backslash and vertical bar #112

Merged
merged 1 commit into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion app/pretty.c
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,25 @@ void zsv_pretty_write_cell(unsigned char *buff, size_t bytes, struct zsv_pretty_
&used_width, &utf8_err);
}
if(bytes_to_print) {
data->write(buff, 1, bytes_to_print, data->write_arg);
if((data->markdown || data->markdown_pad)
&& (memchr(buff, '|', bytes_to_print)
|| memchr(buff, '\\', bytes_to_print))
) {
char *tmp = malloc(bytes_to_print*2);
if(!tmp)
data->parser_status = zsv_status_memory;
else {
size_t tmp_len = 0;
for(size_t i = 0; i < bytes_to_print; i++) {
if(memchr("|\\", buff[i], 2))
tmp[tmp_len++] = '\\';
tmp[tmp_len++] = buff[i];
}
data->write(tmp, 1, tmp_len, data->write_arg);
free(tmp);
}
} else
data->write(buff, 1, bytes_to_print, data->write_arg);
data->line.printed += used_width;

if(ellipsis) {
Expand Down
15 changes: 14 additions & 1 deletion app/test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,25 @@ test-stack2: ${BUILD_DIR}/bin/zsv_stack${EXE}
@${PREFIX} $< ${TEST_DATA_DIR}/stack2-[12].csv ${REDIRECT} ${TMP_DIR}/[email protected]
@${CMP} ${TMP_DIR}/[email protected] expected/[email protected] && ${TEST_PASS} || ${TEST_FAIL}

test-sql test-flatten test-pretty : test-%: ${BUILD_DIR}/bin/zsv_%${EXE}
test-sql test-flatten : test-%: ${BUILD_DIR}/bin/zsv_%${EXE}
@${TEST_INIT}
@( ( ! [ -s "${TEST_DATA_DIR}/test/$*.csv" ] ) && echo "No test input for $*") || \
(${PREFIX} $< ${ARGS-$*} < ${TEST_DATA_DIR}/test/$*.csv ${REDIRECT1} ${TMP_DIR}/[email protected] && \
${CMP} ${TMP_DIR}/[email protected] expected/[email protected] && ${TEST_PASS} || ${TEST_FAIL})

test-pretty: test-pretty-1 test-pretty-escape-chars

test-pretty-1 : test-%-1: ${BUILD_DIR}/bin/zsv_%${EXE}
@${TEST_INIT}
@( ( ! [ -s "${TEST_DATA_DIR}/test/$*.csv" ] ) && echo "No test input for $*") || \
(${PREFIX} $< ${ARGS-$*} < ${TEST_DATA_DIR}/test/$*.csv ${REDIRECT1} ${TMP_DIR}/[email protected] && \
${CMP} ${TMP_DIR}/[email protected] expected/[email protected] && ${TEST_PASS} || ${TEST_FAIL})

test-pretty-escape-chars: ${BUILD_DIR}/bin/zsv_pretty${EXE}
@${TEST_INIT}
@(${PREFIX} $< ${ARGS-$*} < ${TEST_DATA_DIR}/test/pretty-escape.csv -M ${REDIRECT1} ${TMP_DIR}/[email protected] && \
${CMP} ${TMP_DIR}/[email protected] expected/[email protected] && ${TEST_PASS} || ${TEST_FAIL})

test-2tsv: test-2tsv-1 test-2tsv-2

test-2tsv-1 test-2tsv-2: test-% : ${BUILD_DIR}/bin/zsv_2tsv${EXE}
Expand Down
File renamed without changes.
3 changes: 3 additions & 0 deletions app/test/expected/test-pretty-escape-chars.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
|hi |t\|\\here|
|------|-------|
|how\|xx|are |
2 changes: 2 additions & 0 deletions data/test/pretty-escape.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
hi,t|\here
how|xx,are