...

Source file src/k8s.io/kubernetes/cmd/genkubedocs/postprocessing.go

Documentation: k8s.io/kubernetes/cmd/genkubedocs

     1  /*
     2  Copyright 2017 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"os"
    21  	"path/filepath"
    22  	"strings"
    23  
    24  	"github.com/spf13/cobra"
    25  )
    26  
    27  // MarkdownPostProcessing goes though the generated files
    28  func MarkdownPostProcessing(cmd *cobra.Command, dir string, processor func(string) string) error {
    29  	for _, c := range cmd.Commands() {
    30  		if !c.IsAvailableCommand() || c.IsAdditionalHelpTopicCommand() {
    31  			continue
    32  		}
    33  		if err := MarkdownPostProcessing(c, dir, processor); err != nil {
    34  			return err
    35  		}
    36  	}
    37  
    38  	basename := strings.Replace(cmd.CommandPath(), " ", "_", -1) + ".md"
    39  	filename := filepath.Join(dir, basename)
    40  
    41  	markdownBytes, err := os.ReadFile(filename)
    42  	if err != nil {
    43  		return err
    44  	}
    45  
    46  	processedMarkDown := processor(string(markdownBytes))
    47  
    48  	return os.WriteFile(filename, []byte(processedMarkDown), 0644)
    49  }
    50  
    51  // cleanupForInclude parts of markdown that will make difficult to use it as include in the website:
    52  // - The title of the document (this allow more flexibility for include, e.g. include in tabs)
    53  // - The sections see also, that assumes file will be used as a main page
    54  func cleanupForInclude(md string) string {
    55  	lines := strings.Split(md, "\n")
    56  
    57  	cleanMd := ""
    58  	for i, line := range lines {
    59  		if i == 0 {
    60  			continue
    61  		}
    62  		if line == "### SEE ALSO" {
    63  			break
    64  		}
    65  
    66  		cleanMd += line
    67  		if i < len(lines)-1 {
    68  			cleanMd += "\n"
    69  		}
    70  	}
    71  
    72  	return cleanMd
    73  }
    74  

View as plain text