Skip to content

Commit

Permalink
fix mime to pandoc mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
joecorall committed Nov 16, 2024
1 parent 4169106 commit bc49618
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 4 deletions.
3 changes: 2 additions & 1 deletion ci/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ SERVICES=(
"crayfits"
"ffmpeg"
"whisper"
"pandoc"
)
for SERVICE in "${SERVICES[@]}"; do
URL="http://$SERVICE:8080/"
Expand Down Expand Up @@ -69,7 +70,7 @@ for SERVICE in "${SERVICES[@]}"; do
rm vtt.txt
elif [ "$SERVICE" == "pandoc" ]; then
curl -o result.tex \
-H "Accept: text/x-tex" \
-H "Accept: text/x-latex" \
-H "Content-Type: text/markdown" \
--data-binary "@$(pwd)/ci/fixtures/pandoc/input.md" \
"$URL"
Expand Down
4 changes: 2 additions & 2 deletions examples/pandoc/scyllaridae.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ cmdByMimeType:
cmd: /usr/local/bin/pandoc
args:
- "-f"
- "%source-mime-ext"
- "%source-mime-pandoc"
- "-t"
- "%destination-mime-ext"
- "%destination-mime-pandoc"
68 changes: 68 additions & 0 deletions internal/config/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,19 @@ func BuildExecCommand(message api.Payload, c *ServerConfig) (*exec.Cmd, error) {
a = fmt.Sprintf("%s:-", a)
}
args = append(args, a)
} else if a == "%source-mime-pandoc" {
a, err := MimeToPandoc(message.Attachment.Content.SourceMimeType)
if err != nil {
return nil, fmt.Errorf("unknown mime extension: %s", message.Attachment.Content.SourceMimeType)
}

args = append(args, a)
} else if a == "%destination-mime-pandoc" {
a, err := MimeToPandoc(message.Attachment.Content.DestinationMimeType)
if err != nil {
return nil, fmt.Errorf("unknown mime extension: %s", message.Attachment.Content.DestinationMimeType)
}
args = append(args, a)

} else if a == "%target" {
args = append(args, message.Target)
Expand Down Expand Up @@ -312,3 +325,58 @@ func (c *ServerConfig) GetFileStream(r *http.Request, message api.Payload, auth

return sourceResp.Body, http.StatusOK, nil
}

func MimeToPandoc(mimeType string) (string, error) {
mapping := map[string]string{
"text/x-bibtex": "bibtex",
"text/x-biblatex": "biblatex",
"application/xml": "bits",
"text/x-commonmark": "commonmark",
"text/x-commonmark+extensions": "commonmark_x",
"text/x-creole": "creole",
"application/vnd.citationstyles.csl+json": "csljson",
"text/csv": "csv",
"text/tab-separated-values": "tsv",
"text/x-djot": "djot",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": "docx",
"application/docbook+xml": "docbook",
"application/xml-dokuwiki": "dokuwiki",
"application/vnd.endnote+xml": "endnotexml",
"application/epub+zip": "epub",
"application/x-fictionbook+xml": "fb2",
"text/x-gfm": "gfm",
"text/x-haddock": "haddock",
"text/html": "html",
"application/x-ipynb+json": "ipynb",
"application/jats+xml": "jats",
"text/x-jira": "jira",
"application/json": "json",
"application/x-latex": "latex",
"text/markdown": "markdown",
"text/markdown+mmd": "markdown_mmd",
"text/markdown+phpextra": "markdown_phpextra",
"text/markdown+strict": "markdown_strict",
"text/x-mediawiki": "mediawiki",
"application/x-troff-man": "man",
"text/x-muse": "muse",
"application/vnd.haskell.native": "native",
"application/vnd.oasis.opendocument.text": "odt",
"application/x-opml+xml": "opml",
"text/x-org": "org",
"application/x-research-info-systems": "ris",
"application/rtf": "rtf",
"text/x-rst": "rst",
"text/x-txt2tags": "t2t",
"text/x-textile": "textile",
"text/x-tikiwiki": "tikiwiki",
"text/x-twiki": "twiki",
"application/x-typst": "typst",
"text/x-vimwiki": "vimwiki",
}

pandoc, ok := mapping[mimeType]
if !ok {
return "", fmt.Errorf("unknown mime extension: %s", mimeType)
}
return pandoc, nil
}
1 change: 0 additions & 1 deletion internal/config/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ func TestMimeTypes(t *testing.T) {
"application/pdf": "pdf",
"text/csv": "csv",
"text/markdown": "md",
"text/x-tex": "tex",
}

for mimeType, extension := range mimeTypes {
Expand Down

0 comments on commit bc49618

Please sign in to comment.