Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the auto-export snippet for per-file flow with Windows style paths #185

Closed
kaushalmodi opened this issue Aug 9, 2018 · 14 comments
Closed

Comments

@kaushalmodi
Copy link
Owner

kaushalmodi commented Aug 9, 2018

#182 (comment)

I've tried replacing ~/hugo_base_dir/content-org/ with ~/hugo_base_dir/org/ (since I named the directory that contains org files org/), and also the absolute path of the directory (C:\Users\steve\Desktop\Projects\steventammen.com\org) -- I'm on Windows.

May be replace the need of that snippet or the Local Variables eval with an Org bind variable or .dir-locals.el set variable, or something like that..

@kaushalmodi
Copy link
Owner Author

@StevenTammen Can you post the output of this: #182 (comment).

Knowing that output will help me better fix that snippet for Windows uses.

@StevenTammen
Copy link
Contributor

Sure. Here's the full output from an example file after I save:

Saving file c:/Users/steve/Desktop/Projects/steventammen.com/org/posts/test/blah2.org...
Wrote c:/Users/steve/Desktop/Projects/steventammen.com/org/posts/test/blah2.org
fname: "c:/Users/steve/Desktop/Projects/steventammen.com/org/posts/test/blah2.org"
[ox-hugo] Exporting ‘"Blah 2"’ (blah2.org)
org-babel-exp process bash at position 1108...
org-babel-exp process bash at position 1635...
org-babel-exp process bash at position 1805...
org-babel-exp process bash at position 1972...
org-babel-exp process bash at position 2201...
org-babel-exp process bash at position 2290...
org-babel-exp process bash at position 2370...
org-babel-exp process bash at position 2513...
org-babel-exp process bash at position 2616...
org-babel-exp process bash at position 3433...
org-babel-exp process bash at position 3709...
Saving file c:/Users/steve/Desktop/Projects/steventammen.com/content/posts/test/blah2.md...
Wrote c:/Users/steve/Desktop/Projects/steventammen.com/content/posts/test/blah2.md

The relevant bit then is

fname: "c:/Users/steve/Desktop/Projects/steventammen.com/org/posts/test/blah2.org"

@kaushalmodi
Copy link
Owner Author

That explains why it didn't work for you earlier.. Emacs represents paths internally with / (instead of \) even on Windows.

I'll post a cleaner solution for this tomorrow.

@kaushalmodi
Copy link
Owner Author

@StevenTammen Can you try the below and see if it still works. Ensure that you restart emacs so that the earlier workaround is not effective.

Note the doc string I have in there:

On Windows too, the directory paths must be specified with forward-slashes:
Wrong: C:\foo\bar\content-org
Right: C:/foo/bar/content-org/

