roffman 0.4.0

Crate to generate ROFF files used for manual pages.
Documentation
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
use crate::_macro::*;
use crate::{write_quoted_if_whitespace, IntoRoffNode, RoffError, RoffText, Roffable, SynopsisOpt};

use std::io::Write;

#[derive(Clone, Debug)]
/// Building block of ROFF documents.
pub struct RoffNode(RoffNodeInner);

impl RoffNode {
    /// Creates a simple text node.
    pub fn text(content: impl Roffable) -> Self {
        Self(RoffNodeInner::Text(content.roff()))
    }

    /// Creates a new paragraph. When a new paragraph is created the indentation is reset to the
    /// default value.
    pub fn paragraph<I, R>(content: I) -> Self
    where
        I: IntoIterator<Item = R>,
        R: IntoRoffNode,
    {
        Self(RoffNodeInner::Paragraph(
            content
                .into_iter()
                .map(|item| item.into_roff().into_inner())
                .collect(),
        ))
    }

    /// Creates a new indented paragraph with an optional tag.
    pub fn indented_paragraph<I, R>(
        content: I,
        indentation: Option<u8>,
        title: Option<impl Roffable>,
    ) -> Self
    where
        I: IntoIterator<Item = R>,
        R: IntoRoffNode,
    {
        Self(RoffNodeInner::IndentedParagraph {
            content: content
                .into_iter()
                .map(|item| item.into_roff().into_inner())
                .collect(),
            indentation,
            title: title.map(|t| t.roff()),
        })
    }

    /// Creates a new paragraph with a leading tag and the remainder of the paragraph indented.
    pub fn tagged_paragraph<I, R>(content: I, title: impl Roffable) -> Self
    where
        I: IntoIterator<Item = R>,
        R: IntoRoffNode,
    {
        Self(RoffNodeInner::TaggedParagraph {
            content: content
                .into_iter()
                .map(|item| item.into_roff().into_inner())
                .collect(),
            title: title.roff(),
        })
    }

    /// Creates a new example node. An example block usually has the font set to monospaced but that
    /// behavior depends on the viewer used.
    ///
    /// This is an extension introduced in Version 9 Unix, to the original `man` package. Many systems
    /// running AT&T or Plan 9 `troff` support them.
    pub fn example<I, R>(content: I) -> Self
    where
        I: IntoIterator<Item = R>,
        R: Roffable,
    {
        Self(RoffNodeInner::Example(
            content.into_iter().map(|item| item.roff()).collect(),
        ))
    }

    /// Creates a new synopsis node explaining the given `command` with `description` and `opts`.
    ///
    /// This is a GNU extension not defined on systems runing AT&T, Plan 9, or Solaris `troff`.
    pub fn synopsis<I, R, O>(command: impl Roffable, description: I, opts: O) -> Self
    where
        I: IntoIterator<Item = R>,
        R: Roffable,
        O: IntoIterator<Item = SynopsisOpt>,
    {
        Self(RoffNodeInner::Synopsis {
            command: command.roff(),
            text: description.into_iter().map(|item| item.roff()).collect(),
            opts: opts.into_iter().collect(),
        })
    }

    /// Creates a new URL node that will take the form of `[name](address)` where `name` is the
    /// visible part of the URL and address is where it points to.
    ///
    /// This is a GNU extension not defined on systems runing AT&T, Plan 9, or Solaris `troff`.
    pub fn url(name: impl Roffable, address: impl Roffable) -> Self {
        Self(RoffNodeInner::Url {
            name: name.roff(),
            address: address.roff(),
        })
    }

    /// Creates a new email node that will where `address` is the email address and `name` is the
    /// visible URL text. `address` may not be visible if the man page is being viewed as HTML.
    ///
    /// This is a GNU extension not defined on systems runing AT&T, Plan 9, or Solaris `troff`.
    pub fn email(name: impl Roffable, address: impl Roffable) -> Self {
        Self(RoffNodeInner::Email {
            name: name.roff(),
            address: address.roff(),
        })
    }

    /// Returns a node that will be rendered as a registered sign `®`.
    pub fn registered_sign() -> Self {
        Self(RoffNodeInner::RegisteredSign)
    }

    /// Returns a node that will be rendered as a left quote `“`.
    pub fn left_quote() -> Self {
        Self(RoffNodeInner::LeftQuote)
    }

    /// Returns a node that will be rendered as a right quote `”`.
    pub fn right_quote() -> Self {
        Self(RoffNodeInner::RightQuote)
    }

    /// Returns a node that will be rendered as a trademark sign `™`.
    pub fn trademark_sign() -> Self {
        Self(RoffNodeInner::TrademarkSign)
    }

