Skip to content

Commit

Permalink
pretty+markdown: escape backslash and vertical bar
Browse files Browse the repository at this point in the history
  • Loading branch information
liquidaty authored Mar 8, 2023
1 parent 59530fc commit adb83e3
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 2 deletions.
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}/$@.out
@${CMP} ${TMP_DIR}/$@.out expected/$@.out && ${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}/$@.out && \
${CMP} ${TMP_DIR}/$@.out expected/$@.out && ${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}/$@.out && \
${CMP} ${TMP_DIR}/$@.out expected/$@.out && ${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}/$@.out && \
${CMP} ${TMP_DIR}/$@.out expected/$@.out && ${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

0 comments on commit adb83e3

Please sign in to comment.