From f9c93d23bc5cd50d0063dbeed105c4be9c674eac Mon Sep 17 00:00:00 2001 From: Flaviu Tamas Date: Thu, 25 Dec 2014 20:53:37 -0500 Subject: [PATCH 1/2] Mostly get analytics working for docs --- compiler/docgen.nim | 23 +++++++++++++++++++++-- config/nimdoc.cfg | 2 ++ tools/nimweb.nim | 8 +++++++- tools/website.tmpl | 4 +++- 4 files changed, 33 insertions(+), 4 deletions(-) diff --git a/compiler/docgen.nim b/compiler/docgen.nim index 35acf1379f..ab513d2cc8 100644 --- a/compiler/docgen.nim +++ b/compiler/docgen.nim @@ -23,6 +23,7 @@ type id: int # for generating IDs toc, section: TSections indexValFilename: string + gaId: string # Google Analytics ID, null if doesn't exist seenSymbols: StringTableRef # avoids duplicate symbol generation for HTML. PDoc* = ref TDocumentor ## Alias to type less. @@ -61,6 +62,8 @@ proc newDocumentor*(filename: string, config: StringTableRef): PDoc = initRstGenerator(result[], (if gCmd != cmdRst2tex: outHtml else: outLatex), options.gConfigVars, filename, {roSupportRawDirective}, docgenFindFile, compilerMsgHandler) + if config.hasKey("doc.googleAnalytics"): + result.gaId = config["doc.googleAnalytics"] result.seenSymbols = newStringTable(modeCaseInsensitive) result.id = 100 @@ -534,6 +537,7 @@ proc genOutFile(d: PDoc): PRope = var code, content: PRope title = "" + analytics = "" var j = 0 var tmp = "" renderTocEntries(d[], j, 1, tmp) @@ -553,6 +557,21 @@ proc genOutFile(d: PDoc): PRope = # Modules get an automatic title for the HTML, but no entry in the index. title = "Module " & extractFilename(changeFileExt(d.filename, "")) + # if there exists an analytics id, use it + if d.gaId != nil: + analytics = """ + + """ % [d.gaId] + let bodyname = if d.hasToc: "doc.body_toc" else: "doc.body_no_toc" content = ropeFormatNamedVars(getConfigVar(bodyname), ["title", "tableofcontents", "moduledesc", "date", "time", "content"], @@ -562,10 +581,10 @@ proc genOutFile(d: PDoc): PRope = # XXX what is this hack doing here? 'optCompileOnly' means raw output!? code = ropeFormatNamedVars(getConfigVar("doc.file"), ["title", "tableofcontents", "moduledesc", "date", "time", - "content", "author", "version"], + "content", "author", "version", "analytics"], [title.toRope, toc, d.modDesc, toRope(getDateStr()), toRope(getClockStr()), content, d.meta[metaAuthor].toRope, - d.meta[metaVersion].toRope]) + d.meta[metaVersion].toRope, analytics.toRope]) else: code = content result = code diff --git a/config/nimdoc.cfg b/config/nimdoc.cfg index 3abae73885..3375460ccc 100644 --- a/config/nimdoc.cfg +++ b/config/nimdoc.cfg @@ -86,6 +86,7 @@ $moduledesc $content """ +# * $analytics: Google analytics location, includes + """ % [config["doc.googleAnalytics"]] + else: + result.analytics = "" + result.seenSymbols = newStringTable(modeCaseInsensitive) result.id = 100 @@ -537,7 +552,6 @@ proc genOutFile(d: PDoc): PRope = var code, content: PRope title = "" - analytics = "" var j = 0 var tmp = "" renderTocEntries(d[], j, 1, tmp) @@ -557,21 +571,6 @@ proc genOutFile(d: PDoc): PRope = # Modules get an automatic title for the HTML, but no entry in the index. title = "Module " & extractFilename(changeFileExt(d.filename, "")) - # if there exists an analytics id, use it - if d.gaId != nil: - analytics = """ - - """ % [d.gaId] - let bodyname = if d.hasToc: "doc.body_toc" else: "doc.body_no_toc" content = ropeFormatNamedVars(getConfigVar(bodyname), ["title", "tableofcontents", "moduledesc", "date", "time", "content"], @@ -584,7 +583,7 @@ proc genOutFile(d: PDoc): PRope = "content", "author", "version", "analytics"], [title.toRope, toc, d.modDesc, toRope(getDateStr()), toRope(getClockStr()), content, d.meta[metaAuthor].toRope, - d.meta[metaVersion].toRope, analytics.toRope]) + d.meta[metaVersion].toRope, d.analytics.toRope]) else: code = content result = code @@ -649,7 +648,8 @@ proc commandBuildIndex*() = let code = ropeFormatNamedVars(getConfigVar("doc.file"), ["title", "tableofcontents", "moduledesc", "date", "time", - "content", "author", "version"], + "content", "author", "version", "analytics"], ["Index".toRope, nil, nil, toRope(getDateStr()), - toRope(getClockStr()), content, nil, nil]) + toRope(getClockStr()), content, nil, nil, nil]) + # no analytics because context is not available writeRope(code, getOutFile("theindex", HtmlExt))