    /// Nest nodes by indenting all of the nodes inside. Creating a paragraph inside of this structure
    /// won't reset the indentation past the nested indentation level.
    pub fn nested<I, R>(nodes: I) -> Self
    where
        I: IntoIterator<Item = R>,
        R: IntoRoffNode,
    {
        Self(RoffNodeInner::Nested(
            nodes.into_iter().map(R::into_roff).collect(),
        ))
    }

    /// Breaks the line in text. Use this instead of adding raw `\n` characters to actually render
    /// linebreaks.
    pub fn linebreak() -> Self {
        Self(RoffNodeInner::Break)
    }

    /// A long dash `—`. Used for an interruption—such as this one—in a sentence.
    pub fn em_dash() -> Self {
        Self(RoffNodeInner::EmDash)
    }

    /// A long dash `–`. Used to separate the ends of a range, particularly between number like "1–9".
    pub fn en_dash() -> Self {
        Self(RoffNodeInner::EnDash)
    }

    /// Adjustable non-breaking space.  Use this to prevent a break inside a short phrase or
    /// between a numerical quantity and its corresponding unit(s).
    pub fn non_breaking_space() -> Self {
        Self(RoffNodeInner::NonBreakingSpace)
    }

    /// Adds a comment to the generated roff. You can add multiple lines in a single comment and
    /// they will automatically get converted to multiple comment lines.
    pub fn comment<C: AsRef<str>>(comment: C) -> Self {
        Self(RoffNodeInner::Comment(comment.as_ref().to_string()))
    }

    #[inline]
    pub(crate) fn into_inner(self) -> RoffNodeInner {
        self.0
    }

    #[inline]
    pub(crate) fn inner_ref(&self) -> &RoffNodeInner {
        &self.0
    }
}

#[derive(Clone, Debug)]
/// Base struct used to create ROFFs.
pub(crate) enum RoffNodeInner {
    /// The most basic node type, contains only text with style.
    Text(RoffText),
    /// A simple paragraph that can contain nested items.
    Paragraph(Vec<RoffNodeInner>),
    /// Indented paragraph that can contain nested items. If no indentation is provided the default
    /// is `4`.
    IndentedParagraph {
        content: Vec<RoffNodeInner>,
        indentation: Option<u8>,
        title: Option<RoffText>,
    },
    /// Paragraph with a title.
    TaggedParagraph {
        content: Vec<RoffNodeInner>,
        title: RoffText,
    },
    /// An example block where text is monospaced.
    Example(Vec<RoffText>),
    Synopsis {
        command: RoffText,
        text: Vec<RoffText>,
        opts: Vec<SynopsisOpt>,
    },
    Url {
        name: RoffText,
        address: RoffText,
    },
    Email {
        name: RoffText,
        address: RoffText,
    },
    RegisteredSign,
    LeftQuote,
    RightQuote,
    TrademarkSign,
    Nested(Vec<RoffNode>),
    Break,
    EmDash,
    EnDash,
    NonBreakingSpace,
    Comment(String),
}

