Skip to content

Commit

Permalink
Added multiple functions
Browse files Browse the repository at this point in the history
  • Loading branch information
simonp0420 committed Jun 30, 2022
1 parent 9492542 commit f90a84d
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 16 deletions.
5 changes: 3 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ version = "0.1.0"
[deps]
DefaultApplication = "3f0dd361-4fe0-5fc6-8523-80b14ec94d85"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
Scratch = "6c6a2e73-6563-6170-7368-637461726353"
TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76"

[compat]
julia = "1"
DefaultApplication = "1.1"

Scratch = "1.1"
julia = "1"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand Down
146 changes: 132 additions & 14 deletions src/PkgOnlineHelp.jl
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
"""
A small package to help users navigate to Julia package home repositories. See
exported functions `package_site` and `pkg_site`
A package to help users navigate to Julia package home repositories and documentation
sites. See help for exported functions `pkg_site`, `add_pkg_docs`, `remove_pkg_docs`,
`list_pkg_docs`, `pkg_doc_site`, and the convenience macro `@docs`.
"""
module PkgOnlineHelp

export package_site, pkg_site
export pkg_site, add_pkg_docs, remove_pkg_docs, list_pkg_docs, pkg_docs_site, @docs

import Pkg, TOML
import DefaultApplication
using Scratch: @get_scratch!

docstr =
"""
url = package_site(pkgname::AbstractString; autoopen=true)
url = pkg_site(pkgname::AbstractString; autoopen=true)
Expand All @@ -22,15 +21,12 @@ open the URL using the default browser.
# Example
```jldoctest
julia> package_site("StaticArrays")
julia> pkg_site("StaticArrays")
"https://github.com/JuliaArrays/StaticArrays.jl.git"
```
In this example the web site is also opened in the user's browser.
"""

"$docstr"
function package_site(pkgname::AbstractString; autoopen = true)
function pkg_site(pkgname::AbstractString; autoopen = true)
occursin(".jl", pkgname) && (pkgname = split(pkgname, '.')[1])
for depot in DEPOT_PATH
isdir(joinpath(depot, "registries")) || continue
Expand All @@ -47,9 +43,131 @@ function package_site(pkgname::AbstractString; autoopen = true)
return nothing
end

const pkg_site = package_site

"$docstr"
pkg_site
const package_docs_file = "package_doc_sites.toml"
package_docs_dir = "" # will be overwritten by __init__

function __init__()
global package_docs_dir = @get_scratch!("package_docs")
end

function _get_pkgdict()
tomlfile = joinpath(package_docs_dir, package_docs_file)
if isfile(tomlfile)
pkgdict = TOML.parsefile(tomlfile)
else
pkgdict = Dict{String,String}()
end
return (pkgdict, tomlfile)
end


"""
add_pkg_docs(package::AbstractString, url::AbstractString)
Add a web site for a package's documentation to the local database.
"""
function add_pkg_docs(package::AbstractString, url::AbstractString)
pkgdict, tomlfile = _get_pkgdict()
haskey(pkgdict, package) && @warn "Replacing documentation site for $package"
pkgdict[package] = url

open(tomlfile, "w") do io
TOML.print(io, pkgdict)
end
return nothing
end

"""
remove_pkg_docs(package::AbstractString)
Remove an entry for a package's documentation web site from the local database.
"""
function remove_pkg_docs(package::AbstractString)
pkgdict, tomlfile = _get_pkgdict()
if haskey(pkgdict, package)
delete!(pkgdict, package)
open(tomlfile, "w") do io
TOML.print(io, pkgdict)
end
else
@warn "No entry for package $package in database. Nothing to delete."
end
return nothing
end

"""
list_pkg_docs(package::AbstractString)
Remove an entry for a package's documentation web site from the local database.
"""
function list_pkg_docs(io::IO=stdout)
pkgdict, tomlfile = _get_pkgdict()
TOML.print(io, pkgdict)
return nothing
end

"""
url = pkg_docs_site(package::String, autoopen=true)
Attempt to find the URL of a package's documentation site by searching
through the local database established by previous calls to `add_pkg_docs`.
If there is no entry in the database for the requested package, the
package repository URL is obtained instead via a call to `pkg_site`.
If `autoopen` is `true` (default), then the URL is opened using the
default browser.
# Example
Assume that in some previous (or the current) Julia session the user has entered
```
julia> using PkgOnlineHelp
julia> add_pkg_docs("PSSFSS", "https://simonp0420.github.io/PSSFSS.jl/stable/")
```
Then in the same or a later Julia session:
```julia
julia> using PkgOnlineHelp
julia> pkg_docs_site("PSSFSS")
"https://simonp0420.github.io/PSSFSS.jl/stable/"
In addition, the site is opened in the user's default browser.
"""
function pkg_docs_site(package::String, autoopen=true)
pkgdict, _ = _get_pkgdict()
if haskey(pkgdict, package)
url = pkgdict[package]
else
url = pkg_site(package, autoopen=false)
end
autoopen && DefaultApplication.open(url)
return url
end

"""
@docs PackageName
Retrieve the URL of the package's documentation site if previously recorded via a call to
`add_pkg_docs` or to the package's repository otherwise. Open the URL in the user's
default browser.
# Example
```
julia> using PkgOnlineHelp
julia> @docs StaticArrays
"https://github.com/JuliaArrays/StaticArrays.jl.git"
The `StaticArrays` repo site is returned and opened in the browser. In this example
the user had not previously entered the actual documentation site via a prior call
to `add_pkg_docs`.
# See Also
`add_pkg_docs`, `remove_pkg_docs`, `list_pkg_docs`, `pkg_site`
"""
macro docs(package)
:(pkg_docs_site($(string(package))))
end

end

0 comments on commit f90a84d

Please sign in to comment.