1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
/*
* render/text/elements.rs
*
* ftml - Library to parse Wikidot text
* Copyright (C) 2019-2023 Wikijump Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//! Module that implements text rendering for `Element` and its children.
//!
//! The philosophy of this renderer is essentially to output what the HTML
//! renderer would, but with all tags, styling, etc stripped.
//!
//! Only pure, unformatted text should remain. Whitespace formatting
//! (such as indenting each line of a blockquote) should not occur.
//! Any formatting present must be directly justifiable.
use super::TextContext;
use crate::tree::{ContainerType, DefinitionListItem, Element, ListItem, Tab};
pub fn render_elements(ctx: &mut TextContext, elements: &[Element]) {
info!("Rendering elements (length {})", elements.len());
for element in elements {
render_element(ctx, element);
}
}
pub fn render_element(ctx: &mut TextContext, element: &Element) {
info!("Rendering element {}", element.name());
match element {
Element::Container(container) => {
let mut invisible = false;
let add_newlines = match container.ctype() {
// Don't render this at all.
ContainerType::Hidden => return,
// Render it, but invisibly.
// Requires setting a special mode in the context.
ContainerType::Invisible => {
ctx.enable_invisible();
invisible = true;
false
}
// If container is "terminating" (e.g. blockquote, p), then add newlines.
// Also, determine if we add a prefix.
ContainerType::Div
| ContainerType::Paragraph
| ContainerType::Blockquote
| ContainerType::Header(_) => true,
// Wrap any ruby text with parentheses
ContainerType::RubyText => {
ctx.push('(');
false
}
// Inline or miscellaneous container.
_ => false,
};
if add_newlines {
ctx.add_newline();
}
// Render internal elements
render_elements(ctx, container.elements());
// Wrap any ruby text with parentheses
if container.ctype() == ContainerType::RubyText {
ctx.push(')');
}
if add_newlines {
ctx.add_newline();
}
if invisible {
ctx.disable_invisible();
}
}
Element::Module(_) => {
// We don't want to render modules at all
}
Element::Text(text) | Element::Raw(text) | Element::Email(text) => {
ctx.push_str(text);
}
Element::Variable(name) => {
let value = match ctx.variables().get(name) {
Some(value) => str!(value),
None => format!("{{${name}}}"),
};
info!(
"Rendering variable (name '{}', value {})",
name.as_ref(),
value,
);
ctx.push_str(&value);
}
Element::Table(table) => {
if !ctx.ends_with_newline() {
ctx.add_newline();
}
for row in &table.rows {
for cell in &row.cells {
render_elements(ctx, &cell.elements);
}
ctx.add_newline();
}
ctx.add_newline();
}
Element::TabView(tabs) => {
for Tab { label, elements } in tabs {
// Add tab name
ctx.push_str(label);
ctx.add_newline();
// Add tab contents
render_elements(ctx, elements);
ctx.add_newline();
}
}
Element::Anchor { elements, .. } => render_elements(ctx, elements),
Element::AnchorName(_) => {
// Anchor names are an invisible addition to the HTML
// to aid navigation. So in text mode, they are ignored.
}
Element::Link { link, label, .. } => {
let site = ctx.info().site.as_ref();
ctx.handle().get_link_label(site, link, label, |label| {
// Only write the label, i.e. the part that's visible
ctx.push_str(label);
});
}
Element::Image { .. } => {
// Text cannot render images, so we don't add anything
}
Element::List { items, .. } => {
if !ctx.ends_with_newline() {
ctx.add_newline();
}
for item in items {
match item {
ListItem::SubList { element } => render_element(ctx, element),
ListItem::Elements { elements, .. } => {
// Don't do anything if it's empty
if elements.is_empty() {
continue;
}
// Render elements for this list item
render_elements(ctx, elements);
ctx.add_newline();
}
}
}
}
Element::DefinitionList(items) => {
for DefinitionListItem {
key_elements,
value_elements,
..
} in items
{
render_elements(ctx, key_elements);
ctx.push(' ');
render_elements(ctx, value_elements);
ctx.add_newline();
}
ctx.add_newline();
}
Element::RadioButton { .. } | Element::CheckBox { .. } => {
// These cannot be rendered in text mode, and so are ignored.
}
Element::Collapsible { elements, .. } => {
// For collapsibles, we simply show the contents.
// No collapsible labels (open or close) are shown.
render_elements(ctx, elements);
}
Element::TableOfContents { .. } => {
// Doesn't make sense to have a textual table of contents, skip
}
Element::Footnote
| Element::FootnoteBlock { .. }
| Element::BibliographyCite { .. }
| Element::BibliographyBlock { .. } => {
// Footnotes and bibliographies cannot be cleanly rendered in text mode,
// so they are skipped.
}
Element::User { name, .. } => ctx.push_str(name),
Element::Date { value, format, .. } => {
// TEMP
if format.is_some() {
warn!("Time format passed, feature currently not supported!");
}
// TODO handle error
match value.format() {
Ok(datetime) => str_write!(ctx, "{}", datetime),
Err(error) => {
error!("Error formatting date into string: {error}");
str_write!(ctx, "<ERROR>");
}
};
}
Element::Color { elements, .. } => render_elements(ctx, elements),
Element::Code { contents, .. } => {
ctx.add_newline();
ctx.push_str(contents);
ctx.add_newline();
}
Element::Math { .. } | Element::MathInline { .. } => {
// No real way to render arbitrary LaTeX, so we skip it.
}
Element::EquationReference(name) => {
str_write!(ctx, "[{name}]");
}
Element::Embed(_) | Element::Html { .. } | Element::Iframe { .. } => {
// Interactive or HTML elements like this don't make sense in
// text mode, so we skip them.
}
Element::Include {
variables,
elements,
..
} => {
info!(
"Rendering include (variables length {}, elements length {})",
variables.len(),
elements.len(),
);
ctx.variables_mut().push_scope(variables);
render_elements(ctx, elements);
ctx.variables_mut().pop_scope();
}
Element::Style(_) | Element::ClearFloat(_) => {
// Style blocks and clear float do not do anything in text mode
}
Element::LineBreak => ctx.add_newline(),
Element::LineBreaks(amount) => {
for _ in 0..amount.get() {
ctx.add_newline();
}
}
Element::HorizontalRule => {
// We could add dashes, but that looks tacky on anything
// that is not a fixed-width font.
//
// So we take the safe option of doing nothing.
}
Element::Partial(_) => panic!("Encountered partial element during parsing"),
}
}