<NME> Writers.jl
<BEF> """
A module that provides several renderers for `Document` objects. The supported
formats are currently:

  * `:html` -- generates a complete HTML site with navigation and search included.
  * `:latex` -- generates a PDF using LuaLaTeX.

"""
module Writers

import ..Documenter:
    Anchors,
    Builder,
    Documents,
    Expanders,
    Documenter,
    Utilities

import .Utilities: Selectors

#
# Format selector definitions.
#

abstract type FormatSelector <: Selectors.AbstractSelector end

abstract type LaTeXFormat    <: FormatSelector end
abstract type HTMLFormat     <: FormatSelector end

Selectors.order(::Type{LaTeXFormat})    = 2.0
Selectors.order(::Type{HTMLFormat})     = 3.0

Selectors.matcher(::Type{LaTeXFormat},    fmt, _) = isa(fmt, LaTeXWriter.LaTeX)
Selectors.matcher(::Type{HTMLFormat},     fmt, _) = isa(fmt, HTMLWriter.HTML)

Selectors.runner(::Type{LaTeXFormat},    fmt, doc) = LaTeXWriter.render(doc, fmt)

where `format` is one of the values of the [`Formats.Format`](@ref) enumeration.
"""
render(doc::Documents.Document) = render(Writer(doc.user.format), doc)

include("MarkdownWriter.jl")
include("HTMLWriter.jl")
Selectors.runner(::Type{CustomFormat}, _, doc) = CustomWriter.render(doc)

# Definition of `CustomWriter` module below...
```
"""
function render(doc::Documents.Document)
    # Render each format. Additional formats must define an `order`, `matcher`, `runner`, as
    # well as their own rendering methods in a separate module.
    for each in doc.user.format
        Selectors.dispatch(FormatSelector, each, doc)
    end
end

include("HTMLWriter.jl")
include("LaTeXWriter.jl")

end

<MSG> Revert mutation of docstring ref URLs

Fixes #208. After rendering change the URLs of `at-ref` links back to
their original value to allow for multiple renderings per Julia session.

<DFF> @@ -37,7 +37,13 @@ The method should be overloaded in each writer as
 
 where `format` is one of the values of the [`Formats.Format`](@ref) enumeration.
 """
-render(doc::Documents.Document) = render(Writer(doc.user.format), doc)
+function render(doc::Documents.Document)
+    render(Writer(doc.user.format), doc)
+    # Revert all local links to their original URLs.
+    for (link, url) in doc.internal.locallinks
+        link.url = url
+    end
+end
 
 include("MarkdownWriter.jl")
 include("HTMLWriter.jl")

Revert mutation of docstring ref URLs