crowbook_text_processing/
lib.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with
3// this file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5//! Provides some utilities functions for escaping text (to HTML or
6//! LaTeX) and formatting it according to typographic rules (smart
7//! quotes, ellipsis, french rules for non-breaking spaces).
8//!
9//! These functions were originally written for
10//! [Crowbook](https://github.com/lise-henry/crowbook), but have
11//! been published on a separate crate and under a less restrictive
12//! license (MPL instead of LGPL) so they can be used in other projects.
13//!
14//! # Usage
15//!
16//! Just add this line in the `dependencies` section of your `Cargo.toml`
17//! file:
18//!
19//! ```toml
20//! [dependencies]
21//! crowbook-text-processing = "0.2"
22//! ```
23//!
24//! # Example
25//!
26//! ```
27//! use crowbook_text_processing::{FrenchFormatter, clean, escape};
28//!
29//! let s = " Some  string with  too much   whitespaces & around 1% \
30//!          characters that might cause trouble to HTML or LaTeX.";
31//! // Remove unnecessary whitespaces (but doesn't trim as it can have meaning)
32//! let new_s = clean::whitespaces(s);
33//! // Escape forHTML
34//! println!("for HTML: {}", escape::html(new_s.clone()));
35//! // Escape for LaTeX
36//! println!("for LaTeX: {}", escape::tex(new_s));
37//!
38//! // Replace quotes with typographic quotation marks
39//! let s = r#"Some "quoted string" and 'another one'."#;
40//! let new_s = clean::quotes(s);
41//! assert_eq!(&new_s, "Some “quoted string” and ‘another one’.");
42//!
43//! // Replace three consecutive dots with ellipsis character
44//! let s = clean::ellipsis("Foo...");
45//! assert_eq!(&s, "Foo…");
46//!
47//! // Format whitespaces according to french typographic rules, using
48//! // the appropriate non-breaking spaces where needed
49//! let s = " Une chaîne en français ! On voudrait un résultat \
50//!          « typographiquement correct ».";
51//! let french = FrenchFormatter::new();
52//! println!("for text: {}", french.format(s));
53//! println!("for LaTeX: {}", escape::tex(french.format_tex(s)));
54//! ```
55//!
56//! # License
57//!
58//! This is free software, published under the [Mozilla Public License,
59//! version 2.0](https://www.mozilla.org/en-US/MPL/2.0/).
60
61#![deny(missing_docs)]
62
63#[macro_use]
64extern crate lazy_static;
65
66#[cfg(test)] #[macro_use]
67extern crate pretty_assertions;
68
69pub mod escape;
70pub mod clean;
71pub mod caps;
72
73mod french;
74mod common;
75
76pub use french::FrenchFormatter;