cmd docs: fix markdown linter

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
Andrea Luzzardi 2021-06-08 17:49:16 -07:00
parent f54a378d44
commit de3ffe0610
4 changed files with 33 additions and 22 deletions

View File

@ -122,8 +122,21 @@ func FormatValue(val *compiler.Value) string {
return strings.ReplaceAll(valStr, "\n", "\\n") return strings.ReplaceAll(valStr, "\n", "\\n")
} }
// ValueDocString returns the value doc from the comment lines // ValueDocFull returns the full doc of the value
func ValueDocString(val *compiler.Value) string { func ValueDocFull(val *compiler.Value) string {
docs := []string{}
for _, c := range val.Doc() {
docs = append(docs, c.Text())
}
doc := strings.TrimSpace(strings.Join(docs, "\n"))
if len(doc) == 0 {
return "-"
}
return doc
}
// ValueDocOneLine returns the value doc as a single line
func ValueDocOneLine(val *compiler.Value) string {
docs := []string{} docs := []string{}
for _, c := range val.Doc() { for _, c := range val.Doc() {
docs = append(docs, strings.TrimSpace(c.Text())) docs = append(docs, strings.TrimSpace(c.Text()))

View File

@ -107,7 +107,7 @@ func init() {
} }
func mdEscape(s string) string { func mdEscape(s string) string {
escape := []string{"|", "<", ">"} escape := []string{"<", ">"}
for _, c := range escape { for _, c := range escape {
s = strings.ReplaceAll(s, c, `\`+c) s = strings.ReplaceAll(s, c, `\`+c)
} }
@ -151,7 +151,7 @@ func loadCode(packageName string) (*compiler.Value, error) {
func printValuesText(iw io.Writer, libName string, values []*compiler.Value) { func printValuesText(iw io.Writer, libName string, values []*compiler.Value) {
w := tabwriter.NewWriter(iw, 0, 4, len(textPadding), ' ', 0) w := tabwriter.NewWriter(iw, 0, 4, len(textPadding), ' ', 0)
for _, i := range values { for _, i := range values {
docStr := terminalTrim(common.ValueDocString(i)) docStr := terminalTrim(common.ValueDocOneLine(i))
fmt.Fprintf(w, "\t\t%s\t%s\t%s\n", fmt.Fprintf(w, "\t\t%s\t%s\t%s\n",
formatLabel(libName, i), common.FormatValue(i), docStr) formatLabel(libName, i), common.FormatValue(i), docStr)
} }
@ -167,9 +167,8 @@ func printValuesMarkdown(iw io.Writer, libName string, values []*compiler.Value)
fmt.Fprintf(w, "|*%s*\t|``%s``\t|%s\t|\n", fmt.Fprintf(w, "|*%s*\t|``%s``\t|%s\t|\n",
formatLabel(libName, i), formatLabel(libName, i),
mdEscape(common.FormatValue(i)), mdEscape(common.FormatValue(i)),
mdEscape(common.ValueDocString(i))) mdEscape(common.ValueDocOneLine(i)))
} }
fmt.Fprintln(w)
w.Flush() w.Flush()
} }
@ -181,7 +180,7 @@ func valuesToJSON(libName string, values []*compiler.Value) []ValueJSON {
v := ValueJSON{} v := ValueJSON{}
v.Name = formatLabel(libName, i) v.Name = formatLabel(libName, i)
v.Type = common.FormatValue(i) v.Type = common.FormatValue(i)
v.Description = common.ValueDocString(i) v.Description = common.ValueDocOneLine(i)
val = append(val, v) val = append(val, v)
} }
@ -201,18 +200,17 @@ func PrintDoc(ctx context.Context, w io.Writer, packageName string, val *compile
switch format { switch format {
case textFormat: case textFormat:
fmt.Fprintf(w, "Package %s\n", packageName) fmt.Fprintf(w, "Package %s\n", packageName)
fmt.Fprintf(w, "\n%s\n", common.ValueDocString(val)) fmt.Fprintf(w, "\n%s\n", common.ValueDocFull(val))
case markdownFormat: case markdownFormat:
fmt.Fprintf(w, "## Package %s\n", mdEscape(packageName)) fmt.Fprintf(w, "# Package %s\n", mdEscape(packageName))
comment := common.ValueDocString(val) comment := common.ValueDocFull(val)
if comment == "-" { if comment == "-" {
fmt.Println()
break break
} }
fmt.Fprintf(w, "\n%s\n\n", mdEscape(comment)) fmt.Fprintf(w, "\n%s\n", mdEscape(comment))
case jsonFormat: case jsonFormat:
packageJSON.Name = packageName packageJSON.Name = packageName
comment := common.ValueDocString(val) comment := common.ValueDocFull(val)
if comment != "-" { if comment != "-" {
packageJSON.Description = comment packageJSON.Description = comment
} }
@ -235,18 +233,18 @@ func PrintDoc(ctx context.Context, w io.Writer, packageName string, val *compile
} }
// Field Name + Description // Field Name + Description
comment := common.ValueDocString(v) comment := common.ValueDocOneLine(v)
switch format { switch format {
case textFormat: case textFormat:
fmt.Fprintf(w, "\n%s\n\n%s%s\n", name, textPadding, comment) fmt.Fprintf(w, "\n%s\n\n%s%s\n", name, textPadding, comment)
case markdownFormat: case markdownFormat:
fmt.Fprintf(w, "### %s\n\n", name) fmt.Fprintf(w, "\n## %s\n\n", name)
if comment != "-" { if comment != "-" {
fmt.Fprintf(w, "%s\n\n", mdEscape(comment)) fmt.Fprintf(w, "%s\n\n", mdEscape(comment))
} }
case jsonFormat: case jsonFormat:
fieldJSON.Name = name fieldJSON.Name = name
comment := common.ValueDocString(val) comment := common.ValueDocOneLine(val)
if comment != "-" { if comment != "-" {
fieldJSON.Description = comment fieldJSON.Description = comment
} }
@ -263,9 +261,9 @@ func PrintDoc(ctx context.Context, w io.Writer, packageName string, val *compile
fmt.Fprintf(w, "\n%sInputs:\n", textPadding) fmt.Fprintf(w, "\n%sInputs:\n", textPadding)
printValuesText(w, name, inp) printValuesText(w, name, inp)
case markdownFormat: case markdownFormat:
fmt.Fprintf(w, "#### %s Inputs\n\n", mdEscape(name)) fmt.Fprintf(w, "### %s Inputs\n\n", mdEscape(name))
if len(inp) == 0 { if len(inp) == 0 {
fmt.Fprintf(w, "_No input._\n\n") fmt.Fprintf(w, "_No input._\n")
break break
} }
printValuesMarkdown(w, name, inp) printValuesMarkdown(w, name, inp)
@ -284,9 +282,9 @@ func PrintDoc(ctx context.Context, w io.Writer, packageName string, val *compile
fmt.Fprintf(w, "\n%sOutputs:\n", textPadding) fmt.Fprintf(w, "\n%sOutputs:\n", textPadding)
printValuesText(w, name, out) printValuesText(w, name, out)
case markdownFormat: case markdownFormat:
fmt.Fprintf(w, "#### %s Outputs\n\n", mdEscape(name)) fmt.Fprintf(w, "\n### %s Outputs\n\n", mdEscape(name))
if len(out) == 0 { if len(out) == 0 {
fmt.Fprintf(w, "_No output._\n\n") fmt.Fprintf(w, "_No output._\n")
break break
} }
printValuesMarkdown(w, name, out) printValuesMarkdown(w, name, out)

View File

@ -69,7 +69,7 @@ var listCmd = &cobra.Command{
inp.Path(), inp.Path(),
common.FormatValue(inp), common.FormatValue(inp),
isUserSet(st, inp), isUserSet(st, inp),
common.ValueDocString(inp), common.ValueDocOneLine(inp),
) )
} }

View File

@ -72,7 +72,7 @@ func ListOutputs(ctx context.Context, st *state.State, isTTY bool) {
fmt.Fprintf(w, "%s\t%s\t%s\n", fmt.Fprintf(w, "%s\t%s\t%s\n",
out.Path(), out.Path(),
common.FormatValue(out), common.FormatValue(out),
common.ValueDocString(out), common.ValueDocOneLine(out),
) )
} }