Update vendored package heredoc

This commit is contained in:
tcharding 2017-08-21 16:15:54 +10:00
parent 4aaf39a5c0
commit a542bfddb6
4 changed files with 56 additions and 47 deletions

2
Godeps/Godeps.json generated
View File

@ -98,7 +98,7 @@
}, },
{ {
"ImportPath": "github.com/MakeNowJust/heredoc", "ImportPath": "github.com/MakeNowJust/heredoc",
"Rev": "1d91351acdc1cb2f2c995864674b754134b86ca7" "Rev": "bb23615498cded5e105af4ce27de75b089cbe851"
}, },
{ {
"ImportPath": "github.com/Microsoft/go-winio", "ImportPath": "github.com/Microsoft/go-winio",

View File

@ -1,6 +1,6 @@
The MIT License (MIT) The MIT License (MIT)
Copyright (c) 2014 TSUYUSATO Kitsune Copyright (c) 2014-2017 TSUYUSATO Kitsune
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal

View File

@ -1,16 +1,16 @@
#heredoc [![Build Status](https://drone.io/github.com/MakeNowJust/heredoc/status.png)](https://drone.io/github.com/MakeNowJust/heredoc/latest) [![Go Walker](http://gowalker.org/api/v1/badge)](https://gowalker.org/github.com/MakeNowJust/heredoc) # heredoc [![CircleCI](https://circleci.com/gh/MakeNowJust/heredoc.svg?style=svg)](https://circleci.com/gh/MakeNowJust/heredoc) [![Go Walker](http://gowalker.org/api/v1/badge)](https://gowalker.org/github.com/MakeNowJust/heredoc)
##About ## About
Package heredoc provides the here-document with keeping indent. Package heredoc provides the here-document with keeping indent.
##Install ## Install
```console ```console
$ go get github.com/MakeNowJust/heredoc $ go get github.com/MakeNowJust/heredoc
``` ```
##Import ## Import
```go ```go
// usual // usual
@ -19,7 +19,7 @@ import "github.com/MakeNowJust/heredoc"
import . "github.com/MakeNowJust/heredoc/dot" import . "github.com/MakeNowJust/heredoc/dot"
``` ```
##Example ## Example
```go ```go
package main package main
@ -43,11 +43,11 @@ func main() {
} }
``` ```
##API Document ## API Document
- [Go Walker - github.com/MakeNowJust/heredoc](https://gowalker.org/github.com/MakeNowJust/heredoc) - [Go Walker - github.com/MakeNowJust/heredoc](https://gowalker.org/github.com/MakeNowJust/heredoc)
- [Go Walker - github.com/MakeNowJust/heredoc/dot](https://gowalker.org/github.com/MakeNowJust/heredoc/dot) - [Go Walker - github.com/MakeNowJust/heredoc/dot](https://gowalker.org/github.com/MakeNowJust/heredoc/dot)
##License ## License
This software is released under the MIT License, see LICENSE. This software is released under the MIT License, see LICENSE.

View File

@ -1,15 +1,15 @@
// Copyright (c) 2014 TSUYUSATO Kitsune // Copyright (c) 2014-2017 TSUYUSATO Kitsune
// This software is released under the MIT License. // This software is released under the MIT License.
// http://opensource.org/licenses/mit-license.php // http://opensource.org/licenses/mit-license.php
// Package heredoc provides the here-document with keeping indent. // Package heredoc provides creation of here-documents from raw strings.
// //
// Golang supports raw-string syntax. // Golang supports raw-string syntax.
// doc := ` // doc := `
// Foo // Foo
// Bar // Bar
// ` // `
// But raw-string cannot recognize indent. Thus such content is indented string, equivalent to // But raw-string cannot recognize indentation. Thus such content is an indented string, equivalent to
// "\n\tFoo\n\tBar\n" // "\n\tFoo\n\tBar\n"
// I dont't want this! // I dont't want this!
// //
@ -18,7 +18,7 @@
// Foo // Foo
// Bar // Bar
// `) // `)
// It is equivalent to // Is equivalent to
// "Foo\nBar\n" // "Foo\nBar\n"
package heredoc package heredoc
@ -28,11 +28,9 @@ import (
"unicode" "unicode"
) )
// heredoc.Doc retutns unindented string as here-document. const maxInt = int(^uint(0) >> 1)
//
// Process of making here-document: // Doc returns un-indented string as here-document.
// 1. Find most little indent size. (Skip empty lines)
// 2. Remove this indents of lines.
func Doc(raw string) string { func Doc(raw string) string {
skipFirstLine := false skipFirstLine := false
if raw[0] == '\n' { if raw[0] == '\n' {
@ -41,10 +39,18 @@ func Doc(raw string) string {
skipFirstLine = true skipFirstLine = true
} }
minIndentSize := int(^uint(0) >> 1) // Max value of type int
lines := strings.Split(raw, "\n") lines := strings.Split(raw, "\n")
// 1. 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 { for i, line := range lines {
if i == 0 && skipFirstLine { if i == 0 && skipFirstLine {
continue continue
@ -67,23 +73,26 @@ func Doc(raw string) string {
minIndentSize = indentSize minIndentSize = indentSize
} }
} }
return minIndentSize
}
// 2. // 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 { for i, line := range lines {
if i == 0 && skipFirstLine { if i == 0 && skipFirstLine {
continue continue
} }
if len(lines[i]) >= minIndentSize { if len(lines[i]) >= n {
lines[i] = line[minIndentSize:] lines[i] = line[n:]
} }
} }
return lines
return strings.Join(lines, "\n")
} }
// heredoc.Docf returns unindented and formatted string as here-document. // Docf returns unindented and formatted string as here-document.
// This format is same with package fmt's format. // Formatting is done as for fmt.Printf().
func Docf(raw string, args ...interface{}) string { func Docf(raw string, args ...interface{}) string {
return fmt.Sprintf(Doc(raw), args...) return fmt.Sprintf(Doc(raw), args...)
} }