From 6d6f0dad1af993c6cd5e55f5781e3a6e8d07a055 Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Wed, 10 Feb 2021 05:00:10 -0800 Subject: [PATCH 1/6] nim doc now shows correct import name in title --- compiler/docgen.nim | 20 +++++++++++++++++--- compiler/options.nim | 29 ++++++++++++++++++----------- 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/compiler/docgen.nim b/compiler/docgen.nim index 57d5e7b013503..3f670fc498734 100644 --- a/compiler/docgen.nim +++ b/compiler/docgen.nim @@ -63,6 +63,17 @@ proc prettyString(a: object): string = for k, v in fieldPairs(a): result.add k & ": " & $v & "\n" +proc moduleTitle*(conf: ConfigRef, file: AbsoluteFile): string = + var ret = getRelativePathFromConfigPath(conf, file, isTitle = true) + let dir = getNimbleFile(conf, $file).parentDir.AbsoluteDir + if not dir.isEmpty: + let result2 = relativeTo(file, dir) + if not result2.isEmpty and (ret.isEmpty or result2.string.len < ret.string.len): + ret = result2 + if ret.isEmpty: + ret = relativeTo(file, conf.projectPath) + result = ret.string.nativeToUnixPath + proc presentationPath*(conf: ConfigRef, file: AbsoluteFile, isTitle = false): RelativeFile = ## returns a relative file that will be appended to outDir let file2 = $file @@ -72,7 +83,7 @@ proc presentationPath*(conf: ConfigRef, file: AbsoluteFile, isTitle = false): Re getNimbleFile(conf, file2).parentDir.AbsoluteDir case conf.docRoot: of docRootDefault: - result = getRelativePathFromConfigPath(conf, file) + result = getRelativePathFromConfigPath(conf, file, isTitle = false) let dir = nimbleDir() if not dir.isEmpty: let result2 = relativeTo(file, dir) @@ -84,7 +95,7 @@ proc presentationPath*(conf: ConfigRef, file: AbsoluteFile, isTitle = false): Re if dir.isEmpty: bail() else: result = relativeTo(file, dir) of "@path": - result = getRelativePathFromConfigPath(conf, file) + result = getRelativePathFromConfigPath(conf, file, isTitle = false) if result.isEmpty: bail() elif conf.docRoot.len > 0: # we're (currently) requiring `isAbsolute` to avoid confusion when passing @@ -371,7 +382,7 @@ proc belongsToPackage(conf: ConfigRef; module: PSym): bool = result = module.kind == skModule and module.getnimblePkgId == conf.mainPackageId proc externalDep(d: PDoc; module: PSym): string = - if optWholeProject in d.conf.globalOptions or d.conf.docRoot.len > 0: + if optWholeProject in d.conf.globalOptions or (false and d.conf.docRoot.len > 0): let full = AbsoluteFile toFullPath(d.conf, FileIndex module.position) let tmp = getOutFile2(d.conf, presentationPath(d.conf, full), HtmlExt, sfMainModule notin module.flags) result = relativeTo(tmp, d.thisDir, '/').string @@ -1240,6 +1251,7 @@ proc genOutFile(d: PDoc, groupedToc = false): Rope = var code, content: Rope title = "" + titleAlt = "" var j = 0 var tmp = "" renderTocEntries(d[], j, 1, tmp) @@ -1257,10 +1269,12 @@ proc genOutFile(d: PDoc, groupedToc = false): Rope = title = d.meta[metaTitle] let external = presentationPath(d.conf, AbsoluteFile d.filename).changeFileExt(HtmlExt).string.nativeToUnixPath setIndexTerm(d[], external, "", title) + titleAlt = title else: # Modules get an automatic title for the HTML, but no entry in the index. # better than `extractFilename(changeFileExt(d.filename, ""))` as it disambiguates dups title = $presentationPath(d.conf, AbsoluteFile d.filename, isTitle = true).changeFileExt("") + titleAlt = moduleTitle(d.conf, AbsoluteFile d.filename).changeFileExt("") var subtitle = "".rope if d.meta[metaSubtitle] != "": dispA(d.conf, subtitle, "

$1

", diff --git a/compiler/options.nim b/compiler/options.nim index a2d6a51b3638f..378aac044e94c 100644 --- a/compiler/options.nim +++ b/compiler/options.nim @@ -761,8 +761,25 @@ when (NimMajor, NimMinor) < (1, 1) or not declared(isRelativeTo): let ret = relativePath(path, base) result = path.len > 0 and not ret.startsWith ".." -proc getRelativePathFromConfigPath*(conf: ConfigRef; f: AbsoluteFile): RelativeFile = +const stdlibDirs = [ + "pure", "core", "arch", + "pure/collections", + "pure/concurrency", + "pure/unidecode", "impure", + "wrappers", "wrappers/linenoise", + "windows", "posix", "js"] + +const + pkgPrefix = "pkg/" + stdPrefix = "std/" + +proc getRelativePathFromConfigPath*(conf: ConfigRef; f: AbsoluteFile, isTitle: bool): RelativeFile = let f = $f + if isTitle: + for dir in stdlibDirs: + let path = conf.libpath.string / dir / f.lastPathPart + if path.cmpPaths(f) == 0: + return RelativeFile(stdPrefix & f.splitFile.name) template search(paths) = for it in paths: let it = $it @@ -784,18 +801,8 @@ proc findFile*(conf: ConfigRef; f: string; suppressStdlib = false): AbsoluteFile result = rawFindFile2(conf, RelativeFile f.toLowerAscii) patchModule(conf) -const stdlibDirs = [ - "pure", "core", "arch", - "pure/collections", - "pure/concurrency", - "pure/unidecode", "impure", - "wrappers", "wrappers/linenoise", - "windows", "posix", "js"] - proc findModule*(conf: ConfigRef; modulename, currentModule: string): AbsoluteFile = # returns path to module - const pkgPrefix = "pkg/" - const stdPrefix = "std/" var m = addFileExt(modulename, NimExt) if m.startsWith(pkgPrefix): result = findFile(conf, m.substr(pkgPrefix.len), suppressStdlib = true) From fd534dd7d2da1fa87e83a0a4f29d3b442b6cc822 Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Mon, 22 Mar 2021 21:21:56 -0700 Subject: [PATCH 2/6] cleanup --- compiler/docgen.nim | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/compiler/docgen.nim b/compiler/docgen.nim index 3f670fc498734..6c85686f47736 100644 --- a/compiler/docgen.nim +++ b/compiler/docgen.nim @@ -1251,7 +1251,6 @@ proc genOutFile(d: PDoc, groupedToc = false): Rope = var code, content: Rope title = "" - titleAlt = "" var j = 0 var tmp = "" renderTocEntries(d[], j, 1, tmp) @@ -1269,12 +1268,11 @@ proc genOutFile(d: PDoc, groupedToc = false): Rope = title = d.meta[metaTitle] let external = presentationPath(d.conf, AbsoluteFile d.filename).changeFileExt(HtmlExt).string.nativeToUnixPath setIndexTerm(d[], external, "", title) - titleAlt = title else: # Modules get an automatic title for the HTML, but no entry in the index. # better than `extractFilename(changeFileExt(d.filename, ""))` as it disambiguates dups - title = $presentationPath(d.conf, AbsoluteFile d.filename, isTitle = true).changeFileExt("") - titleAlt = moduleTitle(d.conf, AbsoluteFile d.filename).changeFileExt("") + # title = $presentationPath(d.conf, AbsoluteFile d.filename, isTitle = true).changeFileExt("") + title = moduleTitle(d.conf, AbsoluteFile d.filename).changeFileExt("") var subtitle = "".rope if d.meta[metaSubtitle] != "": dispA(d.conf, subtitle, "

$1

