Closed matta closed 1 year ago
Something odd is happening with respect to require-final-newline
. I set up this advice to set require-final-newline
nil
when exporting. The point is to suppress the prompt. Something happens while executing org-hugo-export-to-md
that causes require-final-newline
to become its global value after the export operation occurs.
(defun my-set-require-final-newline (func &rest args)
(let ((require-final-newline nil))
(message "BEGIN with require-final-newline set to %s" require-final-newline)
(apply func args)
(message "END with require-final-newline set to %s" require-final-newline)))
(advice-add #'org-hugo-export-to-md
:around
#'my-set-require-final-newline)
I see this in *Messages*
:
BEGIN with require-final-newline set to nil
Buffer about.md does not end in newline. Add one? (y or n) y
Wrote /home/matt/git/hugo-quickstart/content/about.md
END with require-final-newline set to ask
...and when I (setq require-final-newline nil)
globally the prompt stops happening, but the generated .md
file does not have a final newline.
So we're back to issue #666.
I sent a patch to fix this in ox-hugo
itself, but for those who can't wait the advice I set up in the :config
forms below hacks ox-hugo
to fix the issue too (and will remain harmless even after the fix lands in an ox-hugo
version you are using).
(use-package ox-hugo
:ensure t ; Auto-install the package from Melpa
:pin melpa ; `package-archives' should already have ("melpa" . "https://melpa.org/packages/")
:after ox
:config
(defun my-ox-hugo-ensure-newline (str)
"Return STR with a newline appended if there is not one already."
(if (string-suffix-p "\n" str)
str
(concat str "\n")))
(advice-add #'org-hugo-inner-template
:filter-return
#'my-ox-hugo-ensure-newline))
Thanks for your contribution. I am on vacation and will be able to take a look at this after October second week.
This issue should be now fixed as I merged your https://github.com/kaushalmodi/ox-hugo/pull/672 PR manually in https://github.com/kaushalmodi/ox-hugo/commit/8fbd301c03296253bfb5ee193770e1da3ea2df1a.
The main change from your PR was to not entirely remove org-trim
from org-hugo-inner-template
, but to alteast trim leading blank lines using string-trim-left
.
org-blackfriday-inner-template
is not used by ox-hugo
, so I left it unchanged.
Closing this issue as there was no response.
Update I've rewritten this issue based on new understanding.
How Emacs treats
require-final-newline
The default value for Emacs'
require-final-newline
isnil
, but most programming and text modes setrequire-final-newline
tot
based on the value ofmode-require-final-newline
.This can be confirmed by setting
require-final-newline
tonil
and then loading up a buffer intext-mode
, ormarkdown-mode
,org-mode
or any programming mode. The buffer-local value becomest
.So, the behavior is that in most text and programming modes Emacs appends a final newline automatically, without prompting the user, regardless of their
require-final-newline
setting. This suggests that most users will leaverequire-final-newline
alone, since Emacs just does the right thing in "text and programming" buffers.Emacs handles
(setq require-final-newline 'ask)
in a specific and documented way. From the docstring:In my case, I also have this set to
ask
and exporting fromox-hugo
causes a prompt about adding the final newline.How Org handles
require-final-newline
It seems that most org exporters ensure they append the newline themselves, without leaning on
file.el
and the user'srequire-final-newline
setting. I know this because I have(setq require-final-newline 'ask)
and I use other exporters without being prompted or seeing exported files without newlines.This is not the first time this issues has happened with org export. Here is a similar patch for the same issue sent against org-latex export, but the code has changed a lot since then:
How
ox-hugo
handlesrequire-final-newline
For Markdown generated by
ox-hugo
the generated buffer appears to use the user's globalrequire-final-newline
value. This is strange because the same user can edit Markdown files and will getrequire-final-newline
set tot
by way ofmode-require-final-newline
, since Markdown inherits from either text mode or a prog mode.Note that there is some weird stuff happening during a call to
org-hugo-export-to-md
with respect to this variable. See my next comment: https://github.com/kaushalmodi/ox-hugo/issues/671#issuecomment-1253006047.Actual Behavior
First:
(setq require-final-newline 'ask)
Then export with
ox-hugo
. It prompts to add the final newline to the.md
file.Then:
(setq require-final-newline nil)
Then export with
ox-hugo
. It will generate Markdown without a final newline.Finally:
(setq require-final-newline t)
Then export with
ox-hugo
. It will generate Markdown with a final newline.I have not observed this with other org export formats (html, text). They seem to unconditionally append a final newline without prompting.
Expected Behavior
The "Actual Behavior" above is Emacs behaving as it should given a buffer that isn't in a text or programming mode. I suspect that
ox-hugo
generates Markdown in perhaps fundamental mode, and this is the root of the problem.Ideally,
ox-hugo
would change to generate Markdown files "as if" they were edited in Markdown mode. This would include always behaving as ifrequire-final-newline
wast
, as it is in text and programming modes.This would bring
ox-hugo
behavior in line with most text and programming modes, and most user expectations.I don't think this issue is best resolved by asking the user to set
require-final-newline
tot
. That option there to control Emacs behavior when it encounters an arbitrary file of unknown format (i.e. not a file known to be text, markup, source code, etc). It is the job of specific modes to decide ifmode-require-final-newline
is most appropriate instead. Hereox-hugo
, or perhaps org mode itself, should play the role of these modes and generate output that is most sensible to the format generated -- i.e. always with a final newline for Markdown content. It should allow the user to leaverequire-final-newline
to something they want for all other "don't know the file format" situations.How to Reproduce the Issue
See Actual Behavior
Example Org File
n/a (I think any will do)
Generated Markdown File or Error
n/a (I think any will do)
Ox-Hugo Debug Information
Debug information for
ox-hugo
Emacs Version
Org Version
Hugo Version
Org
load-path
shadowsNo Org mode shadows found in
load-path
Debug Info