0

I hava an input file name like /path/to/orgfiles/file.org

I have an output file name like /path/to/mdfiles/file.md

This two path have been set into two variables:

inputfile = "/path/to/orgfiles/file.org"

outputfile = "/path/to/mdfiles/file.md"

Now I want to use org-export-to-file from ox.el to export inputfile to outputfile, how?

It seems that org-export-to-file can only export file from the current buffer, I couldn't find any function which can specified the INPUT file :(

I tried org-publish but it seems should be used when project involved, doesn't meet my need too.

  • I don't think you can explicitly tell org to take a certain file and export it to an arbitrary path. You can wrap export function to open your file in a buffer, export it, and then use the return value, which will be the name of the exported file to move it where you want your output to be. – DoMiNeLa10 Sep 8 '18 at 11:18
  • Thanks @Drew , I've deleted the one on stackoverflow, first time ask a question, I'll keep it in mind :) – ArchCST Sep 9 '18 at 4:45
1

@DoMiNeLa10 is correct. You have to create a function to do something like this. Here is one that is interactive. M-x export-file-md will prompt you for an input file and an output file name, and then export the org-file to the desired md-file name and open it. It does a little buffer management, e.g. closing the org file if it was not already open.

This isn't the only way to get this done, but it works fine as far as I can tell.

(defun export-file-md (org-file md-file)
  "Export ORG-FILE to MD-FILE."
  (interactive "f\nf")
  (unless (string= "org" (file-name-extension org-file))
    (error "INFILE must be an org file."))
  (unless (string= "md" (file-name-extension md-file))
    (error "OUTFILE must have an .md extension."))
  (let ((current-buffers (buffer-list))
    (tmp-buffer) 
    (open (find-buffer-visiting org-file))
    (org-file-buffer (find-file-noselect org-file)))
    (with-current-buffer org-file-buffer
      (with-current-buffer (setq tmp-buffer (org-org-export-as-org))
    (insert (format "#+EXPORT_FILE_NAME: %s\n" md-file))
    (org-open-file (org-md-export-to-markdown))))
    (kill-buffer tmp-buffer)
    (unless open (kill-buffer org-file-buffer))))
  • Thanks a lot, find-file-noseletct and with-current-buffer is what I'm looking for. (unless open (kill-buffer org-file-buffer)) this one makes it even better with closing the buffers which I don't need, your answer really inspired a lot! – ArchCST Sep 9 '18 at 4:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.