Skip to content

Commit

Permalink
Recognize :trim-pre and :trim-post in the org special block plist
Browse files Browse the repository at this point in the history
`("mark" . (:trim-pre t :trim-post t))` is added to the default value
of `org-hugo-special-block-type-properties` (ref
#540).
  • Loading branch information
kaushalmodi committed Jan 27, 2022
1 parent ce23ad1 commit d80d5c7
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 41 deletions.
13 changes: 9 additions & 4 deletions doc/ox-hugo-manual.org
Expand Up @@ -3404,14 +3404,19 @@ Then simply put /tikz/ snippets in the Org source like so:

{{{test-search(tikzjax)}}}
**** Whitespace Trimming
Whitespace trimming around exported Org Special Blocks is supported
with the help of ~#+header:~ keywords above those blocks.
The default whitespace trimming around exported Org Special Blocks can
be configured by setting the ~:trim-pre~ and ~:trim-post~ properties
for specific special block types in the
~org-hugo-special-block-type-properties~ defcustom.

- If the header has ~:trim-pre t~, whitespace is removed before the
- If ~:trim-pre~ is set to ~t~, whitespace is removed before the
block.
- If the header has ~:trim-post t~, whitespace is removed after the
- If ~:trim-post~ is set to ~t~, whitespace is removed after the
block.

The default values of these properties can be overridden by using the
~#+header:~ keywords above the special blocks.

#+begin_note
If a special block tag is not recognized as an HTML block or inline
element, or as a Hugo shortcode, it defaults to exporting [[#special-blocks--div-tags][with ~<div>~
Expand Down
25 changes: 17 additions & 8 deletions ox-hugo.el
Expand Up @@ -517,6 +517,7 @@ HTML element."

(defcustom org-hugo-special-block-type-properties '(("audio" . (:raw t))
("katex" . (:raw t))
("mark" . (:trim-pre t :trim-post t))
("tikzjax" . (:raw t))
("video" . (:raw t)))
"Alist for storing default properties for special block types.
Expand All @@ -531,12 +532,18 @@ string.
Properties recognized in the PLIST:
- :raw :: When set to `t', the contents of the special block as exported raw
i.e. as typed in the Org buffer.
- :raw :: When set to `t', the contents of the special block as
exported raw i.e. as typed in the Org buffer.
- :trim-pre :: When set to `t', the whitespace before the special
block is removed.
- :trim-pre :: When set to `t', the whitespace after the special
block is removed.
For the special block types not specified in this variable, the
default behavior is same as if (:raw nil) plist were associated
with them."
default behavior is same as if (:raw nil :trim-pre nil :trim-post
nil) plist were associated with them."
:group 'org-export-hugo
:type '(list (cons string (plist :key-type symbol :value-type boolean))))

Expand Down Expand Up @@ -2841,10 +2848,12 @@ INFO is a plist holding export options."
(block-type-plist (cdr (assoc block-type org-hugo-special-block-type-properties)))
(header (org-babel-parse-header-arguments
(car (org-element-property :header special-block))))
(trim-pre (alist-get :trim-pre header))
(trim-pre-tag (or (and trim-pre org-hugo--trim-pre-marker) ""))
(trim-post (alist-get :trim-post header))
(trim-post-tag (or (and trim-post org-hugo--trim-post-marker) ""))
(trim-pre (or (alist-get :trim-pre header) ;`:trim-pre' in #+header has higher precedence.
(plist-get block-type-plist :trim-pre)))
(trim-pre-tag (if trim-pre org-hugo--trim-pre-marker ""))
(trim-post (or (alist-get :trim-post header) ;`:trim-post' in #+header has higher precedence.
(plist-get block-type-plist :trim-pre)))
(trim-post-tag (if trim-post org-hugo--trim-post-marker ""))
(paired-shortcodes (let* ((str (plist-get info :hugo-paired-shortcodes))
(str-list (when (org-string-nw-p str)
(split-string str " "))))
Expand Down
22 changes: 11 additions & 11 deletions test/site/content-org/all-posts.org
Expand Up @@ -4732,8 +4732,9 @@ Here's a link to the [[circle][Circle]].
:EXPORT_FILE_NAME: ws-trimming-around-special-blocks
:EXPORT_DESCRIPTION: Whitespace trimming using ~#+header: :trim-pre t :trim-post t~ before special blocks.
:END:
**** No trimming
**** Whitespace trimmed before and after the ~mark~ special block (default)
Below Org block:

#+begin_src org
line 1
,#+begin_mark
Expand All @@ -4749,12 +4750,12 @@ line 1
abc def
#+end_mark
line 2
**** Whitespace trimmed before and after the ~mark~ special block
**** Whitespace trimmed only before the ~mark~ special block
Below Org block:

#+begin_src org
line 1
,#+header: :trim-pre t :trim-post t
,#+header: :trim-post nil
,#+begin_mark
abc def
,#+end_mark
Expand All @@ -4764,17 +4765,17 @@ line 2
exports and renders as:

line 1
#+header: :trim-pre t :trim-post t
#+header: :trim-post nil
#+begin_mark
abc def
#+end_mark
line 2
**** Whitespace trimmed only before the ~mark~ special block
**** Whitespace trimmed only after the ~mark~ special block
Below Org block:

#+begin_src org
line 1
,#+header: :trim-pre t
,#+header: :trim-pre nil
,#+begin_mark
abc def
,#+end_mark
Expand All @@ -4784,17 +4785,16 @@ line 2
exports and renders as:

line 1
#+header: :trim-pre t
#+header: :trim-pre nil
#+begin_mark
abc def
#+end_mark
line 2
**** Whitespace trimmed only after the ~mark~ special block
**** No trimming
Below Org block:

#+begin_src org
line 1
,#+header: :trim-post t
,#+header: :trim-pre nil :trim-post nil
,#+begin_mark
abc def
,#+end_mark
Expand All @@ -4804,7 +4804,7 @@ line 2
exports and renders as:

line 1
#+header: :trim-post t
#+header: :trim-pre nil :trim-post nil
#+begin_mark
abc def
#+end_mark
Expand Down
28 changes: 10 additions & 18 deletions test/site/content/posts/ws-trimming-around-special-blocks.md
Expand Up @@ -5,7 +5,7 @@ tags = ["special-block", "whitespace", "trimming"]
draft = false
+++

## No trimming {#no-trimming}
## Whitespace trimmed before and after the `mark` special block (default) {#whitespace-trimmed-before-and-after-the-mark-special-block--default}

Below Org block:

Expand All @@ -19,20 +19,16 @@ line 2

exports and renders as:

line 1

<mark>abc def</mark>

line 2
line 1 <mark>abc def</mark> line 2


## Whitespace trimmed before and after the `mark` special block {#whitespace-trimmed-before-and-after-the-mark-special-block}
## Whitespace trimmed only before the `mark` special block {#whitespace-trimmed-only-before-the-mark-special-block}

Below Org block:

```org
line 1
#+header: :trim-pre t :trim-post t
#+header: :trim-post nil
#+begin_mark
abc def
#+end_mark
Expand All @@ -44,13 +40,13 @@ exports and renders as:
line 1 <mark>abc def</mark> line 2


## Whitespace trimmed only before the `mark` special block {#whitespace-trimmed-only-before-the-mark-special-block}
## Whitespace trimmed only after the `mark` special block {#whitespace-trimmed-only-after-the-mark-special-block}

Below Org block:

```org
line 1
#+header: :trim-pre t
#+header: :trim-pre nil
#+begin_mark
abc def
#+end_mark
Expand All @@ -59,18 +55,16 @@ line 2

exports and renders as:

line 1 <mark>abc def</mark>

line 2
line 1 <mark>abc def</mark> line 2


## Whitespace trimmed only after the `mark` special block {#whitespace-trimmed-only-after-the-mark-special-block}
## No trimming {#no-trimming}

Below Org block:

```org
line 1
#+header: :trim-post t
#+header: :trim-pre nil :trim-post nil
#+begin_mark
abc def
#+end_mark
Expand All @@ -79,9 +73,7 @@ line 2

exports and renders as:

line 1

<mark>abc def</mark> line 2
line 1 <mark>abc def</mark> line 2


## Use `<span>` tag if trimming detected {#use-span-tag-if-trimming-detected}
Expand Down

0 comments on commit d80d5c7

Please sign in to comment.