Text diff
What changed between two texts, by line or by word, as a unified diff, and how alike they are.
Version 1.0.0 · 4 functions · 1 crate
Functions
What use diff.qwer gives a program, with the types Quillo sees. To take only some, or under other names: use diff_lines, diff_words from diff (Quillo 0.6).
diff_lines(old: str, new: str) → listThe lines of old and new as changes: [{ op: "equal" | "insert" | "delete", text }], ready to show in green and red.
diff_words(old: str, new: str) → listThe same, word by word (spaces kept as their own changes), for a sentence or a title.
unified_diff(old: str, new: str, old_name: str, new_name: str, context: int) → strA unified diff, as git diff writes one, with context lines around each change.
diff_ratio(old: str, new: str) → floatHow alike two texts are, from 0 to 1, by their characters.
Get it
$ quillo install diff # diff.qwer into this folder$ quillo install diff --example # and its example, diff.qweOr with curl:
$ curl -O https://quillolang.org/plugins/files/diff.qwer$ curl -O https://quillolang.org/plugins/files/diff.qweThen, in your program, all of it or only what you call:
use diff.qweruse diff_lines, diff_words from diff // Quillo 0.6: only theseIt uses this crate, which cargo fetches the first time the plugin is built:
similar = "2"Example
// What changed between two versions of a page.//// quillo run diff.qwe then open http://localhost:8080/changesuse "diff.qwer"let before = "Quillo is a language.\nIt runs on the server.\n"let after = "Quillo is a web language.\nIt runs on the server.\nAnd in the browser.\n"route GET /changes = diff_lines(before, after)route GET /patch = unified_diff(before, after, "page.md", "page.md", 3)route GET /alike = { ratio: diff_ratio(before, after) }$ quillo run diff.qwe$ curl 'localhost:8080/changes'$ curl 'localhost:8080/patch'Source
//! Differences between two texts: line by line, word by word, as a unified diff, and how alike they are.//! version: 1.0.0//!//! [dependencies]//! similar = "2"use serde_json::{json, Value};use similar::{ChangeTag, TextDiff};fn changes<'a, 'b>(d: &TextDiff<'a, 'a, 'b, str>) -> Vec<Value> { d.iter_all_changes().map(|c| { let op = match c.tag() { ChangeTag::Equal => "equal", ChangeTag::Insert => "insert", ChangeTag::Delete => "delete" }; json!({ "op": op, "text": c.value() }) }).collect()}/// The lines of `old` and `new` as changes: `[{ op: "equal" | "insert" | "delete", text }]`, ready to show in green and red.pub fn diff_lines(old: &str, new: &str) -> Vec<Value> { changes(&TextDiff::from_lines(old, new))}/// The same, word by word (spaces kept as their own changes), for a sentence or a title.pub fn diff_words(old: &str, new: &str) -> Vec<Value> { changes(&TextDiff::from_words(old, new))}/// A unified diff, as `git diff` writes one, with `context` lines around each change.pub fn unified_diff(old: &str, new: &str, old_name: &str, new_name: &str, context: i64) -> String { TextDiff::from_lines(old, new).unified_diff().context_radius(context.max(0) as usize).header(old_name, new_name).to_string()}/// How alike two texts are, from 0 to 1, by their characters.pub fn diff_ratio(old: &str, new: &str) -> f64 { TextDiff::from_chars(old, new).ratio() as f64}Related
json_patch.qwer · 1.0.0JSON Patch (RFC 6902) and Merge Patch (RFC 7396): the difference between two values as operations, and patches applied.10 views · 6 downloadsCase case.qwer · 1.0.0Text in snake_case, camelCase, PascalCase, kebab-case, CONSTANT_CASE, Title Case and Sentence case: column names as API keys and labels.7 views · 8 downloadsChinese words chinese_segment.qwer · 1.0.0Chinese text split into words for search indexes and counts, with parts of speech, and written in pinyin with tones, tone numbers or initials for sorting and finding names.7 views · 7 downloadsEmoji emoji.qwer · 1.0.0Emoji for chat and comments: :shortcodes: turned into emoji and back, emoji found with their names, stripped from subjects, and searched by name for a picker.7 views · 8 downloadsFuzzy search fuzzy.qwer · 1.0.0Rank names against what someone typed, typos and all, edit distances, and did-you-mean.7 views · 8 downloadsHTML to text html_text.qwer · 1.0.0HTML 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.7 views · 7 downloadsThread 0 posts
On Text diff: questions, tips, corrections, and code that uses it.
No posts yet. The first one opens the thread.