You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In code examples (e.g. foo_examples_test.go), leading whitespace is stripped from lines of a string declared with backticks, e.g.:
funcExample() {
foo:=`One TwoThree`fmt.Println(foo)
// Output:// One// Two// Three
}
That will be rendered as follows by godoc:
<divclass="expanded"><pclass="exampleHeading toggleButton">▾ <spanclass="text">Example</span></p><p>Code:</p><preclass="code">foo := `One
Two
Three`
fmt.Println(foo)
<spanclass="comment"></pre><p>Output:</p><preclass="output">One
Two
Three
</pre></div></div>
...ergo the code will appear without the crucial indentation in the string:
foo := `One
Two
Three`
fmt.Println(foo)
This bug is present in godoc and also at godoc.org despite the latter rendering code blocks quite differently.
Answers to the questions:
What version of Go are you using (go version)?
go version go1.7 darwin/amd64
What operating system and processor architecture are you using (go env)?
Looks like, it's because example_htmlFunc() and example_textFunc() try to unident function block by replacing "\n<4 spaces>" with "\n". That replacement ends up removing indentation from backticked-string too.
foo := `One
Two
Three
Four`
fmt.Println(foo)
<span class="comment">
Also there's a side issue that it retains the last <span class="comment">.
JayNakrani
changed the title
Indentation stripped from strings in example code blocks.
x/tools/cmd/godoc: indentation stripped from raw string literals in example blocks
Jan 10, 2017
JayNakrani
added
NeedsFix
The path to resolution is known, but the work has not been done.
and removed
NeedsInvestigation
Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
labels
Jan 10, 2017
godoc formats function examples for text or HTML output by
stripping the surrounding braces and un-indenting by replacing
"\n " with "\n". This modifies the content of string literals,
resulting in misleading examples.
This change introduces a function, replaceLeadingIndentation, which
unindents more carefully. It removes the first level of indentation
only outside of string literals. For plain text output, it adds custom
indentation at the beginning of every line including string literals.
Fixesgolang/go#18446
Change-Id: I52a7f5756bdb69c8a66f031452dd35eab947ec1f
Reviewed-on: https://go-review.googlesource.com/36544
Run-TryBot: Jay Conrod <jayconrod@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
Sign up for freeto subscribe to this conversation on GitHub.
Already have an account?
Sign in.
In code examples (e.g.
foo_examples_test.go
), leading whitespace is stripped from lines of a string declared with backticks, e.g.:That will be rendered as follows by godoc:
...ergo the code will appear without the crucial indentation in the string:
This bug is present in
godoc
and also at godoc.org despite the latter rendering code blocks quite differently.Answers to the questions:
What version of Go are you using (
go version
)?What operating system and processor architecture are you using (
go env
)?What did you do?
Used a multi-line string declared with backticks in a code example for my
project frostedmd.
The string has meaningful indentation describing a code block in Markdown,
and the bug (or an identical bug) can be seen in action on Godog.org here:
https://godoc.org/github.com/biztos/frostedmd#ex-package
What did you expect to see?
The string correctly indented in the code block of the example documentation.
What did you see instead?
The string with leading whitespace stripped from indented lines, thus making
the example invalid.
The text was updated successfully, but these errors were encountered: