Text

HTML to text

HTML as readable plain text for the text part of an email: headings, lists and tables laid out, links listed at the end, and one-line previews.

Version 1.0.0 · 3 functions · 1 crate

#email#html#text#export

Functions

What use html_text.qwer gives a program, with the types Quillo sees. To take only some, or under other names: use html_to_text, html_to_bare_text from html_text (Quillo 0.6).

html_to_text(html: str, width: int) → stror an error

html as plain text wrapped at width characters (0: no wrapping): # Headings, * lists, tables in columns, bold and [1]-style link marks, with the links listed at the end, as the text part of an email has them.

html_to_bare_text(html: str, width: int) → stror an error

html as plain text with no marks at all: no #, * or link list, only the words, laid out in lines, for search indexes and previews.

html_preview(html: str, chars: int) → stror an error

The first chars characters of a page's text on one line, for a list of messages or a meta description; "…" at the end when cut.

Get it

$ quillo install html_text               # html_text.qwer into this folder$ quillo install html_text --example     # and its example, html_text.qwe

Or with curl:

$ curl -O https://quillolang.org/plugins/files/html_text.qwer$ curl -O https://quillolang.org/plugins/files/html_text.qwe

Then, in your program, all of it or only what you call:

main.qwe
use html_text.qweruse html_to_text, html_to_bare_text from html_text      // Quillo 0.6: only these

It uses this crate, which cargo fetches the first time the plugin is built:

[dependencies]
html2text = "0.17"

Example

html_text.qwe
// The plain-text part of an HTML email, made from the HTML, and a one-line preview.////   quillo run html_text.qwe      then open http://localhost:8080/email.txtuse "html_text.qwer"let email = "<h1>Your order has shipped</h1><p>Hi Ada, your order <b>#1042</b> is on its way. <a href=\"https://shop.example.com/orders/1042\">Track it here</a>.</p><table><tr><th>Item</th><th>Qty</th><th>Price</th></tr><tr><td>Notebook</td><td>2</td><td>€9.00</td></tr><tr><td>Fountain pen</td><td>1</td><td>€1,225.00</td></tr></table><ul><li>Arrives Thursday</li><li>Signed for at the door</li></ul><p>Questions? <a href=\"mailto:help@example.com\">Write to us</a>.</p>"route GET /email.txt = respond(200, html_to_text(email, 72)?, { "content-type": "text/plain; charset=utf-8" })route GET /preview = { preview: html_preview(email, 60)?, bare: html_to_bare_text("<p>Hello <b>there</b>, <a href='/x'>friend</a></p>", 0)? }
$ quillo run html_text.qwe$ curl 'localhost:8080/email.txt'$ curl 'localhost:8080/preview'

Source

html_text.qwer
//! HTML as readable plain text, for the text part of an email, a terminal or an SMS: headings, lists and tables laid out, links listed at the end.//! version: 1.0.0//!//! [dependencies]//! html2text = "0.17"use html2text::render::TrivialDecorator;const MAX: usize = 5_000_000;fn check(html: &str) -> Result<(), String> {    if html.len() > MAX { Err("HTML up to 5 MB".into()) } else { Ok(()) }}fn width(w: i64) -> usize {    if w <= 0 { 10_000 } else { w.clamp(20, 10_000) as usize }}/// `html` as plain text wrapped at `width` characters (0: no wrapping): `# Headings`, `* lists`, tables in columns, **bold** and `[1]`-style link marks, with the links listed at the end, as the text part of an email has them.pub fn html_to_text(html: &str, width: i64) -> Result<String, String> {    check(html)?;    html2text::config::plain().link_footnotes(true).string_from_read(html.as_bytes(), self::width(width)).map_err(|e| e.to_string())}/// `html` as plain text with no marks at all: no `#`, `*` or link list, only the words, laid out in lines, for search indexes and previews.pub fn html_to_bare_text(html: &str, width: i64) -> Result<String, String> {    check(html)?;    html2text::config::with_decorator(TrivialDecorator::new()).string_from_read(html.as_bytes(), self::width(width)).map_err(|e| e.to_string())}/// The first `chars` characters of a page's text on one line, for a list of messages or a meta description; "…" at the end when cut.pub fn html_preview(html: &str, chars: i64) -> Result<String, String> {    let t = html_to_bare_text(html, 0)?;    let one = t.split_whitespace().collect::<Vec<_>>().join(" ");    let n = chars.clamp(1, 10_000) as usize;    Ok(if one.chars().count() <= n { one } else { one.chars().take(n - 1).collect::<String>().trim_end().to_string() + "…" })}
Templates templates.qwer · 1.0.0Jinja templates your users can edit for emails, invoices and pages: sandboxed, escaped for HTML, with filters such as money and truncate, and checked before they are saved.templatesemailhtmltext7 views · 8 downloadsText wrapping text_wrap.qwer · 1.0.0Text wrapped and filled at a width for plain-text emails and terminals: paragraphs refilled, hanging indents for lists, and > quoting for replies.emailtextterminalexport7 views · 6 downloadsHyphenation hyphenation.qwer · 1.0.0Words hyphenated by the rules of 80 languages and spellings, patterns built in: a word's syllables, soft hyphens put in text and HTML for narrow columns and e-mails, and text wrapped at a width with words broken where they may be.textlanguagesemailwriting6 views · 7 downloadsReadability readability.qwer · 1.0.0A web page's article without its menus, ads and footer: title, author, date, clean HTML, plain text or Markdown, for read-later apps, link previews and summaries.htmlscrapingtextmarkdownimport8 views · 7 downloadsTypography typography.qwer · 1.0.0Typed text set as a typographer would, by language: curly quotes (“ ”, « », „ “) and apostrophes, en and em dashes, ellipses, ×, ©, French spaces before ; : ! ?, and no widows; in plain text or HTML, code left alone.writingtexthtmllocale7 views · 10 downloadsLink finder link_extract.qwer · 1.0.0URLs and emails found in plain text, with trailing punctuation handled, and the text turned into safe HTML with clickable, nofollow links.textlinkscommentsemailhtml7 views · 6 downloads
Thread
77Useful? Like it, or add to its thread below.

Thread 0 posts

On HTML to text: questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.