Skip to content

Commit

Permalink
Merge pull request #646 from kaushalmodi/fix-dont-add-extra-blank-lin…
Browse files Browse the repository at this point in the history
…es-in-lists

fix: Remove the blank line inserted between code blocks and captions
  • Loading branch information
kaushalmodi authored May 27, 2022
2 parents eac4b45 + aa79350 commit 4cc8de4
Show file tree
Hide file tree
Showing 18 changed files with 123 additions and 55 deletions.
67 changes: 66 additions & 1 deletion ox-blackfriday.el
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ tag needs to be `python'."
;;; Define Back-End

(org-export-define-derived-backend 'blackfriday 'md
:filters-alist '((:filter-parse-tree . org-md-separate-elements))
:filters-alist '((:filter-parse-tree . org-blackfriday-separate-elements))
;; Do not clutter the *Org Exporter Dispatch* menu.
;; :menu-entry
;; '(?b "Export to Blackfriday Flavored Markdown"
Expand Down Expand Up @@ -731,6 +731,71 @@ This function is mostly a copy of
html-str))



;;; Filter Functions

;; This function is adapted from `org-md-separate-elements'.
(defun org-blackfriday-separate-elements (tree _backend info)
"Fix blank lines between elements.
TREE is the parse tree being exported.
INFO is a plist used as a communication channel.
Enforce a blank line between elements. There are 3 exceptions
to this rule:
1. Preserve blank lines between sibling items in a plain list,
2. In an item, remove any blank line before the very first
paragraph and the next sub-list when the latter ends the
current item.
3. In an item, if a paragraph is immediately followed by an src
or example block, don't add a blank line after the paragraph.
4. In an item, if an src or example block doesn't have a caption
and is immediately followed by a paragraph, don't add a blank
line after that src or example block."
(org-element-map tree (remq 'item org-element-all-elements) ;Exception 1 in the doc-string
(lambda (el)
(let ((post-blank (cond
;; Exception 2 in the doc-string.
((and (eq (org-element-type el) 'paragraph)
(eq (org-element-type (org-element-property :parent el)) 'item)
(org-export-first-sibling-p el info)
(let ((next-el (org-export-get-next-element el info)))
(and (eq (org-element-type next-el) 'plain-list)
(not (org-export-get-next-element next-el info)))))
0)
;; Exception 3 in the doc-string (paragraph -> src-block).
((and (eq (org-element-type el) 'paragraph)
(eq (org-element-type (org-element-property :parent el)) 'item)
(let ((next-el (org-export-get-next-element el info)))
(memq (org-element-type next-el) '(src-block example-block))))
0)
;; Exception 4 in the doc-string (caption-less src-block -> paragraph).
;; If an src or example block has a caption,
;; that caption will be wrapped in an HTML
;; div block. In that case, we *do* need to
;; leave a blank line after the div block (CommonMark).
((and (memq (org-element-type el) '(src-block example-block))
(eq (org-element-type (org-element-property :parent el)) 'item)
(null (org-element-property :caption el)) ;<-- "no caption" check
(let ((next-el (org-export-get-next-element el info)))
(memq (org-element-type next-el) '(paragraph))))
0)
(t
1))))
(org-element-put-property el :post-blank post-blank)
;; (message "[org-blackfriday-separate-elements DBG] %S post-blank: %d"
;; (org-element-type el)
;; (org-element-property :post-blank el))
)))
;; Return updated tree.
tree)



;;; Transcode Functions

Expand Down
2 changes: 1 addition & 1 deletion ox-hugo.el
Original file line number Diff line number Diff line change
Expand Up @@ -3436,7 +3436,7 @@ their Hugo site's config."
(caption-str
(org-html-convert-special-strings ;Interpret em-dash, en-dash, etc.
(org-export-data-with-backend caption 'html info))))
(format (concat "\n\n<div class=\"src-block-caption\">\n"
(format (concat "\n<div class=\"src-block-caption\">\n"
" <span class=\"src-block-number\">%s:</span>\n"
" %s\n"
"</div>")
Expand Down
26 changes: 26 additions & 0 deletions test/site/content-org/all-posts.org
Original file line number Diff line number Diff line change
Expand Up @@ -1731,6 +1731,9 @@ Another such example, but with space before a hyphen in source block:
:EXPORT_DATE: 2017-08-01
:EXPORT_FILE_NAME: list-has-src-block-but-no-list-syntax
:END:
#+begin_description
Test source blocks inside list.
#+end_description
This case is not affected by /Blackfriday/ [[https://github.com/russross/blackfriday/issues/239][Issue #239]] as the fenced
code block does not have Markdown syntax lists.
- List item 1
Expand All @@ -1740,6 +1743,29 @@ code block does not have Markdown syntax lists.
=def=
#+end_src
- List item 2
-----
{{{oxhugoissue(645)}}}

1. paragraph -> src block with caption -> paragraph -> src block
#+name: code__foo
#+caption: Foo
#+begin_src emacs-lisp
(message "hey")
#+end_src
sandwiched paragraph
#+begin_src emacs-lisp
(message "hey")
#+end_src
last paragraph
2. paragraph -> src block *without* caption -> paragraph -> src block
#+begin_src emacs-lisp
(message "hey")
#+end_src
sandwiched paragraph
#+begin_src emacs-lisp
(message "hey")
#+end_src
last paragraph
**** Source block with list syntax but not in a list
:PROPERTIES:
:EXPORT_DATE: 2017-08-01
Expand Down
5 changes: 0 additions & 5 deletions test/site/content/posts/code-fenced-src-blocks-default.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ Here are few variables that you might like to change in the `local.mk`:

`prefix`
: Org installation directory

```makefile
prefix = /dir/where/you/want/to/install/org # Default: /usr/share
```

The `.el` files will go to `$(prefix)/emacs/site-lisp/org` by
default. If you'd like to change that, you can tweak the
`lispdir` variable.
Expand All @@ -24,15 +22,13 @@ Here are few variables that you might like to change in the `local.mk`:
: Org Info installation directory. I like to keep the
Info file for development version of Org in a separate
directory.

```makefile
infodir = $(prefix)/org/info # Default: $(prefix)/info
```

`ORG_MAKE_DOC`
: Types of Org documentation you'd like to build by
default.

```makefile
# Define below you only need info documentation, the default includes html and pdf
ORG_MAKE_DOC = info pdf card # html
Expand All @@ -42,7 +38,6 @@ Here are few variables that you might like to change in the `local.mk`:
: Packages from the `contrib/` directory that
you'd like to build along with Org. Below are the ones on my
_must-have_ list.

```makefile
# Define if you want to include some (or all) files from contrib/lisp
# just the filename please (no path prefix, no .el suffix), maybe with globbing
Expand Down
5 changes: 0 additions & 5 deletions test/site/content/posts/code-fenced-src-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ Here are few variables that you might like to change in the `local.mk`:

`prefix`
: Org installation directory

```makefile
prefix = /dir/where/you/want/to/install/org # Default: /usr/share
```

The `.el` files will go to `$(prefix)/emacs/site-lisp/org` by
default. If you'd like to change that, you can tweak the
`lispdir` variable.
Expand All @@ -25,15 +23,13 @@ Here are few variables that you might like to change in the `local.mk`:
: Org Info installation directory. I like to keep the
Info file for development version of Org in a separate
directory.

```makefile
infodir = $(prefix)/org/info # Default: $(prefix)/info
```

`ORG_MAKE_DOC`
: Types of Org documentation you'd like to build by
default.

```makefile
# Define below you only need info documentation, the default includes html and pdf
ORG_MAKE_DOC = info pdf card # html
Expand All @@ -43,7 +39,6 @@ Here are few variables that you might like to change in the `local.mk`:
: Packages from the `contrib/` directory that
you'd like to build along with Org. Below are the ones on my
_must-have_ list.

```makefile
# Define if you want to include some (or all) files from contrib/lisp
# just the filename please (no path prefix, no .el suffix), maybe with globbing
Expand Down
5 changes: 0 additions & 5 deletions test/site/content/posts/highlight-shortcode-src-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ Here are few variables that you might like to change in the `local.mk`:

`prefix`
: Org installation directory

{{< highlight makefile >}}
prefix = /dir/where/you/want/to/install/org # Default: /usr/share
{{< /highlight >}}

The `.el` files will go to `$(prefix)/emacs/site-lisp/org` by
default. If you'd like to change that, you can tweak the
`lispdir` variable.
Expand All @@ -31,15 +29,13 @@ Here are few variables that you might like to change in the `local.mk`:
: Org Info installation directory. I like to keep the
Info file for development version of Org in a separate
directory.

{{< highlight makefile >}}
infodir = $(prefix)/org/info # Default: $(prefix)/info
{{< /highlight >}}

`ORG_MAKE_DOC`
: Types of Org documentation you'd like to build by
default.

{{< highlight makefile >}}
# Define below you only need info documentation, the default includes html and pdf
ORG_MAKE_DOC = info pdf card # html
Expand All @@ -49,7 +45,6 @@ Here are few variables that you might like to change in the `local.mk`:
: Packages from the `contrib/` directory that
you'd like to build along with Org. Below are the ones on my
_must-have_ list.

{{< highlight makefile >}}
# Define if you want to include some (or all) files from contrib/lisp
# just the filename please (no path prefix, no .el suffix), maybe with globbing
Expand Down
1 change: 0 additions & 1 deletion test/site/content/posts/inlined-svg.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ style="stroke:#A80036;stroke-width:1.0;"/></g></svg>
class A
url of A is [[https://example.org/]]
```

<div class="src-block-caption">
<span class="src-block-number">Code Snippet 1:</span>
My class diagram
Expand Down
2 changes: 0 additions & 2 deletions test/site/content/posts/links-to-source-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ that the link jump is evident):
````emacs-lisp
(message "Hello")
````

<div class="src-block-caption">
<span class="src-block-number"><a href="#code-snippet--hello">Code Snippet 1</a>:</span>
Hello
Expand Down Expand Up @@ -116,7 +115,6 @@ blandit in.
````emacs-lisp
(message "Hello again")
````

<div class="src-block-caption">
<span class="src-block-number"><a href="#code-snippet--helloagain">Code Snippet 2</a>:</span>
Hello Again
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@ lists. `ox-hugo` provides a hack to get around that bug.
Below is an example of such a case:

- List item 1

```text
​- List item 1.1 in code block
​- List item 1.2 in code block
```
- List item 2

```text
​+ List item 2.1 in code block
​+ List item 2.2 in code block
Expand All @@ -29,13 +27,11 @@ Another such example, but with spaces before the hyphens in example
blocks:

- List item 1

```text
​- List item 1.1 in code block
​- List item 1.2 in code block
```
- List item 2

```text
​+ List item 2.1 in code block
​+ List item 2.2 in code block
Expand Down
31 changes: 30 additions & 1 deletion test/site/content/posts/list-has-src-block-but-no-list-syntax.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
+++
title = "Source block without list syntax in a list"
description = "Test source blocks inside list."
date = 2017-08-01
tags = ["src-block", "lists", "hyphen"]
draft = false
Expand All @@ -9,10 +10,38 @@ This case is not affected by _Blackfriday_ [Issue #239](https://github.com/russr
code block does not have Markdown syntax lists.

- List item 1

```md
*abc*
/def/
=def=
```
- List item 2

---

`ox-hugo` Issue #[645](https://github.com/kaushalmodi/ox-hugo/issues/645)

1. paragraph -&gt; src block with caption -&gt; paragraph -&gt; src block
<a id="code-snippet--foo"></a>
```emacs-lisp
(message "hey")
```
<div class="src-block-caption">
<span class="src-block-number"><a href="#code-snippet--foo">Code Snippet 1</a>:</span>
Foo
</div>

sandwiched paragraph
```emacs-lisp
(message "hey")
```
last paragraph
2. paragraph -&gt; src block **without** caption -&gt; paragraph -&gt; src block
```emacs-lisp
(message "hey")
```
sandwiched paragraph
```emacs-lisp
(message "hey")
```
last paragraph
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ lists. `ox-hugo` provides a hack to get around that bug.
Below is an example of such a case:

- List item 1

```md
​- List item 1.1 in code block
​- List item 1.2 in code block
```
- List item 2

```md
​+ List item 2.1 in code block
​+ List item 2.2 in code block
Expand All @@ -29,13 +27,11 @@ Below is an example of such a case:
Another such example, but with space before a hyphen in source block:

1. First item

```yaml
ports:
foo: bar
```
2. Second item

```yaml
ports:
​ - port: 80
Expand Down
1 change: 0 additions & 1 deletion test/site/content/posts/paragraphs-in-lists.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ draft = false

Just indent the content to be even with the text of the bullet
point, rather than the bullet itself.

```shell
ls -l
```
Expand Down
1 change: 0 additions & 1 deletion test/site/content/posts/prep.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ draft = false
1. Be the root directory for this Hugo site (the directory containing
`config.toml`).
2. Run

```text
hugo server --port 1111
```
Expand Down
Loading

0 comments on commit 4cc8de4

Please sign in to comment.