Debian Codegraph @

src/lib.rs

raw symbols SCIP
1 //! A document in the ROFF format.
2 //!
3 //! [ROFF] is a family of Unix text-formatting languages, implemented
4 //! by the `nroff`, `troff`, and `groff` programs, among others. See
5 //! [groff(7)] for a description of the language. This structure is an
6 //! abstract representation of a document in ROFF format. It is meant
7 //! for writing code to generate ROFF documents, such as manual pages.
8 //!
9 //! # Example
10 //!
11 //! ```
12 //! # use roff::*;
13 //! let doc = Roff::new().text(vec![roman("hello, world")]).render();
14 //! assert!(doc.ends_with("hello, world\n"));
15 //! ```
16 //!
17 //! [ROFF]: https://en.wikipedia.org/wiki/Roff_(software)
18 //! [groff(7)]: https://manpages.debian.org/bullseye/groff/groff.7.en.html
19
20 #![cfg_attr(docsrs, feature(doc_cfg))]
21 #![warn(missing_docs)]
22 #![warn(clippy::print_stderr)]
23 #![warn(clippy::print_stdout)]
24
25 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#;
26 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!;
27
28 /// A ROFF document, consisting of lines.
29 ///
30 /// Lines are either control lines (requests that are built in, or
31 /// invocations of macros), or text lines.
32 ///
33 /// # Example
34 ///
35 /// ```
36 /// # use roff::*;
37 /// let doc = Roff::new()
38 /// .control("TH", ["FOO", "1"])
39 /// .control("SH", ["NAME"])
40 /// .text([roman("foo - do a foo thing")])
41 /// .render();
42 /// assert!(doc.ends_with(".TH FOO 1\n.SH NAME\nfoo \\- do a foo thing\n"));
43 /// ```
44 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:)]
45 pub struct Roffrust-analyzer cargo roff 1.1.1 Roff# {
46 linesrust-analyzer cargo roff 1.1.1 Roff#lines.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Linerust-analyzer cargo roff 1.1.1 Line#>,
47 }
48
49 impl Roffrust-analyzer cargo roff 1.1.1 Roff# {
50 /// Instantiate a `Roff`
51 pub fn newrust-analyzer cargo roff 1.1.1 impl#[Roff]new().() -> Selfrust-analyzer cargo roff 1.1.1 impl#[Roff] {
52 Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default#::defaultrust-analyzer cargo roff 1.1.1 impl#[Roff][Default]default().()
53 }
54
55 /// Append a control line.
56 ///
57 /// The line consist of the name of a built-in command or macro,
58 /// and some number of arguments. Arguments that contain spaces
59 /// will be enclosed with double quotation marks.
60 pub fn controlrust-analyzer cargo roff 1.1.1 impl#[Roff]control().<'a>(
61 &mut self,
62 name: impl Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>,
63 args: impl IntoIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#Item# = &'a str>,
64 ) -> &mut Selfrust-analyzer cargo roff 1.1.1 impl#[Roff] {
65 self.linesrust-analyzer cargo roff 1.1.1 Roff#lines..pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(Linerust-analyzer cargo roff 1.1.1 Line#::controlrust-analyzer cargo roff 1.1.1 impl#[Line]control().(
66 name.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().(),
67 args.into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#into_iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|s| s.to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().()).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(),
68 ));
69 self
70 }
71
72 /// Append a text line.
73 ///
74 /// The line will be rendered in a way that ensures it can't be
75 /// interpreted as a control line. The caller does not need to
76 /// ensure, for example, that the line doesn't start with a
77 /// period ("`.`") or an apostrophe ("`'`").
78 pub fn textrust-analyzer cargo roff 1.1.1 impl#[Roff]text().(&mut self, inlines: impl Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Inlinerust-analyzer cargo roff 1.1.1 Inline#>>) -> &mut Selfrust-analyzer cargo roff 1.1.1 impl#[Roff] {
79 self.linesrust-analyzer cargo roff 1.1.1 Roff#lines..pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(Linerust-analyzer cargo roff 1.1.1 Line#::textrust-analyzer cargo roff 1.1.1 impl#[Line]text().(inlines.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().()));
80 self
81 }
82
83 /// Render as ROFF source text that can be fed to a ROFF implementation.
84 pub fn renderrust-analyzer cargo roff 1.1.1 impl#[Roff]render().(&self) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# {
85 let mut buf = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![];
86 self.to_writerrust-analyzer cargo roff 1.1.1 impl#[Roff]to_writer().(&mut buf).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); // writing to a Vec always works
87 Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::from_utf8rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]from_utf8().(buf)
88 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("output is utf8 if all input is utf8 and our API guarantees that")
89 }
90
91 /// Write to a writer.
92 pub fn to_writerrust-analyzer cargo roff 1.1.1 impl#[Roff]to_writer().(&self, w: &mut dyn Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#> {
93 w.write_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#write_all().(APOSTROPHE_PREABMLErust-analyzer cargo roff 1.1.1 APOSTROPHE_PREABMLE..as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
94 for line in self.linesrust-analyzer cargo roff 1.1.1 Roff#lines..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() {
95 line.renderrust-analyzer cargo roff 1.1.1 impl#[Line]render().(w, Apostrophesrust-analyzer cargo roff 1.1.1 Apostrophes#::Handlerust-analyzer cargo roff 1.1.1 Apostrophes#Handle#)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
96 }
97 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
98 }
99
100 /// Render without handling apostrophes specially.
101 ///
102 /// You probably want [`render`](Roff::render) or
103 /// [`to_writer`](Roff::to_writer) instead of this method.
104 ///
105 /// Without special handling, apostrophes get typeset as right
106 /// single quotes, including in words like "don't". In most
107 /// situations, such as in manual pages, that's unwanted. The
108 /// other methods handle apostrophes specially to prevent it, but
109 /// for completeness, and for testing, this method is provided to
110 /// avoid it.
111 pub fn to_roffrust-analyzer cargo roff 1.1.1 impl#[Roff]to_roff().(&self) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# {
112 let mut buf = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![];
113 for line in self.linesrust-analyzer cargo roff 1.1.1 Roff#lines..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() {
114 // Writing to a Vec always works, so we discard any error.
115 line.renderrust-analyzer cargo roff 1.1.1 impl#[Line]render().(&mut buf, Apostrophesrust-analyzer cargo roff 1.1.1 Apostrophes#::DontHandlerust-analyzer cargo roff 1.1.1 Apostrophes#DontHandle#).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
116 }
117 Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::from_utf8rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]from_utf8().(buf)
118 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("output is utf8 if all input is utf8 and our API guarantees that")
119 }
120 }
121
122 impl<I: Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Inlinerust-analyzer cargo roff 1.1.1 Inline#>> Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<I> for Roffrust-analyzer cargo roff 1.1.1 Roff# {
123 fn fromrust-analyzer cargo roff 1.1.1 impl#[Roff][`From<I>`]from().(other: I) -> Selfrust-analyzer cargo roff 1.1.1 impl#[Roff][`From<I>`] {
124 let mut r = Roffrust-analyzer cargo roff 1.1.1 Roff#::newrust-analyzer cargo roff 1.1.1 impl#[Roff]new().();
125 r.textrust-analyzer cargo roff 1.1.1 impl#[Roff]text().([other.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().()]);
126 r
127 }
128 }
129
130 impl<R: Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Roffrust-analyzer cargo roff 1.1.1 Roff#>> FromIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/FromIterator#<R> for Roffrust-analyzer cargo roff 1.1.1 Roff# {
131 fn from_iterrust-analyzer cargo roff 1.1.1 impl#[Roff][`FromIterator<R>`]from_iter().<I: IntoIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#Item# = R>>(iter: I) -> Selfrust-analyzer cargo roff 1.1.1 impl#[Roff][`FromIterator<R>`] {
132 let mut r = Roffrust-analyzer cargo roff 1.1.1 Roff#::newrust-analyzer cargo roff 1.1.1 impl#[Roff]new().();
133 for i in iter {
134 r.linesrust-analyzer cargo roff 1.1.1 Roff#lines..extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<T>`]extend().(i.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().().linesrust-analyzer cargo roff 1.1.1 Roff#lines.);
135 }
136 r
137 }
138 }
139
140 impl<R: Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Roffrust-analyzer cargo roff 1.1.1 Roff#>> Extendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/Extend#<R> for Roffrust-analyzer cargo roff 1.1.1 Roff# {
141 fn extendrust-analyzer cargo roff 1.1.1 impl#[Roff][`Extend<R>`]extend().<T: IntoIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#Item# = R>>(&mut self, iter: T) {
142 for i in iter {
143 self.linesrust-analyzer cargo roff 1.1.1 Roff#lines..extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<T>`]extend().(i.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().().linesrust-analyzer cargo roff 1.1.1 Roff#lines.);
144 }
145 }
146 }
147
148 /// A part of a text line.
149 ///
150 /// Text will be escaped for ROFF. No inline escape sequences will be
151 /// passed to ROFF. The text may contain newlines, but leading periods
152 /// will be escaped so that they won't be interpreted by ROFF as
153 /// control lines.
154 ///
155 /// Note that the strings stored in the variants are stored as they're
156 /// received from the API user. The `Line::render` function handles
157 /// escaping etc.
158 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)]
159 pub enum Inlinerust-analyzer cargo roff 1.1.1 Inline# {
160 /// Text in the "roman" font, which is the normal font if nothing
161 /// else is specified.
162 Romanrust-analyzer cargo roff 1.1.1 Inline#Roman#(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#),
163
164 /// Text in the italic (slanted) font.
165 Italicrust-analyzer cargo roff 1.1.1 Inline#Italic#(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#),
166
167 /// Text in a bold face font.
168 Boldrust-analyzer cargo roff 1.1.1 Inline#Bold#(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#),
169
170 /// A hard line break. This is an inline element so it's easy to
171 /// insert a line break in a paragraph.
172 LineBreakrust-analyzer cargo roff 1.1.1 Inline#LineBreak#,
173 }
174
175 /// Turn a string slice into inline text in the roman font.
176 ///
177 /// This is equivalent to the [roman] function, but may be more
178 /// convenient to use.
179 impl<S: Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>> Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<S> for Inlinerust-analyzer cargo roff 1.1.1 Inline# {
180 fn fromrust-analyzer cargo roff 1.1.1 impl#[Inline][`From<S>`]from().(s: S) -> Selfrust-analyzer cargo roff 1.1.1 impl#[Inline][`From<S>`] {
181 romanrust-analyzer cargo roff 1.1.1 roman().(s)
182 }
183 }
184
185 /// Return some inline text in the "roman" font.
186 ///
187 /// The roman font is the normal font, if no other font is chosen.
188 pub fn romanrust-analyzer cargo roff 1.1.1 roman().(input: impl Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>) -> Inlinerust-analyzer cargo roff 1.1.1 Inline# {
189 Inlinerust-analyzer cargo roff 1.1.1 Inline#::Romanrust-analyzer cargo roff 1.1.1 Inline#Roman#(input.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().())
190 }
191
192 /// Return some inline text in the bold font.
193 pub fn boldrust-analyzer cargo roff 1.1.1 bold().(input: impl Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>) -> Inlinerust-analyzer cargo roff 1.1.1 Inline# {
194 Inlinerust-analyzer cargo roff 1.1.1 Inline#::Boldrust-analyzer cargo roff 1.1.1 Inline#Bold#(input.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().())
195 }
196
197 /// Return some inline text in the italic font.
198 pub fn italicrust-analyzer cargo roff 1.1.1 italic().(input: impl Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>) -> Inlinerust-analyzer cargo roff 1.1.1 Inline# {
199 Inlinerust-analyzer cargo roff 1.1.1 Inline#::Italicrust-analyzer cargo roff 1.1.1 Inline#Italic#(input.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().())
200 }
201
202 /// Return an inline element for a hard line break.
203 pub fn line_breakrust-analyzer cargo roff 1.1.1 line_break().() -> Inlinerust-analyzer cargo roff 1.1.1 Inline# {
204 Inlinerust-analyzer cargo roff 1.1.1 Inline#::LineBreakrust-analyzer cargo roff 1.1.1 Inline#LineBreak#
205 }
206
207 /// A line in a ROFF document.
208 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)]
209 pub(craterust-analyzer cargo roff 1.1.1 crate/) enum Linerust-analyzer cargo roff 1.1.1 Line# {
210 /// A control line.
211 Controlrust-analyzer cargo roff 1.1.1 Line#Control# {
212 /// Name of control request or macro being invoked.
213 namerust-analyzer cargo roff 1.1.1 Line#Control#name.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#,
214
215 /// Arguments on control line.
216 argsrust-analyzer cargo roff 1.1.1 Line#Control#args.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>,
217 },
218
219 /// A text line.
220 Textrust-analyzer cargo roff 1.1.1 Line#Text#(Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Inlinerust-analyzer cargo roff 1.1.1 Inline#>),
221 }
222
223 impl Linerust-analyzer cargo roff 1.1.1 Line# {
224 /// Append a control line.
225 pub(craterust-analyzer cargo roff 1.1.1 crate/) fn controlrust-analyzer cargo roff 1.1.1 impl#[Line]control().(name: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, args: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>) -> Selfrust-analyzer cargo roff 1.1.1 impl#[Line] {
226 Selfrust-analyzer cargo roff 1.1.1 impl#[Line]::Controlrust-analyzer cargo roff 1.1.1 Line#Control# { namerust-analyzer cargo roff 1.1.1 Line#Control#name., argsrust-analyzer cargo roff 1.1.1 Line#Control#args. }
227 }
228
229 /// Append a text line, consisting of inline elements.
230 pub(craterust-analyzer cargo roff 1.1.1 crate/) fn textrust-analyzer cargo roff 1.1.1 impl#[Line]text().(parts: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Inlinerust-analyzer cargo roff 1.1.1 Inline#>) -> Selfrust-analyzer cargo roff 1.1.1 impl#[Line] {
231 Selfrust-analyzer cargo roff 1.1.1 impl#[Line]::Textrust-analyzer cargo roff 1.1.1 Line#Text#(parts)
232 }
233
234 /// Generate a ROFF line.
235 ///
236 /// All the ROFF code generation and special handling happens here.
237 fn renderrust-analyzer cargo roff 1.1.1 impl#[Line]render().(
238 &self,
239 out: &mut dyn Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#,
240 handle_apostrophes: Apostrophesrust-analyzer cargo roff 1.1.1 Apostrophes#,
241 ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#> {
242 match self {
243 Selfrust-analyzer cargo roff 1.1.1 impl#[Line]::Controlrust-analyzer cargo roff 1.1.1 Line#Control# { namerust-analyzer cargo roff 1.1.1 Line#Control#name., argsrust-analyzer cargo roff 1.1.1 Line#Control#args. } => {
244 writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(out, ".{name}")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
245 for arg in args {
246 writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(out, " {}", &escape_spacesrust-analyzer cargo roff 1.1.1 escape_spaces().(arg))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
247 }
248 }
249 Selfrust-analyzer cargo roff 1.1.1 impl#[Line]::Textrust-analyzer cargo roff 1.1.1 Line#Text#(inlines) => {
250 let mut at_line_start = true;
251 for inline in inlines.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() {
252 // We need to handle line breaking specially: it
253 // introduces a control line to the ROFF, and the
254 // leading period of that mustn't be escaped.
255 match inline {
256 Inlinerust-analyzer cargo roff 1.1.1 Inline#::LineBreakrust-analyzer cargo roff 1.1.1 Inline#LineBreak# => {
257 if at_line_start {
258 writelnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/writeln!!(out, ".br")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
259 } else {
260 writelnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/writeln!!(out, "\n.br")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
261 }
262 }
263 Inlinerust-analyzer cargo roff 1.1.1 Inline#::Romanrust-analyzer cargo roff 1.1.1 Inline#Roman#(text) | Inlinerust-analyzer cargo roff 1.1.1 Inline#::Italicrust-analyzer cargo roff 1.1.1 Inline#Italic#(text) | Inlinerust-analyzer cargo roff 1.1.1 Inline#::Boldrust-analyzer cargo roff 1.1.1 Inline#Bold#(text) => {
264 let mut text = escape_inlinerust-analyzer cargo roff 1.1.1 escape_inline().(text);
265 if handle_apostrophes rust-analyzer cargo roff 1.1.1 impl#[Apostrophes][`PartialEq<Self>`]eq().==rust-analyzer cargo roff 1.1.1 impl#[Apostrophes][`PartialEq<Self>`]eq(). rust-analyzer cargo roff 1.1.1 impl#[Apostrophes][`PartialEq<Self>`]eq().Apostrophesrust-analyzer cargo roff 1.1.1 Apostrophes#::Handlerust-analyzer cargo roff 1.1.1 Apostrophes#Handle# {
266 text = escape_apostrophesrust-analyzer cargo roff 1.1.1 escape_apostrophes().(&text);
267 };
268 let text = escape_leading_ccrust-analyzer cargo roff 1.1.1 escape_leading_cc().(&text);
269 if let Inlinerust-analyzer cargo roff 1.1.1 Inline#::Boldrust-analyzer cargo roff 1.1.1 Inline#Bold#(_) = inline {
270 writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(out, r"\fB{text}\fR")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
271 } else if let Inlinerust-analyzer cargo roff 1.1.1 Inline#::Italicrust-analyzer cargo roff 1.1.1 Inline#Italic#(_) = inline {
272 writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(out, r"\fI{text}\fR")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
273 } else {
274 if at_line_start && starts_with_ccrust-analyzer cargo roff 1.1.1 starts_with_cc().(&text) {
275 // Line would start with a period, so we
276 // insert a non-printable, zero-width glyph to
277 // prevent it from being interpreted as such.
278 // We only do that when it's needed, though,
279 // to avoid making the output ugly.
280 //
281 // Note that this isn't handled by
282 // escape_leading_cc, as it
283 // doesn't know when an inline
284 // element is at the start of a
285 // line.
286 writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(out, r"\&").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
287 }
288 writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(out, "{text}")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
289 }
290 }
291 }
292 at_line_start = false;
293 }
294 }
295 };
296 writelnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/writeln!!(out)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
297 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
298 }
299 }
300
301 /// Does line start with a control character?
302 fn starts_with_ccrust-analyzer cargo roff 1.1.1 starts_with_cc().(line: &str) -> bool {
303 line.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().('.') || line.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().('\'')
304 }
305
306 /// This quotes strings with spaces. This doesn't handle strings with
307 /// quotes in any way: there doesn't seem to a way to escape them.
308 fn escape_spacesrust-analyzer cargo roff 1.1.1 escape_spaces().(w: &str) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# {
309 if w.containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]contains().(' ') {
310 formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("\"{w}\"")
311 } else {
312 w.to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().()
313 }
314 }
315
316 /// Prevent leading periods or apostrophes on lines to be interpreted
317 /// as control lines. Note that this needs to be done for apostrophes
318 /// whether they need special handling for typesetting or not: a
319 /// leading apostrophe on a line indicates a control line.
320 fn escape_leading_ccrust-analyzer cargo roff 1.1.1 escape_leading_cc().(s: &str) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# {
321 s.replacerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]replace().("\n.", "\n\\&.").replacerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]replace().("\n'", "\n\\&'")
322 }
323
324 /// Escape anything that may be interpreted by the roff processor in a
325 /// text line: dashes and backslashes are escaped with a backslash.
326 /// Apostrophes are not handled.
327 fn escape_inlinerust-analyzer cargo roff 1.1.1 escape_inline().(text: &str) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# {
328 text.replacerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]replace().('\\', r"\\").replacerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]replace().('-', r"\-")
329 }
330
331 /// Handle apostrophes.
332 fn escape_apostrophesrust-analyzer cargo roff 1.1.1 escape_apostrophes().(text: &str) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# {
333 text.replacerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]replace().('\'', APOSTROPHErust-analyzer cargo roff 1.1.1 APOSTROPHE.)
334 }
335
336 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:)]
337 enum Apostrophesrust-analyzer cargo roff 1.1.1 Apostrophes# {
338 Handlerust-analyzer cargo roff 1.1.1 Apostrophes#Handle#,
339 DontHandlerust-analyzer cargo roff 1.1.1 Apostrophes#DontHandle#,
340 }
341
342 /// Use the apostrophe string variable.
343 const APOSTROPHErust-analyzer cargo roff 1.1.1 APOSTROPHE.: &str = r"\*(Aq";
344
345 /// A preamble added to the start of rendered output.
346 ///
347 /// This defines a string variable that contains an apostrophe. For
348 /// historical reasons, there seems to be no other portable way to
349 /// represent apostrophes across various implementations of the ROFF
350 /// language. In implementations that produce output like `PostScript`
351 /// or PDF, an apostrophe gets typeset as a right single quote, which
352 /// looks different from an apostrophe. For terminal output ("ASCII"),
353 /// such as when using nroff, an apostrophe looks indistinguishable
354 /// from a right single quote. For manual pages, and similar content,
355 /// an apostrophe is more generally desired than the right single
356 /// quote, so we convert all apostrophe characters in input text into
357 /// a use of the string variable defined in the preamble.
358 ///
359 /// The special handling of apostrophes is avoided in the
360 /// [`to_roff`](Roff::to_roff) method, but it's used in the
361 /// [`render`](Roff::render) and [`to_writer`](Roff::to_writer)
362 /// methods.
363 ///
364 /// See: <https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=507673#65>
365 const APOSTROPHE_PREABMLErust-analyzer cargo roff 1.1.1 APOSTROPHE_PREABMLE.: &str = r#".ie \n(.g .ds Aq \(aq
366 .el .ds Aq '
367 "#;
368
369 #[cfg(test)]
370 mod testrust-analyzer cargo roff 1.1.1 test/ {
371 use superrust-analyzer cargo roff 1.1.1 crate/::*;
372
373 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
374 fn escape_dashrust-analyzer cargo roff 1.1.1 test/escape_dash().() {
375 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(r"\-", escape_inlinerust-analyzer cargo roff 1.1.1 escape_inline().("-"));
376 }
377
378 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
379 fn escape_backslashrust-analyzer cargo roff 1.1.1 test/escape_backslash().() {
380 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(r"\\x", escape_inlinerust-analyzer cargo roff 1.1.1 escape_inline().(r"\x"));
381 }
382
383 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
384 fn escape_backslash_and_dashrust-analyzer cargo roff 1.1.1 test/escape_backslash_and_dash().() {
385 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(r"\\\-", escape_inlinerust-analyzer cargo roff 1.1.1 escape_inline().(r"\-"));
386 }
387
388 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
389 fn escapes_leading_control_charsrust-analyzer cargo roff 1.1.1 test/escapes_leading_control_chars().() {
390 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!("foo\n\\&.bar\n\\&'yo", escape_leading_ccrust-analyzer cargo roff 1.1.1 escape_leading_cc().("foo\n.bar\n'yo"));
391 }
392
393 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
394 fn escape_plainrust-analyzer cargo roff 1.1.1 test/escape_plain().() {
395 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!("abc", escape_inlinerust-analyzer cargo roff 1.1.1 escape_inline().("abc"));
396 }
397
398 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
399 fn render_romanrust-analyzer cargo roff 1.1.1 test/render_roman().() {
400 let text = Roffrust-analyzer cargo roff 1.1.1 Roff#::newrust-analyzer cargo roff 1.1.1 impl#[Roff]new().().textrust-analyzer cargo roff 1.1.1 impl#[Roff]text().([romanrust-analyzer cargo roff 1.1.1 roman().("foo")]).to_roffrust-analyzer cargo roff 1.1.1 impl#[Roff]to_roff().();
401 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(text, "foo\n");
402 }
403
404 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
405 fn render_dashrust-analyzer cargo roff 1.1.1 test/render_dash().() {
406 let text = Roffrust-analyzer cargo roff 1.1.1 Roff#::newrust-analyzer cargo roff 1.1.1 impl#[Roff]new().().textrust-analyzer cargo roff 1.1.1 impl#[Roff]text().([romanrust-analyzer cargo roff 1.1.1 roman().("foo-bar")]).to_roffrust-analyzer cargo roff 1.1.1 impl#[Roff]to_roff().();
407 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(text, "foo\\-bar\n");
408 }
409
410 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
411 fn render_italicrust-analyzer cargo roff 1.1.1 test/render_italic().() {
412 let text = Roffrust-analyzer cargo roff 1.1.1 Roff#::newrust-analyzer cargo roff 1.1.1 impl#[Roff]new().().textrust-analyzer cargo roff 1.1.1 impl#[Roff]text().([italicrust-analyzer cargo roff 1.1.1 italic().("foo")]).to_roffrust-analyzer cargo roff 1.1.1 impl#[Roff]to_roff().();
413 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(text, "\\fIfoo\\fR\n");
414 }
415
416 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
417 fn render_boldrust-analyzer cargo roff 1.1.1 test/render_bold().() {
418 let text = Roffrust-analyzer cargo roff 1.1.1 Roff#::newrust-analyzer cargo roff 1.1.1 impl#[Roff]new().().textrust-analyzer cargo roff 1.1.1 impl#[Roff]text().([boldrust-analyzer cargo roff 1.1.1 bold().("foo")]).to_roffrust-analyzer cargo roff 1.1.1 impl#[Roff]to_roff().();
419 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(text, "\\fBfoo\\fR\n");
420 }
421
422 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
423 fn render_textrust-analyzer cargo roff 1.1.1 test/render_text().() {
424 let text = Roffrust-analyzer cargo roff 1.1.1 Roff#::newrust-analyzer cargo roff 1.1.1 impl#[Roff]new().().textrust-analyzer cargo roff 1.1.1 impl#[Roff]text().([romanrust-analyzer cargo roff 1.1.1 roman().("roman")]).to_roffrust-analyzer cargo roff 1.1.1 impl#[Roff]to_roff().();
425 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(text, "roman\n");
426 }
427
428 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
429 fn render_text_with_leading_periodrust-analyzer cargo roff 1.1.1 test/render_text_with_leading_period().() {
430 let text = Roffrust-analyzer cargo roff 1.1.1 Roff#::newrust-analyzer cargo roff 1.1.1 impl#[Roff]new().().textrust-analyzer cargo roff 1.1.1 impl#[Roff]text().([romanrust-analyzer cargo roff 1.1.1 roman().(".roman")]).to_roffrust-analyzer cargo roff 1.1.1 impl#[Roff]to_roff().();
431 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(text, "\\&.roman\n");
432 }
433
434 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
435 fn render_text_with_newline_periodrust-analyzer cargo roff 1.1.1 test/render_text_with_newline_period().() {
436 let text = Roffrust-analyzer cargo roff 1.1.1 Roff#::newrust-analyzer cargo roff 1.1.1 impl#[Roff]new().().textrust-analyzer cargo roff 1.1.1 impl#[Roff]text().([romanrust-analyzer cargo roff 1.1.1 roman().("foo\n.roman")]).to_roffrust-analyzer cargo roff 1.1.1 impl#[Roff]to_roff().();
437 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(text, "foo\n\\&.roman\n");
438 }
439 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
440 fn render_line_breakrust-analyzer cargo roff 1.1.1 test/render_line_break().() {
441 let text = Roffrust-analyzer cargo roff 1.1.1 Roff#::newrust-analyzer cargo roff 1.1.1 impl#[Roff]new().()
442 .textrust-analyzer cargo roff 1.1.1 impl#[Roff]text().([romanrust-analyzer cargo roff 1.1.1 roman().("roman"), Inlinerust-analyzer cargo roff 1.1.1 Inline#::LineBreakrust-analyzer cargo roff 1.1.1 Inline#LineBreak#, romanrust-analyzer cargo roff 1.1.1 roman().("more")])
443 .to_roffrust-analyzer cargo roff 1.1.1 impl#[Roff]to_roff().();
444 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(text, "roman\n.br\nmore\n");
445 }
446
447 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
448 fn render_controlrust-analyzer cargo roff 1.1.1 test/render_control().() {
449 let text = Roffrust-analyzer cargo roff 1.1.1 Roff#::newrust-analyzer cargo roff 1.1.1 impl#[Roff]new().().controlrust-analyzer cargo roff 1.1.1 impl#[Roff]control().("foo", ["bar", "foo and bar"]).to_roffrust-analyzer cargo roff 1.1.1 impl#[Roff]to_roff().();
450 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(text, ".foo bar \"foo and bar\"\n");
451 }
452 }
453
454 #[doc = include_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/include_str!!("../README.md")]
455 #[cfg(doctest)]
456 pub struct ReadmeDoctests;