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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
//! # roffman - create ROFF man pages in rust with ease!
//!
//!
//! ## Example usage
//! ```
//! use roffman::{Roff, RoffNode, Roffable, SectionNumber, SynopsisOpt};
//!
//! let roff = Roff::new("roffman", SectionNumber::Miscellaneous)
//! .date("August 2021")
//! .section(
//!    "BASIC USAGE",
//!    [
//!        RoffNode::paragraph([
//!            "This is how you create a basic paragraph using roffman.",
//!        ]),
//!        RoffNode::indented_paragraph(
//!            [
//!                "This line should be slightly indented to the ".roff(),
//!                "right.".roff().bold(),
//!            ],
//!            Some(4),
//!            Some("optional-title")
//!        ),
//!        RoffNode::synopsis(
//!                 "roffman-command",
//!                 [
//!                 "This is the description of this command. It will be displayed right next to".roff(),
//! " it".roff().italic()
//! ]                     ,
//!                 [
//!                 SynopsisOpt::new("--opt").description(["some simple opt"]),
//!                 SynopsisOpt::new("--opt-with-arg").argument("ARG").description(["opt with an argument"]),
//!                 SynopsisOpt::new("--bold")
//!        ]),
//!        RoffNode::paragraph(["Example:".roff().bold()]),
//!        RoffNode::example([
//!             r#"
//! impl Roffable for u8 {
//!     fn roff(&self) -> RoffText {
//!         self.to_string().roff()
//!     }
//! }"#,
//!         ]),
//!        RoffNode::url("GitHub", "https://github.com/vv9k/roffman"),
//!        RoffNode::text("\nvv9k"),
//!        RoffNode::trademark_sign(),
//!     ],
//! );
//!
//! let rendered = roff.to_string().unwrap();
//!
//! let output = r#".TH roffman 7 "August 2021"
//! .SH "BASIC USAGE"
//! .P
//! This is how you create a basic paragraph using roffman.
//! .IP optional\-title 4
//! This line should be slightly indented to the \fBright.\fR
//! .SY roffman\-command
//! This is the description of this command. It will be displayed right next to\fI it\fR
//!
//! .OP \-\-opt
//! some simple opt
//!
//! .OP \-\-opt\-with\-arg ARG
//! opt with an argument
//!
//! .OP \-\-bold
//!
//! .YS
//! .P
//! \fBExample:\fR
//! .EX
//!
//! impl Roffable for u8 {
//!     fn roff(&self) \-> RoffText {
//!         self.to_string().roff()
//!     }
//! }
//! .EE
//! .UR https://github.com/vv9k/roffman
//! GitHub
//! .UE
//!
//! vv9k\*(Tm
//! "#;
//!
//! assert_eq!(rendered.trim(), output.trim());
//! ```
//!
//! which will look something like this:
//! ```text
//! roffman(7)                                         Miscellaneous Information Manual                                  roffman(7)
//!
//! BASIC USAGE
//!        This is how you create a basic paragraph using roffman.
//!
//!            This line should be slightly indented to the right.
//!
//!        roffman-command This is the description of this command. It will be displayed right next to it
//!
//!                        [--opt] some simple opt
//!
//!                        [--opt-with-arg ARG] opt with an argument
//!
//!                        [--bold]
//!
//!        Example:
//!
//!        impl Roffable for u8 {
//!            fn roff(&self) -> RoffText {
//!                self.to_string().roff()
//!            }
//!        }
//!        GitHub ⟨https://github.com/vv9k/roffman⟩
//!
//!        vv9k™
//!                                                               August 2021                                             roffman(7)
//! ```

mod escape;
mod node;
mod section;
mod text;

pub use node::RoffNode;
pub use section::Section;
pub use text::{FontStyle, RoffText};

use escape::escape;

use std::error::Error;
use std::fmt;
use std::fmt::Formatter;
use std::io::{self, Write};

