-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixed styles, new functions to view tables, more modular functions
- Loading branch information
Showing
19 changed files
with
400 additions
and
194 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
name = "HTMLTables" | ||
uuid = "b1afcece-b80e-4563-b90e-36b4cc56d3fa" | ||
authors = ["Ceco E. Maples <[email protected]>"] | ||
version = "0.3.0" | ||
version = "0.3.1" | ||
|
||
[deps] | ||
Cascadia = "54eefc05-d75b-58de-a785-1a3403f0919f" | ||
|
This file contains 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 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 |
---|---|---|
@@ -1,21 +1,33 @@ | ||
""" | ||
HTMLTables | ||
Julia package for reading and writing HTML tables. | ||
Julia package for reading, writing, and viewing HTML tables. | ||
Reading HTML tables: | ||
- `HTMLTables.get` reads an HTML table as a string. | ||
- `HTMLTables.read` extracts data from HTML tables. | ||
Writing HTML tables: | ||
- `HTMLTables.write` uses the Tables.jl interface to write an HTML table. | ||
- `HTMLTables.table` uses the Tables.jl interface to write an HTML table as a string. | ||
- `HTMLTables.write` uses the Tables.jl interface to write an HTML table in a file. | ||
Viewing HTML tables: | ||
- `HTMLTables.display` displays a julia table as an HTML table in julia. | ||
- `HTMLTables.open` opens a julia table as an HTML table in the browser. | ||
""" | ||
module HTMLTables | ||
|
||
using Cascadia, Colors, ColorSchemes, Gumbo, HTTP, Tables | ||
|
||
export read, write | ||
export get, read, table, write, display, open | ||
|
||
include("get.jl") | ||
include("read.jl") | ||
|
||
include("table.jl") | ||
include("write.jl") | ||
|
||
include("display.jl") | ||
include("open.jl") | ||
|
||
end |
This file contains 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,10 @@ | ||
""" | ||
HTMLTables.display(tbl; kwargs...) | ||
Displays a julia table as an HTML table in julia. | ||
""" | ||
function display(tbl; kwargs...) | ||
html_table::String = table(tbl; kwargs...) | ||
|
||
Base.display("image/svg+xml", html_table) | ||
end |
This file contains 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,65 @@ | ||
function isurl(source::String)::Bool | ||
url_pattern::Regex = r"(?i)\b((?:https?|ftp):\/\/[\w-]+(\.[\w-]+)+([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?)\b" | ||
|
||
return Base.occursin(url_pattern, source) | ||
end | ||
|
||
""" | ||
HTMLTables.get(source::String; id::String="", classes::Union{Vector{String},String}="", index::Int=1) | ||
Returns an HTML table a source as a string. | ||
## Arguments | ||
- `source::String`: URL or path to the HTML table. | ||
- `id::String`: The id of the HTML table. | ||
- `classes::Union{Vector{String},String}`: The classes of the HTML table. | ||
- `index::Int`: The index of the HTML table in the HTML document. | ||
""" | ||
function get( | ||
source::String; | ||
id::String="", | ||
classes::Union{Vector{String},String}="", | ||
index::Int=1 | ||
) | ||
if index <= 0 | ||
Base.throw(Base.ArgumentError("Index must be a positive integer")) | ||
end | ||
|
||
if isurl(source) == true | ||
response::HTTP.Response = HTTP.get(source) | ||
html_content = Base.String(response.body) | ||
else | ||
html_content = Base.read(source, String) | ||
end | ||
|
||
html_document::Gumbo.HTMLDocument = Gumbo.parsehtml(html_content) | ||
|
||
selector::String = "" | ||
if Base.isempty(id) | ||
if Base.isempty(classes) | ||
selector *= "table" | ||
elseif !Base.isempty(classes) && Base.isa(classes, String) | ||
selector *= "table.$classes" | ||
elseif !Base.isempty(classes) && Base.isa(classes, Vector{String}) | ||
selector *= "table." * Base.join(classes, ".") | ||
end | ||
elseif !Base.isempty(id) | ||
selector *= "#$id" | ||
end | ||
|
||
tables::Vector{Gumbo.HTMLNode} = Base.eachmatch(Cascadia.Selector(selector), html_document.root) | ||
|
||
number_of_tables::Int = Base.length(tables) | ||
|
||
if number_of_tables == 0 | ||
Base.throw(Base.ArgumentError("No HTML tables found")) | ||
elseif index > number_of_tables | ||
Base.throw(Base.ArgumentError("$number_of_tables table(s) found. Index $index does not exist.")) | ||
end | ||
|
||
table::Gumbo.HTMLNode = tables[index] | ||
|
||
return table | ||
end |
This file contains 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,16 @@ | ||
""" | ||
HTMLTables.open(tbl; kwargs...) | ||
Opens a julia table as an HTML table in the browser. | ||
""" | ||
function open(tbl; kwargs...) | ||
path::String = write(tbl; kwargs...) | ||
|
||
if Base.Sys.iswindows() | ||
Base.run(`start $path`) | ||
elseif Base.Sys.islinux() | ||
Base.run(`xdg-open $path`) | ||
elseif Base.Sys.isapple() | ||
Base.run(`open $path`) | ||
end | ||
end |
This file contains 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
Oops, something went wrong.