File: io_input.go

package info (click to toggle)
golang-github-johanneskaufmann-html-to-markdown 2.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,080 kB
  • sloc: makefile: 3
file content (99 lines) | stat: -rw-r--r-- 2,568 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package cmd

import (
	"fmt"
	"io"
	"os"
	"path/filepath"
	"strings"

	"github.com/bmatcuk/doublestar/v4"
)

type input struct {
	inputFullFilepath  string
	outputFullFilepath string
	data               []byte
}

// E.g. "website.html" -> "website"
func fileNameWithoutExtension(fileName string) string {
	return strings.TrimSuffix(fileName, filepath.Ext(fileName))
}

// If the output is a file, it would be "output.md"
var defaultBasename = "output"

func (cli *CLI) listInputs() ([]*input, error) {
	if cli.isStdinPipe && cli.config.inputFilepath != "" {
		return nil, NewCLIError(
			fmt.Errorf("cannot use both stdin and --input at the same time. Use either stdin or specify an input file, but not both"),
		)
	}

	if cli.isStdinPipe {
		data, err := io.ReadAll(cli.Stdin)
		if err != nil {
			return nil, err
		}
		return []*input{
			{
				inputFullFilepath: defaultBasename,
				data:              data,
			},
		}, nil
	}

	if cli.config.inputFilepath != "" {
		matches, err := doublestar.FilepathGlob(cli.config.inputFilepath, doublestar.WithFilesOnly(), doublestar.WithNoFollow())
		if err != nil {
			return nil, err
		}
		if len(matches) == 0 {
			// The inputFilepath wasn't actually a glob but was pointing to an existing folder.
			// The user probably wanted to convert all files in that folder — so we recommend the glob.
			if outInfo, err := os.Stat(cli.config.inputFilepath); err == nil && outInfo.IsDir() {
				return nil, NewCLIError(
					fmt.Errorf("input path %q is a directory, not a file", cli.config.inputFilepath),
					Paragraph("Here is how you can use a glob to match multiple files:"),
					CodeBlock(`html2markdown --input "src/*.html" --output "dist/"`),
				)
			}

			return nil, NewCLIError(
				fmt.Errorf("no files found matching pattern %q", cli.config.inputFilepath),
				Paragraph("Here is how you can use a glob to match multiple files:"),
				CodeBlock(`html2markdown --input "src/*.html" --output "dist/"`),
			)
		}

		var inputs []*input
		for _, match := range matches {
			inputs = append(inputs, &input{
				inputFullFilepath: match,
				data:              nil,
			})
		}

		return inputs, nil
	}

	return nil, NewCLIError(
		fmt.Errorf("the html input should be piped into the cli"),
		Paragraph("Here is how you can use the CLI:"),
		CodeBlock(`echo "<strong>important</strong>" | html2markdown`),
	)
}

func (cli *CLI) readInput(in *input) ([]byte, error) {
	if in.data != nil {
		return in.data, nil
	}

	data, err := os.ReadFile(in.inputFullFilepath)
	if err != nil {
		return nil, err
	}

	return data, nil
}