From 51949fd373516ad6df92814aecbeeda8994e0b20 Mon Sep 17 00:00:00 2001 From: Grzegorz Adam Hankiewicz Date: Mon, 21 Apr 2014 23:27:26 +0200 Subject: [PATCH] Inserts the title index term at the beginning of the buffer. --- lib/packages/docutils/rstgen.nim | 55 ++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/lib/packages/docutils/rstgen.nim b/lib/packages/docutils/rstgen.nim index 8f223ca25..860fdc618 100644 --- a/lib/packages/docutils/rstgen.nim +++ b/lib/packages/docutils/rstgen.nim @@ -76,11 +76,12 @@ proc initRstGenerator*(g: var TRstGenerator, target: TOutputTarget, ## know what to use, pass the results of the `defaultConfig() ## <#defaultConfig>_` proc. ## - ## The `filename` will be used mostly for error reporting, you can pass any - ## non ``nil`` string here. If `filename` ends with the ``.nim`` extension, - ## the title for the document will be set by default to ``Module filename``. - ## This default title can be overriden by the embedded rst, but it helps to - ## prettify the generated index if no title is found. + ## The `filename` parameter will be used for error reporting and creating + ## index hyperlinks to the file, but you can pass an empty string here if you + ## are parsing a stream in memory. If `filename` ends with the ``.nim`` + ## extension, the title for the document will be set by default to ``Module + ## filename``. This default title can be overriden by the embedded rst, but + ## it helps to prettify the generated index if no title is found. ## ## The ``TRstParseOptions``, ``TFindFileHandler`` and ``TMsgHandler`` types ## are defined in the the `packages/docutils/rst module `_. @@ -270,15 +271,19 @@ proc setIndexTerm*(d: var TRstGenerator, id, term: string, linkTitle, linkDesc = "") = ## Adds a `term` to the index using the specified hyperlink identifier. ## - ## The ``d.theIndex`` string will be used to append the term in the format - ## ``termfile#id``. + ## A new entry will be added to the index using the format + ## ``termfile#id``. The file part will come from the `filename` + ## parameter used in a previous call to the `initRstGenerator() + ## <#initRstGenerator>`_ proc. ## - ## The anchor will be the based on the name of the file currently being - ## parsed plus the specified `id`. The `id` will be appended with a hash - ## character only if its length is not zero, otherwise no specific anchor - ## will be generated. In general you should only pass an empty `id` value - ## for the title of standalone rst documents (they are special for the - ## `mergeIndexes() <#mergeIndexes>`_ proc). + ## The `id` will be appended with a hash character only if its length is not + ## zero, otherwise no specific anchor will be generated. In general you + ## should only pass an empty `id` value for the title of standalone rst + ## documents (they are special for the `mergeIndexes() <#mergeIndexes>`_ + ## proc, see `Index (idx) file format `_ + ## for more information). Unlike other index terms, title entries are + ## inserted at the beginning of the accumulated buffer to maintain a logical + ## order of entries. ## ## If `linkTitle` or `linkDesc` are not the empty string, two additional ## columns with their contents will be added. @@ -286,17 +291,25 @@ proc setIndexTerm*(d: var TRstGenerator, id, term: string, ## The index won't be written to disk unless you call `writeIndexFile() ## <#writeIndexFile>`_. The purpose of the index is documented in the `docgen ## tools guide `_. - d.theIndex.add(term) - d.theIndex.add('\t') + assert(not d.theIndex.isNil) + var + entry = term + isTitle = false + entry.add('\t') let htmlFile = changeFileExt(extractFilename(d.filename), HtmlExt) - d.theIndex.add(htmlFile) + entry.add(htmlFile) if id.len > 0: - d.theIndex.add('#') - d.theIndex.add(id) + entry.add('#') + entry.add(id) + else: + isTitle = true if linkTitle.len > 0 or linkDesc.len > 0: - d.theIndex.add('\t' & linkTitle.quoteIndexColumn) - d.theIndex.add('\t' & linkDesc.quoteIndexColumn) - d.theIndex.add("\n") + entry.add('\t' & linkTitle.quoteIndexColumn) + entry.add('\t' & linkDesc.quoteIndexColumn) + entry.add("\n") + + if isTitle: d.theIndex.insert(entry) + else: d.theIndex.add(entry) proc hash(n: PRstNode): int = if n.kind == rnLeaf: