posener / goreadme

Generate readme file from Go doc. Now available as a Github action!
MIT License
213 stars 31 forks source link

Diff recognition interferes with command usage documentation #105

Closed Michael-F-Ellis closed 3 years ago

Michael-F-Ellis commented 3 years ago

First, thanks for creating goreadme. It's a nice solution to a bothersome problem!

In a project with several cmds, I'm trying to automatically get the cmd's usage help into the readme. So I wrote some build code to call each installed cmd with "-h" and put the output into doc.go, e.g.

/*
Usage of rir:
  -c uint
        Length of each chunk to be RIR expanded. 0 means all of input in one chunk.
  -n int
        number of countin beats before each segment (default 4)
  -o uint
        number of bars to overlap chunks. Must be less than chunkLength

*/
package main

But goreadme notices the leading '-' chars and renders the above as:

Usage of rir:

```diff
-c uint
    Length of each chunk to be RIR expanded. 0 means all of input in one chunk.
-n int
    number of countin beats before each segment (default 4)
-o uint
    number of bars to overlap chunks. Must be less than chunkLength


Is there a way to suppress the diff recognition?

Thanks!
posener commented 3 years ago

Thanks Michael! Huh, yes, the diff should be disabled in this case. This is where the diff is being set. I don't see any other option but to globally disable it. I guess we can do it by adding options to the ToMarkdown function, and propagate it all the way through...

Michael-F-Ellis commented 3 years ago

I've got something worked out for my use but it relies on preprocessing before goreadme sees doc.go. I added code to the section of my build script (magefile) that creates doc.go. It detects leading - signs and replaces them with \u2010 hyphen runes so that the diff detection doesn't trigger.

// Need to conceal leading '-' chars so goreadme won't interpret them as diff lines.
    lines := strings.Split(help, "\n")
    for i, line := range lines {
        trimmed := strings.TrimLeft(line, " \t")
        if strings.HasPrefix(trimmed, "-") {
            lines[i] = strings.Replace(line, "-", "\u2010", 1)
        }
    }
    text = strings.Join(lines, "\n")

That solves my problem since I never put diffs in comments, but it's clearly too much of a special case hack to include in goreadme.

FWIW, I think the best general solution is to add pre and post filter hooks as command line options, e.g.

   -PreFilter string   
      System command applied to the input text on ToMarkdown
  -PostFilter string
      System command applied to the Markdown output from ToMarkdown