From 0c26abe0a88a72a04cf37617295c931826c1791f Mon Sep 17 00:00:00 2001 From: Zandr Martin Date: Mon, 8 Jul 2019 13:17:59 -0400 Subject: [PATCH] enforce 1-step indentation Allow multi-step indentation in literal blocks, and allow multi-step dedents. --- src/main.c | 10 +++++----- test/indent | 35 +++++++++++++++++++++++++++++++++++ test/lib.sh | 2 +- 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/src/main.c b/src/main.c index 2508a7f..6b948ec 100644 --- a/src/main.c +++ b/src/main.c @@ -286,19 +286,19 @@ static int parse_indent(struct parser *p, int *indent, bool write) { ++i; } parser_pushch(p, ch); - if (ch == '\n' && *indent != 0) { - // Don't change indent when we encounter empty lines + if ((ch == '\n' || ch == UTF8_INVALID) && *indent != 0) { + // Don't change indent when we encounter empty lines or EOF return *indent; } if (write) { - if (i < *indent) { + if ((i - *indent) > 1) { + parser_fatal(p, "Indented by an amount greater than 1"); + } else if (i < *indent) { for (int j = *indent; i < j; --j) { roff_macro(p, "RE", NULL); } } else if (i == *indent + 1) { fprintf(p->output, ".RS 4\n"); - } else if (i != *indent && ch == '\t') { - parser_fatal(p, "Indented by an amount greater than 1"); } } *indent = i; diff --git a/test/indent b/test/indent index 087e30c..9ea38e1 100755 --- a/test/indent +++ b/test/indent @@ -19,3 +19,38 @@ Not indented Not indented EOF end 0 + +begin "Disallows multi-step indents" +scdoc </dev/null +test(8) + +Not indented + Indented one level + Indented three levels +Not indented +EOF +end 1 + +begin "Allows indentation changes > 1 in literal blocks" +scdoc </dev/null +test(8) + +This is some code: + +\`\`\` +foobar: + # asdf +\`\`\` +EOF +end 0 + +begin "Allows multi-step dedents" +scdoc </dev/null +test(8) + +Not indented + Indented one level + Indented two levels +Not indented +EOF +end 0 diff --git a/test/lib.sh b/test/lib.sh index a26c734..b12ec75 100644 --- a/test/lib.sh +++ b/test/lib.sh @@ -2,7 +2,7 @@ printf '== %s\n' "$0" trap "printf '\n'" EXIT begin() { - printf '%-40s' "$1" + printf '%-50s' "$1" } scdoc() {