mod _macro {
    pub(crate) const SPACE: &[u8] = b" ";
    pub(crate) const QUOTE: &[u8] = b"\"";
    pub(crate) const ENDL: &[u8] = b"\n";
    pub(crate) const BOLD: &[u8] = b"\\fB";
    pub(crate) const ITALIC: &[u8] = b"\\fI";
    pub(crate) const FONT_END: &[u8] = b"\\fR";
    pub(crate) const SECTION_HEADER: &[u8] = b".SH";
    pub(crate) const SUB_HEADER: &[u8] = b".SS";
    pub(crate) const TITLE_HEADER: &[u8] = b".TH";
    pub(crate) const PARAGRAPH: &[u8] = b".P";
    pub(crate) const INDENTED_PARAGRAPH: &[u8] = b".IP";
    pub(crate) const TAGGED_PARAGRAPH: &[u8] = b".TP";
    pub(crate) const NESTED_START: &[u8] = b".RS";
    pub(crate) const NESTED_END: &[u8] = b".RE";
    pub(crate) const EXAMPLE_START: &[u8] = b".EX";
    pub(crate) const EXAMPLE_END: &[u8] = b".EE";
    pub(crate) const SYNOPSIS_START: &[u8] = b".SY";
    pub(crate) const SYNOPSIS_END: &[u8] = b".YS";
    pub(crate) const SYNOPSIS_OPT: &[u8] = b".OP";
    pub(crate) const URL_START: &[u8] = b".UR";
    pub(crate) const URL_END: &[u8] = b".UE";
    pub(crate) const MAIL_START: &[u8] = b".MT";
    pub(crate) const MAIL_END: &[u8] = b".ME";
    pub(crate) const LEFT_QUOTE: &[u8] = b"\\(lq";
    pub(crate) const RIGHT_QUOTE: &[u8] = b"\\(rq";
    pub(crate) const REGISTERED_SIGN: &[u8] = b"\\(rg";
    pub(crate) const TRADEMARK_SIGN: &[u8] = b"\\(tm";
    pub(crate) const BREAK: &[u8] = b".br";
    pub(crate) const EM_DASH: &[u8] = b"\\(em";
    pub(crate) const EN_DASH: &[u8] = b"\\(en";
    pub(crate) const NON_BREAKING_SPACE: &[u8] = b"\\~";
    pub(crate) const COMMENT: &[u8] = b"\\\"";
}
use _macro::{ENDL, QUOTE, SPACE, TITLE_HEADER};

#[derive(Debug)]
/// An error type returned by the functions used in this crate.
pub enum RoffError {
    StringRenderFailed(String),
    RenderFailed(io::Error),
}

impl fmt::Display for RoffError {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            RoffError::StringRenderFailed(err) => {
                write!(f, "Failed to render ROFF to string - `{}`", err)
            }
            RoffError::RenderFailed(err) => write!(f, "Failed to render ROFF - `{}`", err),
        }
    }
}

impl Error for RoffError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            RoffError::RenderFailed(err) => Some(err),
            _ => None,
        }
    }
}

impl From<io::Error> for RoffError {
    fn from(err: io::Error) -> Self {
        Self::RenderFailed(err)
    }
}

fn write_quoted(roff: &RoffText, writer: &mut impl Write) -> Result<(), RoffError> {
    writer.write_all(QUOTE)?;
    roff.render(writer)?;
    writer.write_all(QUOTE)?;
    Ok(())
}

fn write_quoted_if_whitespace(roff: &RoffText, writer: &mut impl Write) -> Result<(), RoffError> {
    if roff
        .content()
        .as_bytes()
        .iter()
        .any(u8::is_ascii_whitespace)
    {
        write_quoted(roff, writer)
    } else {
        roff.render(writer)
    }
}

#[derive(Clone, Debug)]
/// Represents a ROFF document that can be rendered and displayed
/// with tools like [`man`](https://man7.org/linux/man-pages/man1/man.1.html).
pub struct Roff {
    title: RoffText,
    date: Option<RoffText>,
    section: SectionNumber,
    sections: Vec<Section>,
}

