From 9633b7a3cc31e729e6cf18baf6e36620ddec11df Mon Sep 17 00:00:00 2001 From: pikastech Date: Mon, 20 Mar 2023 09:59:50 +0800 Subject: [PATCH] fix indent for docgen --- port/linux/init.sh | 1 + src/PikaParser.c | 23 +++++++++++++++-------- src/dataString.c | 13 ++++++++++++- src/dataString.h | 3 ++- test/doc-test.cpp | 1 + 5 files changed, 31 insertions(+), 10 deletions(-) diff --git a/port/linux/init.sh b/port/linux/init.sh index 816b0fd45..d532d3675 100644 --- a/port/linux/init.sh +++ b/port/linux/init.sh @@ -11,6 +11,7 @@ git config --add core.filemode false chmod +x *.sh sh update-compiler.sh chmod +x package/pikascript/rust-msc-latest-linux +cp package/pikascript/rust-msc-latest-linux /bin cd package/pikascript/pikascript-core # git checkout master cd $ROOT diff --git a/src/PikaParser.c b/src/PikaParser.c index 337c972b1..d00a9c2da 100644 --- a/src/PikaParser.c +++ b/src/PikaParser.c @@ -3215,21 +3215,28 @@ static char* _parser_fixDocStringIndent(Parser* self, char* sBuff = strsCopy(&self->lineBuffs, sDocString); Arg* aOut = arg_newStr(""); char* sOut = NULL; + int bGetIndent = 0; uint32_t iLineNum = strCountSign(sBuff, '\n'); int indentThis = 0; for (int i = 0; i < iLineNum; i++) { char* sLine = strsPopToken(&self->lineBuffs, &sBuff, '\n'); - if (i == 0) { - indentThis = strGetInedent(sLine); + if (strIsBlank(sLine)) { + continue; } - if (strGetInedent(sLine) >= indentThis) { - sLine = sLine + indentThis; + if (!bGetIndent) { + bGetIndent = 1; + indentThis = strGetIndent(sLine); } - for (int k = 0; k < indent; k++) { - aOut = arg_strAppend(aOut, " "); + if (bGetIndent) { + if (strGetIndent(sLine) >= indentThis) { + sLine = sLine + indentThis; + } + for (int k = 0; k < indent; k++) { + aOut = arg_strAppend(aOut, " "); + } + aOut = arg_strAppend(aOut, sLine); + aOut = arg_strAppend(aOut, "\n"); } - aOut = arg_strAppend(aOut, sLine); - aOut = arg_strAppend(aOut, "\n"); } sOut = strsCopy(&self->lineBuffs, arg_getStr(aOut)); arg_deinit(aOut); diff --git a/src/dataString.c b/src/dataString.c index e0cfbb4a4..eb6b35602 100644 --- a/src/dataString.c +++ b/src/dataString.c @@ -380,7 +380,7 @@ int strPathGetFileName(char* input, char* output) { return i - j - 1; } -int strGetInedent(char* string) { +int strGetIndent(char* string) { int indent = 0; int len = strGetSize(string); for (int j = 0; j < len; j++) { @@ -392,3 +392,14 @@ int strGetInedent(char* string) { } return indent; } + +int strIsBlank(char* string) { + int len = strGetSize(string); + for (int j = 0; j < len; j++) { + if (string[j] != ' ' && string[j] != '\t' && string[j] != '\r' && + string[j] != '\n') { + return 0; + } + } + return 1; +} diff --git a/src/dataString.h b/src/dataString.h index 4520e1450..97473fe69 100644 --- a/src/dataString.h +++ b/src/dataString.h @@ -67,6 +67,7 @@ int strPathFormat(char* input, char* output); int strPathJoin(char* input1, char* input2, char* output); int strPathGetFolder(char* input, char* output); int strPathGetFileName(char* input, char* output); -int strGetInedent(char* string); +int strGetIndent(char* string); +int strIsBlank(char* string); #endif diff --git a/test/doc-test.cpp b/test/doc-test.cpp index cae7860bb..39cc7f124 100644 --- a/test/doc-test.cpp +++ b/test/doc-test.cpp @@ -57,6 +57,7 @@ TEST_FILE2DOCFILE(configparser, "test/out/configparser.md") TEST_FILE2DOCFILE(mqtt_file, "package/pikascript/mqtt.py", "test/out/mqtt.md") +TEST_FILE2DOCFILE(requests_file, "package/pikascript/requests.py", "test/out/requests.md") #endif