Mostly get analytics working for docs

This commit is contained in:
Flaviu Tamas 2014-12-25 20:53:37 -05:00
commit f9c93d23bc
4 changed files with 33 additions and 4 deletions

View file

@ -23,6 +23,7 @@ type
id: int # for generating IDs id: int # for generating IDs
toc, section: TSections toc, section: TSections
indexValFilename: string indexValFilename: string
gaId: string # Google Analytics ID, null if doesn't exist
seenSymbols: StringTableRef # avoids duplicate symbol generation for HTML. seenSymbols: StringTableRef # avoids duplicate symbol generation for HTML.
PDoc* = ref TDocumentor ## Alias to type less. 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), initRstGenerator(result[], (if gCmd != cmdRst2tex: outHtml else: outLatex),
options.gConfigVars, filename, {roSupportRawDirective}, options.gConfigVars, filename, {roSupportRawDirective},
docgenFindFile, compilerMsgHandler) docgenFindFile, compilerMsgHandler)
if config.hasKey("doc.googleAnalytics"):
result.gaId = config["doc.googleAnalytics"]
result.seenSymbols = newStringTable(modeCaseInsensitive) result.seenSymbols = newStringTable(modeCaseInsensitive)
result.id = 100 result.id = 100
@ -534,6 +537,7 @@ proc genOutFile(d: PDoc): PRope =
var var
code, content: PRope code, content: PRope
title = "" title = ""
analytics = ""
var j = 0 var j = 0
var tmp = "" var tmp = ""
renderTocEntries(d[], j, 1, 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. # Modules get an automatic title for the HTML, but no entry in the index.
title = "Module " & extractFilename(changeFileExt(d.filename, "")) title = "Module " & extractFilename(changeFileExt(d.filename, ""))
# if there exists an analytics id, use it
if d.gaId != nil:
analytics = """
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', '$1', 'auto');
ga('send', 'pageview');
</script>
""" % [d.gaId]
let bodyname = if d.hasToc: "doc.body_toc" else: "doc.body_no_toc" let bodyname = if d.hasToc: "doc.body_toc" else: "doc.body_no_toc"
content = ropeFormatNamedVars(getConfigVar(bodyname), ["title", content = ropeFormatNamedVars(getConfigVar(bodyname), ["title",
"tableofcontents", "moduledesc", "date", "time", "content"], "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!? # XXX what is this hack doing here? 'optCompileOnly' means raw output!?
code = ropeFormatNamedVars(getConfigVar("doc.file"), ["title", code = ropeFormatNamedVars(getConfigVar("doc.file"), ["title",
"tableofcontents", "moduledesc", "date", "time", "tableofcontents", "moduledesc", "date", "time",
"content", "author", "version"], "content", "author", "version", "analytics"],
[title.toRope, toc, d.modDesc, toRope(getDateStr()), [title.toRope, toc, d.modDesc, toRope(getDateStr()),
toRope(getClockStr()), content, d.meta[metaAuthor].toRope, toRope(getClockStr()), content, d.meta[metaAuthor].toRope,
d.meta[metaVersion].toRope]) d.meta[metaVersion].toRope, analytics.toRope])
else: else:
code = content code = content
result = code result = code

View file

@ -86,6 +86,7 @@ $moduledesc
$content $content
""" """
# * $analytics: Google analytics location, includes <script> tags
doc.file = """<?xml version="1.0" encoding="utf-8" ?> doc.file = """<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
@ -1243,6 +1244,7 @@ dt pre > span.Operator ~ span.Identifier, dt pre > span.Operator ~ span.Operator
</div> </div>
</div> </div>
</div> </div>
$analytics
</body> </body>
</html> </html>
""" """

View file

@ -23,6 +23,7 @@ type
gitCommit: string gitCommit: string
quotations: Table[string, tuple[quote, author: string]] quotations: Table[string, tuple[quote, author: string]]
numProcessors: int # Set by parallelBuild:n, only works for values > 0. numProcessors: int # Set by parallelBuild:n, only works for values > 0.
gaId: string # google analytics ID, nil means analytics are disabled
TRssItem = object TRssItem = object
year, month, day, title: string year, month, day, title: string
TAction = enum TAction = enum
@ -144,7 +145,12 @@ proc parseCmdLine(c: var TConfigData) =
c.vars[substr(val, 0, idx-1)] = substr(val, idx+1) c.vars[substr(val, 0, idx-1)] = substr(val, idx+1)
of "website": action = actOnlyWebsite of "website": action = actOnlyWebsite
of "pdf": action = actPdf of "pdf": action = actPdf
else: quit(usage) of "googleanalytics":
c.gaId = val
c.nimArgs.add("--doc.googleAnalytics:" & val & " ")
else:
echo("Invalid argument $1" % [key])
quit(usage)
of cmdEnd: break of cmdEnd: break
if c.infile.len == 0: quit(usage) if c.infile.len == 0: quit(usage)

View file

@ -192,13 +192,15 @@ View at: localhost:5000
</footer> </footer>
<script> <script>
# if c.gaId != nil:
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga'); })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-48159761-1', 'nim-lang.org'); ga('create', '${c.gaId}', 'nim-lang.org');
ga('send', 'pageview'); ga('send', 'pageview');
# end if
var timer; var timer;
var prevIndex = 0; var prevIndex = 0;