", From 903d6e55915b745146b0642fd36ae12c004217f7 Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Mon, 22 Mar 2021 21:24:14 -0700 Subject: [PATCH 3/6] cleanup --- compiler/docgen.nim | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/compiler/docgen.nim b/compiler/docgen.nim index 6c85686f47736..7a896a36699c2 100644 --- a/compiler/docgen.nim +++ b/compiler/docgen.nim @@ -74,7 +74,7 @@ proc moduleTitle*(conf: ConfigRef, file: AbsoluteFile): string = ret = relativeTo(file, conf.projectPath) result = ret.string.nativeToUnixPath -proc presentationPath*(conf: ConfigRef, file: AbsoluteFile, isTitle = false): RelativeFile = +proc presentationPath*(conf: ConfigRef, file: AbsoluteFile): RelativeFile = ## returns a relative file that will be appended to outDir let file2 = $file template bail() = @@ -108,10 +108,7 @@ proc presentationPath*(conf: ConfigRef, file: AbsoluteFile, isTitle = false): Re bail() if isAbsolute(result.string): result = file.string.splitPath()[1].RelativeFile - if isTitle: - result = result.string.nativeToUnixPath.RelativeFile - else: - result = result.string.replace("..", dotdotMangle).RelativeFile + result = result.string.replace("..", dotdotMangle).RelativeFile doAssert not result.isEmpty doAssert not isAbsolute(result.string) @@ -1270,8 +1267,6 @@ proc genOutFile(d: PDoc, groupedToc = false): Rope = setIndexTerm(d[], external, "", title) else: # Modules get an automatic title for the HTML, but no entry in the index. - # better than `extractFilename(changeFileExt(d.filename, ""))` as it disambiguates dups - # title = $presentationPath(d.conf, AbsoluteFile d.filename, isTitle = true).changeFileExt("") title = moduleTitle(d.conf, AbsoluteFile d.filename).changeFileExt("") var subtitle = "".rope if d.meta[metaSubtitle] != "": From 6de86ef81f6d34ebb1954025e0ca6c8a725cd71a Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Mon, 22 Mar 2021 21:52:35 -0700 Subject: [PATCH 4/6] cleanup --- compiler/docgen.nim | 22 +++++++++++++--------- compiler/options.nim | 2 +- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/compiler/docgen.nim b/compiler/docgen.nim index 7a896a36699c2..53ad5d02143d9 100644 --- a/compiler/docgen.nim +++ b/compiler/docgen.nim @@ -63,16 +63,20 @@ proc prettyString(a: object): string = for k, v in fieldPairs(a): result.add k & ": " & $v & "\n" -proc moduleTitle*(conf: ConfigRef, file: AbsoluteFile): string = +proc canonicalImport*(conf: ConfigRef, file: AbsoluteFile): string = + ##[ + Shows the canonical module import, e.g.: + std/tables, fusion/pointers, system/assertions, std/private/asciitables + ]## var ret = getRelativePathFromConfigPath(conf, file, isTitle = true) let dir = getNimbleFile(conf, $file).parentDir.AbsoluteDir if not dir.isEmpty: - let result2 = relativeTo(file, dir) - if not result2.isEmpty and (ret.isEmpty or result2.string.len < ret.string.len): - ret = result2 + let relPath = relativeTo(file, dir) + if not relPath.isEmpty and (ret.isEmpty or relPath.string.len < ret.string.len): + ret = relPath if ret.isEmpty: ret = relativeTo(file, conf.projectPath) - result = ret.string.nativeToUnixPath + result = ret.string.nativeToUnixPath.changeFileExt("") proc presentationPath*(conf: ConfigRef, file: AbsoluteFile): RelativeFile = ## returns a relative file that will be appended to outDir @@ -83,7 +87,7 @@ proc presentationPath*(conf: ConfigRef, file: AbsoluteFile): RelativeFile = getNimbleFile(conf, file2).parentDir.AbsoluteDir case conf.docRoot: of docRootDefault: - result = getRelativePathFromConfigPath(conf, file, isTitle = false) + result = getRelativePathFromConfigPath(conf, file) let dir = nimbleDir() if not dir.isEmpty: let result2 = relativeTo(file, dir) @@ -95,7 +99,7 @@ proc presentationPath*(conf: ConfigRef, file: AbsoluteFile): RelativeFile = if dir.isEmpty: bail() else: result = relativeTo(file, dir) of "@path": - result = getRelativePathFromConfigPath(conf, file, isTitle = false) + result = getRelativePathFromConfigPath(conf, file) if result.isEmpty: bail() elif conf.docRoot.len > 0: # we're (currently) requiring `isAbsolute` to avoid confusion when passing @@ -379,7 +383,7 @@ proc belongsToPackage(conf: ConfigRef; module: PSym): bool = result = module.kind == skModule and module.getnimblePkgId == conf.mainPackageId proc externalDep(d: PDoc; module: PSym): string = - if optWholeProject in d.conf.globalOptions or (false and d.conf.docRoot.len > 0): + if optWholeProject in d.conf.globalOptions or d.conf.docRoot.len > 0: let full = AbsoluteFile toFullPath(d.conf, FileIndex module.position) let tmp = getOutFile2(d.conf, presentationPath(d.conf, full), HtmlExt, sfMainModule notin module.flags) result = relativeTo(tmp, d.thisDir, '/').string @@ -1267,7 +1271,7 @@ proc genOutFile(d: PDoc, groupedToc = false): Rope = setIndexTerm(d[], external, "", title) else: # Modules get an automatic title for the HTML, but no entry in the index. - title = moduleTitle(d.conf, AbsoluteFile d.filename).changeFileExt("") + title = canonicalImport(d.conf, AbsoluteFile d.filename) var subtitle = "".rope if d.meta[metaSubtitle] != "": dispA(d.conf, subtitle, "

