Skip to content

Commit 0458506

Browse files
authored
Merge pull request #122 from kian99/minor-qol-tweaks-v3
#122 # Description This PR recreates #120. This PR makes some minor QoL improvements: 1. (First commit) Links to a different section of text in markdown should use - instead of _ for links with spaces. If a command has subcommands, the generated markdown currently resemble `#command_subcommand` and wouldn't properly navigate, instead `#command-subcommand` works. 2. (First commit) There is no line break between the end of the index file and the markdown separator `----` which causes issues when converting the markdown to other formats. 3. (Second commit) Always print command usage. The usage is only printed if a command has arguments, commonly empty for `list` style commands. But these commands should also have a usage section in the docs.
2 parents f6bfe6c + eb95f3f commit 0458506

File tree

4 files changed

+88
-11
lines changed

4 files changed

+88
-11
lines changed

documentation.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ func (c *documentationCommand) writeIndex(w io.Writer) error {
328328
}
329329
// TODO: handle subcommands ??
330330
}
331-
_, err = fmt.Fprintf(w, "---\n\n")
331+
_, err = fmt.Fprintf(w, "\n---\n\n")
332332
return err
333333
}
334334

@@ -380,7 +380,7 @@ func (c *documentationCommand) formatCommand(ref commandReference, title bool, c
380380
return fmt.Sprintf("%s%s", prefix, target)
381381
},
382382
LinkForSubcommand: func(s string) string {
383-
return c.linkForCommand(strings.Join(append(commandSeq[1:], s), "_"))
383+
return c.linkForCommand(strings.Join(append(commandSeq[1:], s), "-"))
384384
},
385385
})
386386
return buf.String()

markdown.go

+7-9
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,13 @@ func PrintMarkdown(w io.Writer, cmd InfoCommand, opts MarkdownOptions) error {
7373
fmt.Fprintln(&doc)
7474

7575
// Usage
76-
if strings.TrimSpace(info.Args) != "" {
77-
fmt.Fprintln(&doc, "## Usage")
78-
fmt.Fprintf(&doc, "```")
79-
fmt.Fprint(&doc, opts.UsagePrefix)
80-
fmt.Fprintf(&doc, "%s [%ss] %s", info.Name, getFlagsName(info.FlagKnownAs), info.Args)
81-
fmt.Fprintf(&doc, "```")
82-
fmt.Fprintln(&doc)
83-
fmt.Fprintln(&doc)
84-
}
76+
fmt.Fprintln(&doc, "## Usage")
77+
fmt.Fprintf(&doc, "```")
78+
fmt.Fprint(&doc, opts.UsagePrefix)
79+
fmt.Fprintf(&doc, "%s [%ss] %s", info.Name, getFlagsName(info.FlagKnownAs), info.Args)
80+
fmt.Fprintf(&doc, "```")
81+
fmt.Fprintln(&doc)
82+
fmt.Fprintln(&doc)
8583

8684
// Options
8785
printFlags(&doc, cmd)

markdown_test.go

+56
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,59 @@ func (*markdownSuite) TestOutput(c *gc.C) {
105105
c.Assert(err, jc.ErrorIsNil)
106106
c.Check(buf.String(), gc.Equals, string(expected))
107107
}
108+
109+
// TestOutputWithoutArgs tests that the output of the PrintMarkdown function is
110+
// correct when a command does not need arguments, e.g. list commands.
111+
func (*markdownSuite) TestOutputWithoutArgs(c *gc.C) {
112+
seeAlso := []string{"add-cloud", "update-cloud", "remove-cloud", "update-credential"}
113+
subcommands := map[string]string{
114+
"foo": "foo the bar baz",
115+
"bar": "bar the baz foo",
116+
"baz": "baz the foo bar",
117+
}
118+
119+
command := &docTestCommand{
120+
info: &cmd.Info{
121+
Name: "clouds",
122+
Args: "", //Empty args should still result in a usage field.
123+
Purpose: "List clouds.",
124+
Doc: "details for clouds...",
125+
Examples: "examples for clouds...",
126+
SeeAlso: seeAlso,
127+
Aliases: []string{"list-clouds"},
128+
Subcommands: subcommands,
129+
},
130+
}
131+
132+
// These functions verify the provided argument is in the expected set.
133+
linkForCommand := func(s string) string {
134+
for _, cmd := range seeAlso {
135+
if cmd == s {
136+
return "https://docs.com/" + cmd
137+
}
138+
}
139+
c.Fatalf("linkForCommand called with unexpected command %q", s)
140+
return ""
141+
}
142+
143+
linkForSubcommand := func(s string) string {
144+
_, ok := subcommands[s]
145+
if !ok {
146+
c.Fatalf("linkForSubcommand called with unexpected subcommand %q", s)
147+
}
148+
return "https://docs.com/clouds/" + s
149+
}
150+
151+
expected, err := os.ReadFile("testdata/list-clouds.md")
152+
c.Assert(err, jc.ErrorIsNil)
153+
154+
var buf bytes.Buffer
155+
err = cmd.PrintMarkdown(&buf, command, cmd.MarkdownOptions{
156+
Title: `Command "juju clouds"`,
157+
UsagePrefix: "juju ",
158+
LinkForCommand: linkForCommand,
159+
LinkForSubcommand: linkForSubcommand,
160+
})
161+
c.Assert(err, jc.ErrorIsNil)
162+
c.Check(buf.String(), gc.Equals, string(expected))
163+
}

testdata/list-clouds.md

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Command "juju clouds"
2+
3+
> See also: [add-cloud](https://docs.com/add-cloud), [update-cloud](https://docs.com/update-cloud), [remove-cloud](https://docs.com/remove-cloud), [update-credential](https://docs.com/update-credential)
4+
5+
**Aliases:** list-clouds
6+
7+
## Summary
8+
List clouds.
9+
10+
## Usage
11+
```juju clouds [options] ```
12+
13+
## Examples
14+
examples for clouds...
15+
16+
## Details
17+
details for clouds...
18+
19+
## Subcommands
20+
- [bar](https://docs.com/clouds/bar)
21+
- [baz](https://docs.com/clouds/baz)
22+
- [foo](https://docs.com/clouds/foo)
23+

0 commit comments

Comments
 (0)