Skip to content

Commit

Permalink
fix #16973 ; nim doc now shows correct, canonical import name in title (
Browse files Browse the repository at this point in the history
#16999)

* nim doc now shows correct import name in title
  • Loading branch information
timotheecour authored Mar 23, 2021
1 parent d6a1602 commit 64e6670
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 20 deletions.
25 changes: 18 additions & 7 deletions compiler/docgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,22 @@ proc prettyString(a: object): string =
for k, v in fieldPairs(a):
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
let file2 = $file
template bail() =
Expand Down Expand Up @@ -97,10 +112,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)

Expand Down Expand Up @@ -1259,8 +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.
# better than `extractFilename(changeFileExt(d.filename, ""))` as it disambiguates dups
title = $presentationPath(d.conf, AbsoluteFile d.filename, isTitle = true).changeFileExt("")
title = canonicalImport(d.conf, AbsoluteFile d.filename)
var subtitle = "".rope
if d.meta[metaSubtitle] != "":
dispA(d.conf, subtitle, "<h2 class=\"subtitle\">$1</h2>",
Expand Down
29 changes: 18 additions & 11 deletions compiler/options.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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 = false): 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
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions nimdoc/test_out_index_dot_html/expected/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<link href='https://fonts.googleapis.com/css?family=Source+Code+Pro:400,500,600' rel='stylesheet' type='text/css'/>

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

<script type="text/javascript" src="dochack.js"></script>
Expand Down Expand Up @@ -64,7 +64,7 @@
<body>
<div class="document" id="documentId">
<div class="container">
<h1 class="title">foo</h1>
<h1 class="title">nimdoc/test_out_index_dot_html/foo</h1>
<div class="row">
<div class="three columns">
<div class="theme-switch-wrapper">
Expand Down

0 comments on commit 64e6670

Please sign in to comment.