diff --git a/crates/nu-cli/src/commands/to_html.rs b/crates/nu-cli/src/commands/to_html.rs index 2ec54d8d4..4581cb6c2 100644 --- a/crates/nu-cli/src/commands/to_html.rs +++ b/crates/nu-cli/src/commands/to_html.rs @@ -15,6 +15,7 @@ pub struct ToHTMLArgs { html_color: bool, no_color: bool, dark: bool, + partial: bool, theme: Option>, } @@ -33,6 +34,11 @@ impl WholeStreamCommand for ToHTML { "indicate your background color is a darker color", Some('d'), ) + .switch( + "partial", + "only output the html for the content itself", + Some('p'), + ) .named( "theme", SyntaxShape::String, @@ -244,6 +250,7 @@ async fn to_html( html_color, no_color, dark, + partial, theme, }, input, @@ -252,39 +259,55 @@ async fn to_html( 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 = "".to_string(); + let mut output_string = String::new(); let mut regex_hm: HashMap = HashMap::new(); let color_hm = get_colors(dark, &theme); // change the color of the page - output_string.push_str(&format!( - // r"", - r"", - color_hm - .get("background") - .expect("Error getting background color"), - color_hm - .get("foreground") - .expect("Error getting foreground color") - )); + if !partial { + output_string.push_str(&format!( + r"", + color_hm + .get("background") + .expect("Error getting background color"), + color_hm + .get("foreground") + .expect("Error getting foreground color") + )); + } else { + output_string.push_str(&format!( + "
", + color_hm + .get("background") + .expect("Error getting background color"), + color_hm + .get("foreground") + .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), + Some(headers) => html_table(input, headers), None => { let value = &input[0]; html_value(value) } }, _ => match headers { - Some(headers) => html_table(input, headers, color_hm), + Some(headers) => html_table(input, headers), None => html_list(input), }, }; output_string.push_str(&inner_value); - output_string.push_str(""); + + if !partial { + output_string.push_str(""); + } else { + output_string.push_str("
") + } // Check to see if we want to remove all color or change ansi to html colors if html_color { @@ -312,25 +335,14 @@ fn html_list(list: Vec) -> String { output_string } -fn html_table(table: Vec, headers: Vec, color_hm: HashMap<&str, String>) -> String { +fn html_table(table: Vec, headers: Vec) -> String { let mut output_string = String::new(); // Add grid lines to html // let mut output_string = ""); - // output_string.push_str(""); - - // change the color of tables - output_string.push_str(&format!( - r"
", - color_hm - .get("background") - .expect("Error getting background color"), - color_hm - .get("foreground") - .expect("Error getting foreground color") - )); + output_string.push_str("
"); output_string.push_str(""); for header in &headers { diff --git a/crates/nu-cli/tests/format_conversions/html.rs b/crates/nu-cli/tests/format_conversions/html.rs index 5983493bd..f21c5ccb8 100644 --- a/crates/nu-cli/tests/format_conversions/html.rs +++ b/crates/nu-cli/tests/format_conversions/html.rs @@ -15,6 +15,21 @@ fn out_html_simple() { ); } +#[test] +fn out_html_partial() { + let actual = nu!( + cwd: ".", pipeline( + r#" + echo 3 | to html -p + "# + )); + + assert_eq!( + actual.out, + "
3
" + ); +} + #[test] fn out_html_table() { let actual = nu!( @@ -26,7 +41,7 @@ fn out_html_table() { assert_eq!( actual.out, - r"
name
darren
" + r"
name
darren
" ); }