pax_global_header00006660000000000000000000000064135276561370014531gustar00rootroot0000000000000052 comment=bbd9af33722b79a933ef99c7c94833065e2d6c20 heredoc-1.0.0/000077500000000000000000000000001352765613700131405ustar00rootroot00000000000000heredoc-1.0.0/.circleci/000077500000000000000000000000001352765613700147735ustar00rootroot00000000000000heredoc-1.0.0/.circleci/config.yml000066400000000000000000000003051352765613700167610ustar00rootroot00000000000000version: 2.0 jobs: build: docker: - image: circleci/golang:1.12 steps: - checkout - run: command: | export GO11MODULES=on go test -v heredoc-1.0.0/LICENSE000066400000000000000000000021011352765613700141370ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2014-2019 TSUYUSATO Kitsune 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. heredoc-1.0.0/README.md000066400000000000000000000022161352765613700144200ustar00rootroot00000000000000# heredoc [![Build Status](https://circleci.com/gh/MakeNowJust/heredoc.svg?style=svg)](https://circleci.com/gh/MakeNowJust/heredoc) [![GoDoc](https://godoc.org/github.com/MakeNowJusti/heredoc?status.svg)](https://godoc.org/github.com/MakeNowJust/heredoc) ## About Package heredoc provides the here-document with keeping indent. ## Install ```console $ go get github.com/MakeNowJust/heredoc ``` ## Import ```go // usual import "github.com/MakeNowJust/heredoc" ``` ## Example ```go package main import ( "fmt" "github.com/MakeNowJust/heredoc" ) func main() { fmt.Println(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, ... // } ``` ## API Document - [heredoc - GoDoc](https://godoc.org/github.com/MakeNowJust/heredoc) ## License This software is released under the MIT License, see LICENSE. heredoc-1.0.0/dot/000077500000000000000000000000001352765613700137265ustar00rootroot00000000000000heredoc-1.0.0/dot/dot.go000066400000000000000000000016511352765613700150460ustar00rootroot00000000000000// Copyright (c) 2014-2019 TSUYUSATO Kitsune // This software is released under the MIT License. // http://opensource.org/licenses/mit-license.php // Package heredoc_dot is the set of shortcuts for dot import. // // It is useful for using in test, but I don't recommend to use this in // production code. // See https://github.com/golang/lint/issues/179. // // For example: // // package main // // import ( // "fmt" // "runtime" // . "github.com/MakeNowJust/heredoc/dot" // ) // // func main() { // fmt.Printf(D(` // GOROOT: %s // GOARCH: %s // GOOS : %s // `), runtime.GOROOT(), runtime.GOARCH, runtime.GOOS) // } package heredoc_dot import "github.com/MakeNowJust/heredoc" // Shortcut heredoc.Doc. func D(raw string) string { return heredoc.Doc(raw) } // Shortcut heredoc.Docf. func Df(raw string, args ...interface{}) string { return heredoc.Docf(raw, args...) } heredoc-1.0.0/example_test.go000066400000000000000000000031421352765613700161610ustar00rootroot00000000000000// Copyright (c) 2014-2019 TSUYUSATO Kitsune // This software is released under the MIT License. // http://opensource.org/licenses/mit-license.php package heredoc_test import ( "fmt" ) import "github.com/MakeNowJust/heredoc" func ExampleDoc_lipsum() { 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, ... // } func ExampleDoc_spec() { // 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 ExampleDocf() { 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 } heredoc-1.0.0/go.mod000066400000000000000000000000571352765613700142500ustar00rootroot00000000000000module github.com/MakeNowJust/heredoc go 1.12 heredoc-1.0.0/heredoc.go000066400000000000000000000043631352765613700151060ustar00rootroot00000000000000// Copyright (c) 2014-2019 TSUYUSATO Kitsune // This software is released under the MIT License. // http://opensource.org/licenses/mit-license.php // Package heredoc provides creation of here-documents from raw strings. // // Golang supports raw-string syntax. // // doc := ` // Foo // Bar // ` // // But raw-string cannot recognize indentation. Thus such content is an 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 // `) // // Is equivalent to // // "Foo\nBar\n" package heredoc import ( "fmt" "strings" "unicode" ) const maxInt = int(^uint(0) >> 1) // Doc returns un-indented string as here-document. func Doc(raw string) string { skipFirstLine := false if len(raw) > 0 && raw[0] == '\n' { raw = raw[1:] } else { skipFirstLine = true } lines := strings.Split(raw, "\n") minIndentSize := getMinIndent(lines, skipFirstLine) lines = removeIndentation(lines, minIndentSize, skipFirstLine) return strings.Join(lines, "\n") } // getMinIndent calculates the minimum indentation in lines, excluding empty lines. func getMinIndent(lines []string, skipFirstLine bool) int { minIndentSize := maxInt for i, line := range lines { if i == 0 && skipFirstLine { continue } indentSize := 0 for _, r := range []rune(line) { if unicode.IsSpace(r) { indentSize += 1 } else { break } } if len(line) == indentSize { if i == len(lines)-1 && indentSize < minIndentSize { lines[i] = "" } } else if indentSize < minIndentSize { minIndentSize = indentSize } } return minIndentSize } // removeIndentation removes n characters from the front of each line in lines. // Skips first line if skipFirstLine is true, skips empty lines. func removeIndentation(lines []string, n int, skipFirstLine bool) []string { for i, line := range lines { if i == 0 && skipFirstLine { continue } if len(lines[i]) >= n { lines[i] = line[n:] } } return lines } // Docf returns unindented and formatted string as here-document. // Formatting is done as for fmt.Printf(). func Docf(raw string, args ...interface{}) string { return fmt.Sprintf(Doc(raw), args...) } heredoc-1.0.0/heredoc_test.go000066400000000000000000000017661352765613700161510ustar00rootroot00000000000000// Copyright (c) 2014-2019 TSUYUSATO Kitsune // This software is released under the MIT License. // http://opensource.org/licenses/mit-license.php package heredoc import ( "testing" ) type testCase struct { raw, expect string } var tests = []testCase{ {"", ""}, {` Foo Bar `, "Foo\nBar\n"}, {`Foo Bar`, "Foo\nBar"}, {`Foo Bar `, "Foo\n\t\nBar\n"}, // Second line contains two tabs. {` Foo Bar Hoge `, "Foo\n\tBar\n\t\tHoge\n\t\t\t"}, {`Foo Bar`, "Foo Bar"}, { ` Foo Bar `, "Foo\nBar\n"}, } func TestDoc(t *testing.T) { for i, test := range tests { result := Doc(test.raw) if result != test.expect { t.Errorf("tests[%d] failed: expected=> %#v, result=> %#v", i, test.expect, result) } } } func TestDocf(t *testing.T) { tc := ` int: %3d string: %s ` i := 42 s := "Hello" expect := "int: 42\nstring: Hello\n" result := Docf(tc, i, s) if result != expect { t.Errorf("test failed: expected=> %#v, result=> %#v", expect, result) } }