~bigbes/confluence-md-utilities

ref: fb67f6989dc2cabe60498e0e7b8f7a51e1e9826b confluence-md-utilities/cmd/fmt.go -rw-r--r-- 1.3 KiB
fb67f698 — Eugene Blikh Initial commit: mdcx — Markdown to Confluence XML converter 2 months ago
                                                                                
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
package cmd

import (
	"fmt"
	"io"
	"os"

	"github.com/spf13/cobra"

	"sourcecraft.dev/bigbes/markdown-to-confluence-xml/format"
)

var (
	fmtOutput string
	fmtIndent string
)

var fmtCmd = &cobra.Command{
	Use:   "fmt [input.xml]",
	Short: "Pretty-print Confluence storage XML",
	Long: `Format Confluence storage XML with sensible indentation.

Block elements (p, h1-h6, ul, ol, li, table, tr, td, th, macros, layout)
get their own lines with indentation. Inline elements (strong, em, code,
a, ac:link, ac:image) stay on the same line. CDATA content inside code
blocks is preserved as-is.

Reads from stdin if no file is specified.`,
	Args: cobra.MaximumNArgs(1),
	RunE: func(cmd *cobra.Command, args []string) error {
		var input []byte
		var err error

		if len(args) > 0 {
			input, err = os.ReadFile(args[0])
		} else {
			input, err = io.ReadAll(os.Stdin)
		}
		if err != nil {
			return fmt.Errorf("reading input: %w", err)
		}

		result := format.PrettyXML(string(input), fmtIndent)

		if fmtOutput != "" {
			return os.WriteFile(fmtOutput, []byte(result), 0644)
		}
		fmt.Print(result)
		return nil
	},
}

func init() {
	fmtCmd.Flags().StringVarP(&fmtOutput, "output", "o", "", "Output file (default: stdout)")
	fmtCmd.Flags().StringVar(&fmtIndent, "indent", "  ", "Indentation string (default: 2 spaces)")
	rootCmd.AddCommand(fmtCmd)
}