fix #16973 ; nim doc now shows correct, canonical import name in title (#16999)

* nim doc now shows correct import name in title
This commit is contained in:
Timothee Cour 2021-03-23 01:31:23 -07:00 • committed by GitHub
commit 64e6670de4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 20 deletions

View file

@ -63,7 +63,22 @@ proc prettyString(a: object): string =
for k, v in fieldPairs(a): for k, v in fieldPairs(a):
result.add k & ": " & $v & "\n" result.add k & ": " & $v & "\n"
proc presentationPath*(conf: ConfigRef, file: AbsoluteFile, isTitle = false): RelativeFile = proc canonicalImport*(conf: ConfigRef, file: AbsoluteFile): string =
##[
Shows the canonical module import, e.g.:
system, 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 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.changeFileExt("")
proc presentationPath*(conf: ConfigRef, file: AbsoluteFile): RelativeFile =
## returns a relative file that will be appended to outDir ## returns a relative file that will be appended to outDir
let file2 = $file let file2 = $file
template bail() = template bail() =
@ -97,9 +112,6 @@ proc presentationPath*(conf: ConfigRef, file: AbsoluteFile, isTitle = false): Re
bail() bail()
if isAbsolute(result.string): if isAbsolute(result.string):
result = file.string.splitPath()[1].RelativeFile 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 result.isEmpty
doAssert not isAbsolute(result.string) doAssert not isAbsolute(result.string)
@ -1259,8 +1271,7 @@ proc genOutFile(d: PDoc, groupedToc = false): Rope =
setIndexTerm(d[], external, "", title) setIndexTerm(d[], external, "", title)
else: else:
# Modules get an automatic title for the HTML, but no entry in the index. # 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 = canonicalImport(d.conf, AbsoluteFile d.filename)
title = $presentationPath(d.conf, AbsoluteFile d.filename, isTitle = true).changeFileExt("")
var subtitle = "".rope var subtitle = "".rope
if d.meta[metaSubtitle] != "": if d.meta[metaSubtitle] != "":
dispA(d.conf, subtitle, "<h2 class=\"subtitle\">$1</h2>", dispA(d.conf, subtitle, "<h2 class=\"subtitle\">$1</h2>",

View file

@ -761,8 +761,25 @@ when (NimMajor, NimMinor) < (1, 1) or not declared(isRelativeTo):
let ret = relativePath(path, base) let ret = relativePath(path, base)
result = path.len > 0 and not ret.startsWith ".." 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 = false): RelativeFile =
let f = $f 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) = template search(paths) =
for it in paths: for it in paths:
let it = $it let it = $it
@ -784,18 +801,8 @@ proc findFile*(conf: ConfigRef; f: string; suppressStdlib = false): AbsoluteFile
result = rawFindFile2(conf, RelativeFile f.toLowerAscii) result = rawFindFile2(conf, RelativeFile f.toLowerAscii)
patchModule(conf) 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 = proc findModule*(conf: ConfigRef; modulename, currentModule: string): AbsoluteFile =
# returns path to module # returns path to module
const pkgPrefix = "pkg/"
const stdPrefix = "std/"
var m = addFileExt(modulename, NimExt) var m = addFileExt(modulename, NimExt)
if m.startsWith(pkgPrefix): if m.startsWith(pkgPrefix):
result = findFile(conf, m.substr(pkgPrefix.len), suppressStdlib = true) result = findFile(conf, m.substr(pkgPrefix.len), suppressStdlib = true)

View file

@ -17,7 +17,7 @@
<link href='https://fonts.googleapis.com/css?family=Source+Code+Pro:400,500,600' rel='stylesheet' type='text/css'/> <link href='https://fonts.googleapis.com/css?family=Source+Code+Pro:400,500,600' rel='stylesheet' type='text/css'/>
<!-- CSS --> <!-- CSS -->
<title>foo</title> <title>nimdoc/test_out_index_dot_html/foo</title>
<link rel="stylesheet" type="text/css" href="nimdoc.out.css"> <link rel="stylesheet" type="text/css" href="nimdoc.out.css">
<script type="text/javascript" src="dochack.js"></script> <script type="text/javascript" src="dochack.js"></script>
@ -64,7 +64,7 @@ window.addEventListener('DOMContentLoaded', main);
<body> <body>
<div class="document" id="documentId"> <div class="document" id="documentId">
<div class="container"> <div class="container">
<h1 class="title">foo</h1> <h1 class="title">nimdoc/test_out_index_dot_html/foo</h1>
<div class="row"> <div class="row">
<div class="three columns"> <div class="three columns">
<div class="theme-switch-wrapper"> <div class="theme-switch-wrapper">