Skip to content

Commit

Permalink
Merge pull request #2153 from rsteube/pandoc-integrate-file
Browse files Browse the repository at this point in the history
pandoc: integrate file completion
  • Loading branch information
rsteube authored Jan 8, 2024
2 parents 3c160d9 + 92f2043 commit ba50ee7
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 37 deletions.
56 changes: 31 additions & 25 deletions completers/pandoc_completer/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/completers/pandoc_completer/cmd/action"
"github.com/rsteube/carapace-bin/pkg/actions/net/http"
"github.com/rsteube/carapace-bin/pkg/actions/tools/pandoc"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -113,39 +113,45 @@ func init() {

carapace.Gen(rootCmd).FlagCompletion(carapace.ActionMap{
// TODO intended-code-classes
"abbreviations": carapace.ActionFiles(),
"bibliography": carapace.ActionFiles(),
"citation-abbreviations": carapace.ActionFiles(),
"csl": carapace.ActionFiles(),
"data-dir": carapace.ActionDirectories(),
"defaults": carapace.ActionFiles(),
"email-obfuscation": carapace.ActionValues("none", "javascript", "references"),
"eol": carapace.ActionValues("crlf", "lf", "native"),
"epub-cover-image": carapace.ActionFiles(),
"epub-embed-font": carapace.ActionFiles(),
"epub-metadata": carapace.ActionFiles(),
"epub-subdirectory": carapace.ActionDirectories(),
"extract-media": carapace.ActionDirectories(),
"filter": carapace.ActionFiles(),
"from": action.ActionInputFormats(),
"highlight-style": action.ActionHighlightStyles(),
"abbreviations": carapace.ActionFiles(),
"bibliography": carapace.ActionFiles(),
"citation-abbreviations": carapace.ActionFiles(),
"csl": carapace.ActionFiles(),
"data-dir": carapace.ActionDirectories(),
"defaults": carapace.ActionFiles(),
"email-obfuscation": carapace.ActionValues("none", "javascript", "references"),
"eol": carapace.ActionValues("crlf", "lf", "native"),
"epub-cover-image": carapace.ActionFiles(),
"epub-embed-font": carapace.ActionFiles(),
"epub-metadata": carapace.ActionFiles(),
"epub-subdirectory": carapace.ActionDirectories(),
"extract-media": carapace.ActionDirectories(),
"filter": carapace.ActionFiles(),
"from": pandoc.ActionInputFormats(),
"highlight-style": carapace.Batch(
carapace.ActionFiles(".theme"),
pandoc.ActionHighlightStyles(),
).ToA(),
"include-after-body": carapace.ActionFiles(),
"include-before-body": carapace.ActionFiles(),
"include-in-header": carapace.ActionFiles(),
"ipynb-output": carapace.ActionValues("all", "none", "best"),
"list-extensions": action.ActionFormats(),
"list-extensions": pandoc.ActionFormats(),
"log": carapace.ActionFiles(),
"lua-filter": carapace.ActionFiles(".lua"),
"markdown-headings": carapace.ActionValues("setext", "atx"),
"metadata-file": carapace.ActionFiles(),
"output": carapace.ActionFiles(),
"pdf-engine": carapace.ActionFiles(),
"print-default-data-file": carapace.ActionFiles(),
"print-default-template": action.ActionFormats(),
"print-highlight-style": action.ActionHighlightStyles(),
"read": action.ActionInputFormats(),
"reference-doc": carapace.ActionFiles(),
"reference-location": carapace.ActionValues("block", "section", "document"),
"print-default-template": pandoc.ActionFormats(),
"print-highlight-style": carapace.Batch(
carapace.ActionFiles(".theme"),
pandoc.ActionHighlightStyles(),
).ToA(),
"read": pandoc.ActionInputFormats(),
"reference-doc": carapace.ActionFiles(),
"reference-location": carapace.ActionValues("block", "section", "document"),
"request-header": carapace.ActionMultiParts(":", func(c carapace.Context) carapace.Action {
switch len(c.Parts) {
case 0:
Expand All @@ -159,11 +165,11 @@ func init() {
"resource-path": carapace.ActionDirectories(),
"syntax-definition": carapace.ActionFiles(),
"template": carapace.ActionFiles(),
"to": action.ActionOutputFormats(),
"to": pandoc.ActionOutputFormats(),
"top-level-division": carapace.ActionValues("default", "section", "chapter", "part"),
"track-changes": carapace.ActionValues("accept", "reject", "all"),
"wrap": carapace.ActionValues("auto", "none", "preserve"),
"write": action.ActionOutputFormats(),
"write": pandoc.ActionOutputFormats(),
})

carapace.Gen(rootCmd).PositionalAnyCompletion(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package action
package pandoc

import (
"strings"

"github.com/rsteube/carapace"
"github.com/rsteube/carapace/pkg/util"
)

// ActionInputFormats completes input formats
func ActionInputFormats() carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
if !strings.ContainsAny(c.Value, "+-") {
Expand All @@ -18,9 +18,10 @@ func ActionInputFormats() carapace.Action {
fields := extensionFields(c.Value)
fields = fields[:len(fields)-1] // omit currently completed extension
return ActionExtensions(fields[0]).Invoke(c).Filter(fields[1:]...).Prefix(strings.Join(fields, "")).ToA()
})
}).Tag("input formats")
}

// ActionOutputFormats completes output formats
func ActionOutputFormats() carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
if !strings.ContainsAny(c.Value, "+-") {
Expand All @@ -32,7 +33,7 @@ func ActionOutputFormats() carapace.Action {
fields := extensionFields(c.Value)
fields = fields[:len(fields)-1] // omit currently completed extension
return ActionExtensions(fields[0]).Invoke(c).Filter(fields[1:]...).Prefix(strings.Join(fields, "")).ToA()
})
}).Tag("output formats")
}

func extensionFields(s string) []string {
Expand All @@ -51,6 +52,7 @@ func extensionFields(s string) []string {
return fields
}

// ActionFormats completes formats
func ActionFormats() carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return carapace.Batch(
Expand All @@ -60,18 +62,24 @@ func ActionFormats() carapace.Action {
})
}

// ActionExtensions completes extensions
func ActionExtensions(format string) carapace.Action {
return carapace.ActionExecCommand("pandoc", "--list-extensions", format)(func(output []byte) carapace.Action {
lines := strings.Split(string(output), "\n")
return carapace.ActionValues(lines[:len(lines)-1]...)
})
}).Tag("extensions")
}

// ActionHighlightStyles completes highlight styles
func ActionHighlightStyles() carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
if util.HasPathPrefix(c.Value) {
return carapace.ActionFiles(".theme")
}
return carapace.ActionValues("pygments", "kate", "monochrome", "breezeDark", "espresso", "zenburn", "haddock", "tango")
})
return carapace.ActionValues(
"breezeDark",
"espresso",
"haddock",
"kate",
"monochrome",
"pygments",
"tango",
"zenburn",
).Tag("highlight styles")
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package action
package pandoc

import (
"strings"
Expand Down

0 comments on commit ba50ee7

Please sign in to comment.