pax_global_header00006660000000000000000000000064141672613660014526gustar00rootroot0000000000000052 comment=273d76dfb27f763ed9a86826be7c55520d46672b roff-0.1.0/000077500000000000000000000000001416726136600124605ustar00rootroot00000000000000roff-0.1.0/.github/000077500000000000000000000000001416726136600140205ustar00rootroot00000000000000roff-0.1.0/.github/FUNDING.yml000066400000000000000000000000171416726136600156330ustar00rootroot00000000000000github: muesli roff-0.1.0/.github/workflows/000077500000000000000000000000001416726136600160555ustar00rootroot00000000000000roff-0.1.0/.github/workflows/build.yml000066400000000000000000000011171416726136600176770ustar00rootroot00000000000000name: build on: [push, pull_request] jobs: build: strategy: matrix: go-version: [~1.12, ^1] os: [ubuntu-latest, macos-latest, windows-latest] runs-on: ${{ matrix.os }} env: GO111MODULE: "on" steps: - name: Install Go uses: actions/setup-go@v2 with: go-version: ${{ matrix.go-version }} - name: Checkout code uses: actions/checkout@v2 - name: Download Go modules run: go mod download - name: Build run: go build -v ./... - name: Test run: go test ./... roff-0.1.0/.github/workflows/coverage.yml000066400000000000000000000013211416726136600203700ustar00rootroot00000000000000name: coverage on: [push, pull_request] jobs: coverage: strategy: matrix: go-version: [^1] os: [ubuntu-latest] runs-on: ${{ matrix.os }} env: GO111MODULE: "on" steps: - name: Install Go uses: actions/setup-go@v2 with: go-version: ${{ matrix.go-version }} - name: Checkout code uses: actions/checkout@v2 - name: Coverage env: COVERALLS_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | go test -race -covermode atomic -coverprofile=profile.cov ./... GO111MODULE=off go get github.com/mattn/goveralls $(go env GOPATH)/bin/goveralls -coverprofile=profile.cov -service=github roff-0.1.0/.github/workflows/lint-soft.yml000066400000000000000000000011731416726136600205210ustar00rootroot00000000000000name: lint-soft on: push: pull_request: permissions: contents: read # Optional: allow read access to pull request. Use with `only-new-issues` option. pull-requests: read jobs: golangci: name: lint-soft runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: golangci-lint uses: golangci/golangci-lint-action@v2 with: # Optional: golangci-lint command line arguments. args: --config .golangci-soft.yml --issues-exit-code=0 # Optional: show only new issues if it's a pull request. The default value is `false`. only-new-issues: true roff-0.1.0/.github/workflows/lint.yml000066400000000000000000000011011416726136600175370ustar00rootroot00000000000000name: lint on: push: pull_request: permissions: contents: read # Optional: allow read access to pull request. Use with `only-new-issues` option. pull-requests: read jobs: golangci: name: lint runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: golangci-lint uses: golangci/golangci-lint-action@v2 with: # Optional: golangci-lint command line arguments. #args: # Optional: show only new issues if it's a pull request. The default value is `false`. only-new-issues: true roff-0.1.0/.gitignore000066400000000000000000000004151416726136600144500ustar00rootroot00000000000000# Binaries for programs and plugins *.exe *.exe~ *.dll *.so *.dylib # Test binary, built with `go test -c` *.test # Output of the go coverage tool, specifically when used with LiteIDE *.out # Dependency directories (remove the comment below to include it) # vendor/ roff-0.1.0/.golangci-soft.yml000066400000000000000000000012701416726136600160150ustar00rootroot00000000000000run: tests: false issues: include: - EXC0001 - EXC0005 - EXC0011 - EXC0012 - EXC0013 max-issues-per-linter: 0 max-same-issues: 0 linters: enable: # - dupl - exhaustive # - exhaustivestruct - goconst - godot - godox - gomnd - gomoddirectives - goprintffuncname - ifshort # - lll - misspell - nakedret - nestif - noctx - nolintlint - prealloc - wrapcheck # disable default linters, they are already enabled in .golangci.yml disable: - deadcode - errcheck - gosimple - govet - ineffassign - staticcheck - structcheck - typecheck - unused - varcheck roff-0.1.0/.golangci.yml000066400000000000000000000006051416726136600150450ustar00rootroot00000000000000run: tests: false issues: include: - EXC0001 - EXC0005 - EXC0011 - EXC0012 - EXC0013 max-issues-per-linter: 0 max-same-issues: 0 linters: enable: - bodyclose - exportloopref - goimports - gosec - nilerr - predeclared - revive - rowserrcheck - sqlclosecheck - tparallel - unconvert - unparam - whitespace roff-0.1.0/LICENSE000066400000000000000000000020671416726136600134720ustar00rootroot00000000000000MIT License Copyright (c) 2022 Christian Muehlhaeuser Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. roff-0.1.0/README.md000066400000000000000000000034421416726136600137420ustar00rootroot00000000000000# roff [![Latest Release](https://img.shields.io/github/release/muesli/roff.svg)](https://github.com/muesli/roff/releases) [![Build Status](https://github.com/muesli/roff/workflows/build/badge.svg)](https://github.com/muesli/roff/actions) [![Coverage Status](https://coveralls.io/repos/github/muesli/roff/badge.svg?branch=main)](https://coveralls.io/github/muesli/roff?branch=main) [![Go ReportCard](https://goreportcard.com/badge/muesli/roff)](https://goreportcard.com/report/muesli/roff) [![GoDoc](https://godoc.org/github.com/golang/gddo?status.svg)](https://pkg.go.dev/github.com/muesli/roff) roff lets you write roff documents in Go ## Tutorial Import the library: ```go import "github.com/muesli/roff" ``` Then start a new roff document and write to it: ```go doc := roff.NewDocument() doc.Heading(1, "Title", "A short description", time.Now()) // a section of text doc.Section("Introduction") doc.Text("Here is a quick introduction to writing roff documents with Go!") // fonts doc.Section("Fonts") doc.Text("This is a text with a bold font: ") doc.TextBold("I am bold!") doc.Paragraph() doc.Text("This is a text with an italic font: ") doc.TextItalic("I am italic!") // indentation doc.Section("Indentation") doc.Text("This block of text is left-aligned to the section.") doc.Indent(4) doc.Text("This block of text is totally indented.") doc.IndentEnd() doc.Text("... left-aligned again!") // lists doc.Section("Lists") doc.Text("A list:") doc.Paragraph() doc.Indent(4) doc.List("First list item") doc.List("Second list item") ``` Fetch the roff document as a string and you're done: ```go fmt.Println(doc.String()) ``` ## Feedback Got some feedback or suggestions? Please open an issue or drop me a note! * [Twitter](https://twitter.com/mueslix) * [The Fediverse](https://mastodon.social/@fribbledom) roff-0.1.0/examples/000077500000000000000000000000001416726136600142765ustar00rootroot00000000000000roff-0.1.0/examples/example.go000066400000000000000000000016271416726136600162660ustar00rootroot00000000000000package main import ( "fmt" "time" "github.com/muesli/roff" ) func main() { doc := roff.NewDocument() doc.Heading(1, "Title", "A short description", time.Now()) // a section of text doc.Section("Introduction") doc.Text("Here is a quick introduction to writing roff documents with Go!") // fonts doc.Section("Fonts") doc.Text("This is a text with a bold font: ") doc.TextBold("I am bold!") doc.Paragraph() doc.Text("This is a text with an italic font: ") doc.TextItalic("I am italic!") // indentation doc.Section("Indentation") doc.Text("This block of text is left-aligned to the section.") doc.Indent(4) doc.Text("This block of text is totally indented.") doc.IndentEnd() doc.Text("... left-aligned & happy again!") // lists doc.Section("Lists") doc.Text("A list:") doc.Paragraph() doc.Indent(4) doc.List("First list item") doc.List("Second list item") fmt.Println(doc.String()) } roff-0.1.0/go.mod000066400000000000000000000000471416726136600135670ustar00rootroot00000000000000module github.com/muesli/roff go 1.17 roff-0.1.0/roff.go000066400000000000000000000071721416726136600137520ustar00rootroot00000000000000package roff import ( "bytes" "fmt" "strings" "time" ) const ( // Title heading (Document structure macro) TitleHeading = `.TH %[1]s %[2]d "%[4]s" "%[3]s" "%[5]s"` // Paragraph macro Paragraph = "\n.PP" // Relative-indent start (Document structure macro) Indent = "\n.RS" // Relative-indent end (Document structure macro) IndentEnd = "\n.RE" // Indented paragraph IndentedParagraph = "\n.IP" // Section heading (Document structure macro) SectionHeading = "\n.SH %s" // Tagged paragraph TaggedParagraph = "\n.TP" // Bold escape Bold = `\fB` // Italic escape Italic = `\fI` // Return to previous font setting PreviousFont = `\fP` ) // Document is a roff document. type Document struct { buffer bytes.Buffer } // NewDocument returns a new roff Document. func NewDocument() *Document { return &Document{} } // write writes the given text to the internal buffer. Following the roff docs, // we prevent empty lines in its output, as that may mysteriously break some // roff renderers. func (tr *Document) writef(format string, args ...interface{}) { if bytes.HasSuffix(tr.buffer.Bytes(), []byte("\n")) && strings.HasPrefix(format, "\n") { // prevent empty lines in output format = strings.TrimPrefix(format, "\n") } fmt.Fprintf(&tr.buffer, format, args...) } func (tr *Document) writelnf(format string, args ...interface{}) { tr.writef(format+"\n", args...) } // Heading writes the title heading of the document. func (tr *Document) Heading(section uint, title, description string, ts time.Time) { tr.writef(TitleHeading, strings.ToUpper(title), section, title, ts.Format("2006-01-02"), description) } // Paragraph starts a new paragraph. func (tr *Document) Paragraph() { tr.writelnf(Paragraph) } // Indent increases the indentation level. func (tr *Document) Indent(n int) { if n >= 0 { tr.writelnf(Indent+" %d", n) } else { tr.writelnf(Indent) } } // IndentEnd decreases the indentation level. func (tr *Document) IndentEnd() { tr.writelnf(IndentEnd) } // TaggedParagraph starts a new tagged paragraph. func (tr *Document) TaggedParagraph(indentation int) { if indentation >= 0 { tr.writelnf(TaggedParagraph+" %d", indentation) } else { tr.writelnf(TaggedParagraph) } } // List writes a list item. func (tr *Document) List(text string) { tr.writelnf(IndentedParagraph+" \\(bu 3\n%s", escapeText(strings.TrimSpace(text))) } // Section writes a section heading. func (tr *Document) Section(text string) { tr.writelnf(SectionHeading, strings.ToUpper(text)) } // EndSection ends the current section. func (tr *Document) EndSection() { tr.writelnf("") } // Text writes text. func (tr *Document) Text(text string) { inList := false for i, s := range strings.Split(text, "\n") { if i > 0 && !inList { // start a new paragraph if we're not in a list tr.Paragraph() } if strings.HasPrefix(s, "*") { // list item if !inList { // start a new indented list if we're not in one tr.Indent(-1) inList = true } tr.List(s[1:]) } else { // regular text if inList { // end the list if we're in one tr.IndentEnd() inList = false } tr.writef(escapeText(s)) } } } // TextBold writes text in bold. func (tr *Document) TextBold(text string) { tr.writef(Bold) tr.Text(text) tr.writef(PreviousFont) } // TextItalic writes text in italic. func (tr *Document) TextItalic(text string) { tr.writef(Italic) tr.Text(text) tr.writef(PreviousFont) } // String returns the roff document as a string. func (tr Document) String() string { return tr.buffer.String() } func escapeText(s string) string { s = strings.ReplaceAll(s, `\`, `\e`) s = strings.ReplaceAll(s, ".", "\\&.") return s } roff-0.1.0/roff_test.go000066400000000000000000000032031416726136600150000ustar00rootroot00000000000000package roff import ( "testing" "time" ) func TestTitleHeading(t *testing.T) { doc := NewDocument() doc.Heading(1, "Title", "A short description", time.Now()) if doc.String() != `.TH TITLE 1 "2022-01-11" "Title" "A short description"` { t.Error("Expected title heading, got:", doc.String()) } } func TestSectionHeading(t *testing.T) { doc := NewDocument() doc.Section("Test") if doc.String() != "\n.SH TEST\n" { t.Error("Expected section heading, got:", []byte(doc.String())) } } func TestText(t *testing.T) { doc := NewDocument() doc.Text("Test") if doc.String() != "Test" { t.Error("Expected text, got:", []byte(doc.String())) } } func TestTextBold(t *testing.T) { doc := NewDocument() doc.TextBold("Test") if doc.String() != `\fBTest\fP` { t.Error("Expected bold text, got:", []byte(doc.String())) } } func TestTextItalic(t *testing.T) { doc := NewDocument() doc.TextItalic("Test") if doc.String() != `\fITest\fP` { t.Error("Expected italic text, got:", []byte(doc.String())) } } func TestParagraph(t *testing.T) { doc := NewDocument() doc.Paragraph() if doc.String() != "\n.PP\n" { t.Error("Expected italic text, got:", []byte(doc.String())) } } func TestIndentation(t *testing.T) { doc := NewDocument() doc.Indent(4) doc.Text("Test") doc.IndentEnd() if doc.String() != "\n.RS 4\nTest\n.RE\n" { t.Error("Expected indentation, got:", []byte(doc.String())) } } func TestList(t *testing.T) { doc := NewDocument() doc.Indent(4) doc.List("First") doc.List("Second") if doc.String() != "\n.RS 4\n.IP \\(bu 3\nFirst\n.IP \\(bu 3\nSecond\n" { t.Error("Expected list, got:", []byte(doc.String())) } }