diff options
author | Andrey Makarov <ph.makarov@gmail.com> | 2023-07-07 04:38:37 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-07 12:38:37 +0200 |
commit | 2e987cb75a02be74b53150774316036f0befba88 (patch) | |
tree | f9d99bc93093f2fce0e2c2cb7558348fee8eb00c | |
parent | 148ff74c9385909fc1e10c8d29499fb815a94fb8 (diff) | |
download | Nim-2e987cb75a02be74b53150774316036f0befba88.tar.gz |
Tolerate markup errors for doc comments (#19607) (#22235)
Follow-up to #21576 (for solving #19607). 1) errors in Markdown mode for `.nim` doc comments are reported with red color but allow to generate `.html` with the comment represented by literate block (monospaced text). We suppose that it's what people want for (supposedly) small doc comments. And this behavior is also a bit more Markdown-ish in the sense that Markdown generally does not have the concept of parsing error. - However, for standalone `.md` it's **not** applied because for large files the consequences are way bigger. (In {.doctype: rst.} mode the behavior is the same as before -- report the error and stop.) In future, when our parser can handle Markdown without errors according to the spec, this code will most probably be not needed.
-rw-r--r-- | compiler/docgen.nim | 16 | ||||
-rw-r--r-- | compiler/msgs.nim | 6 |
2 files changed, 17 insertions, 5 deletions
diff --git a/compiler/docgen.nim b/compiler/docgen.nim index 81e23b069..958d804b3 100644 --- a/compiler/docgen.nim +++ b/compiler/docgen.nim @@ -259,7 +259,16 @@ template declareClosures(currentFilename: AbsoluteFile, destFile: string) = of mwUnusedImportdoc: k = warnRstUnusedImportdoc of mwRstStyle: k = warnRstStyle {.gcsafe.}: - globalError(conf, newLineInfo(conf, AbsoluteFile filename, line, col), k, arg) + let errorsAsWarnings = (roPreferMarkdown in d.sharedState.options) and + not d.standaloneDoc # not tolerate errors in .rst/.md files + if whichMsgClass(msgKind) == mcError and errorsAsWarnings: + liMessage(conf, newLineInfo(conf, AbsoluteFile filename, line, col), + k, arg, doNothing, instLoc(), ignoreError=true) + # when our Markdown parser fails, we currently can only terminate the + # parsing (and then we will return monospaced text instead of markup): + raiseRecoverableError("") + else: + globalError(conf, newLineInfo(conf, AbsoluteFile filename, line, col), k, arg) proc docgenFindFile(s: string): string {.gcsafe.} = result = options.findFile(conf, s).string @@ -311,8 +320,9 @@ proc newDocumentor*(filename: AbsoluteFile; cache: IdentCache; conf: ConfigRef, standaloneDoc = false, preferMarkdown = true, hasToc = true): PDoc = let destFile = getOutFile2(conf, presentationPath(conf, filename), outExt, false).string - declareClosures(currentFilename = filename, destFile = destFile) new(result) + let d = result # pass `d` to `declareClosures`: + declareClosures(currentFilename = filename, destFile = destFile) result.module = module result.conf = conf result.cache = cache @@ -424,7 +434,7 @@ proc genComment(d: PDoc, n: PNode): PRstNode = toColumn(n.info) + DocColOffset, d.conf, d.sharedState) except ERecoverableError: - result = nil + result = newRstNode(rnLiteralBlock, @[newRstLeaf(n.comment)]) proc genRecCommentAux(d: PDoc, n: PNode): PRstNode = if n == nil: return nil diff --git a/compiler/msgs.nim b/compiler/msgs.nim index 05ace315e..03d38398a 100644 --- a/compiler/msgs.nim +++ b/compiler/msgs.nim @@ -511,7 +511,8 @@ proc formatMsg*(conf: ConfigRef; info: TLineInfo, msg: TMsgKind, arg: string): s conf.toFileLineCol(info) & " " & title & getMessageStr(msg, arg) proc liMessage*(conf: ConfigRef; info: TLineInfo, msg: TMsgKind, arg: string, - eh: TErrorHandling, info2: InstantiationInfo, isRaw = false) {.gcsafe, noinline.} = + eh: TErrorHandling, info2: InstantiationInfo, isRaw = false, + ignoreError = false) {.gcsafe, noinline.} = var title: string color: ForegroundColor @@ -576,7 +577,8 @@ proc liMessage*(conf: ConfigRef; info: TLineInfo, msg: TMsgKind, arg: string, " compiler msg initiated here", KindColor, KindFormat % $hintMsgOrigin, resetStyle, conf.unitSep) - handleError(conf, msg, eh, s, ignoreMsg) + if not ignoreError: + handleError(conf, msg, eh, s, ignoreMsg) if msg in fatalMsgs: # most likely would have died here but just in case, we restore state conf.m.errorOutputs = errorOutputsOld |