(defconst my/hugo-org-content-dirs '("C:/Users/steve/Desktop/Projects/steventammen.com/org/")
  "List of directories containing Org files meant for per-file flow.

On Windows too, the directory paths must be specified with forward-slashes:
  Wrong: C:\\foo\\bar\\content-org\\
  Right: C:/foo/bar/content-org/")

(defun my/org-hugo-publish-current-buffer-as-post ()
  "Export current file if it's a valid post for per-file `ox-hugo' flow.

Current file is exported using `org-hugo-export-to-md' if:
- it is present in one of the directories in
  `my/hugo-org-content-dirs', and
- contains the \"#+title\" keyword.

This function is meant to be added to `after-save-hook' only for
Org files."
  (cl-letf ((fname (file-truename (buffer-file-name)))
            ((symbol-function 'file-in-org-content-dir-p)
             (lambda (dir)
               (string-prefix-p (file-truename dir) fname :ignore-case))))
    (when (and fname
               (cl-remove-if-not #'file-in-org-content-dir-p
                                 my/hugo-org-content-dirs)
               (save-excursion ;Check that the file has #+title keyword
                 (goto-char (point-min))
                 (>= (let ((case-fold-search t))
                      (how-many "^#\\+title:"))
                    1)))
      (org-hugo-export-to-md))))

(defun my/org-mode-hook-fn ()
  "My Org mode customization."
  (add-hook 'after-save-hook #'my/org-hugo-publish-current-buffer-as-post :append :local))

(add-hook 'org-mode-hook #'my/org-mode-hook-fn)

@kaushalmodi kaushalmodi changed the title Verify that the auto-export snippet for per-file flow works with Windows style paths Fix the auto-export snippet for per-file flow with Windows style paths Aug 10, 2018
@StevenTammen
Copy link
Contributor

The code above is not working for me. I had already tried changing the slashes when I was first having problems: double backslashes, forward slashes, etc. Nothing seemed to work

Here's the full code I tested in my .spacemacs user configuration section:

;; ox-hugo config, auto-export on save
(use-package ox-hugo
  :ensure t
  :after ox
  :init
(defconst my/hugo-org-content-dirs '("C:/Users/steve/Desktop/Projects/steventammen.com/org/")
  "List of directories containing Org files meant for per-file flow.

On Windows too, the directory paths must be specified with forward-slashes:
  Wrong: C:\\foo\\bar\\content-org\\
  Right: C:/foo/bar/content-org/")

(defun my/org-hugo-publish-current-buffer-as-post ()
  "Export current file if it's a valid post for per-file `ox-hugo' flow.

Current file is exported using `org-hugo-export-to-md' if:
- it is present in one of the directories in
  `my/hugo-org-content-dirs', and
- contains the \"#+title\" keyword.

This function is meant to be added to `after-save-hook' only for
Org files."
  (cl-letf ((fname (file-truename (buffer-file-name)))
            ((symbol-function 'file-in-org-content-dir-p)
             (lambda (dir)
               (string-prefix-p (file-truename dir) fname :ignore-case))))
    (when (and fname
               (cl-remove-if-not #'file-in-org-content-dir-p
                                 my/hugo-org-content-dirs)
               (save-excursion ;Check that the file has #+title keyword
                 (goto-char (point-min))
                 (>= (let ((case-fold-search t))
                      (how-many "^#\\+title:"))
                    1)))
      (org-hugo-export-to-md))))

(defun my/org-mode-hook-fn ()
  "My Org mode customization."
  (add-hook 'after-save-hook #'my/org-hugo-publish-current-buffer-as-post :append :local))

(add-hook 'org-mode-hook #'my/org-mode-hook-fn))

@kaushalmodi
Copy link
Owner Author

kaushalmodi commented Aug 10, 2018

Thanks for trying. Try this one another iteration:

;; ox-hugo config, auto-export on save
(use-package ox-hugo
  :ensure t
  :after ox
  :init
  (defconst my/hugo-org-content-dirs '("C:/Users/steve/Desktop/Projects/steventammen.com/org/")
    "List of directories containing Org files meant for per-file flow.

On Windows too, the directory paths must be specified with forward-slashes:
  Wrong: C:\\foo\\bar\\content-org\\
  Right: C:/foo/bar/content-org/")

  (defun my/org-hugo-publish-current-buffer-as-post ()
    "Export current file if it's a valid post for per-file `ox-hugo' flow.

Current file is exported using `org-hugo-export-to-md' if:
- it is present in one of the directories in
  `my/hugo-org-content-dirs', and
- contains the \"#+title\" keyword.

This function is meant to be added to `after-save-hook' only for
Org files."
    (cl-letf ((fname (buffer-file-name))
              ((symbol-function 'file-in-org-content-dir-p)
               (lambda (dir)
                 (message "dbg: dir: %S" dir)
                 (message "dbg: dir truename: %S" (file-truename dir))
                 (message "dbg: fname: %S" fname)
                 (message "dbg: is-prefix?: %S" 
                          (string-prefix-p (expand-file-name dir) fname :ignore-case))
                 (string-prefix-p (expand-file-name dir) fname :ignore-case))))
      (message "dbg: valid dirs: %S" (cl-remove-if-not #'file-in-org-content-dir-p
                                                       my/hugo-org-content-dirs))
      (message "dbg: num titles: %d" (save-excursion ;Check that the file has #+title keyword
                                       (goto-char (point-min))
                                       (let ((case-fold-search t))
                                         (how-many "^#\\+title:"))))
      (when (and fname
                 (cl-remove-if-not #'file-in-org-content-dir-p
                                   my/hugo-org-content-dirs)
                 (save-excursion ;Check that the file has #+title keyword
                   (goto-char (point-min))
                   (>= (let ((case-fold-search t))
                        (how-many "^#\\+title:"))
                      1)))
        (org-hugo-export-to-md))))

  (defun my/org-mode-hook-fn ()
    "My Org mode customization."
    (add-hook 'after-save-hook #'my/org-hugo-publish-current-buffer-as-post :append :local))

  (add-hook 'org-mode-hook #'my/org-mode-hook-fn))

This should work.. Also please paste the messages that get printed by this. If this works, I will update the ox-hugo manual with the final version without debug messages.

@StevenTammen
Copy link
Contributor

For some reason nothing is working now. The solution that was working last night is no longer working. It was working before I deleted that code from my config to try your later solution, but when I put it back in (in exactly the same place), it no longer works.

I have no idea what's going on. It may well be something entirely apart from your code (i.e., it may be related to my specific configuration). I'll try restarting things to see if I can't get at least something to work.

Any ideas?

@kaushalmodi
Copy link
Owner Author

I'll try restarting things to see if I can't get at least something to work.

That usually helps.

As I don't have emacs on Windows, I need your help to fix the above snippet.

If you can let me know the messages printed by my last snippet, this can be fixed.

I the meanwhile, I am working on a better way to have auto-exporting to work even for per-file flow. You wouldn't need any such snippet any more.

@StevenTammen
Copy link
Contributor

Well, I'm giving up since I'm getting frustrated. I really should have backed up my config file before I changed it, but I didn't, and now I can't get anything to work. I can still export manually, but perhaps I'll wait for that better solution before I spend more time dealing with the old snippets and their mysteriously inconsistent behavior.

I would be less annoyed if it had never worked to begin with...

kaushalmodi added a commit that referenced this issue Aug 10, 2018
Works for either flow: per-subtree or per-file

Fixes #185.
@kaushalmodi
Copy link
Owner Author

kaushalmodi commented Aug 10, 2018

Git commit more often! :)

Well... once your frustration has worn off try out this branch: https://github.com/kaushalmodi/ox-hugo/tree/auto-export-on-save

  1. With that branch checked out, and ox-hugo required from that (don't evaluate any of the above snippets in :init.. delete them for good). Manually require it .. I need to still work on it so that that manual step is not needed.. so it's not merged yet. Even if this step doesn't work for you.. I should have the finished version commited in master branch by Monday.
  2. Put this in C:/Users/steve/Desktop/Projects/steventammen.com/.dir-locals.el file:
("org"
 . ((org-mode . ((org-hugo-auto-export-on-save . t))))))
  1. Open one of the Org files in your "org" directory.
  2. Edit something and save.. should auto-export.. (fingers crossed.. worked for me :D)

@StevenTammen
Copy link
Contributor

StevenTammen commented Aug 10, 2018

I'd be happy to test, but I'm not entirely sure how to accomplish step 1 vis-a-vis Spacemacs. If you perhaps explained it in more detail I could try it. Or I could just wait.


Well, I used the following

dotspacemacs-additional-packages
'(
   (ox-hugo :fetcher github :repo "kaushalmodi/ox-hugo" :branch "auto-export-on-save")
 )

...

(defun dotspacemacs/user-config ()
  (require 'ox-hugo)
 )

And copied the code above into C:/Users/steve/Desktop/Projects/steventammen.com/.dir-locals.el, but I get .dir-locals error: Wrong type argument: listp, "org".

I might have done something wrong. It also looks like your code snippet above contains too many parentheses on the end (one extra). But maybe I'm wrong on that too.

kaushalmodi added a commit that referenced this issue Aug 11, 2018
Works for either flow: per-subtree or per-file

Fixes #185.
kaushalmodi added a commit that referenced this issue Aug 11, 2018
Works for either flow: per-subtree or per-file

Fixes #185.
@kaushalmodi
Copy link
Owner Author

This got auto-closed as I just merged the branch that was linked with this issue.

Follow the instructions on https://ox-hugo.scripter.co/doc/auto-export-on-saving/. Let me know if it finally works for you or not, or if the documentation needs any clarification.

@StevenTammen
Copy link
Contributor

It works perfectly for me, and this is much simpler for those of us who are just getting into elisp/Emacs packages. You also got this implemented really fast! 👍

Some documentation suggestions:

Clarifying exactly what code goes in the config file

On the auto-export page you say the following:

So with ox-hugo installed, add this to your Emacs config:

(require 'ox-hugo-auto-export)

But on the usage page, two of the methods presented (use-package and Spacemacs -- which is what I used) do not use require but use-package.

You could perhaps make this more clear by rephrasing to something like

So with ox-hugo installed, add this to your Emacs config:

(require 'ox-hugo-auto-export)
...
If you use use-package or are running Spacemacs, setting up auto-exporting is done a little differently. See usage.

Org directory

It took me a couple minutes to figure out exactly what dir/in/the/project/containing/org/files/ meant when I was adding

(("dir/in/the/project/containing/org/files/"
  . ((org-mode . ((org-hugo-auto-export-on-save . t))))))

Since for me the path was just org/, I was sitting there thinking "wow, this is really short compared to dir/in/the/project/containing/org/files/". Of course, that doesn't matter, but it did make me wonder initially if the path was supposed to be absolute.

Perhaps something like the below would be easier for us newer people?

If you want to enable auto-exporting for the whole project, add the below to the .dir-locals.el file in the project root. Note that dir/in/the/project/containing/org/files/ is the path of the org directory relative to the root directory.

(("dir/in/the/project/containing/org/files/"
  . ((org-mode . ((org-hugo-auto-export-on-save . t))))))

Multiple projects

Maybe this is just self-evident, but it was not documented as far as I could tell: is setting up ox-hugo with multiple projects as simple as adding a .dir-locals.el file to each project? Do you have to do anything else special?

If you can just add an extra file, you might make the section above read something like

If you want to enable auto-exporting for the whole project, add the below to the .dir-locals.el file in the project root. Note that dir/in/the/project/containing/org/files/ is the path of the org directory relative to the root directory.

(("dir/in/the/project/containing/org/files/"
  . ((org-mode . ((org-hugo-auto-export-on-save . t))))))

If you have multiple projects, simply repeat the process of adding this to the .dir-locals.el file for each project, and auto-exporting should work for all of them.

@kaushalmodi
Copy link
Owner Author

kaushalmodi commented Aug 11, 2018

It works perfectly for me, and this is much simpler for those of us who are just getting into elisp/Emacs packages. You also got this implemented really fast! 👍

Yay! 🎉 Glad it finally worked out. Thanks for the nudge to clean this up.

But on the usage page, two of the methods presented (use-package and Spacemacs -- which is what I used) do not use require but use-package.

use-package is alternative way to use require et al. I used use-package in Spacemacs snippet as I believe it comes installed with it. I have though, made the doc bit more clear about this.

If you use use-package or are running Spacemacs, setting up auto-exporting is done a little differently.

Not necessarily.. you can use the require method too (that's the inbuilt supported way; use-package is an external package that comes as part of Spacemacs).

It took me a couple minutes to figure out exactly what dir/in/the/project/containing/org/files/ meant when I was adding ..

OK, I have hopefully improved that section now.

Maybe this is just self-evident, but it was not documented as far as I could tell: is setting up ox-hugo with multiple projects as simple as adding a .dir-locals.el file to each project? Do you have to do anything else special?

Nope, that's it. Multiple projects support was one of the reasons to do this whole revamp.

Please check out the updated documentation -- I have incorporated your feedback in there. Thanks!


PS: I appreciate your well-formatted replies.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants