Skip to content

Add mdox-gen-file#19

Closed
saswatamcode wants to merge 3 commits intobwplotka:masterfrom
saswatamcode:file-gen
Closed

Add mdox-gen-file#19
saswatamcode wants to merge 3 commits intobwplotka:masterfrom
saswatamcode:file-gen

Conversation

@saswatamcode
Copy link
Copy Markdown
Collaborator

This PR adds an mdox-gen-file option which allows fetching code from local files into documentation like below. Addresses #18

```go mdox-gen-file="main.go" mdox-gen-lines=47-56

This results in the following when formatted,

```go mdox-gen-file="main.go" mdox-gen-lines=47-56
	switch logFormat {
	case logFormatJson:
		return level.NewFilter(log.NewJSONLogger(log.NewSyncWriter(os.Stderr)), lvl)
	case logFormatLogfmt:
		return level.NewFilter(log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr)), lvl)
	case logFormatCLILog:
		fallthrough
	default:
		return level.NewFilter(clilog.New(log.NewSyncWriter(os.Stderr)), lvl)
	}
. . .

If no line numbers are given like below, entire file is brought in by default (useful for blogs),

```go mdox-gen-file="main.go" mdox-gen-lines=47-56

Copy link
Copy Markdown
Owner

@bwplotka bwplotka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing! Some suggestions, plus some test needed, otherwise LGTM!

case true:
val := strings.Split(linesToCopy, "-")
start, errStart := strconv.Atoi(val[0])
if errStart != nil {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if errStart != nil {
if err != nil {

Usually it's fine to shadow err variable if in short scope.


switch lineOk {
case true:
val := strings.Split(linesToCopy, "-")
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would propose the same semantics as Go slice, WDYT?

Suggested change
val := strings.Split(linesToCopy, "-")
val := strings.Split(linesToCopy, ":")

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! I think this would be better!

text = append(text, "\n"...)
}
line++
}
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's check scanner.Err() after this loop (same below)

}
line++
}
case false:
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we reuse the above, but just use start 0 and end = math.MaxInt64? (:

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Will make the necessary changes!

Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com>
Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com>
Copy link
Copy Markdown
Owner

@bwplotka bwplotka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, almost there 🤗

line++
}
if err := scanner.Err(); err != nil {
return nil, errors.Errorf("scanner couldn't read: %v", err)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return nil, errors.Errorf("scanner couldn't read: %v", err)
return nil, errors.Wrap(err, "scanner read")

)

func TestFormat_FormatSingle_CodeBlockTransformer(t *testing.T) {
f := mdformatter.New(context.Background(), mdformatter.WithCodeBlockTransformer(NewCodeBlockTransformer()))
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not putting this in our e2e framework with input output markdown files? (:

Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com>
Copy link
Copy Markdown
Owner

@bwplotka bwplotka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noticed 🙈

To make sure we have one way of doing things... what's stops us without this PR to have mdox doing the same with:

```go mdox-gen-exec="sed -n '3,6p' main.go"
```

🤔

While this PR is great, I fear it would be best to decouple this logic and allow better tools sed to be used 🙈 WDYT?

IF you agree, we could add test case with sed to ensure this works instead of full code.

@saswatamcode
Copy link
Copy Markdown
Collaborator Author

Sure! Will close this and open a new PR with the sed test case!

@saswatamcode saswatamcode mentioned this pull request May 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants