gitee.com/mirrors/Hugo-Go@v0.47.1/commands/gendoc.go (about)

     1  // Copyright 2016 The Hugo Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package commands
    15  
    16  import (
    17  	"fmt"
    18  	"path"
    19  	"path/filepath"
    20  	"strings"
    21  	"time"
    22  
    23  	"github.com/gohugoio/hugo/helpers"
    24  	"github.com/gohugoio/hugo/hugofs"
    25  	"github.com/spf13/cobra"
    26  	"github.com/spf13/cobra/doc"
    27  	jww "github.com/spf13/jwalterweatherman"
    28  )
    29  
    30  var _ cmder = (*genDocCmd)(nil)
    31  
    32  type genDocCmd struct {
    33  	gendocdir string
    34  	*baseCmd
    35  }
    36  
    37  func newGenDocCmd() *genDocCmd {
    38  	const gendocFrontmatterTemplate = `---
    39  date: %s
    40  title: "%s"
    41  slug: %s
    42  url: %s
    43  ---
    44  `
    45  
    46  	cc := &genDocCmd{}
    47  
    48  	cc.baseCmd = newBaseCmd(&cobra.Command{
    49  		Use:   "doc",
    50  		Short: "Generate Markdown documentation for the Hugo CLI.",
    51  		Long: `Generate Markdown documentation for the Hugo CLI.
    52  
    53  This command is, mostly, used to create up-to-date documentation
    54  of Hugo's command-line interface for http://gohugo.io/.
    55  
    56  It creates one Markdown file per command with front matter suitable
    57  for rendering in Hugo.`,
    58  
    59  		RunE: func(cmd *cobra.Command, args []string) error {
    60  			if !strings.HasSuffix(cc.gendocdir, helpers.FilePathSeparator) {
    61  				cc.gendocdir += helpers.FilePathSeparator
    62  			}
    63  			if found, _ := helpers.Exists(cc.gendocdir, hugofs.Os); !found {
    64  				jww.FEEDBACK.Println("Directory", cc.gendocdir, "does not exist, creating...")
    65  				if err := hugofs.Os.MkdirAll(cc.gendocdir, 0777); err != nil {
    66  					return err
    67  				}
    68  			}
    69  			now := time.Now().Format("2006-01-02")
    70  			prepender := func(filename string) string {
    71  				name := filepath.Base(filename)
    72  				base := strings.TrimSuffix(name, path.Ext(name))
    73  				url := "/commands/" + strings.ToLower(base) + "/"
    74  				return fmt.Sprintf(gendocFrontmatterTemplate, now, strings.Replace(base, "_", " ", -1), base, url)
    75  			}
    76  
    77  			linkHandler := func(name string) string {
    78  				base := strings.TrimSuffix(name, path.Ext(name))
    79  				return "/commands/" + strings.ToLower(base) + "/"
    80  			}
    81  
    82  			jww.FEEDBACK.Println("Generating Hugo command-line documentation in", cc.gendocdir, "...")
    83  			doc.GenMarkdownTreeCustom(cmd.Root(), cc.gendocdir, prepender, linkHandler)
    84  			jww.FEEDBACK.Println("Done.")
    85  
    86  			return nil
    87  		},
    88  	})
    89  
    90  	cc.cmd.PersistentFlags().StringVar(&cc.gendocdir, "dir", "/tmp/hugodoc/", "the directory to write the doc.")
    91  
    92  	// For bash-completion
    93  	cc.cmd.PersistentFlags().SetAnnotation("dir", cobra.BashCompSubdirsInDir, []string{})
    94  
    95  	return cc
    96  }