roff API

roff

package

API reference for the roff package.

S
struct

Document

Document wraps types.Document adding helper methods.

pkg/v1/roff/roff.go:21-23
type Document struct

Methods

writef
Method

Parameters

format string
args ...any
func (*Document) writef(format string, args ...any)
{
	if bytes.HasSuffix(d.Buffer.Bytes(), []byte("\n")) && strings.HasPrefix(format, "\n") {
		format = strings.TrimPrefix(format, "\n")
	}
	fmt.Fprintf(&d.Buffer, format, args...)
}
writelnf
Method

Parameters

format string
args ...any
func (*Document) writelnf(format string, args ...any)
{
	d.writef(format+"\n", args...)
}
Heading
Method

Heading writes the document heading.

Parameters

section uint
title string
desc string
func (*Document) Heading(section uint, title, desc string, ts time.Time)
{
	d.writef(types.TitleHeading, strings.ToUpper(title), section, title, ts.Format("2006-01-02"), desc)
}
Paragraph
Method

Paragraph starts a new paragraph.

func (*Document) Paragraph()
{ d.writelnf(types.Paragraph) }
Indent
Method

Indent increases the indentation level.

Parameters

n int
func (*Document) Indent(n int)
{
	if n >= 0 {
		d.writelnf(types.Indent+" %d", n)
	} else {
		d.writelnf(types.Indent)
	}
}
IndentEnd
Method

IndentEnd decreases the indentation level.

func (*Document) IndentEnd()
{ d.writelnf(types.IndentEnd) }

TaggedParagraph starts a tagged paragraph.

Parameters

indentation int
func (*Document) TaggedParagraph(indentation int)
{
	if indentation >= 0 {
		d.writelnf(types.TaggedParagraph+" %d", indentation)
	} else {
		d.writelnf(types.TaggedParagraph)
	}
}
List
Method

List writes a list item.

Parameters

text string
func (*Document) List(text string)
{
	d.writelnf(types.IndentedParagraph+" \\(bu 3\n%s", escapeText(strings.TrimSpace(text)))
}
Section
Method

Section writes a section heading.

Parameters

text string
func (*Document) Section(text string)
{
	d.writelnf(types.SectionHeading, strings.ToUpper(text))
}
SubSection
Method

SubSection writes a subsection heading.

Parameters

text string
func (*Document) SubSection(text string)
{
	d.writelnf(types.SubSectionHeading, strings.ToUpper(text))
}
EndSection
Method

EndSection ends the current section.

func (*Document) EndSection()
{ d.writelnf("") }
EndSubSection
Method

EndSubSection ends the current subsection.

func (*Document) EndSubSection()
{ d.writelnf("") }
Text
Method

Text writes text handling basic lists.

Parameters

text string
func (*Document) Text(text string)
{
	inList := false
	for i, line := range strings.Split(text, "\n") {
		if i > 0 && !inList {
			d.Paragraph()
		}
		if strings.HasPrefix(line, "*") {
			if !inList {
				d.Indent(-1)
				inList = true
			}
			d.List(line[1:])
		} else {
			if inList {
				d.IndentEnd()
				inList = false
			}
			d.writef(escapeText(line))
		}
	}
}
TextBold
Method

TextBold writes bold text.

Parameters

text string
func (*Document) TextBold(text string)
{
	d.writef(types.Bold)
	d.Text(text)
	d.writef(types.PreviousFont)
}
TextItalic
Method

TextItalic writes italic text.

Parameters

text string
func (*Document) TextItalic(text string)
{
	d.writef(types.Italic)
	d.Text(text)
	d.writef(types.PreviousFont)
}
String
Method

String returns the document as a string.

Returns

string
func (*Document) String() string
{ return d.Buffer.String() }
F
function

NewDocument

NewDocument creates a new roff document.

Returns

pkg/v1/roff/roff.go:26-28
func NewDocument() *Document

{
	return &Document{}
}
F
function

escapeText

Parameters

s
string

Returns

string
pkg/v1/roff/roff.go:131-135
func escapeText(s string) string

{
	s = strings.ReplaceAll(s, `\`, `\e`)
	s = strings.ReplaceAll(s, ".", "\\&.")
	return s
}