diff options
| author | Jaewoong Jung <jungjw@google.com> | 2019-03-11 10:54:36 -0700 |
|---|---|---|
| committer | Jaewoong Jung <jungjw@google.com> | 2019-03-11 11:16:31 -0700 |
| commit | 8bc6bf109058174828b7bba8fce131095e7efacf (patch) | |
| tree | 73f6ce99e771c200ae05c6bb9d5116c183dce897 | |
| parent | eaf410fe20ca1e592b5577c76eebcd5b0c70bd8e (diff) | |
| download | android_build_blueprint-8bc6bf109058174828b7bba8fce131095e7efacf.tar.gz android_build_blueprint-8bc6bf109058174828b7bba8fce131095e7efacf.tar.bz2 android_build_blueprint-8bc6bf109058174828b7bba8fce131095e7efacf.zip | |
bpdoc preformatted text improvments
1. Apply preformatted text (<pre>...</pre>) handling logic to module
type text too. It used to be applied to property texts only.
2. Improve <pre> handling logic itself for better readability.
- Insert a blank line before <pre>.
- Prevent from ending <pre> blocks prematurely by checking if an
unindented line isn't just a blank line between indented lines.
Change-Id: Id40fa668d4c6781caf7ed140b2f40784cdeb8c35
| -rw-r--r-- | bootstrap/bpdoc/bpdoc.go | 2 | ||||
| -rw-r--r-- | bootstrap/bpdoc/properties.go | 43 | ||||
| -rw-r--r-- | bootstrap/bpdoc/reader.go | 2 | ||||
| -rw-r--r-- | bootstrap/bpdoc/reader_test.go | 2 |
4 files changed, 26 insertions, 23 deletions
diff --git a/bootstrap/bpdoc/bpdoc.go b/bootstrap/bpdoc/bpdoc.go index ffb2cc0..8ce41cf 100644 --- a/bootstrap/bpdoc/bpdoc.go +++ b/bootstrap/bpdoc/bpdoc.go @@ -36,7 +36,7 @@ type ModuleType struct { PkgPath string // Text is the contents of the comment documenting the module type. - Text string + Text template.HTML // PropertyStructs is a list of PropertyStruct objects that contain information about each // property struct that is used by the module type, containing all properties that are valid diff --git a/bootstrap/bpdoc/properties.go b/bootstrap/bpdoc/properties.go index 1126752..23b1ffd 100644 --- a/bootstrap/bpdoc/properties.go +++ b/bootstrap/bpdoc/properties.go @@ -224,30 +224,11 @@ func structProperties(structType *ast.StructType) (props []Property, err error) typ = fmt.Sprintf("%T", f.Type) } - var html template.HTML - - lines := strings.Split(text, "\n") - preformatted := false - for _, line := range lines { - r, _ := utf8.DecodeRuneInString(line) - indent := unicode.IsSpace(r) - if indent && !preformatted { - html += "<pre>\n" - } else if !indent && preformatted { - html += "</pre>\n" - } - preformatted = indent - html += template.HTML(template.HTMLEscapeString(line)) + "\n" - } - if preformatted { - html += "</pre>\n" - } - props = append(props, Property{ Name: name, Type: typ, Tag: reflect.StructTag(tag), - Text: html, + Text: formatText(text), Properties: innerProps, }) } @@ -279,3 +260,25 @@ func filterPropsByTag(props *[]Property, key, value string, exclude bool) { *props = filtered } + +func formatText(text string) template.HTML { + var html template.HTML + lines := strings.Split(text, "\n") + preformatted := false + for _, line := range lines { + r, _ := utf8.DecodeRuneInString(line) + indent := unicode.IsSpace(r) + if indent && !preformatted { + html += "<pre>\n\n" + preformatted = true + } else if !indent && line != "" && preformatted { + html += "</pre>\n" + preformatted = false + } + html += template.HTML(template.HTMLEscapeString(line)) + "\n" + } + if preformatted { + html += "</pre>\n" + } + return html +} diff --git a/bootstrap/bpdoc/reader.go b/bootstrap/bpdoc/reader.go index 0a77844..a39ee3c 100644 --- a/bootstrap/bpdoc/reader.go +++ b/bootstrap/bpdoc/reader.go @@ -77,7 +77,7 @@ func (r *Reader) ModuleType(name string, factory reflect.Value) (*ModuleType, er return &ModuleType{ Name: name, PkgPath: pkgPath, - Text: text, + Text: formatText(text), }, nil } diff --git a/bootstrap/bpdoc/reader_test.go b/bootstrap/bpdoc/reader_test.go index 8545ac6..b8ff109 100644 --- a/bootstrap/bpdoc/reader_test.go +++ b/bootstrap/bpdoc/reader_test.go @@ -59,7 +59,7 @@ func TestModuleTypeDocs(t *testing.T) { t.Fatal(err) } - if mt.Text != "foo docs.\n" { + if mt.Text != "foo docs.\n\n" { t.Errorf("unexpected docs %q", mt.Text) } |
