Skip to content

Commit

Permalink
fix: Don't introduce extra blank lines after list item paragraphs
Browse files Browse the repository at this point in the history
  • Loading branch information
kaushalmodi committed May 27, 2022
1 parent 71cb5f2 commit 9471c21
Show file tree
Hide file tree
Showing 13 changed files with 96 additions and 43 deletions.
53 changes: 52 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,57 @@ 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 that
paragraph."
(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.
((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)
(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
20 changes: 20 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,23 @@ code block does not have Markdown syntax lists.
=def=
#+end_src
- List item 2
-----
{{{oxhugoissue(645)}}}

1. item one
2. This paragraph is followed by a source block *with* caption.
#+name: code__foo
#+caption: Foo
#+begin_src emacs-lisp
(message "hey")
#+end_src


1. item one
2. This paragraph is followed by a source block without caption.
#+begin_src emacs-lisp
(message "hey")
#+end_src
**** Source block with list syntax but not in a list
:PROPERTIES:
:EXPORT_DATE: 2017-08-01
Expand Down
4 changes: 0 additions & 4 deletions test/site/content/posts/code-fenced-src-blocks-default.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ 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
```
Expand All @@ -24,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 @@ -42,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
4 changes: 0 additions & 4 deletions test/site/content/posts/code-fenced-src-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ 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
```
Expand All @@ -25,15 +24,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 +40,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
4 changes: 0 additions & 4 deletions test/site/content/posts/highlight-shortcode-src-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ 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 >}}
Expand All @@ -31,15 +30,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 +46,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
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
25 changes: 24 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,32 @@ 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. item one
2. This paragraph is followed by a source block **with** caption.
<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>

<!--listend-->

1. item one
2. This paragraph is followed by a source block without caption.
```emacs-lisp
(message "hey")
```
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
4 changes: 0 additions & 4 deletions test/site/content/posts/shortcode-src-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ 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 >}}
Expand All @@ -21,15 +20,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 @@ -39,7 +36,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
Loading

0 comments on commit 9471c21

Please sign in to comment.