larstvei / ox-gfm

Github Flavored Markdown Back-End for Org Export Engine
230 stars 44 forks source link

export as a command #35

Open nrolland opened 5 years ago

nrolland commented 5 years ago

Is there a way to programmatically export an org file using an elisp script ?

I am trying this and I am not sure what to do with an "org-publish-cache" error I get

khinshankhan commented 5 years ago

I have found 2 ways to use it thus far. The first is to make an org file and have it like so:

#+BEGIN_SRC emacs-lisp

(require 'find-lisp)

(defun org2md (file-name)
  (save-window-excursion
    (find-file file-name)
    (org-gfm-export-to-markdown)))

(mapc 'org2md (find-lisp-find-files "PATH/TO/ORG/FILES" "\\.org$"))
#+END_SRC

and run it using like C-c C-c in emacs.

The second is to make an elisp script and have the contents like so:

(require 'find-lisp)

(require 'org)
(eval-after-load "org"
  '(load "PATH/TO/ox-gfm.el"))

(defun org2md (file-name)
  (save-window-excursion
    (find-file file-name)
    (org-gfm-export-to-markdown)))

(mapc 'org2md (find-lisp-find-files (elt argv 0) "\\.org$"))

and run it using like emacs --script fname.el PATH/TO/ORG/FILES

The load can be skipped by copying over the file's contents. For some reason, just a '(require 'ox-gfm nil t) doesn't work, and I have no idea why. Been racking my brains over this for the better part of the day.

You can use combinations of the methods I've listed above (I've tested a few variations, but I'm too lazy to write them out..). These are generalized to convert a directory full of org files to be gfm files, because I personally needed that, so hopefully this helps with your problem too (and any other onlookers).

It should be noted that I'm fairly new to the elisp scripting game, so I'm not sure if I've done extra work or if there are other ways, but these worked thus far.

khinshankhan commented 5 years ago

One more thing about the path to ox-gfm.el, I found the following worked well:

'(load (concat user-emacs-directory "elpa/ox-gfm-20170628.2102/ox-gfm.el"))

since it utilizes user-emacs-directory (well assumes you installed the packaged).

I wanted this as a separate comment, because it's sort of separate information. Sorry if any of it was lengthy or annoying. ¯_(ツ)_/¯

Would love to hear if anyone had any solutions or feedback though!

larstvei commented 5 years ago

Does this work for exporting latex, html, normal markdown or plain text?

khinshankhan commented 5 years ago

I haven't thoroughly tested between other file types since I actually found this yesterday and only needed the org to gfm. However, if it works properly with the M-x org-gfm-export-to-markdown, I would think you just need to edit the regex that choose files (ie (mapc 'org2md (find-lisp-find-files (elt argv 0) "\\.org$"))) to be choose the files you want (eg for latex it may be (mapc 'org2md (find-lisp-find-files (elt argv 0) "\\.tex$"))).

Also noticed a slight typo from before and fixed it.