campoy / embedmd

embedmd: embed code into markdown and keep everything in sync
Apache License 2.0
767 stars 62 forks source link

support a flag to exclude matching lines #46

Closed jba closed 7 years ago

jba commented 7 years ago

I would like to use a single file containing many nondescript snippets like

client, err := storage.NewClient(ctx)

Currently the regexps used to match will appear in the output, forcing me to use regexps that are fragile (may match multiple snippets, may fail to match if I change the snippet).

I'd rather write

// START AUTH
client, err := storage.NewClient(ctx)
// END AUTH

So I can match the markers but not have them in the output.

I propose a -x flag that provides this behavior.

campoy commented 7 years ago

I think this would be an interesting feature to be added, but I'd personally would make it a flag of the embedmd comment rather than the program itself.

Maybe something like:

[embedmd]:# (pathOrURL /start regexp/ /end regexp/ OMIT_MATCHING_LINES)

In your case this would look like:

[embedmd]:# (main.go /START AUTH/ /END AUTH/ OMIT_MATCHING_LINES)

Does that seem good?

campoy commented 7 years ago

Actually, I'm thinking it could be two different flags:

OMIT_MATCH: the text matching the regexp is omitted

MATCH_LINES: the match is expanded to include the whole line (basically adds .* and the beginning of the start regexp and at the end of the end regexp)

So, given:

// START AUTH
client, err := storage.NewClient(ctx)
// END AUTH

No flags

[embedmd]:# (main.go /START AUTH/ /END AUTH/)

gives

START AUTH
client, err := storage.NewClient(err)
// END AUTH

MATCH_LINES

[embedmd]:# (main.go /START AUTH/ /END AUTH/ MATCH_LINES)

gives

// START AUTH
client, err := storage.NewClient(err)
// END AUTH

OMIT_MATCH

[embedmd]:# (main.go /START AUTH/ /END AUTH/ OMIT_MATCH)

gives


client, err := storage.NewClient(err)
// 

MATCH_LINES OMIT_MATCH

[embedmd]:# (main.go /START AUTH/ /END AUTH/ OMIT_MATCH MATCH_LINES)

gives

client, err := storage.NewClient(err)

MATCH_LINES is not needed, since the equivalent behavior can be accomplished by transforming /start/ /end/ into /.*start/ /end.*/. So I'd recommend implementing OMIT_MATCH first, then reassess if MATCH_LINES is needed.

jba commented 7 years ago

We decided to go a different way, so we no longer need this feature. Sorry for the noise.

mojito317 commented 4 years ago

@campoy this feature idea seems good, and it would be useful for me and for others as well. Do you plan to make it?