mirror of
https://github.com/nushell/nushell
synced 2024-12-25 12:33:17 +00:00
Add simple to-html output and bump version (#1487)
This commit is contained in:
parent
b6363f3ce1
commit
2d078849cb
8 changed files with 145 additions and 4 deletions
9
Cargo.lock
generated
9
Cargo.lock
generated
|
@ -1489,6 +1489,12 @@ version = "0.4.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "023b39be39e3a2da62a94feb433e91e8bcd37676fbc8bea371daf52b7a769a3e"
|
||||
|
||||
[[package]]
|
||||
name = "htmlescape"
|
||||
version = "0.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e9025058dae765dee5070ec375f591e2ba14638c63feff74f13805a72e523163"
|
||||
|
||||
[[package]]
|
||||
name = "http"
|
||||
version = "0.1.21"
|
||||
|
@ -2232,7 +2238,7 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "nu"
|
||||
version = "0.11.0"
|
||||
version = "0.11.1"
|
||||
dependencies = [
|
||||
"clap",
|
||||
"crossterm 0.16.0",
|
||||
|
@ -2312,6 +2318,7 @@ dependencies = [
|
|||
"git2",
|
||||
"glob",
|
||||
"hex 0.4.0",
|
||||
"htmlescape",
|
||||
"ichwh",
|
||||
"indexmap",
|
||||
"itertools 0.9.0",
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
[package]
|
||||
name = "nu"
|
||||
version = "0.11.0"
|
||||
authors = ["Yehuda Katz <wycats@gmail.com>", "Jonathan Turner <jonathan.d.turner@gmail.com>", "Andrés N. Robalino <andres@androbtech.com>"]
|
||||
description = "A shell for the GitHub era"
|
||||
version = "0.11.1"
|
||||
authors = ["The Nu Authors"]
|
||||
description = "A new kind of shell"
|
||||
license = "MIT"
|
||||
edition = "2018"
|
||||
readme = "README.md"
|
||||
|
|
|
@ -44,6 +44,7 @@ getset = "0.1.0"
|
|||
git2 = { version = "0.11.0", default_features = false }
|
||||
glob = "0.3.0"
|
||||
hex = "0.4"
|
||||
htmlescape = "0.3.1"
|
||||
ichwh = "0.3"
|
||||
indexmap = { version = "1.3.2", features = ["serde-1"] }
|
||||
itertools = "0.9.0"
|
||||
|
|
|
@ -313,6 +313,7 @@ pub fn create_default_context(
|
|||
// File format output
|
||||
whole_stream_command(ToBSON),
|
||||
whole_stream_command(ToCSV),
|
||||
whole_stream_command(ToHTML),
|
||||
whole_stream_command(ToJSON),
|
||||
whole_stream_command(ToSQLite),
|
||||
whole_stream_command(ToDB),
|
||||
|
|
|
@ -87,6 +87,7 @@ pub(crate) mod table;
|
|||
pub(crate) mod tags;
|
||||
pub(crate) mod to_bson;
|
||||
pub(crate) mod to_csv;
|
||||
pub(crate) mod to_html;
|
||||
pub(crate) mod to_json;
|
||||
pub(crate) mod to_sqlite;
|
||||
pub(crate) mod to_toml;
|
||||
|
@ -191,6 +192,7 @@ pub(crate) use table::Table;
|
|||
pub(crate) use tags::Tags;
|
||||
pub(crate) use to_bson::ToBSON;
|
||||
pub(crate) use to_csv::ToCSV;
|
||||
pub(crate) use to_html::ToHTML;
|
||||
pub(crate) use to_json::ToJSON;
|
||||
pub(crate) use to_sqlite::ToDB;
|
||||
pub(crate) use to_sqlite::ToSQLite;
|
||||
|
|
101
crates/nu-cli/src/commands/to_html.rs
Normal file
101
crates/nu-cli/src/commands/to_html.rs
Normal file
|
@ -0,0 +1,101 @@
|
|||
use crate::commands::WholeStreamCommand;
|
||||
use crate::data::value::format_leaf;
|
||||
use crate::prelude::*;
|
||||
use futures::StreamExt;
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::{Primitive, ReturnSuccess, Signature, UntaggedValue, Value};
|
||||
use nu_source::AnchorLocation;
|
||||
|
||||
pub struct ToHTML;
|
||||
|
||||
impl WholeStreamCommand for ToHTML {
|
||||
fn name(&self) -> &str {
|
||||
"to-html"
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
Signature::build("to-html")
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"Convert table into simple HTML"
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
args: CommandArgs,
|
||||
registry: &CommandRegistry,
|
||||
) -> Result<OutputStream, ShellError> {
|
||||
to_html(args, registry)
|
||||
}
|
||||
}
|
||||
|
||||
fn to_html(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
|
||||
let args = args.evaluate_once(registry)?;
|
||||
let name_tag = args.name_tag();
|
||||
//let name_span = name_tag.span;
|
||||
let stream = async_stream! {
|
||||
let input: Vec<Value> = args.input.values.collect().await;
|
||||
let headers = nu_protocol::merge_descriptors(&input);
|
||||
let mut output_string = "<html><body>".to_string();
|
||||
|
||||
if !headers.is_empty() && (headers.len() > 1 || headers[0] != "<value>") {
|
||||
output_string.push_str("<table>");
|
||||
|
||||
output_string.push_str("<tr>");
|
||||
for header in &headers {
|
||||
output_string.push_str("<th>");
|
||||
output_string.push_str(&htmlescape::encode_minimal(&header));
|
||||
output_string.push_str("</th>");
|
||||
}
|
||||
output_string.push_str("</tr>");
|
||||
}
|
||||
|
||||
for row in input {
|
||||
match row.value {
|
||||
UntaggedValue::Primitive(Primitive::Binary(b)) => {
|
||||
// This might be a bit much, but it's fun :)
|
||||
match row.tag.anchor {
|
||||
Some(AnchorLocation::Url(f)) |
|
||||
Some(AnchorLocation::File(f)) => {
|
||||
let extension = f.split('.').last().map(String::from);
|
||||
match extension {
|
||||
Some(s) if ["png", "jpg", "bmp", "gif", "tiff"].contains(&s.as_str()) => {
|
||||
output_string.push_str("<img src=\"data:image/");
|
||||
output_string.push_str(&s);
|
||||
output_string.push_str(";base64,");
|
||||
output_string.push_str(&base64::encode(&b));
|
||||
output_string.push_str("\">");
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
UntaggedValue::Row(row) => {
|
||||
output_string.push_str("<tr>");
|
||||
for header in &headers {
|
||||
let data = row.get_data(header);
|
||||
output_string.push_str("<td>");
|
||||
output_string.push_str(&format_leaf(data.borrow()).plain_string(100_000));
|
||||
output_string.push_str("</td>");
|
||||
}
|
||||
output_string.push_str("</tr>");
|
||||
}
|
||||
p => {
|
||||
output_string.push_str(&(htmlescape::encode_minimal(&format_leaf(&p).plain_string(100_000)).replace("\n", "<br>")));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !headers.is_empty() && (headers.len() > 1 || headers[0] != "<value>") {
|
||||
output_string.push_str("</table>");
|
||||
}
|
||||
output_string.push_str("</body></html>");
|
||||
|
||||
yield ReturnSuccess::value(UntaggedValue::string(output_string).into_value(name_tag));
|
||||
};
|
||||
|
||||
Ok(stream.to_output_stream())
|
||||
}
|
28
crates/nu-cli/tests/format_conversions/html.rs
Normal file
28
crates/nu-cli/tests/format_conversions/html.rs
Normal file
|
@ -0,0 +1,28 @@
|
|||
use nu_test_support::{nu, pipeline};
|
||||
|
||||
#[test]
|
||||
fn out_html_simple() {
|
||||
let actual = nu!(
|
||||
cwd: ".", pipeline(
|
||||
r#"
|
||||
echo 3 | to-html
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "<html><body>3</body></html>");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn out_html_table() {
|
||||
let actual = nu!(
|
||||
cwd: ".", pipeline(
|
||||
r#"
|
||||
echo '{"name": "jason"}' | from-json | to-html
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(
|
||||
actual,
|
||||
"<html><body><table><tr><th>name</th></tr><tr><td>jason</td></tr></table></body></html>"
|
||||
);
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
mod bson;
|
||||
mod csv;
|
||||
mod html;
|
||||
mod json;
|
||||
mod ods;
|
||||
mod sqlite;
|
||||
|
|
Loading…
Reference in a new issue