github.com/dancsecs/gotomd@v0.0.0-20240310162206-65c4805cf510/get_doc.go (about)

     1  /*
     2     Golang To Github Markdown Utility: gotomd
     3     Copyright (C) 2023, 2024 Leslie Dancsecs
     4  
     5     This program is free software: you can redistribute it and/or modify
     6     it under the terms of the GNU General Public License as published by
     7     the Free Software Foundation, either version 3 of the License, or
     8     (at your option) any later version.
     9  
    10     This program is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13     GNU General Public License for more details.
    14  
    15     You should have received a copy of the GNU General Public License
    16     along with this program.  If not, see <https://www.gnu.org/licenses/>.
    17  */
    18  
    19  package main
    20  
    21  import (
    22  	"strings"
    23  )
    24  
    25  func markGoCode(content string) string {
    26  	return "```go\n" + strings.TrimRight(content, "\n") + "\n```"
    27  }
    28  
    29  func markBashCode(content string) string {
    30  	return "```bash\n" + strings.TrimRight(content, "\n") + "\n```"
    31  }
    32  
    33  func getDoc(cmd string) (string, error) {
    34  	var (
    35  		dInfo *docInfo
    36  		res   string
    37  	)
    38  
    39  	dir, action, err := parseCmds(cmd)
    40  	for i, mi := 0, len(dir); i < mi && err == nil; i++ {
    41  		dInfo, err = getInfo(dir[i], action[i])
    42  		if err == nil {
    43  			if res != "" {
    44  				res += "\n\n"
    45  			}
    46  
    47  			res += dInfo.declGoLang() + "\n\n" +
    48  				dInfo.docMarkdown()
    49  		}
    50  	}
    51  
    52  	if err == nil {
    53  		return res, nil
    54  	}
    55  
    56  	return "", err
    57  }
    58  
    59  func getDocDecl(cmd string) (string, error) {
    60  	var (
    61  		dInfo *docInfo
    62  		res   string
    63  	)
    64  
    65  	dir, action, err := parseCmds(cmd)
    66  	if err == nil {
    67  		for i, mi := 0, len(dir); i < mi && err == nil; i++ {
    68  			dInfo, err = getInfo(dir[i], action[i])
    69  			if err == nil {
    70  				if res != "" {
    71  					res += "\n"
    72  				}
    73  
    74  				res += strings.Join(dInfo.header, "\n")
    75  			}
    76  		}
    77  	}
    78  
    79  	if err == nil {
    80  		return markGoCode(res), nil
    81  	}
    82  
    83  	return "", err
    84  }
    85  
    86  func getDocDeclSingle(cmd string) (string, error) {
    87  	var (
    88  		dInfo *docInfo
    89  		res   string
    90  	)
    91  
    92  	dir, action, err := parseCmds(cmd)
    93  	if err == nil {
    94  		for i, mi := 0, len(dir); i < mi && err == nil; i++ {
    95  			dInfo, err = getInfo(dir[i], action[i])
    96  			if err == nil {
    97  				if res != "" {
    98  					res += "\n"
    99  				}
   100  
   101  				res += dInfo.oneLine()
   102  			}
   103  		}
   104  	}
   105  
   106  	if err == nil {
   107  		return markGoCode(res), nil
   108  	}
   109  
   110  	return "", err
   111  }
   112  
   113  func getDocDeclNatural(cmd string) (string, error) {
   114  	var (
   115  		dInfo *docInfo
   116  		res   string
   117  	)
   118  
   119  	dir, action, err := parseCmds(cmd)
   120  	if err == nil {
   121  		for i, mi := 0, len(dir); i < mi && err == nil; i++ {
   122  			dInfo, err = getInfo(dir[i], action[i])
   123  			if err == nil {
   124  				if res != "" {
   125  					res += "\n\n"
   126  				}
   127  
   128  				res += dInfo.naturalComments() + "\n"
   129  				res += dInfo.oneLine()
   130  			}
   131  		}
   132  	}
   133  
   134  	if err == nil {
   135  		return markGoCode(res), nil
   136  	}
   137  
   138  	return "", err
   139  }