Skip to content

Commit 64e6670

Browse files
authored
fix #16973 ; nim doc now shows correct, canonical import name in title (#16999)
* nim doc now shows correct import name in title
1 parent d6a1602 commit 64e6670

File tree

3 files changed

+38
-20
lines changed

3 files changed

+38
-20
lines changed

compiler/docgen.nim

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,22 @@ proc prettyString(a: object): string =
6363
for k, v in fieldPairs(a):
6464
result.add k & ": " & $v & "\n"
6565

66-
proc presentationPath*(conf: ConfigRef, file: AbsoluteFile, isTitle = false): RelativeFile =
66+
proc canonicalImport*(conf: ConfigRef, file: AbsoluteFile): string =
67+
##[
68+
Shows the canonical module import, e.g.:
69+
system, std/tables, fusion/pointers, system/assertions, std/private/asciitables
70+
]##
71+
var ret = getRelativePathFromConfigPath(conf, file, isTitle = true)
72+
let dir = getNimbleFile(conf, $file).parentDir.AbsoluteDir
73+
if not dir.isEmpty:
74+
let relPath = relativeTo(file, dir)
75+
if not relPath.isEmpty and (ret.isEmpty or relPath.string.len < ret.string.len):
76+
ret = relPath
77+
if ret.isEmpty:
78+
ret = relativeTo(file, conf.projectPath)
79+
result = ret.string.nativeToUnixPath.changeFileExt("")
80+
81+
proc presentationPath*(conf: ConfigRef, file: AbsoluteFile): RelativeFile =
6782
## returns a relative file that will be appended to outDir
6883
let file2 = $file
6984
template bail() =
@@ -97,10 +112,7 @@ proc presentationPath*(conf: ConfigRef, file: AbsoluteFile, isTitle = false): Re
97112
bail()
98113
if isAbsolute(result.string):
99114
result = file.string.splitPath()[1].RelativeFile
100-
if isTitle:
101-
result = result.string.nativeToUnixPath.RelativeFile
102-
else:
103-
result = result.string.replace("..", dotdotMangle).RelativeFile
115+
result = result.string.replace("..", dotdotMangle).RelativeFile
104116
doAssert not result.isEmpty
105117
doAssert not isAbsolute(result.string)
106118

@@ -1259,8 +1271,7 @@ proc genOutFile(d: PDoc, groupedToc = false): Rope =
12591271
setIndexTerm(d[], external, "", title)
12601272
else:
12611273
# Modules get an automatic title for the HTML, but no entry in the index.
1262-
# better than `extractFilename(changeFileExt(d.filename, ""))` as it disambiguates dups
1263-
title = $presentationPath(d.conf, AbsoluteFile d.filename, isTitle = true).changeFileExt("")
1274+
title = canonicalImport(d.conf, AbsoluteFile d.filename)
12641275
var subtitle = "".rope
12651276
if d.meta[metaSubtitle] != "":
12661277
dispA(d.conf, subtitle, "<h2 class=\"subtitle\">$1</h2>",

compiler/options.nim

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -761,8 +761,25 @@ when (NimMajor, NimMinor) < (1, 1) or not declared(isRelativeTo):
761761
let ret = relativePath(path, base)
762762
result = path.len > 0 and not ret.startsWith ".."
763763

764-
proc getRelativePathFromConfigPath*(conf: ConfigRef; f: AbsoluteFile): RelativeFile =
764+
const stdlibDirs = [
765+
"pure", "core", "arch",
766+
"pure/collections",
767+
"pure/concurrency",
768+
"pure/unidecode", "impure",
769+
"wrappers", "wrappers/linenoise",
770+
"windows", "posix", "js"]
771+
772+
const
773+
pkgPrefix = "pkg/"
774+
stdPrefix = "std/"
775+
776+
proc getRelativePathFromConfigPath*(conf: ConfigRef; f: AbsoluteFile, isTitle = false): RelativeFile =
765777
let f = $f
778+
if isTitle:
779+
for dir in stdlibDirs:
780+
let path = conf.libpath.string / dir / f.lastPathPart
781+
if path.cmpPaths(f) == 0:
782+
return RelativeFile(stdPrefix & f.splitFile.name)
766783
template search(paths) =
767784
for it in paths:
768785
let it = $it
@@ -784,18 +801,8 @@ proc findFile*(conf: ConfigRef; f: string; suppressStdlib = false): AbsoluteFile
784801
result = rawFindFile2(conf, RelativeFile f.toLowerAscii)
785802
patchModule(conf)
786803

787-
const stdlibDirs = [
788-
"pure", "core", "arch",
789-
"pure/collections",
790-
"pure/concurrency",
791-
"pure/unidecode", "impure",
792-
"wrappers", "wrappers/linenoise",
793-
"windows", "posix", "js"]
794-
795804
proc findModule*(conf: ConfigRef; modulename, currentModule: string): AbsoluteFile =
796805
# returns path to module
797-
const pkgPrefix = "pkg/"
798-
const stdPrefix = "std/"
799806
var m = addFileExt(modulename, NimExt)
800807
if m.startsWith(pkgPrefix):
801808
result = findFile(conf, m.substr(pkgPrefix.len), suppressStdlib = true)

nimdoc/test_out_index_dot_html/expected/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<link href='https://fonts.googleapis.com/css?family=Source+Code+Pro:400,500,600' rel='stylesheet' type='text/css'/>
1818

1919
<!-- CSS -->
20-
<title>foo</title>
20+
<title>nimdoc/test_out_index_dot_html/foo</title>
2121
<link rel="stylesheet" type="text/css" href="nimdoc.out.css">
2222

2323
<script type="text/javascript" src="dochack.js"></script>
@@ -64,7 +64,7 @@
6464
<body>
6565
<div class="document" id="documentId">
6666
<div class="container">
67-
<h1 class="title">foo</h1>
67+
<h1 class="title">nimdoc/test_out_index_dot_html/foo</h1>
6868
<div class="row">
6969
<div class="three columns">
7070
<div class="theme-switch-wrapper">

0 commit comments

Comments
 (0)