// HTML table rendering for `DataFrame`. The output is a standard
// `` — optional `class` / `` header row,
// a `` of data rows, and (when the row count is capped) a
// `` truncation banner — so it drops straight into a web page or
// a notebook's rich-display slot. Null cells render as an empty
// ``, delegating to `Scalar::to_string` (which maps `Null` to
// the empty string), so a null and a genuine empty-string cell are
// indistinguishable in the output (the renderer carries no null marker).
//
// Like `to_markdown`, this is a pure, dependency-free renderer that
// lives on `DataFrame` rather than in `io` (the v0.2 IO-1 boundary:
// rendering stays in `frame`, only external serialisation lives in
// `io`). Values are read column-major through `column_series()` /
// `to_scalars()`, exactly as `to_markdown` does, so the v0.3 Stage 2
// `Series::storage` change is invisible here.

///|
/// Options for `DataFrame::to_html_with_options`. Fields are read-only
/// outside the package — build them through `HtmlOptions::default()` and
/// chain the `with_*` setters:
///
///   * `max_rows` — cap the number of data rows. `None` (the default)
///     renders every row; `Some(n)` shows the first `n` and appends a
///     `` banner (`... (K more rows)`) for the remainder. A
///     negative `n` clamps to 0 (header + banner, no data rows),
///     matching `to_markdown_with_limit`.
///   * `table_class` — value of the `
`, a `
` attribute. /// `None` (the default) omits the attribute. /// * `caption` — text of a leading `` `... (K more rows)` banner /// for the remainder. A negative `n` clamps to 0. Chainable. pub fn HtmlOptions::with_max_rows(self : HtmlOptions, n : Int) -> HtmlOptions { { ..self, max_rows: Some(n) } } ///| /// Return a copy of these options whose `
` element. `None` (the /// default) omits it. /// * `escape` — when `true` (the default) every emitted string — /// caption, header names, cell values, and the class attribute — has /// `&` / `<` / `>` / `"` / `'` rewritten to their HTML entities, so /// untrusted data can't inject markup. Set `false` (via /// `with_escape`) to pass strings through verbatim for trusted input /// that intentionally carries HTML. pub struct HtmlOptions { max_rows : Int? table_class : String? caption : String? escape : Bool } derive(Eq, Debug) ///| /// The default options: render all rows, no `class`, no ``, and /// HTML-escape every emitted string. Chain `with_max_rows` / /// `with_table_class` / `with_caption` / `with_escape` to override. pub fn HtmlOptions::default() -> HtmlOptions { { max_rows: None, table_class: None, caption: None, escape: true } } ///| /// Return a copy of these options that caps the rendered table at the /// first `n` data rows, appending a `
` carries /// `class=""`. Chainable. pub fn HtmlOptions::with_table_class( self : HtmlOptions, cls : String, ) -> HtmlOptions { { ..self, table_class: Some(cls) } } ///| /// Return a copy of these options that prepends a `
` element /// containing `text`. Chainable. pub fn HtmlOptions::with_caption( self : HtmlOptions, text : String, ) -> HtmlOptions { { ..self, caption: Some(text) } } ///| /// Return a copy of these options with HTML escaping set to `escape`. /// Pass `false` to emit strings verbatim (trusted input that carries /// intentional markup). Chainable. pub fn HtmlOptions::with_escape( self : HtmlOptions, escape : Bool, ) -> HtmlOptions { { ..self, escape, } } ///| /// Render a `DataFrame` as an HTML ``, using the default options /// (all rows, no `class` / `` with one `` with one `` per record (a ``. `&` / `<` / /// `>` / `"` / `'` are escaped to their HTML entities. /// /// Empty frames degrade gracefully: /// - 0 columns → returns the empty string (MoonFrame's own choice for a /// degenerate frame); /// - N columns / 0 rows → header with an empty ``. /// /// Total — pure string assembly over already-validated frame data. pub fn DataFrame::to_html(self : DataFrame) -> String { format_html(self, self.nrows(), HtmlOptions::default()) } ///| /// Render a `DataFrame` as an HTML `
`, HTML-escaped). /// /// The output is a `
` per column followed by a /// `
` per cell, in /// declaration order); a null cell renders as `
` under `options` — an /// optional `class` / `` /// `... (K more rows)` banner, and optional escaping. See `HtmlOptions`. /// /// Total — `options.max_rows` is clamped into `[0, nrows]`, so no input /// can fail. pub fn DataFrame::to_html_with_options( self : DataFrame, options : HtmlOptions, ) -> String { let nrows = self.nrows() let shown = match options.max_rows { None => nrows Some(n) => clamp_take(n, nrows) } format_html(self, shown, options) } // ── internals ────────────────────────────────────────────────────────── ///| /// Render the first `shown` rows of `df` as an HTML table under /// `options`. When `shown < df.nrows()` a `` banner reports the /// hidden remainder. Unlike `to_markdown`'s banner (appended *after* the /// table text), the HTML banner is a `` *inside* `
`, an optional row cap with a `
`, so the /// truncation decision is made here rather than in the public wrappers. /// /// A 0-column frame renders as the empty string (there is no table to /// draw); otherwise the header / `` are always emitted, so a /// 0-row frame is still a useful header + empty ``. fn format_html(df : DataFrame, shown : Int, options : HtmlOptions) -> String { let names = df.columns() let ncols = names.length() if ncols == 0 { return "" } let escape = options.escape // Pre-render the visible cells once (raw strings, the column-major frame read // transposed into per-row cells); each is HTML-escaped on write below. let cells = table_cell_matrix(df, shown) let buf = StringBuilder::new() buf <+ "\n" if options.caption is Some(text) { buf <+ "\n" } // Header. buf <+ "\n" for name in names { buf <+ "" } buf <+ "\n\n" // Body — one `` per visible record. A null cell renders empty // (`Scalar::to_string` maps `Null` to ""). buf <+ "\n" for r in 0.." for c in 0..\{html_escape(cells[r][c], escape)}" } buf <+ "\n" } buf <+ "\n" // Truncation banner, spanning every column. The banner text is fixed // (digits + ASCII), so it needs no escaping. let nrows = df.nrows() if shown < nrows { buf <+ "\n\n\n" } buf <+ "
\{html_escape(text, escape)}
\{html_escape(name, escape)}
\{more_rows_banner(nrows - shown)}
\n" buf.to_string() } ///| /// HTML-escape `s` when `escape` is `true`: `&` → `&`, `<` → `<`, /// `>` → `>`, `"` → `"`, `'` → `'`. Processing /// codepoint-by-codepoint means the `&` rule can't double-escape the `&` it /// just introduced. When `escape` is `false`, `s` is returned verbatim /// (trusted input). The full conventional set (including `'`) is escaped, so /// a cell or caption is safe in both `"`-quoted and `'`-quoted attribute /// contexts, not only the `>`-delimited text the renderer emits today. fn html_escape(s : String, escape : Bool) -> String { if !escape { return s } let buf = StringBuilder::new() for ch in s.iter() { match ch { '&' => buf.write_string("&") '<' => buf.write_string("<") '>' => buf.write_string(">") '"' => buf.write_string(""") '\'' => buf.write_string("'") _ => buf.write_char(ch) } } buf.to_string() }