Escape the shortcodes with markdown too

Now the {{% .. %}} shortcodes will be escaped too.

Add a new function org-hugo--escape-hugo-shortcode.

https://github.com/kaushalmodi/ox-hugo/issues/94
master
Kaushal Modi 8 years ago
parent d44632a929
commit ec61dea12c
  1. 25
      ox-hugo.el
  2. 13
      test/site/content-org/all-posts.org
  3. 13
      test/site/content/posts/source-block-md-with-hugo-shortcodes.md

@ -771,6 +771,23 @@ contents according to the current headline."
This function is meant to be used as a predicate for
`org-export-get-ordinal'.")
(defun org-hugo--escape-hugo-shortcode (code lang)
"Escape Hugo shortcodes if present in CODE string.
The escaping is enabled only if LANG is \"md\".
- Shortcode with Markdown : {{% foo %}} -> {{%/* foo */%}}
- Shortcode without Markdown : {{< foo >}} -> {{</* foo */>}}
Return the escaped/unescaped string."
(if (string= lang "md")
(replace-regexp-in-string
"\\({{<\\)\\([^}][^}]*\\)\\(>}}\\)" "\\1/*\\2*/\\3"
(replace-regexp-in-string
"\\({{%\\)\\([^}][^}]*\\)\\(%}}\\)" "\\1/*\\2*/\\3" code))
code))
;;; Transcode Functions
@ -1309,9 +1326,7 @@ channel."
;; which is the default syntax highlighter after Hugo
;; v0.28.
(setq ret1 (replace-regexp-in-string (concat "\\`\\(```+\\)" lang) "\\1" ret1)))
;; Only in Markdown (md) source block, escape the Hugo shortcodes.
(when (string= lang "md")
(setq ret1 (replace-regexp-in-string "\\({{<\\)\\([^>}]+\\)\\(>}}\\)" "\\1/*\\2*/\\3" ret1)))
(setq ret1 (org-hugo--escape-hugo-shortcode ret1 lang))
ret1))
;; 2. If number-lines is non-nil, or
;; 3. If hl-lines is non-nil, or
@ -1340,9 +1355,7 @@ channel."
(setq hllines-str (concat "hl_lines=" hl-lines))
(when number-lines
(setq hllines-str (concat ", " hllines-str))))
;; Only in Markdown (md) source block, escape the Hugo shortcodes.
(when (string= lang "md")
(setq code (replace-regexp-in-string "\\({{<\\)\\([^>}]+\\)\\(>}}\\)" "\\1/*\\2*/\\3" code)))
(setq code (org-hugo--escape-hugo-shortcode code lang))
(format "{{< highlight %s%s>}}\n%s{{< /highlight >}}\n"
lang
(format highlight-args-str linenos-str hllines-str)

@ -823,24 +823,29 @@ Reference -- [[https://github.com/gohugoio/hugo/issues/4006][hugo#4006]]
*** Shortcodes escaped
The =figure= shortcodes in the two Markdown source code blocks below
should *not* be expanded.. they should be visible verbatim.
- {&lbrace;< .. >}&rbrace; --- [[https://gohugo.io/content-management/shortcodes/#shortcodes-without-markdown][Shortcodes without Markdown]]
- {&lbrace;% .. %}&rbrace; --- [[https://gohugo.io/content-management/shortcodes/#shortcodes-with-markdown][Shortcodes with Markdown]]
**** Code block using code fences
#+BEGIN_SRC md
{{< figure src="http://orgmode.org/img/org-mode-unicorn-logo.png" >}}
{{% figure src="http://orgmode.org/img/org-mode-unicorn-logo.png" %}}
#+END_SRC
**** Code block using =highlight= shortcode
Here, the =-n= switch is added to the Org source block to
auto-enable[fn:4] using the =highlight= shortcode.
#+BEGIN_SRC md -n
{{< figure src="http://orgmode.org/img/org-mode-unicorn-logo.png" >}}
{{% figure src="http://orgmode.org/img/org-mode-unicorn-logo.png" %}}
#+END_SRC
*** Shortcodes *not* escaped
The =figure= shortcodes in the Markdown source code blocks below
*should* be expanded.. you should be seeing a nice little unicorn
below.
The =figure= shortcodes in the below example block *should* be
expanded.. you should be seeing little unicorns below.
#+BEGIN_EXAMPLE
{{< figure src="http://orgmode.org/img/org-mode-unicorn-logo.png" >}}
{{% figure src="http://orgmode.org/img/org-mode-unicorn-logo.png" %}}
#+END_EXAMPLE
Above a =#+BEGIN_EXAMPLE= .. =#+END_EXAMPLE= block is used just
Above a =#+BEGIN_EXAMPLE= .. =#+END_EXAMPLE= block is chosen
arbitrarily. The Hugo shortcodes will remain unescaped in *any*
source/example block except for _Markdown source blocks_ (annotated
with =md= language).

@ -9,11 +9,15 @@ draft = false
The `figure` shortcodes in the two Markdown source code blocks below
should **not** be expanded.. they should be visible verbatim.
- {&lbrace;< .. >}&rbrace; --- [Shortcodes without Markdown](https://gohugo.io/content-management/shortcodes/#shortcodes-without-markdown)
- {&lbrace;% .. %}&rbrace; --- [Shortcodes with Markdown](https://gohugo.io/content-management/shortcodes/#shortcodes-with-markdown)
### Code block using code fences {#code-block-using-code-fences}
```md
{{</* figure src="http://orgmode.org/img/org-mode-unicorn-logo.png" */>}}
{{%/* figure src="http://orgmode.org/img/org-mode-unicorn-logo.png" */%}}
```
@ -24,20 +28,21 @@ auto-enable[^fn:1] using the `highlight` shortcode.
{{< highlight md "linenos=table, linenostart=1">}}
{{</* figure src="http://orgmode.org/img/org-mode-unicorn-logo.png" */>}}
{{%/* figure src="http://orgmode.org/img/org-mode-unicorn-logo.png" */%}}
{{< /highlight >}}
## Shortcodes **not** escaped {#shortcodes-not-escaped}
The `figure` shortcodes in the Markdown source code blocks below
**should** be expanded.. you should be seeing a nice little unicorn
below.
The `figure` shortcodes in the below example block **should** be
expanded.. you should be seeing little unicorns below.
```text
{{< figure src="http://orgmode.org/img/org-mode-unicorn-logo.png" >}}
{{% figure src="http://orgmode.org/img/org-mode-unicorn-logo.png" %}}
```
Above a `#+BEGIN_EXAMPLE` .. `#+END_EXAMPLE` block is used just
Above a `#+BEGIN_EXAMPLE` .. `#+END_EXAMPLE` block is chosen
arbitrarily. The Hugo shortcodes will remain unescaped in **any**
source/example block except for <span class="underline">Markdown source blocks</span> (annotated
with `md` language).

Loading…
Cancel
Save