impl Roff {
    /// Create a new `Roff` with a `title` and a `section`.
    pub fn new(title: impl Roffable, section: SectionNumber) -> Self {
        Self {
            title: title.roff(),
            date: None,
            section,
            sections: vec![],
        }
    }

    /// Renders this roff to a `String` returning an error if a write fails or the rendered
    /// output contains invalid UTF-8 byte sequences.
    pub fn to_string(&self) -> Result<String, RoffError> {
        let mut writer = std::io::BufWriter::new(vec![]);
        self.render(&mut writer)
            .map_err(|e| RoffError::StringRenderFailed(e.to_string()))?;
        String::from_utf8(
            writer
                .into_inner()
                .map_err(|e| RoffError::StringRenderFailed(e.to_string()))?,
        )
        .map_err(|e| RoffError::StringRenderFailed(e.to_string()))
    }

    /// Builder method for adding a date to this roff.
    pub fn date(mut self, date: impl Roffable) -> Self {
        self.date = Some(date.roff());
        self
    }

    /// Add an already defined section to this roff.
    pub fn add_section(mut self, section: Section) -> Self {
        self.sections.push(section);
        self
    }

    /// Builder method for adding a new section to this roff.
    pub fn section<I, R>(self, title: impl Roffable, content: I) -> Self
    where
        I: IntoIterator<Item = R>,
        R: IntoRoffNode,
    {
        self.add_section(Section::new(title, content))
    }

    fn write_title(&self, writer: &mut impl Write) -> Result<(), RoffError> {
        writer.write_all(SPACE)?;
        write_quoted_if_whitespace(&self.title, writer)
    }

    fn write_section(&self, writer: &mut impl Write) -> Result<(), RoffError> {
        writer.write_all(SPACE)?;
        write_quoted_if_whitespace(&self.section.roff(), writer)
    }

    fn write_date(&self, writer: &mut impl Write) -> Result<(), RoffError> {
        if let Some(date) = &self.date {
            writer.write_all(SPACE)?;
            write_quoted_if_whitespace(date, writer)?;
        }
        Ok(())
    }

    fn write_title_header(&self, writer: &mut impl Write) -> Result<(), RoffError> {
        writer.write_all(TITLE_HEADER)?;
        self.write_title(writer)?;
        self.write_section(writer)?;
        self.write_date(writer)?;
        writer.write_all(ENDL)?;
        Ok(())
    }

    /// Renders this `Roff` to a `writer` returning an error if any of the writes fails.
    pub fn render<W: Write>(&self, writer: &mut W) -> Result<(), RoffError> {
        self.write_title_header(writer)?;

        let mut was_text = false;
        for section in &self.sections {
            was_text = section.render(writer, was_text)?;
        }

        Ok(())
    }
}

#[derive(Copy, Clone, Debug, PartialEq)]
/// Defines the section to which the given ROFF belongs.
pub enum SectionNumber {
    ///Commands that can be executed by the user from within a shell.
    UserCommands,
    /// Functions which wrap operations performed by the kernel.
    SystemCalls,
    /// All library functions excluding the system call wrappers (Most of the libc functions).
    LibraryCalls,
    /// Files found in `/dev` which allow to access to devices through the kernel.
    Devices,
    /// Describes various human-readable file formats and configuration files.
    FileFormatsAndConfigurationFiles,
    /// Games and funny little programs available on the system.
    Games,
    /// Overviews or descriptions of various topics, conventions, and protocols, character set
    /// standards, the standard filesystem layout, and miscellaneous other things.
    Miscellaneous,
    /// Commands like `mount(8)`, many of which only root can execute.
    SystemManagementCommands,
    /// A custom section number.
    Custom(u8),
}

impl From<SectionNumber> for u8 {
    fn from(s: SectionNumber) -> Self {
        use SectionNumber::*;
        match s {
            UserCommands => 1,
            SystemCalls => 2,
            LibraryCalls => 3,
            Devices => 4,
            FileFormatsAndConfigurationFiles => 5,
            Games => 6,
            Miscellaneous => 7,
            SystemManagementCommands => 8,
            Custom(n) => n,
        }
    }
}

