summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKaushal Modi <kaushal.modi@gmail.com>2020-05-28 03:13:24 -0400
committerGitHub <noreply@github.com>2020-05-28 00:13:24 -0700
commite62ccaa4dcc4f909e58c31be9073eb75b7f27950 (patch)
tree143c12802c5c5a9715c562b4a233fb44c9a80df3
parent1fc40db984041ebc65470677ec69e621d26fd4df (diff)
downloadNim-e62ccaa4dcc4f909e58c31be9073eb75b7f27950.tar.gz
Propagate the outDir to rstgen to fix hrefs for modules in subdirs (#14479)
-rw-r--r--compiler/docgen.nim3
-rw-r--r--lib/packages/docutils/rstgen.nim16
-rw-r--r--nimdoc/testproject/expected/subdir/subdir_b/utils.idx12
-rw-r--r--nimdoc/testproject/expected/testproject.idx14
-rw-r--r--nimdoc/testproject/expected/theindex.html2
5 files changed, 41 insertions, 6 deletions
diff --git a/compiler/docgen.nim b/compiler/docgen.nim
index 207294597..d1e2d42c5 100644
--- a/compiler/docgen.nim
+++ b/compiler/docgen.nim
@@ -47,7 +47,6 @@ type
     exampleCounter: int
     emitted: IntSet # we need to track which symbols have been emitted
                     # already. See bug #3655
-    destFile*: AbsoluteFile
     thisDir*: AbsoluteDir
     exampleGroups: OrderedTable[string, ExampleGroup]
     wroteSupportFiles*: bool
@@ -179,6 +178,7 @@ proc newDocumentor*(filename: AbsoluteFile; cache: IdentCache; conf: ConfigRef,
   result.module = module
   result.conf = conf
   result.cache = cache
+  result.outDir = conf.outDir
   initRstGenerator(result[], (if conf.cmd != cmdRst2tex: outHtml else: outLatex),
                    conf.configVars, filename.string, {roSupportRawDirective, roSupportMarkdown},
                    docgenFindFile, compilerMsgHandler)
@@ -1366,4 +1366,3 @@ proc commandBuildIndex*(conf: ConfigRef, dir: string, outFile = RelativeFile"")
 
   if not writeRope(code, filename):
     rawMessage(conf, errCannotOpenFile, filename.string)
-
diff --git a/lib/packages/docutils/rstgen.nim b/lib/packages/docutils/rstgen.nim
index dbc1a4845..6a7b33cc0 100644
--- a/lib/packages/docutils/rstgen.nim
+++ b/lib/packages/docutils/rstgen.nim
@@ -27,8 +27,8 @@
 
 import strutils, os, hashes, strtabs, rstast, rst, highlite, tables, sequtils,
   algorithm, parseutils
-
 import "$lib/../compiler/nimpaths"
+import "$lib/../compiler/pathutils"
 
 const
   HtmlExt = "html"
@@ -57,7 +57,9 @@ type
     options*: RstParseOptions
     findFile*: FindFileHandler
     msgHandler*: MsgHandler
-    filename*: string
+    outDir*: AbsoluteDir      ## output directory, initialized by docgen.nim
+    destFile*: AbsoluteFile   ## output (HTML) file, initialized by docgen.nim
+    filename*: string         ## source Nim or Rst file
     meta*: array[MetaEnum, string]
     currentSection: string ## \
     ## Stores the empty string or the last headline/overline found in the rst
@@ -755,7 +757,15 @@ proc renderHeadline(d: PDoc, n: PRstNode, result: var string) =
 
   # Generate index entry using spaces to indicate TOC level for the output HTML.
   assert n.level >= 0
-  setIndexTerm(d, changeFileExt(extractFilename(d.filename), HtmlExt), refname, tmp.stripTocHtml,
+  let
+    htmlFileRelPath = if d.outDir.isEmpty():
+                        # /foo/bar/zoo.nim -> zoo.html
+                        changeFileExt(extractFilename(d.filename), HtmlExt)
+                      else: # d is initialized in docgen.nim
+                        # outDir   = /foo              -\
+                        # destFile = /foo/bar/zoo.html -|-> bar/zoo.html
+                        d.destFile.relativeTo(d.outDir, '/').string
+  setIndexTerm(d, htmlFileRelPath, refname, tmp.stripTocHtml,
     spaces(max(0, n.level)) & tmp)
 
 proc renderOverline(d: PDoc, n: PRstNode, result: var string) =
diff --git a/nimdoc/testproject/expected/subdir/subdir_b/utils.idx b/nimdoc/testproject/expected/subdir/subdir_b/utils.idx
new file mode 100644
index 000000000..c848fc26a
--- /dev/null
+++ b/nimdoc/testproject/expected/subdir/subdir_b/utils.idx
@@ -0,0 +1,12 @@
+This is now a header	subdir/subdir_b/utils.html#this-is-now-a-header	 This is now a header	
+Next header	subdir/subdir_b/utils.html#this-is-now-a-header-next-header	  Next header	
+And so on	subdir/subdir_b/utils.html#next-header-and-so-on	   And so on	
+More headers	subdir/subdir_b/utils.html#more-headers	 More headers	
+Up to level 6	subdir/subdir_b/utils.html#more-headers-up-to-level-6	      Up to level 6	
+enumValueA	subdir/subdir_b/utils.html#enumValueA	SomeType.enumValueA	
+enumValueB	subdir/subdir_b/utils.html#enumValueB	SomeType.enumValueB	
+enumValueC	subdir/subdir_b/utils.html#enumValueC	SomeType.enumValueC	
+SomeType	subdir/subdir_b/utils.html#SomeType	utils: SomeType	
+someType	subdir/subdir_b/utils.html#someType_2	utils: someType(): SomeType	
+aEnum	subdir/subdir_b/utils.html#aEnum.t	utils: aEnum(): untyped	
+bEnum	subdir/subdir_b/utils.html#bEnum.t	utils: bEnum(): untyped	
diff --git a/nimdoc/testproject/expected/testproject.idx b/nimdoc/testproject/expected/testproject.idx
new file mode 100644
index 000000000..106c9cb76
--- /dev/null
+++ b/nimdoc/testproject/expected/testproject.idx
@@ -0,0 +1,14 @@
+C_A	testproject.html#C_A	testproject: C_A	
+C_B	testproject.html#C_B	testproject: C_B	
+C_C	testproject.html#C_C	testproject: C_C	
+C_D	testproject.html#C_D	testproject: C_D	
+foo	testproject.html#foo.t,SomeType,SomeType	testproject: foo(a, b: SomeType)	
+bar	testproject.html#bar,T,T	testproject: bar[T](a, b: T): T	
+baz	testproject.html#baz,T,T	testproject: baz[T](a, b: T): T	
+buzz	testproject.html#buzz,T,T	testproject: buzz[T](a, b: T): T	
+bar	testproject.html#bar.m	testproject: bar(): untyped	
+aVariable	testproject.html#aVariable	testproject: aVariable	
+A	testproject.html#A	testproject: A	
+B	testproject.html#B	testproject: B	
+someFunc	testproject.html#someFunc	testproject: someFunc()	
+isValid	testproject.html#isValid,T	testproject: isValid[T](x: T): bool	
diff --git a/nimdoc/testproject/expected/theindex.html b/nimdoc/testproject/expected/theindex.html
index a76f5961f..18a7ca2c2 100644
--- a/nimdoc/testproject/expected/theindex.html
+++ b/nimdoc/testproject/expected/theindex.html
@@ -72,7 +72,7 @@ function main() {
 <div class="document" id="documentId">
   <div class="container">
     <h1 class="title">Index</h1>
-    Modules: <a href="testproject.html">testproject</a>, <a href="utils.html">utils</a>.<br/><p /><h2>API symbols</h2>
+    Modules: <a href="subdir/subdir_b/utils.html">subdir/subdir_b/utils</a>, <a href="testproject.html">testproject</a>.<br/><p /><h2>API symbols</h2>
 <dl><dt><a name="A" href="#A"><span>A:</span></a></dt><dd><ul class="simple">
 <li><a class="reference external"
           data-doc-search-tag="testproject: A" href="testproject.html#A">testproject: A</a></li>