heredoc

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2016 License: MIT, Apache-2.0 Imports: 3 Imported by: 0

README

#heredoc Build Status Go Walker

##About

Package heredoc provides the here-document with keeping indent.

##Install

$ go get github.com/MakeNowJust/heredoc

##Import

// usual
import "github.com/MakeNowJust/heredoc"
// shortcuts
import . "github.com/MakeNowJust/heredoc/dot"

##Example

package main

import (
	"fmt"
	. "github.com/MakeNowJust/heredoc/dot"
)

func main() {
	fmt.Println(D(`
		Lorem ipsum dolor sit amet, consectetur adipisicing elit,
		sed do eiusmod tempor incididunt ut labore et dolore magna
		aliqua. Ut enim ad minim veniam, ...
	`))
	// Output:
	// Lorem ipsum dolor sit amet, consectetur adipisicing elit,
	// sed do eiusmod tempor incididunt ut labore et dolore magna
	// aliqua. Ut enim ad minim veniam, ...
	//
}

##API Document

##License

This software is released under the MIT License, see LICENSE.

Documentation

Overview

Package heredoc provides the here-document with keeping indent.

Golang supports raw-string syntax.

doc := `
	Foo
	Bar
`

But raw-string cannot recognize indent. Thus such content is indented string, equivalent to

"\n\tFoo\n\tBar\n"

I dont't want this!

However this problem is solved by package heredoc.

doc := heredoc.Doc(`
	Foo
	Bar
`)

It is equivalent to

"Foo\nBar\n"

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Doc

func Doc(raw string) string

heredoc.Doc retutns unindented string as here-document.

Process of making here-document:

  1. Find most little indent size. (Skip empty lines)
  2. Remove this indents of lines.
Example (Lipsum)
package main

import (
	"fmt"

	"github.com/MakeNowJust/heredoc"
)

func main() {
	fmt.Print(heredoc.Doc(`
		Lorem ipsum dolor sit amet, consectetur adipisicing elit,
		sed do eiusmod tempor incididunt ut labore et dolore magna
		aliqua. Ut enim ad minim veniam, ...
	`))
}
Output:

Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, ...
Example (Spec)
package main

import (
	"fmt"

	"github.com/MakeNowJust/heredoc"
)

func main() {
	// Single line string is no change.
	fmt.Println(heredoc.Doc(`It is single line.`))
	// If first line is empty, heredoc.Doc removes first line.
	fmt.Println(heredoc.Doc(`
		It is first line.
		It is second line.`))
	// If last line is empty and more little length than indents,
	// heredoc.Doc removes last line's content.
	fmt.Println(heredoc.Doc(`
		Next is last line.
	`))
	fmt.Println("Previous is last line.")
}
Output:

It is single line.
It is first line.
It is second line.
Next is last line.

Previous is last line.

func Docf

func Docf(raw string, args ...interface{}) string

heredoc.Docf returns unindented and formatted string as here-document. This format is same with package fmt's format.

Example
package main

import (
	"fmt"

	"github.com/MakeNowJust/heredoc"
)

func main() {
	libName := "github.com/MakeNowJust/heredoc"
	author := "TSUYUSATO Kitsune (@MakeNowJust)"
	fmt.Printf(heredoc.Docf(`
		Library Name  : %s
		Author        : %s
		Repository URL: http://%s.git
	`, libName, author, libName))
}
Output:

Library Name  : github.com/MakeNowJust/heredoc
Author        : TSUYUSATO Kitsune (@MakeNowJust)
Repository URL: http://github.com/MakeNowJust/heredoc.git

Types

This section is empty.

Directories

Path Synopsis
Package heredoc_dot is the set of shortcuts for dot import.
Package heredoc_dot is the set of shortcuts for dot import.

Jump to

Keyboard shortcuts

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