mirror of
https://github.com/nushell/nushell.git
synced 2024-11-22 16:33:37 +01:00
Add list output for to html
(#2273)
This commit is contained in:
parent
18a4505b9b
commit
878b748a41
@ -250,6 +250,8 @@ async fn to_html(
|
|||||||
) = args.process(®istry).await?;
|
) = args.process(®istry).await?;
|
||||||
let input: Vec<Value> = input.collect().await;
|
let input: Vec<Value> = input.collect().await;
|
||||||
let headers = nu_protocol::merge_descriptors(&input);
|
let headers = nu_protocol::merge_descriptors(&input);
|
||||||
|
let headers = Some(headers)
|
||||||
|
.filter(|headers| !headers.is_empty() && (headers.len() > 1 || headers[0] != ""));
|
||||||
let mut output_string = "<html>".to_string();
|
let mut output_string = "<html>".to_string();
|
||||||
let mut regex_hm: HashMap<u32, (&str, String)> = HashMap::new();
|
let mut regex_hm: HashMap<u32, (&str, String)> = HashMap::new();
|
||||||
let color_hm = get_colors(dark, &theme);
|
let color_hm = get_colors(dark, &theme);
|
||||||
@ -266,12 +268,57 @@ async fn to_html(
|
|||||||
.expect("Error getting foreground color")
|
.expect("Error getting foreground color")
|
||||||
));
|
));
|
||||||
|
|
||||||
|
let inner_value = match input.len() {
|
||||||
|
0 => String::default(),
|
||||||
|
1 => match headers {
|
||||||
|
Some(headers) => html_table(input, headers, color_hm),
|
||||||
|
None => {
|
||||||
|
let value = &input[0];
|
||||||
|
html_value(value)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ => match headers {
|
||||||
|
Some(headers) => html_table(input, headers, color_hm),
|
||||||
|
None => html_list(input),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
output_string.push_str(&inner_value);
|
||||||
|
output_string.push_str("</body></html>");
|
||||||
|
|
||||||
|
// Check to see if we want to remove all color or change ansi to html colors
|
||||||
|
if html_color {
|
||||||
|
setup_html_color_regexes(&mut regex_hm, dark, &theme);
|
||||||
|
output_string = run_regexes(®ex_hm, &output_string);
|
||||||
|
} else if no_color {
|
||||||
|
setup_no_color_regexes(&mut regex_hm);
|
||||||
|
output_string = run_regexes(®ex_hm, &output_string);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(OutputStream::one(ReturnSuccess::value(
|
||||||
|
UntaggedValue::string(output_string).into_value(name_tag),
|
||||||
|
)))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn html_list(list: Vec<Value>) -> String {
|
||||||
|
let mut output_string = String::new();
|
||||||
|
output_string.push_str("<ol>");
|
||||||
|
for value in list {
|
||||||
|
output_string.push_str("<li>");
|
||||||
|
output_string.push_str(&html_value(&value));
|
||||||
|
output_string.push_str("</li>");
|
||||||
|
}
|
||||||
|
output_string.push_str("</ol>");
|
||||||
|
output_string
|
||||||
|
}
|
||||||
|
|
||||||
|
fn html_table(table: Vec<Value>, headers: Vec<String>, color_hm: HashMap<&str, String>) -> String {
|
||||||
|
let mut output_string = String::new();
|
||||||
// Add grid lines to html
|
// Add grid lines to html
|
||||||
// let mut output_string = "<html><head><style>".to_string();
|
// let mut output_string = "<html><head><style>".to_string();
|
||||||
// output_string.push_str("table, th, td { border: 2px solid black; border-collapse: collapse; padding: 10px; }");
|
// output_string.push_str("table, th, td { border: 2px solid black; border-collapse: collapse; padding: 10px; }");
|
||||||
// output_string.push_str("</style></head><body>");
|
// output_string.push_str("</style></head><body>");
|
||||||
|
|
||||||
if !headers.is_empty() && (headers.len() > 1 || headers[0] != "") {
|
|
||||||
// output_string.push_str("<table>");
|
// output_string.push_str("<table>");
|
||||||
|
|
||||||
// change the color of tables
|
// change the color of tables
|
||||||
@ -286,20 +333,36 @@ async fn to_html(
|
|||||||
));
|
));
|
||||||
|
|
||||||
output_string.push_str("<tr>");
|
output_string.push_str("<tr>");
|
||||||
|
|
||||||
for header in &headers {
|
for header in &headers {
|
||||||
output_string.push_str("<th>");
|
output_string.push_str("<th>");
|
||||||
output_string.push_str(&htmlescape::encode_minimal(&header));
|
output_string.push_str(&htmlescape::encode_minimal(&header));
|
||||||
output_string.push_str("</th>");
|
output_string.push_str("</th>");
|
||||||
}
|
}
|
||||||
output_string.push_str("</tr>");
|
output_string.push_str("</tr>");
|
||||||
|
|
||||||
|
for row in table {
|
||||||
|
if let UntaggedValue::Row(row) = row.value {
|
||||||
|
output_string.push_str("<tr>");
|
||||||
|
for header in &headers {
|
||||||
|
let data = row.get_data(header);
|
||||||
|
output_string.push_str("<td>");
|
||||||
|
output_string.push_str(&html_value(data.borrow()));
|
||||||
|
output_string.push_str("</td>");
|
||||||
|
}
|
||||||
|
output_string.push_str("</tr>");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
output_string.push_str("</table>");
|
||||||
|
|
||||||
|
output_string
|
||||||
}
|
}
|
||||||
|
|
||||||
for row in input {
|
fn html_value(value: &Value) -> String {
|
||||||
match row.value {
|
let mut output_string = String::new();
|
||||||
|
match &value.value {
|
||||||
UntaggedValue::Primitive(Primitive::Binary(b)) => {
|
UntaggedValue::Primitive(Primitive::Binary(b)) => {
|
||||||
// This might be a bit much, but it's fun :)
|
// This might be a bit much, but it's fun :)
|
||||||
match row.tag.anchor {
|
match &value.tag.anchor {
|
||||||
Some(AnchorLocation::Url(f)) | Some(AnchorLocation::File(f)) => {
|
Some(AnchorLocation::Url(f)) | Some(AnchorLocation::File(f)) => {
|
||||||
let extension = f.split('.').last().map(String::from);
|
let extension = f.split('.').last().map(String::from);
|
||||||
match extension {
|
match extension {
|
||||||
@ -333,7 +396,7 @@ async fn to_html(
|
|||||||
}
|
}
|
||||||
UntaggedValue::Primitive(Primitive::String(ref b)) => {
|
UntaggedValue::Primitive(Primitive::String(ref b)) => {
|
||||||
// This might be a bit much, but it's fun :)
|
// This might be a bit much, but it's fun :)
|
||||||
match row.tag.anchor {
|
match &value.tag.anchor {
|
||||||
Some(AnchorLocation::Url(f)) | Some(AnchorLocation::File(f)) => {
|
Some(AnchorLocation::Url(f)) | Some(AnchorLocation::File(f)) => {
|
||||||
let extension = f.split('.').last().map(String::from);
|
let extension = f.split('.').last().map(String::from);
|
||||||
match extension {
|
match extension {
|
||||||
@ -341,7 +404,7 @@ async fn to_html(
|
|||||||
output_string.push_str("<img src=\"data:image/svg+xml;base64,");
|
output_string.push_str("<img src=\"data:image/svg+xml;base64,");
|
||||||
output_string.push_str(&base64::encode(&b.as_bytes()));
|
output_string.push_str(&base64::encode(&b.as_bytes()));
|
||||||
output_string.push_str("\">");
|
output_string.push_str("\">");
|
||||||
continue;
|
return output_string;
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
@ -349,46 +412,16 @@ async fn to_html(
|
|||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
output_string.push_str(
|
output_string.push_str(
|
||||||
&(htmlescape::encode_minimal(&format_leaf(&row.value).plain_string(100_000))
|
&htmlescape::encode_minimal(&format_leaf(&value.value).plain_string(100_000))
|
||||||
.replace("\n", "<br>")),
|
.replace("\n", "<br>"),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
UntaggedValue::Row(row) => {
|
other => output_string.push_str(
|
||||||
output_string.push_str("<tr>");
|
&htmlescape::encode_minimal(&format_leaf(other).plain_string(100_000))
|
||||||
for header in &headers {
|
.replace("\n", "<br>"),
|
||||||
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>");
|
output_string
|
||||||
}
|
|
||||||
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] != "") {
|
|
||||||
output_string.push_str("</table>");
|
|
||||||
}
|
|
||||||
output_string.push_str("</body></html>");
|
|
||||||
|
|
||||||
// Check to see if we want to remove all color or change ansi to html colors
|
|
||||||
if html_color {
|
|
||||||
setup_html_color_regexes(&mut regex_hm, dark, &theme);
|
|
||||||
output_string = run_regexes(®ex_hm, &output_string);
|
|
||||||
} else if no_color {
|
|
||||||
setup_no_color_regexes(&mut regex_hm);
|
|
||||||
output_string = run_regexes(®ex_hm, &output_string);
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(OutputStream::one(ReturnSuccess::value(
|
|
||||||
UntaggedValue::string(output_string).into_value(name_tag),
|
|
||||||
)))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn setup_html_color_regexes(
|
fn setup_html_color_regexes(
|
||||||
|
@ -28,6 +28,7 @@ use serde::{Deserialize, Serialize};
|
|||||||
use std::hash::{Hash, Hasher};
|
use std::hash::{Hash, Hasher};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::time::SystemTime;
|
use std::time::SystemTime;
|
||||||
|
|
||||||
/// The core structured values that flow through a pipeline
|
/// The core structured values that flow through a pipeline
|
||||||
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Serialize, Deserialize)]
|
||||||
pub enum UntaggedValue {
|
pub enum UntaggedValue {
|
||||||
|
Loading…
Reference in New Issue
Block a user