-
Notifications
You must be signed in to change notification settings - Fork 56
Prepare for a new version #145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
asinghvi17
wants to merge
8
commits into
master
Choose a base branch
from
as/new_version
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
de73c3d
Show descriptions of data sets. (#146)
asinghvi17 f74c7f2
Add new dataset instructions (#147)
asinghvi17 eb81b48
Add a regex-based HTML to Markdown rewriter for docs
asinghvi17 bda35b3
More and better docs for `description`
asinghvi17 9154051
Inject metadata into all DataFrames indicating origin from RDatasets
asinghvi17 7882892
Document `description` in the README
asinghvi17 2b92f04
Make `description` a bit more robust
asinghvi17 0f6fbce
Address PR review feedback for v1.0.0 release
asinghvi17 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| R --no-save <<END | ||
| source("src/update_doc.r") | ||
| update_docs(".") | ||
| END |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| R --no-save <<END | ||
| source("src/update_doc.r") | ||
| update_package_doc(".", "$1") | ||
| END |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,19 +6,152 @@ const Dataset_typedetect_rows = Dict{Tuple{String, String}, Union{Vector,Dict}}( | |
|
|
||
| function dataset(package_name::AbstractString, dataset_name::AbstractString) | ||
| basename = joinpath(@__DIR__, "..", "data", package_name) | ||
|
|
||
| # First, identify possible files | ||
| rdataname = joinpath(basename, string(dataset_name, ".RData")) | ||
| rdaname = joinpath(basename, string(dataset_name, ".rda")) | ||
| if isfile(rdaname) | ||
| return load(rdaname)[dataset_name] | ||
| end | ||
|
|
||
| csvname = joinpath(basename, string(dataset_name, ".csv.gz")) | ||
| if isfile(csvname) | ||
| return open(csvname,"r") do io | ||
| # Then, check to see which exists. If none exist, error. | ||
| dataset = if isfile(rdataname) | ||
| load(rdataname)[dataset_name] | ||
| elseif isfile(rdaname) | ||
| load(rdaname)[dataset_name] | ||
| elseif isfile(csvname) | ||
| open(csvname,"r") do io | ||
| uncompressed = IOBuffer(read(GzipDecompressorStream(io))) | ||
| DataFrame(CSV.File(uncompressed, delim=',', quotechar='\"', missingstring="NA", | ||
| types=get(Dataset_typedetect_rows, (package_name, dataset_name), nothing)) ) | ||
| end | ||
| else | ||
| error("Unable to locate dataset file $rdaname or $csvname") | ||
| end | ||
| # Finally, inject metadata into the dataframe to indicate origin: | ||
| metadata!(dataset, "RDatasets.jl", (string(package_name), string(dataset_name))) | ||
| return dataset | ||
| end | ||
|
|
||
|
|
||
| """ | ||
| RDatasets.description(package_name::AbstractString, dataset_name::AbstractString) | ||
| RDatasets.description(df::AbstractDataFrame; default=nothing) | ||
|
|
||
| Return an `RDatasetDescription` object containing the description of the dataset. | ||
|
|
||
| Invoke this function in exactly the same way you would invoke `dataset` to get the dataset itself. | ||
|
|
||
| This object prints well in the REPL, and can also be shown as Markdown or HTML. | ||
|
|
||
| When passing a `DataFrame`, it must have been obtained from `RDatasets.dataset`. If the DataFrame | ||
| does not have the required metadata, an error is thrown unless a `default` value is provided, | ||
| in which case that value is returned instead. | ||
| """ | ||
| function description(package_name::AbstractString, dataset_name::AbstractString) | ||
| doc_html_file = joinpath(@__DIR__, "..", "doc", package_name, "$dataset_name.html") | ||
| if isfile(doc_html_file) | ||
| return RDatasetDescription(read(doc_html_file, String)) | ||
| else | ||
| return RDatasetDescription("No description available.") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd distinguish two cases:
|
||
| end | ||
| end | ||
|
|
||
| # This is a convenience function to get the description of a dataset from a DataFrame. | ||
| # Since we set metadata on the DataFrame, we can use this to get the description. | ||
| function description(df::AbstractDataFrame; default=nothing) | ||
| if "RDatasets.jl" in metadatakeys(df) | ||
| package_name, dataset_name = metadata(df, "RDatasets.jl") | ||
| return description(package_name, dataset_name) | ||
| elseif default !== nothing | ||
| return default | ||
| else | ||
| throw(ArgumentError("DataFrame does not have RDatasets.jl metadata. Use a DataFrame obtained from `RDatasets.dataset`, or provide a `default` value.")) | ||
| end | ||
| end | ||
|
|
||
| """ | ||
| RDatasetDescription(content::String) | ||
|
|
||
| A type to hold the content of a dataset description. | ||
|
|
||
| The main purpose of its existence is to provide a way to display the content | ||
| differently in HTML and Markdown contexts. | ||
|
|
||
| Obtained through [`RDatasets.description`](@ref). | ||
| """ | ||
| struct RDatasetDescription | ||
| content::String | ||
| end | ||
|
|
||
| function Base.show(io::IO, mime::MIME"text/plain", d::RDatasetDescription) | ||
| s = description_to_markdown(d.content) | ||
| # Here, we show a Markdown.jl object, which the REPL can render correctly | ||
| # as markdown, as it does in help-mode. | ||
| show(io, mime, Markdown.parse(s)) | ||
| end | ||
| function Base.show(io::IO, mime::MIME"text/markdown", d::RDatasetDescription) | ||
| s = description_to_markdown(d.content) | ||
| # Here, we return a Markdown string directly. This is useful for e.g. documentation, | ||
| # where we want to render the markdown as HTML. | ||
| show(io, mime, s) | ||
| end | ||
| # This returns raw HTML documentation. | ||
| function Base.show(io::IO, mime::MIME"text/html", d::RDatasetDescription) | ||
| show(io, mime, Docs.HTML(d.content)) | ||
| end | ||
|
|
||
|
|
||
| """ | ||
| description_to_markdown(string::String) | ||
|
|
||
| Converts an HTML string to markdown. This function is written specifically | ||
| for HTML descriptions in RDatasets.jl, and so is a bit opinionated on what to | ||
| replace, etc. | ||
|
|
||
| It replaces all known HTML tags using regex, and then removes all other HTML tags. | ||
|
|
||
| ## Behaviour | ||
|
|
||
| Currently, it handles the following HTML tags: | ||
| - `<h1>`, `<h2>`, `<h3>`, `<h4>`, `<h5>`, `<h6>` -> `#`, `##`, `###`, `####`, `#####`, `######` | ||
| - `<title>` -> `#` | ||
| - `<code>` -> `` `code` `` | ||
| - `<pre>` -> "```R\\npre\\n```" | ||
| - `<EM>` -> `*EM*` | ||
| - `<B>` -> `**B**` | ||
| - `–` -> `-` | ||
|
|
||
| ## TODOs | ||
|
|
||
| - Tables | ||
| - Links | ||
| - Images | ||
| """ | ||
| function description_to_markdown(string) | ||
| html_header_regex = r"<h(?'hnum'\d)>(?'content'[^<]+)<\/h\g'hnum'>" | ||
| function regexmatch2md(matched_string) | ||
| m = match(html_header_regex, matched_string) | ||
| if isnothing(m.captures[1]) || isnothing(m.captures[2]) | ||
| return matched_string | ||
| end | ||
|
|
||
| hnum = parse(Int, m[:hnum]) | ||
| content = m[:content] | ||
|
|
||
| return join(("\n", "#"^hnum, " ", content, "\n\n")) | ||
| end | ||
| error("Unable to locate dataset file $rdaname or $csvname") | ||
| title_matcher_regex = r"<title>(?'content'[^<]+)<\/title>" | ||
| code_matcher_regex = r"<code>(?'content'[^<]+)<\/code>" | ||
| pre_matcher_regex = r"<pre>(?'content'[^<]+)<\/pre>" | ||
| emph_matcher_regex = r"<(?i)EM(?-i)>(?'content'[^<]+)<\/(?i)EM(?-i)>" | ||
| b_matcher_regex = r"<(?i)B(?-i)>(?'content'[^<]+)<\/(?i)B(?-i)>" | ||
| new_string = replace( | ||
| string, | ||
| html_header_regex => regexmatch2md, | ||
| title_matcher_regex => titlestr -> "# " * match(title_matcher_regex, titlestr)[:content], | ||
| code_matcher_regex => codestr -> "`" * match(code_matcher_regex, codestr)[:content] * "`", | ||
| pre_matcher_regex => prestr -> "\n```R\n" * match(pre_matcher_regex, prestr)[:content] * "\n```\n", | ||
| emph_matcher_regex => emphstr -> "*" * match(emph_matcher_regex, emphstr)[:content] * "*", | ||
| b_matcher_regex => bstr -> "**" * match(b_matcher_regex, bstr)[:content] * "**", | ||
| "–" => "-", | ||
| ) | ||
| nohtml = replace(new_string, Regex("<[^>]*>") => "") | ||
| return replace(nohtml, Regex("\n\n+") => "\n\n") | ||
| end | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.