Sublime Forum

Pandoc command from Sublime

#1

Hi,
I am quite new to Sublime Text.
Usually, I convert my *.md files to *.docx from command prompt, using

pandoc --filter pandoc-citeproc -t docx 1.md -o 1.docx

Is it possible to invoke this command from Sublime itself?
I suppose that there is trivial solution, but I am a n00b in this…

0 Likes

#2

A general rule of thumb for Sublime is that if there’s a command that you can execute from a command prompt, you can automate it in Sublime. The only exception to this is that Sublime doesn’t directly support gathering input from something that you run (i.e. an interactive program), so for that you need a bit of extra setup.

What you want for something like this is a build system. Despite the name, this is something that you can provide exactly what you’d type on the command line and have Sublime execute it for you.

For your sample, that would look something like the following example:

{
    "shell_cmd": "pandoc --filter pandoc-siteproc -t \"${file}\" -o \"${file_base_name}.docx\"",
    "working_dir": "${file_path}",
    "selector": "text.html.markdown"
}

The shell_cmd specifies what you’d type (with various variables that expand out based on the current file name), working_dir specifies what the “current” directory is set to while the program executes, and the selector tells Sublime in what circumstances it should automatically select this build.

To use it, select Tools > Build System > New Build System from the menu, replace the stub text with this, and then save it as a sublime-build file in the location that Sublime will default to (which is your User package).

You can then select it directly from Tools > Build System in the menu (it will appear with the same name as you used when you saved the file), or select Automatic from that menu and Sublime will pick this build when the current file is a markdown file.

Important Tip: external programs expect the file to be on disk so they can access them, so make sure that if you create a new file, you save it manually at least once to give it a name. The Tools > Save All On Build item will make sure unsaved files are automatically saved before a build starts, but this only applies to files that have a name on disk already.

The links above are to the documentation on build systems. If you need more guidance/examples, I have a video series on build systems that covers the topic in details.

1 Like

#3

Hi, @OdatNurd
Thank you for detailed answer. I use your code, but there is error

Unknown extension:  ukim

My files are in D:\OneDrive - UKIM - Rector’s office\Documents!Notes
So, I watch your videos (very helpful thank you) and try

"PATH": "$PATH;D:/OneDrive - UKIM - Rector's office/Documents/!Notes"

The same error.
Then I make new project in D:\1 with 1.md in the folder
New error

Unknown output format d:\1\1.md

I am lost.

0 Likes

#4

Those errors are errors coming out of the pandoc tool; if Windows was having trouble finding the executable you’d see something different.

Looks like I accidentally glossed over part of the command line that you provided, sorry about that; try this instead:

{
    "shell_cmd": "pandoc --filter pandoc-siteproc -t docx \"${file_name}\" -o \"${file_base_name}.docx\"",
    "working_dir": "${file_path}",
    "selector": "text.html.markdown"
}

The value of the shell_cmd is what you’d enter into the shell; I missed out the value for the argument of the -t command line argument, so it looks like Pandoc was interpreting the name of the file as the document type.

This also slightly changes the variable expansion for the name of the file to compile so that it just contains the file name and not the full path leading up to it. That probably doesn’t matter (I don’t use pandoc personally), but it never hurts to go with what you know works. :slight_smile:

1 Like

#5

@OdatNurd

Thank you, it’s working now. :smiley: Just one typo in the code. Instead

pandoc-siteproc

it’s

pandoc-citeproc

EDIT:

Great videos. Using them I manage to start MS Word to open the created *docx file, but

{
"shell_cmd": "pandoc --filter pandoc-citeproc -t docx \"${file_name}\" -o \"${file_base_name}.docx\"",
"working_dir": "${file_path}",
"selector": "text.html.markdown",
// This command open markdown file in MS Word like .txt file
//"shell_cmd": "\"C:/Program Files (x86)/Microsoft Office/Office16/winword.exe\" \"$file\"",
// This command give an error in MS Word "Sorry we can't find your file"
//"shell_cmd": "\"C:/Program Files (x86)/Microsoft Office/Office16/winword.exe\" \"${file_base_name}.docx\""
}

So, I figured that I have to somehow wait for *docx file to be produced, but the question is: how?

EDIT 2: OK, I thing I found other solution: Pandoc package convert my *md to *docx, but w/o citations. This customization works great.

However, I think that @OdatNurd solution is more elegant, if I can convert and open *docx in one build.

And @OdatNurd thank you once more for your detailed answer to my questions and for great videos, I learn a lot from them. :+1:

1 Like