Skip to content

Commit

Permalink
Implement generic solution for #60
Browse files Browse the repository at this point in the history
The earlier org-hugo-pygments-code-fences boolean var is now replaced
with symbol list defcustom org-hugo-langs-no-desc-in-code-fences.  To
get the same behavior as setting org-hugo-pygments-code-fences to t,
set org-hugo-langs-no-desc-in-code-fences to '(org).
  • Loading branch information
kaushalmodi committed Aug 7, 2017
1 parent 6ea8881 commit 6f40cf9
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 36 deletions.
15 changes: 10 additions & 5 deletions example-site/content-org/all-posts.org
Original file line number Diff line number Diff line change
Expand Up @@ -402,19 +402,24 @@ code block does not have Markdown syntax lists.
:EXPORT_FILE_NAME: org-source-block
:END:
Test case for the case where user has set
=org-hugo-pygments-code-fences= to a non-nil value.
=org-hugo-langs-no-descr-in-code-fences= to a list containing the
element =org=.

/As this variable is dependent on user's config, it is not set to be
exported by default./
/As this variable is dependent on user's config, this post is not set
to be exported by default./

The [[https://discourse.gohugo.io/t/fenced-code-block-with-language-unsupported-by-pygments/7710][issue]] with Hugo will be seen if:
- =pygmentsCodeFences = true= is set in the Hugo site =config.toml=,
- =org-hugo-pygments-code-fences= is set to =nil=, and
- a source block's language is set to one that's not supported by
Pygments (like [[https://bitbucket.org/birkenfeld/pygments-main/issues/719/wishlist-support-org][org]], and thus the below example with source code
language set to =org=).
language set to =org=), and
- =org-hugo-langs-no-descr-in-code-fences= is set to a value not
containing that lanaguage descriptor (=org= in this case).
#+BEGIN_SRC org
# Org comment
Export this post after setting
=org-hugo-langs-no-descr-in-code-fences= to =(org)= and temporarily
removing the =noexport= tag.
#+END_SRC
* Formatting :formatting:
** General
Expand Down
28 changes: 28 additions & 0 deletions example-site/content/posts/org-source-block.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
+++
title = "Org Source Block"
tags = ["src-block"]
draft = false
+++

Test case for the case where user has set
`org-hugo-langs-no-descr-in-code-fences` to a list containing the
element `org`.

_As this variable is dependent on user's config, this post is not set
to be exported by default._

The [issue](https://discourse.gohugo.io/t/fenced-code-block-with-language-unsupported-by-pygments/7710) with Hugo will be seen if:

- `pygmentsCodeFences = true` is set in the Hugo site `config.toml`,
- a source block's language is set to one that's not supported by
Pygments (like [org](https://bitbucket.org/birkenfeld/pygments-main/issues/719/wishlist-support-org), and thus the below example with source code
language set to `org`), and
- `org-hugo-langs-no-descr-in-code-fences` is set to a value not
containing that lanaguage descriptor (`org` in this case).

```
# Org comment
Export this post after setting
=org-hugo-langs-no-descr-in-code-fences= to =(org)= and temporarily
removing the =noexport= tag.
```
66 changes: 35 additions & 31 deletions ox-hugo.el
Original file line number Diff line number Diff line change
Expand Up @@ -374,16 +374,21 @@ This also affects the Hugo categories set via Org tags using the
:type 'boolean
:safe #'booleanp)

(defcustom org-hugo-pygments-code-fences nil
"Mirror of Hugo `pygmentsCodeFences' variable.
(defcustom org-hugo-langs-no-descr-in-code-fences '()
"List of languages whose descriptors should not be exported to Markdown.
This variable is effective only if the HUGO_CODE_FENCE option is set.
If `pygmentsCodeFences' is `true', set this variable to a non-nil
value, else set it to nil."
If `pygmentsCodeFences' is `true', and if a language is not supported
by Pygments, the HTML of that fenced code block is not rendered
correctly by Hugo. In such cases, it is better to leave out the
language descriptor and allow the code block to render as an Org
example block.
Example value: (org)."
:group 'org-export-hugo
:type 'boolean
:safe #'booleanp)
:type '(repeat symbol)
:safe #'listp)


;;; Define Back-End
Expand Down Expand Up @@ -851,35 +856,34 @@ INFO is a plist used as a communication channel."
(defun org-hugo-src-block (src-block _contents info)
"Convert SRC-BLOCK element to Hugo-compatible element.
If the HUGO_CODE_FENCE property is set to t (default), the
Markdown style triple-backquoted code blocks are created.
Otherwise, the code block is wrapped in Hugo `highlight'
shortcode.
If the HUGO_CODE_FENCE property is set to t (default), the Markdown
style triple-backquoted code blocks are created. Otherwise, the code
block is wrapped in Hugo `highlight' shortcode.
INFO is a plist used as a communication channel."
(if (org-hugo--plist-value-true-p :hugo-code-fence info)
(let (ret)
(setq ret (org-blackfriday-src-block src-block nil info))
(when org-hugo-pygments-code-fences
;; With the pygmentsCodeFences options enabled in Hugo,
;; `org' is not recognized as a "language". This is
;; probably because Pygments does not have a lexer for Org.
;; Issue on Pygments repo: https://bitbucket.org/birkenfeld/pygments-main/issues/719/wishlist-support-org
;; So attempt to do below:
;; ```org
;; # org comment
;; ```
;; will not result in a <code> tag wrapped block in HTML.
;; So override the language to be an empty string in such cases.
;; FIXME: Remove this lang override once Pygments support is added.
(setq ret (replace-regexp-in-string "\\`\\(```\\)org" "\\1" ret)))
ret)
(let* ((lang (org-element-property :language src-block))
(code (org-export-format-code-default src-block info)))
(format "{{< highlight %s>}}\n%s{{< /highlight >}}\n" lang code))))
(let ((lang (org-element-property :language src-block)))
(if (org-hugo--plist-value-true-p :hugo-code-fence info)
(let ((ret (org-blackfriday-src-block src-block nil info)))
(when (member (intern lang) org-hugo-langs-no-descr-in-code-fences)
;; With the pygmentsCodeFences options enabled in Hugo,
;; `org' is not recognized as a "language". This is
;; probably because Pygments does not have a lexer for Org.
;; Issue on Pygments repo:
;; https://bitbucket.org/birkenfeld/pygments-main/issues/719/wishlist-support-org
;; So attempt to do below:
;; ```org
;; # org comment
;; ```
;; will not result in a <code> tag wrapped block in HTML.
;; So override the language to be an empty string in such cases.
(setq ret (replace-regexp-in-string
(concat "\\`\\(```\\)" lang)
"\\1" ret)))
ret)
(let ((code (org-export-format-code-default src-block info)))
(format "{{< highlight %s>}}\n%s{{< /highlight >}}\n" lang code)))))



;;; Filter Functions

;;;; Body Filter
Expand Down

0 comments on commit 6f40cf9

Please sign in to comment.