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

Leading tabs in code block are not retained in org reader with -p #10071

Closed
minyez opened this issue Aug 8, 2024 · 9 comments
Closed

Leading tabs in code block are not retained in org reader with -p #10071

minyez opened this issue Aug 8, 2024 · 9 comments

Comments

@minyez
Copy link

minyez commented Aug 8, 2024

Explain the problem.
I tried to convert an org-mode file to Markdown. When using pandoc with -p option to preserve the tabs in the Makefile source block, I found the leading tab is converted to single space while the other tabs remain unexpanded.

Minimal example

$ cat example.org
#+begin_src makefile
%.o: %.cpp
	$(CXX)	-o	$@	$<
#+end_src

There is a leading tab and tabs between entries in the directive. When converted to markdown with and without -p

$ pandoc -f org -t json -p example.org
{"pandoc-api-version":[1,23,1],"meta":{},"blocks":[{"t":"CodeBlock","c":[["",["makefile"],[]],"%.o: %.cpp\n $(CXX)\t-o\t$@\t$<\n"]}]}
$ pandoc -f org -t json example.org
{"pandoc-api-version":[1,23,1],"meta":{},"blocks":[{"t":"CodeBlock","c":[["",["makefile"],[]],"%.o: %.cpp\n    $(CXX)  -o  $@  $<\n"]}]}

On the other hand, the markdown reader seems fine (I don't know how to make markdown block within a code block correctly rendered, so I added a comma before backticks)

$ cat example.md
,```makefile
%.o: %.cpp
	$(CXX)	-o	$@	$<
,```
$ pandoc -f markdown -t json -p example.md
{"pandoc-api-version":[1,23,1],"meta":{},"blocks":[{"t":"CodeBlock","c":[["",["makefile"],[]],"%.o: %.cpp\n\t$(CXX)\t-o\t$@\t$<"]}]}
$ pandoc -f markdown -t json example.md
{"pandoc-api-version":[1,23,1],"meta":{},"blocks":[{"t":"CodeBlock","c":[["",["makefile"],[]],"%.o: %.cpp\n    $(CXX)  -o  $@  $<"]}]}

Expected behavior

$ pandoc -f org -t json -p example.org
{"pandoc-api-version":[1,23,1],"meta":{},"blocks":[{"t":"CodeBlock","c":[["",["makefile"],[]],"%.o: %.cpp\n\t$(CXX)\t-o\t$@\t$<\n"]}]}

Pandoc version?
pandoc is installed using homebrew on macOS Sonoma

$ pandoc --version
pandoc 3.3
Features: +server +lua
Scripting engine: Lua 5.4
@minyez minyez added the bug label Aug 8, 2024
@tarleb
Copy link
Collaborator

tarleb commented Aug 8, 2024

Pandoc's behavior is consistent with that of the Emacs org-mode exporters as far as I can tell. Is there an Org-mode option that would allow to preserve tabs when exporting?

@minyez
Copy link
Author

minyez commented Aug 8, 2024

Hi @tarleb as far as I know there is no org-export variable directly related to tab, but there is a variable called org-src-preserve-indentation to control how to handle code block indentation on export. When it is nil, the minimum leading whitespace characters (including tabs) are removed and the code lines are re-indented. When non-nil, the leading characters are preserved.

For the current example, I use org-export to do an org-to-org conversion. Emacs is run with emacs -Q. When org-src-preserve-indentation is t

#+begin_src makefile
%.o: %.cpp
	$(CXX)	-o	$@	$<
#+end_src

All tabs are retained. When org-src-preserve-indentation is nil

#+begin_src makefile
  %.o: %.cpp
          $(CXX)	-o	$@	$<
#+end_src

It replaces the leading tab with 8 white spaces, and add 2 spaces on both lines (the number of spaces depends on another variable). The other tabs are preserved.

@tarleb
Copy link
Collaborator

tarleb commented Aug 8, 2024

Interesting, thanks. If I understand correctly, then setting org-src-preserve-indentation to a non-nil value is equivalent to adding the -i flag to all src blocks.

In principle, it should be enough to add the -i flag to the src block while also calling pandoc with the -p command line flag.

#+begin_src makefile -i
%.o: %.cpp
	$(CXX)	-o	$@	$<
#+end_src

However, it seems that the leading tab is currently lost and converted to a single space. I'm therefore re-adding the bug label.

@tarleb tarleb added the bug label Aug 8, 2024
@minyez
Copy link
Author

minyez commented Aug 8, 2024

Thanks for your infomation, I was never aware of the capability to add this kind of flag to src block. I tried just now but didn't find any difference when exporting it. Did I miss something?

$ cat example.org
#+begin_src makefile -i
%.o: %.cpp
	$(CXX)	-o	$@	$<
#+end_src
$ pandoc -f org -t json -p example.org
{"pandoc-api-version":[1,23,1],"meta":{},"blocks":[{"t":"CodeBlock","c":[["",["makefile"],[]],"%.o: %.cpp\n $(CXX)\t-o\t$@\t$<\n"]}]}

@tarleb tarleb closed this as completed in b2755f7 Aug 8, 2024
@tarleb
Copy link
Collaborator

tarleb commented Aug 8, 2024

Exactly, that's the bug, and it should be fixed now. Please try the next nightly built, which should become available in about 15 hours.

@minyez
Copy link
Author

minyez commented Aug 8, 2024

I will try then, thanks a lot for the quick fix! A remaining question is: is the -i flag the same as the one of pandoc CLI, i.e. -i/--incremental?

The reason I am asking is that there seems no way to retain -i-type source block flags when doing an org-to-org export, which is necessary when using ox-pandoc for conversion. So I would prefer using a command line flag similar to -i-type flag for certain source block. Sorry I was completely wrong. -i is retained in org-to-org export.

@tarleb
Copy link
Collaborator

tarleb commented Aug 8, 2024

The -i flag of the pandoc CLI and of orgmode src blocks are entirely separate; they only incidentally share the same characters.

@minyez
Copy link
Author

minyez commented Aug 8, 2024

Thanks for clarification, I understand now it is a orgmode src block flag rather than a pandoc one. I was only aware of head arguments starting with :. Could you please maybe share your knowledge of similar - flags, or direct me to some documentation? I searched the "16 Working with Source Code" section of org-mode manual, but found nothing relevant.

@tarleb
Copy link
Collaborator

tarleb commented Aug 8, 2024

E.g. https://orgmode.org/manual/Literal-Examples.html
But pandoc supports only a subset of those.

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