Skip to content

GUI HTML browser for documentation #34278

@mgkuhn

Description

@mgkuhn

MATLAB has a very nice command doc that starts a browser to display the manual, either the front page, or the man page of any specified function. I just had a go at drafting an equivalent doc() function for Julia, because I very much would like to have easy off-line access to the HTML documentation of both Julia and any packages in the current environment:

  • Some people may want to use Julia in places without practical Internet access (“in the field”, “on the far side of the moon”, etc.).
  • Online documentation found via Internet search may not be about the software version currently used locally (which may in particular become an issue once Julia is more commonly used installed from Linux distributions).
  • I think there is a general expectation that software comes with documentation included locally, and that users are first pointed at the local documentation, rather than some web site.

Before I turn this into a first PR, I'd like to some feedback, e.g. on

  • where in the code base this should go
  • what existing standards (file naming conventions, etc.) and mechanisms (index files or data structures) there are already to locate local HTML documentation of objects in packages

I'd hope such a doc() function can one day replace the current URL in the banner.

As a prerequisite, Julia first needs a function hasdesktop() to query if the current process has access to a GUI desktop (otherwise it makes no sense to try starting a web browser), and a function browse_url() to fire up a GUI web browser and show a provided URL. Both these functions are obviously highly platform specific, but I think I have now working solutions for Linux/Unix, Windows and macOS. (UPDATE: these functions are now in a registered new package Desktop.jl)

It then needs to locate the relevant HTML page on the local disk (across a range of different installations or build environments), or figure out a likely online location as a fallback if no suitable page can be found on the local file system. The code below currently does this only for the main manual, the HTML source of which is already part of the standard Julia distributions.

Any ideas/suggestions on how to best do this for the local HTML manuals of Base and stdlib, which are usually also pre-installed already?

And what about obtaining or building and caching local HTML documentation for installed packages? These are today normally installed by Pkg.jl without any HTML documentation included.

#!/usr/bin/env julia
using Base.Filesystem
using Desktop

manual_index = nothing

function isrelativeurl(url)
    if occursin(r"^(https?|ftp|file|mailto):/"i, url) ; return false ; end
    return !isabspath(url)
end

"""
Search for the URL of the main index.html page of the Julia manual
and store it in global variable `manual_index`.
"""
function doc_init()
    paths = [
        "../share/doc/julia/html/en/index.html",
        "../../doc/_build/html/en/index.html",
        "https://docs.julialang.org/en/v$(VERSION.major).$(VERSION.minor)/"
    ];
    for i in eachindex(paths)
        if isrelativeurl(paths[i])
            paths[i] = joinpath(Sys.BINDIR, paths[i]);
            if !isfile(paths[i])
                continue
            end
            global manual_index = "file://" * realpath(paths[i])
            return
        else
            manual_index = paths[i]
            return
        end
    end
    @error "Cannot find Julia HTML documentation in " * join(paths, ':')
end

"""
    doc()

Open the Julia documentation in a web browser.

This currently only opens the front page of the Julia manual.
(Hopefully, a future version of this function will also use an index of
all documentation strings shown in the manual of Julia or any
installed packages to get to the right page for any available package,
module, function, method, constant, etc., to give users maximum offline
access to the HTML documentation.)

See also: [`@doc`](@ref), [`browse`](@ref), [`hasdesktop`](@ref)
"""
function doc()
    if manual_index === nothing ; doc_init(); end
    if !(hasdesktop() && browse_url(manual_index))
        print("Use a web browser to look at\n$manual_index\n")
    end
end

# basic demo
doc_init()
doc()

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsystemThe documentation building systempackagesPackage management and loading

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions