linejoin

package module
v0.0.0-...-b25f0f7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 26, 2013 License: MIT Imports: 2 Imported by: 0

README

linejoin is a go package that implements a write filter (linejoin.Writer) that joins lines ending with a continuation token.

It was designed to allow writing templates without having to cram things together to avoid extra newlines in the output; for example, you can do this

{{if .Foo \}}
{{range .Bar \}}
int {{.X}} = {{.Y}};
{{end \}}
{{end \}}

instead of

{{if .Foo}}{{range .Bar}}
int {{.X}} = {{.Y}};{{end}}{{end}}

See the package docs for examples.

Documentation

Overview

Package linejoin implements a write filter (linejoin.Writer) that joins lines ending with a continuation token.

It was designed to be used with templates, to allow a more natural template layout that does not add too much whitespace to the output.

Example (Buffer)
package main

import (
	"bitbucket.org/edmccard/linejoin"
	"bytes"
	"io"
	"os"
	"text/template"
)

// This example demonstrates using line-joining by preprocessing a template
// through a buffer.
const example2 = `{{/*Example*/ \}}
{{template "Foo" \}}
{{template "Bar" \}}
{{define "Foo" \}}
Only two lines
{{end \}}
{{define "Bar" \}}
of text.
{{end \}}
`

func main() {
	b := new(bytes.Buffer)
	filter := linejoin.NewWriter(b, ` \}}`, "}}")
	io.WriteString(filter, example2)
	t := template.Must(template.New("Example").Parse(b.String()))
	t.Execute(os.Stdout, nil)

}
Output:

Only two lines
of text.
Example (Template)
package main

import (
	"bitbucket.org/edmccard/linejoin"
	"os"
	"text/template"
)

// This example demonstrates using line-joining directly from a template,
// by executing the template to a linejoin.Writer.
const example = `##
{{template "Foo"}}##
{{template "Bar"}}##
{{define "Foo"}}##
Only two lines
{{end}}##
{{define "Bar"}}##
of text.
{{end}}##
`

func main() {
	t := template.Must(template.New("Example").Parse(example))

	// Create the filter.
	filter := linejoin.NewWriter(os.Stdout, "##", "")
	t.Execute(filter, nil)

}
Output:

Only two lines
of text.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

A Writer is a filter that joins lines, by deleting any newlines which follow a given continuation token.

The Writer buffers input internally, so clients should call Flush after the last call to Write.

func NewWriter

func NewWriter(output io.Writer, token string, subst string) *Writer

NewWriter returns a new linejoin.Writer.

output	the filter output
token	the continuation token
subst	the replacement token

func (*Writer) Flush

func (w *Writer) Flush() (err error)

Flush should be called after the last call to Write.

func (*Writer) Write

func (w *Writer) Write(p []byte) (n int, err error)

Write writes p to the writer w, which filters the output by removing any newlines ("\r", "\r\n", or "\n") which are preceded by w's continuation token (which is then replaced with w's replacement token.)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL