|
| 1 | +package roff |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/vanilla-os/sdk/pkg/v1/roff/types" |
| 10 | +) |
| 11 | + |
| 12 | +// Document wraps types.Document adding helper methods. |
| 13 | +type Document struct { |
| 14 | + types.Document |
| 15 | +} |
| 16 | + |
| 17 | +// NewDocument creates a new roff document. |
| 18 | +func NewDocument() *Document { |
| 19 | + return &Document{} |
| 20 | +} |
| 21 | + |
| 22 | +func (d *Document) writef(format string, args ...any) { |
| 23 | + if bytes.HasSuffix(d.Buffer.Bytes(), []byte("\n")) && strings.HasPrefix(format, "\n") { |
| 24 | + format = strings.TrimPrefix(format, "\n") |
| 25 | + } |
| 26 | + fmt.Fprintf(&d.Buffer, format, args...) |
| 27 | +} |
| 28 | + |
| 29 | +func (d *Document) writelnf(format string, args ...any) { |
| 30 | + d.writef(format+"\n", args...) |
| 31 | +} |
| 32 | + |
| 33 | +// Heading writes the document heading. |
| 34 | +func (d *Document) Heading(section uint, title, desc string, ts time.Time) { |
| 35 | + d.writef(types.TitleHeading, strings.ToUpper(title), section, title, ts.Format("2006-01-02"), desc) |
| 36 | +} |
| 37 | + |
| 38 | +// Paragraph starts a new paragraph. |
| 39 | +func (d *Document) Paragraph() { d.writelnf(types.Paragraph) } |
| 40 | + |
| 41 | +// Indent increases the indentation level. |
| 42 | +func (d *Document) Indent(n int) { |
| 43 | + if n >= 0 { |
| 44 | + d.writelnf(types.Indent+" %d", n) |
| 45 | + } else { |
| 46 | + d.writelnf(types.Indent) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +// IndentEnd decreases the indentation level. |
| 51 | +func (d *Document) IndentEnd() { d.writelnf(types.IndentEnd) } |
| 52 | + |
| 53 | +// TaggedParagraph starts a tagged paragraph. |
| 54 | +func (d *Document) TaggedParagraph(indentation int) { |
| 55 | + if indentation >= 0 { |
| 56 | + d.writelnf(types.TaggedParagraph+" %d", indentation) |
| 57 | + } else { |
| 58 | + d.writelnf(types.TaggedParagraph) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +// List writes a list item. |
| 63 | +func (d *Document) List(text string) { |
| 64 | + d.writelnf(types.IndentedParagraph+" \\(bu 3\n%s", escapeText(strings.TrimSpace(text))) |
| 65 | +} |
| 66 | + |
| 67 | +// Section writes a section heading. |
| 68 | +func (d *Document) Section(text string) { |
| 69 | + d.writelnf(types.SectionHeading, strings.ToUpper(text)) |
| 70 | +} |
| 71 | + |
| 72 | +// SubSection writes a subsection heading. |
| 73 | +func (d *Document) SubSection(text string) { |
| 74 | + d.writelnf(types.SubSectionHeading, strings.ToUpper(text)) |
| 75 | +} |
| 76 | + |
| 77 | +// EndSection ends the current section. |
| 78 | +func (d *Document) EndSection() { d.writelnf("") } |
| 79 | + |
| 80 | +// EndSubSection ends the current subsection. |
| 81 | +func (d *Document) EndSubSection() { d.writelnf("") } |
| 82 | + |
| 83 | +// Text writes text handling basic lists. |
| 84 | +func (d *Document) Text(text string) { |
| 85 | + inList := false |
| 86 | + for i, line := range strings.Split(text, "\n") { |
| 87 | + if i > 0 && !inList { |
| 88 | + d.Paragraph() |
| 89 | + } |
| 90 | + if strings.HasPrefix(line, "*") { |
| 91 | + if !inList { |
| 92 | + d.Indent(-1) |
| 93 | + inList = true |
| 94 | + } |
| 95 | + d.List(line[1:]) |
| 96 | + } else { |
| 97 | + if inList { |
| 98 | + d.IndentEnd() |
| 99 | + inList = false |
| 100 | + } |
| 101 | + d.writef(escapeText(line)) |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +// TextBold writes bold text. |
| 107 | +func (d *Document) TextBold(text string) { |
| 108 | + d.writef(types.Bold) |
| 109 | + d.Text(text) |
| 110 | + d.writef(types.PreviousFont) |
| 111 | +} |
| 112 | + |
| 113 | +// TextItalic writes italic text. |
| 114 | +func (d *Document) TextItalic(text string) { |
| 115 | + d.writef(types.Italic) |
| 116 | + d.Text(text) |
| 117 | + d.writef(types.PreviousFont) |
| 118 | +} |
| 119 | + |
| 120 | +// String returns the document as a string. |
| 121 | +func (d *Document) String() string { return d.Buffer.String() } |
| 122 | + |
| 123 | +func escapeText(s string) string { |
| 124 | + s = strings.ReplaceAll(s, `\`, `\e`) |
| 125 | + s = strings.ReplaceAll(s, ".", "\\&.") |
| 126 | + return s |
| 127 | +} |
0 commit comments