From ce3d73e68de3c5f507971dda42ad3c030e21bb89 Mon Sep 17 00:00:00 2001 From: Werner Thumann Date: Wed, 2 Feb 2022 21:13:06 +0100 Subject: [PATCH 1/2] Do not write TOC when running knitCheck --- src/Knit.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Knit.kt b/src/Knit.kt index 76b3b1a..a22464e 100644 --- a/src/Knit.kt +++ b/src/Knit.kt @@ -434,7 +434,7 @@ fun KnitContext.knit(inputFile: File): Boolean { } addAll(markdown.postTocText) } - if (newLines != markdown.inText) writeLines(inputFile, newLines) + if (newLines != markdown.inText) writeLinesIfNeeded(inputFile, newLines) // check apiRefs for (apiRef in allApiRefs) { if (apiRef.name in remainingApiRefNames) { From 128fa858fa2b65dc47bdb971df68d84fa10525f4 Mon Sep 17 00:00:00 2001 From: Werner Thumann Date: Thu, 3 Feb 2022 17:42:09 +0100 Subject: [PATCH 2/2] Avoid reading file twice when TOC is out-of-date --- src/Knit.kt | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/Knit.kt b/src/Knit.kt index a22464e..335222b 100644 --- a/src/Knit.kt +++ b/src/Knit.kt @@ -434,7 +434,7 @@ fun KnitContext.knit(inputFile: File): Boolean { } addAll(markdown.postTocText) } - if (newLines != markdown.inText) writeLinesIfNeeded(inputFile, newLines) + if (newLines != markdown.inText) checkOrWrite(inputFile, markdown.inText, newLines) // check apiRefs for (apiRef in allApiRefs) { if (apiRef.name in remainingApiRefNames) { @@ -662,12 +662,16 @@ fun KnitContext.writeLinesIfNeeded(file: File, outLines: List) { null } if (outLines != oldLines) { - if (check) { - val text = formatOutdated(oldLines, outLines) - log.outdated("WARNING: $file: $text") - } else { - writeLines(file, outLines) - } + checkOrWrite(file, oldLines, outLines) + } +} + +private fun KnitContext.checkOrWrite(file: File, oldLines: List?, outLines: List) { + if (check) { + val text = formatOutdated(oldLines, outLines) + log.outdated("WARNING: $file: $text") + } else { + writeLines(file, outLines) } }