Skip to content

Commit

Permalink
feat: add no-styles flag for html docs
Browse files Browse the repository at this point in the history
If `-no-styles` flag is enabled, HTML generated documentation
will not include built-in CSS styles.
  • Loading branch information
g4s8 committed Dec 24, 2023
1 parent 49a3cac commit 6dd1141
Show file tree
Hide file tree
Showing 13 changed files with 83 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type Config struct {
`plaintext` or `html`.
* `-all` - Generate documentation for all types in the file.
* `-env-prefix` - Environment variable prefix.
* `-no-styles` - Disable built-int CSS styles for HTML format.

## Example

Expand Down
39 changes: 39 additions & 0 deletions _examples/complex-nostyle.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Environment Variables</title>

</head>
<body>
<section>
<article>
<h1>Environment Variables</h1>

<h2>ComplexConfig</h2>
<p>ComplexConfig is an example configuration structure.
It contains a few fields with different types of tags.
It is trying to cover all the possible cases.</p>
<ul>
<li><code>SECRET</code> (from-file) - Secret is a secret value that is read from a file.</li>
<li><code>PASSWORD</code> (from-file, default: <code>/tmp/password</code>) - Password is a password that is read from a file.</li>
<li><code>CERTIFICATE</code> (expand, from-file, default: <code>${CERTIFICATE_FILE}</code>) - Certificate is a certificate that is read from a file.</li>
<li><code>SECRET_KEY</code> (<strong>required</strong>) - Key is a secret key.</li>
<li><code>SECRET_VAL</code> (<strong>required</strong>, non-empty) - SecretVal is a secret value.</li>
<li><code>HOSTS</code> (separated by "<code>:</code>", <strong>required</strong>) - Hosts is a list of hosts.</li>
<li><code>WORDS</code> (comma-separated, from-file, default: <code>one,two,three</code>) - Words is just a list of words.</li>
<li><code>COMMENT</code> (<strong>required</strong>, default: <code>This is a comment.</code>) - Just a comment.</li>

</ul>

<h2>NextConfig</h2>

<ul>
<li><code>MOUNT</code> (<strong>required</strong>) - Mount is a mount point.</li>

</ul>

</article>
</section>
</body>
</html>
1 change: 1 addition & 0 deletions _examples/complex.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package main
//go:generate go run ../ -output complex.md -all
//go:generate go run ../ -output complex.txt -all -format plaintext
//go:generate go run ../ -output x_complex.md -all -env-prefix X_
//go:generate go run ../ -output complex-nostyle.html -format html -all -no-styles
type ComplexConfig struct {
// Secret is a secret value that is read from a file.
Secret string `env:"SECRET,file"`
Expand Down
2 changes: 1 addition & 1 deletion _examples/complex.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="utf-8">
<title>Environment Variables</title>
<style>
<style>
* {
box-sizing: border-box;
}
Expand Down
2 changes: 1 addition & 1 deletion _examples/config.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="utf-8">
<title>Environment Variables</title>
<style>
<style>
* {
box-sizing: border-box;
}
Expand Down
1 change: 1 addition & 0 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@ Options:
`plaintext` or `html`.
- `-all` - Generate documentation for all types in the file.
- `-env-prefix` - Environment variable prefix.
- `-no-styles` - Disable built-int CSS styles for HTML format.
*/
package main
10 changes: 9 additions & 1 deletion generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type generator struct {
all bool // generate documentation for all types in the file
tmpl template
prefix string
noStyles bool
}

type generatorOption func(*generator) error
Expand Down Expand Up @@ -57,6 +58,13 @@ func withPrefix(prefix string) generatorOption {
}
}

func withNoStyles() generatorOption {
return func(g *generator) error {
g.noStyles = true
return nil
}
}

func newGenerator(fileName string, execLine int, opts ...generatorOption) (*generator, error) {
g := &generator{fileName: fileName, execLine: execLine}
for _, opt := range opts {
Expand All @@ -77,7 +85,7 @@ func (g *generator) generate(out io.Writer) error {
return fmt.Errorf("inspect file: %w", err)
}
renderer := templateRenderer(g.tmpl)
rctx := newRenderContext(data, g.prefix)
rctx := newRenderContext(data, g.prefix, g.noStyles)
if err := renderer(rctx, out); err != nil {
return err
}
Expand Down
9 changes: 9 additions & 0 deletions generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ func TestOptions(t *testing.T) {
t.Fatal("expected prefix to be 'prefix'")
}
})
t.Run("WithNoStyles", func(t *testing.T) {
g, err := newGenerator("stub", 1, withNoStyles())
if err != nil {
t.Fatal("new generator error", err)
}
if g.noStyles != true {
t.Fatal("expected noStyles to be true")
}
})
t.Run("empty", func(t *testing.T) {
g, err := newGenerator("stub", 1)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type appConfig struct {
execLine int
all bool
envPrefix string
noStyles bool
}

func (cfg *appConfig) parseFlags(f *flag.FlagSet) error {
Expand All @@ -23,6 +24,7 @@ func (cfg *appConfig) parseFlags(f *flag.FlagSet) error {
f.StringVar(&cfg.formatName, "format", "", "Output format, default `markdown`")
f.BoolVar(&cfg.all, "all", false, "Generate documentation for all types in the file")
f.StringVar(&cfg.envPrefix, "env-prefix", "", "Environment variable prefix")
f.BoolVar(&cfg.noStyles, "no-styles", false, "Disable styles in html output")
if err := f.Parse(os.Args[1:]); err != nil {
return fmt.Errorf("parsing CLI args: %w", err)
}
Expand Down Expand Up @@ -99,6 +101,9 @@ func run(cfg *appConfig) (err error) {
if cfg.envPrefix != "" {
opts = append(opts, withPrefix(cfg.envPrefix))
}
if cfg.noStyles {
opts = append(opts, withNoStyles())
}
gen, err := newGenerator(cfg.inputFileName, cfg.execLine, opts...)
if err != nil {
return fmt.Errorf("creating generator: %w", err)
Expand Down
4 changes: 4 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func TestConfig(t *testing.T) {
"cmd",
"-output", "test.md",
"-type", "test",
"-no-styles",
"-format", "markdown",
"-env-prefix", "TEST_",
"-all",
Expand All @@ -39,6 +40,9 @@ func TestConfig(t *testing.T) {
if !cfg.all {
t.Fatal("Invalid all flag")
}
if !cfg.noStyles {
t.Fatal("Invalid no styles flag")
}

if err := cfg.parseEnv(); err != nil {
t.Fatal("Invalid environment:", err)
Expand Down
4 changes: 3 additions & 1 deletion render.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ type renderItem struct {
type renderContext struct {
Title string
Sections []renderSection
Styles bool
}

func newRenderContext(scopes []*EnvScope, envPrefix string) renderContext {
func newRenderContext(scopes []*EnvScope, envPrefix string, noStyles bool) renderContext {
res := renderContext{
Sections: make([]renderSection, len(scopes)),
Styles: !noStyles,
}
res.Title = "Environment Variables"
for i, scope := range scopes {
Expand Down
9 changes: 7 additions & 2 deletions render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func TestRender(t *testing.T) {
`</html>`))
})
t.Run("sections", func(t *testing.T) {
rc := renderContext{Title: "Sections", Sections: testRenderSections}
rc := renderContext{Title: "Sections", Sections: testRenderSections, Styles: true}
t.Run("markdown", testRenderer(tmplMarkdown, rc,
"# Sections",
"## First",
Expand All @@ -107,6 +107,8 @@ func TestRender(t *testing.T) {
`<head>`,
`<meta charset="utf-8">`,
`<title>Sections</title>`,
`<style>`,
`</style>`,
`</head>`,
`<section>`,
`<article>`,
Expand Down Expand Up @@ -138,11 +140,14 @@ func TestNewRenderContext(t *testing.T) {
},
},
}
rc := newRenderContext(src, "PREFIX_")
rc := newRenderContext(src, "PREFIX_", false)
const title = "Environment Variables"
if rc.Title != title {
t.Errorf("expected title %q, got %q", title, rc.Title)
}
if rc.Styles != true {
t.Errorf("expected styles %v, got %v", true, rc.Styles)
}
if len(rc.Sections) != 1 {
t.Fatalf("expected 1 section, got %d", len(rc.Sections))
}
Expand Down
2 changes: 2 additions & 0 deletions templ/html.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<head>
<meta charset="utf-8">
<title>{{ .Title }}</title>
{{ if .Styles -}}
<style>
* {
box-sizing: border-box;
Expand Down Expand Up @@ -70,6 +71,7 @@ p {
margin-bottom: 16px;
}
</style>
{{- end }}
</head>
<body>
<section>
Expand Down

0 comments on commit 6dd1141

Please sign in to comment.