~bigbes/confluence-md-utilities

ref: e0e81bc69b5622c2bd20261155fc36519a7621d8 confluence-md-utilities/cmd/mdcx/embed.go -rw-r--r-- 2.1 KiB
e0e81bc6 — Eugene Blikh chore: rename module to go.bigb.es/confluence-md-utilities a month 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main

import (
	"fmt"
	"io"
	"os"

	"github.com/spf13/cobra"

	"go.bigb.es/confluence-md-utilities/converter"
	"go.bigb.es/confluence-md-utilities/template"
)

var (
	embedTemplate    string
	embedOutput      string
	embedMarkerStart string
	embedMarkerEnd   string
)

var embedCmd = &cobra.Command{
	Use:   "embed [input.md]",
	Short: "Embed Markdown into a Confluence XML template",
	Long: `Convert Markdown to Confluence XML and embed it into a template document.
The template must contain marker comments to indicate where content should be inserted:

  <!-- MD_CONTENT_START -->
  <!-- MD_CONTENT_END -->

Reads Markdown from stdin if no file is specified.`,
	Args: cobra.MaximumNArgs(1),
	RunE: func(cmd *cobra.Command, args []string) error {
		if embedTemplate == "" {
			return fmt.Errorf("--template flag is required")
		}

		// Read markdown input
		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)
		}

		// Read template
		tmpl, err := os.ReadFile(embedTemplate)
		if err != nil {
			return fmt.Errorf("reading template: %w", err)
		}

		// Convert markdown to confluence XML
		xmlContent, err := converter.MarkdownToConfluence(input)
		if err != nil {
			return fmt.Errorf("converting markdown: %w", err)
		}

		// Embed into template
		result, err := template.Embed(string(tmpl), xmlContent, embedMarkerStart, embedMarkerEnd)
		if err != nil {
			return fmt.Errorf("embedding: %w", err)
		}

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

func init() {
	embedCmd.Flags().StringVarP(&embedTemplate, "template", "t", "", "Template XML file (required)")
	embedCmd.Flags().StringVarP(&embedOutput, "output", "o", "", "Output file (default: stdout)")
	embedCmd.Flags().StringVar(&embedMarkerStart, "marker-start", template.DefaultMarkerStart, "Start marker comment")
	embedCmd.Flags().StringVar(&embedMarkerEnd, "marker-end", template.DefaultMarkerEnd, "End marker comment")
	rootCmd.AddCommand(embedCmd)
}