msnoigrs / ox-rst

reStructuredText Back-End for Org-Mode Export Engine
116 stars 18 forks source link

Add consideration for inline image in a paragraph. #27

Closed ghost closed 7 years ago

ghost commented 7 years ago

The function "org-rst-paragraph" I modified was not considering a parameter value as directives.
Therefore, when the content value was multi-line directive expression such as inline image with a caption,
it treat content as a paragraph like below:

| .. figure:: /path/to/image.jpg
| caption

So I added a conditional to avoid any directives to be treated as a paragraph.

msnoigrs commented 7 years ago

Why don't you use 'export fragments`?

+BEGIN_EXPORT rst

.. figure:: /path/to/image.jpg caption

+END_EXPORT

ghost commented 7 years ago

Sorry, I didn't make my point clear enough in my pull request comment above.
I didn't mean rst expression written directly in org file.

What I mentioned was, a valid org expression like below,

#+CAPTION: caption
[[/path/to/image.jpg]]

being converted to:

| .. figure:: /path/to/image.jpg
| caption

I checked the value of "contents" which org-rst-paragraph received, and it turned out to be rst expression:

.. figure:: /path/to/image.jpg
caption

The input expression looks to be converted to rst expression first, and passed to org-rst-paragraph after that.
I'm not familiar with org-export framework conversion process, but I think each function might receive converted value in some other cases.
For org-rst-paragraph, treating converted directive lines as a plain text paragraph to add "| " to each line head causes trouble.
That's why I thought it was necessary to add avoidance for rst directive that starts with "..".

msnoigrs commented 7 years ago

Hmm. It works file for me without your PR. Which version of Org do you use?

msnoigrs commented 7 years ago

I guess you might want that 'line-break-preservation` \n: option to nil.

+OPTIONS: \n:nil

see: http://orgmode.org/manual/Export-settings.html#Export-settings

ghost commented 7 years ago

Thanks for your suggestion, but from:

#+CAPTION: caption
#+OPTION:\n:nil
[[/path/to/img.jpg]]

I got:

.. image:: /path/to/img.jpg

The caption imformation is lost.

My org-version is 9.0.3, and please make sure that org-export-preserve-breaks variable is set to t because org-rst-paragraph just returns contents value if org-export-preserve-breaks is set to nil.

msnoigrs commented 7 years ago

Please try this:

+OPTIONS: \n:nil

+CAPTION: caption

[[/path/to/img.jpg]]

ghost commented 7 years ago

Thanks I tried that.
From the code you suggested, I actually got:

.. figure:: /path/to/img.jpg

    caption

So it works.

However, specifying nil to \n option effect the whole file.

Example:

This line is a part of a paragraph.
So I need these lines to be added "| " prefix.
And I want to insert an image with caption below.

#+OPTIONS: \n:nil

#+CAPTION: caption
[[/path/to/img.jpg]]

I expect:

| This line is a part of a paragraph.
| So these lines need "| " prefix.
| And I want to insert an image with caption below.

.. figure:: /path/to/img.jpg

    caption

However, I get:

This line is a part of a paragraph.
So these lines need "| " prefix.
And I want to insert an image with caption below.

.. figure:: /path/to/img.jpg

    caption

and, without setting nil to \n option:

This line is a part of a paragraph.
So I need these lines to be added "| " prefix.
And I want to insert an image with caption below.

#+CAPTION: caption
[[/path/to/img.jpg]]

I get:

| This line is a part of a paragraph.
| So these lines need "| " prefix.
| And I want to insert an image with caption below.

| .. figure:: /path/to/img.jpg
| caption

When I write in org-mode with org-export-preserve-breaks set to t and try to insert some images, I have to edit rst file after conversion.
That's the problem I thought I should fix to get the right rst expressions from the first conversion.

msnoigrs commented 7 years ago

As you say, there is a slight problem when org-export-preserve-breaks is t, but It's not possible to get states of link(figure/image) in paragraph for the moment. AFAIK, line breaks will not preserved except for 'Line Blocks' by the reStructuredText specification, and org-export-preserve-breaks is global or individual file setting. Therefor ox-rst.el does not support org-export-preserve-breaks is t for the moment. In addition, your PR is not valid for when normal paragraphs have begging '..' sentences.

If you use 'Verse block', you can get your expected results.

#+OPTIONS: \n:nil

#+BEGIN_VERSE
This line is a part of a paragraph.
So I need these lines to be added "| " prefix.
And I want to insert an image with caption below.
#+END_VERSE

#+CAPTION: caption
[[/path/to/img.jpg]]
ghost commented 7 years ago

I know it works with VERSE block, I actually did so at first.
But I didn't like it because it's only necessary for ox-rst exporting, which means I have to change my way to write org document reducing its simplicity of structure.

However, I understand what you've decided and you're right about the plain sentence starting with "..". I don't know how to tell it from converted reStructuredText in the conversion process right now. Maybe conditional with ".. " will do a bit better, but still not proper enough.

Closing this for now.
Thank you for your patience!