File: exec.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 (189 lines) | stat: -rw-r--r-- 3,569 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package cmd

import (
	"bytes"
	"flag"
	"fmt"
	"os"
	"strings"

	"github.com/andybalholm/cascadia"
)

var (
	projectBinary = "html2markdown"
)

// OsExiter is the function used when the app exits. If not set defaults to os.Exit.
var OsExiter = os.Exit

// - - - - - - - - - - - - - //

type Config struct {
	// args are the positional (non-flag) command-line arguments.
	args []string

	inputFilepath   string
	outputFilepath  string
	outputOverwrite bool

	// - - - - - General - - - - - //
	version bool
	domain  string

	includeSelector cascadia.SelectorGroup
	excludeSelector cascadia.SelectorGroup

	// - - - - - Options - - - - - //
	strongDelimiter string

	// - - - - - Plugins - - - - - //
	enablePluginStrikethrough bool

	enablePluginTable       bool
	tableSkipEmptyRows      bool
	tableHeaderPromotion    bool
	tableSpanCellBehavior   string
	tablePresentationTables bool
}

// Release holds the information (from the 3 ldflags) that goreleaser sets.
type Release struct {
	// Current Git tag (the v prefix is stripped)
	Version string

	// Current git commit SHA
	Commit string

	// Date in the RFC3339 format
	Date string
}
type CLI struct {
	Stdin  ReadWriterWithStat
	Stdout ReadWriterWithStat
	Stderr ReadWriterWithStat

	OsArgs []string

	Release Release

	isStdinPipe  bool
	isStdoutPipe bool
	isStderrPipe bool

	flags  *flag.FlagSet
	config Config

	usageText bytes.Buffer
}

func (cli *CLI) Init() error {
	var err error
	cli.isStdinPipe, err = isPipe(cli.Stdin)
	if err != nil {
		return fmt.Errorf("error while checking stdin for is pipe: %w", err)
	}
	cli.isStdoutPipe, err = isPipe(cli.Stdout)
	if err != nil {
		return fmt.Errorf("error while checking stdout for is pipe: %w", err)
	}
	cli.isStderrPipe, err = isPipe(cli.Stderr)
	if err != nil {
		return fmt.Errorf("error while checking stderr for is pipe: %w", err)
	}

	cli.initFlags(cli.OsArgs[0])

	err = cli.initUsageText()
	if err != nil {
		return fmt.Errorf("error while initializing the usage text: %w", err)
	}

	return nil
}
func (cli *CLI) Execute() {

	warnings, err := cli.run()

	for _, warning := range warnings {
		cli.PrintWarn(warning)
	}

	if err == flag.ErrHelp {
		cli.printUsage()

		OsExiter(0)
		return
	} else if err != nil {
		cli.PrintErr(err)

		OsExiter(1) // General Error
		return
	} else {
		OsExiter(0)
		return
	}
}

func (cli *CLI) run() ([]error, error) {

	err := cli.parseFlags(cli.OsArgs[1:])
	if err != nil {
		return nil, err
	}

	if len(cli.config.args) != 0 {

		return nil, NewCLIError(
			fmt.Errorf("unknown arguments: %s", strings.Join(cli.config.args, " ")),
			Paragraph("Here is how you can use the CLI:"),
			CodeBlock(`echo "<strong>important</strong>" | html2markdown`),
		)
	}

	if cli.config.version {
		cli.printVersion()
		return nil, nil
	}

	// - - - - - - - - - - - - - - - //

	inputs, err := cli.listInputs()
	if err != nil {
		return nil, err
	}

	outputType, err := determineOutputType(cli.config.inputFilepath, len(inputs), cli.config.outputFilepath)
	if err != nil {
		return nil, err
	}

	err = ensureOutputDirectories(outputType, cli.config.outputFilepath)
	if err != nil {
		return nil, err
	}

	err = calculateOutputPaths(cli.config.inputFilepath, inputs)
	if err != nil {
		return nil, err
	}

	for _, input := range inputs {
		data, err := cli.readInput(input)
		if err != nil {
			return nil, err
		}

		markdown, err := cli.convert(data)
		if err != nil {
			return nil, err
		}

		err = cli.writeOutput(outputType, input.outputFullFilepath, markdown)
		if err != nil {
			return nil, err
		}
	}

	return nil, nil
}