impl Roffable for SectionNumber {
    fn roff(&self) -> RoffText {
        u8::from(*self).roff()
    }
}

#[derive(Clone, Debug)]
/// An option used by the [`RoffNode::synopsis`](RoffNode::synopsis) block.
pub struct SynopsisOpt {
    name: RoffText,
    argument: Option<RoffText>,
    description: Option<Vec<RoffText>>,
}

impl SynopsisOpt {
    /// Creates a new option used in a synopsis block.
    pub fn new<R: Roffable>(name: R) -> Self {
        Self {
            name: name.roff(),
            argument: None,
            description: None,
        }
    }

    /// Set the name of the argument that this option takes.
    pub fn argument<R: Roffable>(mut self, argument: R) -> Self {
        self.argument = Some(argument.roff());
        self
    }

    /// Set the description for this command synopsis.
    pub fn description<I, R>(mut self, description: I) -> Self
    where
        I: IntoIterator<Item = R>,
        R: Roffable,
    {
        self.description = Some(description.into_iter().map(|item| item.roff()).collect());
        self
    }
}

/// A trait that describes items that can be turned into a [`RoffNode`](RoffNode).
pub trait IntoRoffNode {
    /// Convert this item into a `RoffNode`.
    fn into_roff(self) -> RoffNode;
}

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

impl<R: Roffable> IntoRoffNode for R {
    fn into_roff(self) -> RoffNode {
        RoffNode::text(self.roff())
    }
}

/// Convenience trait to convert items to [`RoffText`](RoffText).
pub trait Roffable {
    /// Returns this item as [`RoffText`](RoffText).
    fn roff(&self) -> RoffText;
}

impl Roffable for String {
    fn roff(&self) -> RoffText {
        RoffText::new(self.clone(), None)
    }
}

impl Roffable for &String {
    fn roff(&self) -> RoffText {
        RoffText::new((*self).clone(), None)
    }
}

impl Roffable for &str {
    fn roff(&self) -> RoffText {
        RoffText::new(self.to_string(), None)
    }
}

impl Roffable for &&str {
    fn roff(&self) -> RoffText {
        (*self).roff()
    }
}

impl Roffable for std::borrow::Cow<'_, str> {
    fn roff(&self) -> RoffText {
        self.as_ref().roff()
    }
}

impl Roffable for u8 {
    fn roff(&self) -> RoffText {
        RoffText::new(self.to_string(), None)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn it_roffs() {
        let roff = Roff::new("test", SectionNumber::UserCommands)
            .section(
                "test section 1",
                [RoffNode::paragraph([
                    "this is some very ".roff(),
                    "special".roff().bold(),
                    " text".roff(),
                ])],
            )
            .section(
                "test section 2",
                [RoffNode::indented_paragraph(
                    [
                        "Lorem ipsum".roff().italic(),
                        " dolor sit amet, consectetur adipiscing elit. Vivamus quis malesuada eros.".roff()
                            .roff(),
                    ],
                    Some(4),
                    None::<&str>
                )],
            )
            .section(
                "test section 3",
                [RoffNode::tagged_paragraph(
                    ["tagged paragraph with some content".roff()],
                    "paragraph title".roff().bold(),
                )],
            )
            .section(
                "test section 4",
                [
                RoffNode::indented_paragraph(
                    [
                        "Indented paragraph with a title",
                    ],
                    Some(4),
                    Some("Paragraph title with spaces")
                ),
                RoffNode::indented_paragraph(
                    [
                        "Another indented paragraph",
                    ],
                    Some(2),
                    Some("title-no-spaces")
                )
                ],
            )
            ;

        let rendered = roff.to_string().unwrap();
        assert_eq!(
            r#".TH test 1
.SH "test section 1"
.P
this is some very \fBspecial\fR text
.SH "test section 2"
.IP "" 4
\fILorem ipsum\fR dolor sit amet, consectetur adipiscing elit. Vivamus quis malesuada eros.
.SH "test section 3"
.TP
\fBparagraph title\fR
tagged paragraph with some content
.SH "test section 4"
.IP "Paragraph title with spaces" 4
Indented paragraph with a title
.IP title\-no\-spaces 2
Another indented paragraph
"#,
            rendered
        )
    }

    #[test]
    fn it_nests_roffs() {
        let roff = Roff::new("test", SectionNumber::UserCommands).add_section(
            Section::new(
                "BASE SECTION",
                [
                    RoffNode::paragraph([
                        RoffNode::text("some text in first paragraph."),
                        RoffNode::nested([RoffNode::paragraph([
                            RoffNode::text("some nested paragraph"),
                            RoffNode::nested([RoffNode::paragraph([RoffNode::text(
                                "some doubly nested paragraph",
                            )])]),
                            RoffNode::text("some text after nested para"),
                        ])]),
                    ]),
                    RoffNode::paragraph(["back two levels left", " without roffs"]),
                ],
            )
            .subtitle("with some subtitle..."),
        );

        let rendered = roff.to_string().unwrap();
        assert_eq!(
            rendered,
            r#".TH test 1
.SH "BASE SECTION"
.SS "with some subtitle..."
.P
some text in first paragraph.
.RS
.P
some nested paragraph
.RS
.P
some doubly nested paragraph
.RE
some text after nested para
.RE
.P
back two levels left without roffs"#,
        )
    }

    #[test]
    fn it_roffs_examples() {
        let roff = Roff::new("test-examples", SectionNumber::LibraryCalls).section(
            "BASE SECTION",
            vec![
                RoffNode::text("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus quis malesuada eros."),
                RoffNode::example(vec![
                "let example = String::new()\n",
                "let x = example.clone();\n",
                "if x.len() > 0 {\n",
                "\tprintln!(\"{}\", x);\n",
                "}\n",
                ])
            ],
        );

        let rendered = roff.to_string().unwrap();
        assert_eq!(
            r#".TH test\-examples 3
.SH "BASE SECTION"
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus quis malesuada eros.
.EX
let example = String::new()
let x = example.clone();
if x.len() > 0 {
	println!(\(dq{}\(dq, x);
}

.EE
"#,
            rendered
        )
    }

    #[test]
    fn synopsis_works() {
        let roff = Roff::new("test-synopsis", SectionNumber::Miscellaneous).section(
            "SYNOPSIS",
            vec![
                RoffNode::synopsis("ls", ["lists files in the given".roff(), "path".roff().italic(), ".".roff()],
                vec![
                    SynopsisOpt::new("-l").description(["use a long listing format"]),
                    SynopsisOpt::new("-L, --dereference").description(["when showing file information for a symbolic link, show information for the file the link references rather than for the link itself"]),
                    SynopsisOpt::new("--block-size").argument("SIZE").description(["with -l, scale sizes by SIZE when printing them"]),
                ]
                )
            ],
        );

        let rendered = roff.to_string().unwrap();
        assert_eq!(
            r#".TH test\-synopsis 7
.SH SYNOPSIS
.SY ls
lists files in the given\fIpath\fR.

.OP \-l
use a long listing format

.OP "\-L, \-\-dereference"
when showing file information for a symbolic link, show information for the file the link references rather than for the link itself

.OP \-\-block\-size SIZE
with \-l, scale sizes by SIZE when printing them
.YS
"#,
            rendered
        )
    }

    #[test]
    fn urls_and_emails_work() {
        let roff = Roff::new("test-urls", SectionNumber::Miscellaneous).section(
            "URLS",
            vec![
                RoffNode::url("GitHub", "https://github.com/vv9k/roffman"),
                RoffNode::url("crates.io", "https://crates.io/crates/roffman"),
                RoffNode::url("docs.rs", "https://docs.rs/roffman"),
                RoffNode::url("", "https://docs.rs/roffman"),
                RoffNode::url("", ""),
                RoffNode::email("John Test", "test@invalid.domain"),
                RoffNode::email("", "test@invalid.domain"),
                RoffNode::email("", ""),
            ],
        );

        let rendered = roff.to_string().unwrap();
        assert_eq!(
            r#".TH test\-urls 7
.SH URLS
.UR https://github.com/vv9k/roffman
GitHub
.UE
.UR https://crates.io/crates/roffman
crates.io
.UE
.UR https://docs.rs/roffman
docs.rs
.UE
.UR https://docs.rs/roffman
.UE
.UR 
.UE
.MT test@invalid.domain
John Test
.ME
.MT test@invalid.domain
.ME
.MT 
.ME
"#,
            rendered
        )
    }

    #[test]
    fn special_strings_work() {
        let roff = Roff::new("test-strings", SectionNumber::Miscellaneous).section(
            "STRINGS",
            vec![
                RoffNode::left_quote(),
                RoffNode::text("this is some example quoted text."),
                RoffNode::right_quote(),
                RoffNode::text(" "),
                RoffNode::registered_sign(),
                RoffNode::text(" roffman"),
                RoffNode::trademark_sign(),
                RoffNode::linebreak(),
                RoffNode::text("123"),
                RoffNode::en_dash(),
                RoffNode::text("321"),
                RoffNode::linebreak(),
                RoffNode::text("some text"),
                RoffNode::em_dash(),
                RoffNode::text("interupted sentence in the middle"),
                RoffNode::em_dash(),
                RoffNode::text("more text..."),
                RoffNode::linebreak(),
                RoffNode::text("64"),
                RoffNode::non_breaking_space(),
                RoffNode::text("KiB"),
                RoffNode::linebreak(),
                RoffNode::en_dash(),
                RoffNode::paragraph(["paragraph after special sequence"]),
                RoffNode::comment("that was interesting indeed..."),
                RoffNode::comment("this should span\nover multiple\nlines correctly."),
            ],
        );

        let rendered = roff.to_string().unwrap();
        assert_eq!(
            rendered,
            r#".TH test\-strings 7
.SH STRINGS
\(lqthis is some example quoted text.\(rq \(rg roffman\(tm
.br
123\(en321
.br
some text\(eminterupted sentence in the middle\(emmore text...
.br
64\~KiB
.br
\(en
.P
paragraph after special sequence\"that was interesting indeed...
\"this should span
\"over multiple
\"lines correctly.
"#
        )
    }

    #[test]
    fn section_after_text_renders() {
        let roff = Roff::new("test-sections", SectionNumber::Miscellaneous)
            .section("TEXTS", vec![RoffNode::text("this is some example text.")])
            .section(
                "NEXT",
                vec![
                    RoffNode::text("this is some example text on second section.\n"),
                    RoffNode::text("this is some example.\n"),
                    RoffNode::text("this is some example text."),
                ],
            )
            .section("THIRD", vec![RoffNode::text("this is some example text.")]);

        let rendered = roff.to_string().unwrap();
        assert_eq!(
            r#".TH test\-sections 7
.SH TEXTS
this is some example text.
.SH NEXT
this is some example text on second section.
this is some example.
this is some example text.
.SH THIRD
this is some example text."#,
            rendered
        )
    }

    #[test]
    fn breaks_line() {
        let roff = Roff::new("test-breaks", SectionNumber::Miscellaneous).section(
            "BREAKS",
            vec![
                RoffNode::text("this is some example text."),
                RoffNode::linebreak(),
                RoffNode::text("this is some example text on second line."),
                RoffNode::linebreak(),
                RoffNode::text("this is some example text on third line."),
            ],
        );

        let rendered = roff.to_string().unwrap();
        assert_eq!(
            rendered,
            r#".TH test\-breaks 7
.SH BREAKS
this is some example text.
.br
this is some example text on second line.
.br
this is some example text on third line."#
        )
    }
}