pax_global_header00006660000000000000000000000064134144752310014516gustar00rootroot0000000000000052 comment=8478954c3bc893cf36c5ee7c822266b993a3b3ee dedent-1.1.0/000077500000000000000000000000001341447523100127605ustar00rootroot00000000000000dedent-1.1.0/.travis.yml000066400000000000000000000001351341447523100150700ustar00rootroot00000000000000language: go go: - "1.6" - "1.7" - "1.8" - "1.9" - "1.10" - "1.11" sudo: false dedent-1.1.0/LICENSE000066400000000000000000000020721341447523100137660ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2018 Peter Lithammer 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. dedent-1.1.0/README.md000066400000000000000000000025361341447523100142450ustar00rootroot00000000000000# Dedent [![Build Status](https://travis-ci.org/lithammer/dedent.svg?branch=master)](https://travis-ci.org/lithammer/dedent) [![Godoc](https://img.shields.io/badge/godoc-reference-blue.svg?style=flat)](https://godoc.org/github.com/lithammer/dedent) Removes common leading whitespace from multiline strings. Inspired by [`textwrap.dedent`](https://docs.python.org/3/library/textwrap.html#textwrap.dedent) in Python. ## Usage / example Imagine the following snippet that prints a multiline string. You want the indentation to both look nice in the code as well as in the actual output. ```go package main import ( "fmt" "github.com/lithammer/dedent" ) func main() { s := ` Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur justo tellus, facilisis nec efficitur dictum, fermentum vitae ligula. Sed eu convallis sapien.` fmt.Println(Dedent(s)) fmt.Println("-------------") fmt.Println(s) } ``` To illustrate the difference, here's the output: ```bash $ go run main.go Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur justo tellus, facilisis nec efficitur dictum, fermentum vitae ligula. Sed eu convallis sapien. ------------- Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur justo tellus, facilisis nec efficitur dictum, fermentum vitae ligula. Sed eu convallis sapien. ``` ## License MIT dedent-1.1.0/dedent.go000066400000000000000000000024651341447523100145610ustar00rootroot00000000000000package dedent import ( "regexp" "strings" ) var ( whitespaceOnly = regexp.MustCompile("(?m)^[ \t]+$") leadingWhitespace = regexp.MustCompile("(?m)(^[ \t]*)(?:[^ \t\n])") ) // Dedent removes any common leading whitespace from every line in text. // // This can be used to make multiline strings to line up with the left edge of // the display, while still presenting them in the source code in indented // form. func Dedent(text string) string { var margin string text = whitespaceOnly.ReplaceAllString(text, "") indents := leadingWhitespace.FindAllStringSubmatch(text, -1) // Look for the longest leading string of spaces and tabs common to all // lines. for i, indent := range indents { if i == 0 { margin = indent[1] } else if strings.HasPrefix(indent[1], margin) { // Current line more deeply indented than previous winner: // no change (previous winner is still on top). continue } else if strings.HasPrefix(margin, indent[1]) { // Current line consistent with and no deeper than previous winner: // it's the new winner. margin = indent[1] } else { // Current line and previous winner have no common whitespace: // there is no margin. margin = "" break } } if margin != "" { text = regexp.MustCompile("(?m)^"+margin).ReplaceAllString(text, "") } return text } dedent-1.1.0/dedent_test.go000066400000000000000000000102651341447523100156150ustar00rootroot00000000000000package dedent import ( "fmt" "testing" ) const errorMsg = "\nexpected %q\ngot %q" type dedentTest struct { text, expect string } func TestDedentNoMargin(t *testing.T) { texts := []string{ // No lines indented "Hello there.\nHow are you?\nOh good, I'm glad.", // Similar with a blank line "Hello there.\n\nBoo!", // Some lines indented, but overall margin is still zero "Hello there.\n This is indented.", // Again, add a blank line. "Hello there.\n\n Boo!\n", } for _, text := range texts { if text != Dedent(text) { t.Errorf(errorMsg, text, Dedent(text)) } } } func TestDedentEven(t *testing.T) { texts := []dedentTest{ { // All lines indented by two spaces text: " Hello there.\n How are ya?\n Oh good.", expect: "Hello there.\nHow are ya?\nOh good.", }, { // Same, with blank lines text: " Hello there.\n\n How are ya?\n Oh good.\n", expect: "Hello there.\n\nHow are ya?\nOh good.\n", }, { // Now indent one of the blank lines text: " Hello there.\n \n How are ya?\n Oh good.\n", expect: "Hello there.\n\nHow are ya?\nOh good.\n", }, } for _, text := range texts { if text.expect != Dedent(text.text) { t.Errorf(errorMsg, text.expect, Dedent(text.text)) } } } func TestDedentUneven(t *testing.T) { texts := []dedentTest{ { // Lines indented unevenly text: ` def foo(): while 1: return foo `, expect: ` def foo(): while 1: return foo `, }, { // Uneven indentation with a blank line text: " Foo\n Bar\n\n Baz\n", expect: "Foo\n Bar\n\n Baz\n", }, { // Uneven indentation with a whitespace-only line text: " Foo\n Bar\n \n Baz\n", expect: "Foo\n Bar\n\n Baz\n", }, } for _, text := range texts { if text.expect != Dedent(text.text) { t.Errorf(errorMsg, text.expect, Dedent(text.text)) } } } // Dedent() should not mangle internal tabs. func TestDedentPreserveInternalTabs(t *testing.T) { text := " hello\tthere\n how are\tyou?" expect := "hello\tthere\nhow are\tyou?" if expect != Dedent(text) { t.Errorf(errorMsg, expect, Dedent(text)) } // Make sure that it preserves tabs when it's not making any changes at all if expect != Dedent(expect) { t.Errorf(errorMsg, expect, Dedent(expect)) } } // Dedent() should not mangle tabs in the margin (i.e. tabs and spaces both // count as margin, but are *not* considered equivalent). func TestDedentPreserveMarginTabs(t *testing.T) { texts := []string{ " hello there\n\thow are you?", // Same effect even if we have 8 spaces " hello there\n\thow are you?", } for _, text := range texts { d := Dedent(text) if text != d { t.Errorf(errorMsg, text, d) } } texts2 := []dedentTest{ { // Dedent() only removes whitespace that can be uniformly removed! text: "\thello there\n\thow are you?", expect: "hello there\nhow are you?", }, { text: " \thello there\n \thow are you?", expect: "hello there\nhow are you?", }, { text: " \t hello there\n \t how are you?", expect: "hello there\nhow are you?", }, { text: " \thello there\n \t how are you?", expect: "hello there\n how are you?", }, } for _, text := range texts2 { if text.expect != Dedent(text.text) { t.Errorf(errorMsg, text.expect, Dedent(text.text)) } } } func ExampleDedent() { s := ` Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur justo tellus, facilisis nec efficitur dictum, fermentum vitae ligula. Sed eu convallis sapien.` fmt.Println(Dedent(s)) fmt.Println("-------------") fmt.Println(s) // Output: // Lorem ipsum dolor sit amet, // consectetur adipiscing elit. // Curabitur justo tellus, facilisis nec efficitur dictum, // fermentum vitae ligula. Sed eu convallis sapien. // ------------- // // Lorem ipsum dolor sit amet, // consectetur adipiscing elit. // Curabitur justo tellus, facilisis nec efficitur dictum, // fermentum vitae ligula. Sed eu convallis sapien. } func BenchmarkDedent(b *testing.B) { for i := 0; i < b.N; i++ { Dedent(`Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur justo tellus, facilisis nec efficitur dictum, fermentum vitae ligula. Sed eu convallis sapien.`) } } dedent-1.1.0/go.mod000066400000000000000000000000431341447523100140630ustar00rootroot00000000000000module github.com/lithammer/dedent