Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ go 1.23.0

require (
github.com/Microsoft/go-winio v0.6.2
github.com/beevik/etree v1.5.1
github.com/bmatcuk/doublestar/v4 v4.10.0
github.com/charmbracelet/huh v1.0.0
github.com/charmbracelet/lipgloss v1.1.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiE
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
github.com/aymanbagabas/go-udiff v0.3.1 h1:LV+qyBQ2pqe0u42ZsUEtPiCaUoqgA9gYRDs3vj1nolY=
github.com/aymanbagabas/go-udiff v0.3.1/go.mod h1:G0fsKmG+P6ylD0r6N/KgQD/nWzgfnl8ZBcNLgcbrw8E=
github.com/beevik/etree v1.5.1 h1:TC3zyxYp+81wAmbsi8SWUpZCurbxa6S8RITYRSkNRwo=
github.com/beevik/etree v1.5.1/go.mod h1:gPNJNaBGVZ9AwsidazFZyygnd+0pAU38N4D+WemwKNs=
github.com/bmatcuk/doublestar/v4 v4.10.0 h1:zU9WiOla1YA122oLM6i4EXvGW62DvKZVxIe6TYWexEs=
github.com/bmatcuk/doublestar/v4 v4.10.0/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
github.com/catppuccin/go v0.3.0 h1:d+0/YicIq+hSTo5oPuRi5kOpqkVA5tAsU6dNhvRu+aY=
Expand Down
224 changes: 10 additions & 214 deletions shortcuts/slides/slides_xml_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,18 @@ import (
"bytes"
"context"
"fmt"
"regexp"
"strconv"
"strings"

"github.com/beevik/etree"

"github.com/larksuite/cli/errs"
"github.com/larksuite/cli/extension/fileio"
"github.com/larksuite/cli/internal/validate"
"github.com/larksuite/cli/shortcuts/common"
)

// SlidesXMLGet fetches the full XML presentation content. When --output is
// provided it writes reindented XML to a local file, and --raw prints
// reindented XML to stdout; otherwise it returns the server's original
// content unmodified in the standard JSON envelope. Use --slide-id or
// --slide-number to fetch one page.
// provided it writes to a local file; otherwise it returns the XML in the
// standard JSON envelope. Use --slide-id or --slide-number to fetch one page,
// and use --raw for direct XML stdout.
var SlidesXMLGet = common.Shortcut{
Service: "slides",
Command: "+xml-get",
Expand All @@ -35,8 +30,8 @@ var SlidesXMLGet = common.Shortcut{
AuthTypes: []string{"user", "bot"},
Flags: []common.Flag{
{Name: "presentation", Desc: "xml_presentation_id, slides URL, or wiki URL that resolves to slides", Required: true},
{Name: "output", Desc: "local XML output path; the saved file is formatted for readability; must be a relative path within the current directory; existing file is overwritten; omit to return the server's original XML in the JSON envelope"},
{Name: "raw", Type: "bool", Desc: "print formatted XML to stdout without the JSON envelope; incompatible with --output and --jq"},
{Name: "output", Desc: "local XML output path; must be a relative path within the current directory; existing file is overwritten; omit to return XML in the JSON envelope"},
{Name: "raw", Type: "bool", Desc: "print raw XML to stdout instead of the JSON envelope; incompatible with --output and --jq"},
{Name: "slide-id", Desc: "slide page identifier; omit both slide selectors to fetch full presentation XML"},
{Name: "slide-number", Type: "int", Desc: "1-based slide page number; omit both slide selectors to fetch full presentation XML"},
{Name: "revision-id", Type: "int", Default: "-1", Desc: "presentation revision_id; -1 means latest"},
Expand Down Expand Up @@ -113,10 +108,10 @@ var SlidesXMLGet = common.Shortcut{
}
dry.GET(path).Params(params)
if outputPath := strings.TrimSpace(runtime.Str("output")); outputPath != "" {
return dry.Set("output", outputPath).Set("stdout_content", "suppressed; formatted XML content is saved to --output during execution")
return dry.Set("output", outputPath).Set("stdout_content", "suppressed; XML content is saved to --output during execution")
}
if runtime.Bool("raw") {
return dry.Set("output", "<stdout>").Set("stdout_content", "formatted XML content is printed to stdout during execution")
return dry.Set("output", "<stdout>").Set("stdout_content", "raw XML content is printed to stdout during execution")
}
return dry.Set("output", "<stdout>").Set("stdout_content", "JSON envelope with XML content is printed to stdout during execution")
},
Expand Down Expand Up @@ -255,31 +250,22 @@ func fetchSlidesXMLGetContent(runtime *common.RuntimeContext, presentationID str
return content, out, nil
}

// outputSlidesXMLGetContent routes the fetched XML to its output surface.
// Only the text surfaces are reindented: --raw stdout and --output files are
// read directly by humans and line tools. The JSON envelope carries the
// server content verbatim instead -- inside a JSON string every newline is
// escaped to \n, so formatting there buys no readability and only inflates
// the payload, while passthrough keeps that read path byte-exact without
// even parsing the content.
func outputSlidesXMLGetContent(runtime *common.RuntimeContext, content string, outputPath string, out map[string]interface{}) error {
if outputPath == "" {
if !runtime.Bool("raw") {
runtime.OutFormatRaw(out, nil, nil)
return nil
}
formatted, _ := prettyPrintXMLOrOriginal(runtime, content)
if _, err := fmt.Fprint(runtime.IO().Out, formatted); err != nil {
if _, err := fmt.Fprint(runtime.IO().Out, content); err != nil {
return errs.NewInternalError(errs.SubtypeFileIO, "write XML content to stdout: %v", err).WithCause(err)
}
return nil
}

formatted, prettyPrinted := prettyPrintXMLOrOriginal(runtime, content)
result, err := runtime.FileIO().Save(outputPath, fileio.SaveOptions{
ContentType: "application/xml",
ContentLength: int64(len(formatted)),
}, bytes.NewReader([]byte(formatted)))
ContentLength: int64(len(content)),
}, bytes.NewReader([]byte(content)))
if err != nil {
return common.WrapSaveErrorTyped(err)
}
Expand All @@ -294,7 +280,6 @@ func outputSlidesXMLGetContent(runtime *common.RuntimeContext, content string, o
"path": resolvedPath,
"size": result.Size(),
"content_saved": true,
"pretty_printed": prettyPrinted,
}
for _, key := range []string{"revision_id", "remove_attr_id", "slide_id", "slide_number"} {
if value, ok := out[key]; ok {
Expand All @@ -304,192 +289,3 @@ func outputSlidesXMLGetContent(runtime *common.RuntimeContext, content string, o
runtime.Out(fileOut, nil)
return nil
}

// textBearingTags are the SML elements whose schema content model is
// mixed (arbitrary text interleaved with inline markup): the <p> paragraph
// container and its inline formatting children, plus chart title/subtitle.
// See slides_xml_schema_definition.xml, <p> element docs: a deliberate space
// or tab is represented via &#32;/&#9; character references. Those references
// are masked before XML parsing so etree cannot resolve away their lexical
// representation, and reindentStructural never descends into these elements.
var textBearingTags = map[string]bool{
"p": true,
"strong": true,
"em": true,
"u": true,
"span": true,
"del": true,
"a": true,
"shadow": true,
"outline": true,
"chartTitle": true,
"chartSubTitle": true,
}

// prettyPrintXML reindents xmlContent so structural elements (presentation,
// slide, shape, style, ...) each sit on their own line. The server returns
// XML as a single unbroken line, and this is what makes the --raw and
// --output text surfaces readable; the JSON envelope path never calls it
// (see outputSlidesXMLGetContent).
//
// Reindentation never enters a textBearingTags element. XML whitespace
// character references are masked before parsing and restored after
// serialization, and CDATA sections are preserved rather than collapsed into
// escaped text. This covers the schema-documented space/tab references as well
// as CR/LF references: serializing a CR reference as a literal line ending
// would make a later XML parse normalize it to LF and break read-modify-write
// round trips.
func prettyPrintXML(xmlContent string) (string, error) {
maskedContent, maskedReferences := maskSMLWhitespaceCharacterReferences(xmlContent)
doc := etree.NewDocument()
doc.ReadSettings.PreserveCData = true
if err := doc.ReadFromString(maskedContent); err != nil {
return "", err
}
if root := doc.Root(); root != nil {
reindentStructural(root, 0)
}
out, err := doc.WriteToString()
if err != nil {
return "", err
}
out = restoreSMLWhitespaceCharacterReferences(out, maskedReferences)
if !strings.HasSuffix(out, "\n") {
out += "\n"
}
return out, nil
}

// prettyPrintXMLOrOriginal keeps xml-get best-effort: if the server returns
// content that is not strictly valid XML, callers still receive the original
// content and a warning on stderr instead of losing the read path. The bool
// reports whether pretty-printing succeeded, surfaced as pretty_printed in
// --output file metadata.
func prettyPrintXMLOrOriginal(runtime *common.RuntimeContext, xmlContent string) (string, bool) {
out, err := prettyPrintXML(xmlContent)
if err != nil {
fmt.Fprintf(runtime.IO().ErrOut, "warning: XML pretty-print skipped; returning original server content: %v\n", err)
return xmlContent, false
}
return out, true
}

type maskedXMLCharacterReference struct {
placeholder string
reference string
}

var numericXMLCharacterReferencePattern = regexp.MustCompile(`&#(?:[0-9]+|x[0-9A-Fa-f]+);`)

// maskSMLWhitespaceCharacterReferences protects XML whitespace character
// references (space, tab, CR, and LF) from etree's parse/write normalization.
// Each original spelling is restored exactly, including decimal, hexadecimal,
// and zero-padded forms. The placeholder prefix is chosen not to occur in the
// input, so restoring cannot rewrite user-authored content accidentally.
func maskSMLWhitespaceCharacterReferences(xmlContent string) (string, []maskedXMLCharacterReference) {
placeholderPrefix := "LARKCLI_XML_WHITESPACE_REFERENCE_"
for strings.Contains(xmlContent, placeholderPrefix) {
placeholderPrefix += "_"
}

maskedReferences := make([]maskedXMLCharacterReference, 0)
maskedContent := numericXMLCharacterReferencePattern.ReplaceAllStringFunc(xmlContent, func(reference string) string {
digits := reference[2 : len(reference)-1]
base := 10
if strings.HasPrefix(digits, "x") {
base = 16
digits = digits[1:]
}
value, err := strconv.ParseUint(digits, base, 32)
if err != nil {
return reference
}
switch value {
case ' ', '\t', '\r', '\n':
default:
return reference
}

placeholder := fmt.Sprintf("%s%d_", placeholderPrefix, len(maskedReferences))
maskedReferences = append(maskedReferences, maskedXMLCharacterReference{
placeholder: placeholder,
reference: reference,
})
return placeholder
})
return maskedContent, maskedReferences
}

func restoreSMLWhitespaceCharacterReferences(xmlContent string, maskedReferences []maskedXMLCharacterReference) string {
for _, masked := range maskedReferences {
xmlContent = strings.ReplaceAll(xmlContent, masked.placeholder, masked.reference)
}
return xmlContent
}

// reindentStructural inserts newline+indent whitespace between an element's
// direct children so each nested element sits on its own line, recursing
// only into children that are not textBearingTags. Existing whitespace-only
// CharData between children is treated as pre-existing formatting and
// dropped before reinserting it at the correct depth; non-whitespace CharData
// is never touched, and text-bearing subtrees are never entered at all.
func reindentStructural(e *etree.Element, depth int) {
// A node without element children has no nested structure to reindent.
// Leaving the whole leaf untouched also preserves whitespace-only xs:string
// and simple-content values such as <title> and <chartField>, including
// adjacent plain-text and CDATA nodes.
if textBearingTags[e.Tag] || !hasElementChild(e) {
return
}

for i := len(e.Child) - 1; i >= 0; i-- {
if cd, ok := e.Child[i].(*etree.CharData); ok && isAllWhitespace(cd.Data) {
e.RemoveChildAt(i)
}
}
if len(e.Child) == 0 {
return
}

_, lastIsCharData := e.Child[len(e.Child)-1].(*etree.CharData)
childIndent := "\n" + strings.Repeat(" ", depth+1)
closeIndent := "\n" + strings.Repeat(" ", depth)

for i := len(e.Child) - 1; i >= 0; i-- {
child := e.Child[i]
if ce, ok := child.(*etree.Element); ok {
reindentStructural(ce, depth+1)
}
if _, isCharData := child.(*etree.CharData); !isCharData {
e.InsertChildAt(i, etree.NewCharData(childIndent))
}
}
if !lastIsCharData {
e.AddChild(etree.NewCharData(closeIndent))
}
}

func hasElementChild(e *etree.Element) bool {
for _, child := range e.Child {
if _, ok := child.(*etree.Element); ok {
return true
}
}
return false
}

// isAllWhitespace reports whether s is non-empty and consists only of XML
// whitespace characters (space, tab, CR, LF).
func isAllWhitespace(s string) bool {
if s == "" {
return false
}
for _, r := range s {
switch r {
case ' ', '\t', '\n', '\r':
default:
return false
}
}
return true
}
Loading
Loading