$1

", diff --git a/compiler/options.nim b/compiler/options.nim index 378aac044e94c..2d63043df3904 100644 --- a/compiler/options.nim +++ b/compiler/options.nim @@ -773,7 +773,7 @@ const pkgPrefix = "pkg/" stdPrefix = "std/" -proc getRelativePathFromConfigPath*(conf: ConfigRef; f: AbsoluteFile, isTitle: bool): RelativeFile = +proc getRelativePathFromConfigPath*(conf: ConfigRef; f: AbsoluteFile, isTitle = false): RelativeFile = let f = $f if isTitle: for dir in stdlibDirs: From fb70719c8600784564e446874996bea886e0fac4 Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Mon, 22 Mar 2021 22:16:53 -0700 Subject: [PATCH 5/6] cleanup --- compiler/docgen.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/docgen.nim b/compiler/docgen.nim index 53ad5d02143d9..dded231d773f3 100644 --- a/compiler/docgen.nim +++ b/compiler/docgen.nim @@ -66,7 +66,7 @@ proc prettyString(a: object): string = proc canonicalImport*(conf: ConfigRef, file: AbsoluteFile): string = ##[ Shows the canonical module import, e.g.: - std/tables, fusion/pointers, system/assertions, std/private/asciitables + system, std/tables, fusion/pointers, system/assertions, std/private/asciitables ]## var ret = getRelativePathFromConfigPath(conf, file, isTitle = true) let dir = getNimbleFile(conf, $file).parentDir.AbsoluteDir From 5bdaeaedee5c212484e1fbb1bc5497eda53da518 Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Mon, 22 Mar 2021 22:17:26 -0700 Subject: [PATCH 6/6] fix tests --- nimdoc/test_out_index_dot_html/expected/index.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nimdoc/test_out_index_dot_html/expected/index.html b/nimdoc/test_out_index_dot_html/expected/index.html index 4646732f1f7d5..db7470050877e 100644 --- a/nimdoc/test_out_index_dot_html/expected/index.html +++ b/nimdoc/test_out_index_dot_html/expected/index.html @@ -17,7 +17,7 @@ -foo +nimdoc/test_out_index_dot_html/foo @@ -64,7 +64,7 @@
-

foo

+

nimdoc/test_out_index_dot_html/foo