impl RoffNodeInner {
    pub fn render<W: Write>(&self, writer: &mut W, mut was_text: bool) -> Result<bool, RoffError> {
        match self {
            RoffNodeInner::Text(text) => {
                text.render(writer)?;
                was_text = true;
            }
            RoffNodeInner::Paragraph(content) => {
                if was_text {
                    writer.write_all(ENDL)?;
                }
                writer.write_all(PARAGRAPH)?;
                writer.write_all(ENDL)?;
                for node in content {
                    was_text = node.render(writer, was_text)?;
                }
            }
            RoffNodeInner::IndentedParagraph {
                content,
                indentation,
                title,
            } => {
                if was_text {
                    writer.write_all(ENDL)?;
                }
                writer.write_all(INDENTED_PARAGRAPH)?;
                if let Some(indentation) = indentation {
                    writer.write_all(SPACE)?;
                    if let Some(title) = title {
                        write_quoted_if_whitespace(title, writer)?;
                    } else {
                        writer.write_all(QUOTE)?;
                        writer.write_all(QUOTE)?;
                    }
                    writer.write_all(SPACE)?;
                    indentation.roff().render(writer)?;
                }
                writer.write_all(ENDL)?;
                for node in content {
                    was_text = node.render(writer, was_text)?;
                }
                writer.write_all(ENDL)?;
                was_text = false;
            }
            RoffNodeInner::TaggedParagraph {
                content,
                title: tag,
            } => {
                if was_text {
                    writer.write_all(ENDL)?;
                }
                writer.write_all(TAGGED_PARAGRAPH)?;
                writer.write_all(ENDL)?;
                tag.render(writer)?;
                writer.write_all(ENDL)?;

                for node in content {
                    was_text = node.render(writer, was_text)?;
                }
                writer.write_all(ENDL)?;
                was_text = false;
            }
            RoffNodeInner::Example(content) => {
                if was_text {
                    writer.write_all(ENDL)?;
                }
                writer.write_all(EXAMPLE_START)?;
                writer.write_all(ENDL)?;
                for node in content {
                    node.render(writer)?;
                }
                writer.write_all(ENDL)?;
                writer.write_all(EXAMPLE_END)?;
                writer.write_all(ENDL)?;
                was_text = false;
            }
            RoffNodeInner::Synopsis {
                command,
                text,
                opts,
            } => {
                if was_text {
                    writer.write_all(ENDL)?;
                }
                writer.write_all(SYNOPSIS_START)?;
                writer.write_all(SPACE)?;
                write_quoted_if_whitespace(command, writer)?;
                writer.write_all(ENDL)?;
                for elem in text {
                    elem.render(writer)?;
                }
                if !text.is_empty() {
                    writer.write_all(ENDL)?;
                }
                for op in opts {
                    writer.write_all(ENDL)?;
                    writer.write_all(SYNOPSIS_OPT)?;
                    writer.write_all(SPACE)?;
                    write_quoted_if_whitespace(&op.name, writer)?;
                    if let Some(arg) = &op.argument {
                        writer.write_all(SPACE)?;
                        write_quoted_if_whitespace(arg, writer)?;
                    }
                    writer.write_all(ENDL)?;
                    if let Some(description) = &op.description {
                        for elem in description {
                            elem.render(writer)?;
                        }
                    }
                    writer.write_all(ENDL)?;
                }
                writer.write_all(SYNOPSIS_END)?;
                writer.write_all(ENDL)?;
                was_text = false;
            }
            RoffNodeInner::Url { address, name } => {
                if was_text {
                    writer.write_all(ENDL)?;
                }
                writer.write_all(URL_START)?;
                writer.write_all(SPACE)?;
                address.render(writer)?;
                writer.write_all(ENDL)?;
                name.render(writer)?;
                if !name.content().is_empty() {
                    writer.write_all(ENDL)?;
                }
                writer.write_all(URL_END)?;
                writer.write_all(ENDL)?;
                was_text = false;
            }
            RoffNodeInner::Email { address, name } => {
                if was_text {
                    writer.write_all(ENDL)?;
                }
                writer.write_all(MAIL_START)?;
                writer.write_all(SPACE)?;
                address.render(writer)?;
                writer.write_all(ENDL)?;
                name.render(writer)?;
                if !name.content().is_empty() {
                    writer.write_all(ENDL)?;
                }
                writer.write_all(MAIL_END)?;
                writer.write_all(ENDL)?;
                was_text = false;
            }
            RoffNodeInner::Nested(nodes) => {
                if was_text {
                    writer.write_all(ENDL)?;
                }
                writer.write_all(NESTED_START)?;
                writer.write_all(ENDL)?;
                was_text = false;
                for node in nodes {
                    was_text = node.inner_ref().render(writer, was_text)?;
                }

                if was_text {
                    writer.write_all(ENDL)?;
                }
                writer.write_all(NESTED_END)?;
                writer.write_all(ENDL)?;
                was_text = false;
            }
            RoffNodeInner::Break => {
                writer.write_all(ENDL)?;
                writer.write_all(BREAK)?;
                writer.write_all(ENDL)?;
            }
            RoffNodeInner::RegisteredSign => {
                writer.write_all(REGISTERED_SIGN)?;
                was_text = true;
            }
            RoffNodeInner::LeftQuote => {
                writer.write_all(LEFT_QUOTE)?;
                was_text = true;
            }
            RoffNodeInner::RightQuote => {
                writer.write_all(RIGHT_QUOTE)?;
                was_text = true;
            }
            RoffNodeInner::TrademarkSign => {
                writer.write_all(TRADEMARK_SIGN)?;
                was_text = true;
            }
            RoffNodeInner::EmDash => {
                writer.write_all(EM_DASH)?;
                was_text = true;
            }
            RoffNodeInner::EnDash => {
                writer.write_all(EN_DASH)?;
                was_text = true;
            }
            RoffNodeInner::NonBreakingSpace => {
                writer.write_all(NON_BREAKING_SPACE)?;
                was_text = true;
            }
            RoffNodeInner::Comment(comment) => {
                for line in comment.split('\n') {
                    writer.write_all(COMMENT)?;
                    writer.write_all(line.as_bytes())?;
                    writer.write_all(ENDL)?;
                }
                was_text = false
            }
        }

        Ok(was_text)
    }
}

impl IntoRoffNode for RoffNodeInner {
    fn into_roff(self) -> RoffNode {
        RoffNode(self)
    }
}