Skip to content

Commit

Permalink
Merge pull request #484 from kaushalmodi/multi-line-footnotes
Browse files Browse the repository at this point in the history
Multi line footnotes
  • Loading branch information
kaushalmodi authored Dec 18, 2021
2 parents 4a5bfeb + 71f9bd2 commit 1ea466d
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 84 deletions.
45 changes: 29 additions & 16 deletions ox-blackfriday.el
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,14 @@ INFO is a plist used as a communication channel."
(concat indent "- [" title "]" "(#" anchor ")")))

;;;; Footnote section
(defun org-blackfriday-footnote-section (info &optional is-cjk)
(defun org-blackfriday-footnote-section (info multi-line-footnote &optional is-cjk)
"Format the footnote section.
INFO is a plist used as a communication channel.
If MULTI-LINE-FOOTNOTE is non-nil, the footnote definition is not
collapsed into a single line.
IS-CJK should be set to non-nil if the language is Chinese,
Japanese or Korean."
(let ((fn-alist (org-export-collect-footnote-definitions info))
Expand All @@ -143,21 +147,30 @@ Japanese or Korean."
;; (message "fn: %s" (org-export-data fn info)) ;This gives error
;; (message "fn nth 2 car: %s" (org-export-data (nth 2 fn) info))
(setq def (org-trim (org-export-data (nth 2 fn) info)))
;; Support multi-line footnote definitions by folding all
;; footnote definition lines into a single line as Blackfriday
;; does not support that.
(setq def (if is-cjk
(replace-regexp-in-string
"\n" " " ;If the footnote still has newlines, replace them with spaces
(replace-regexp-in-string
;; Do not insert spaces when joining newlines for
;; CJK languages.
"\\([[:multibyte:]]\\)[[:blank:]]*\n[[:blank:]]*\\([[:multibyte:]]\\)" "\\1\\2"
def))
(replace-regexp-in-string "\n" " " def)))

;; Replace multiple consecutive spaces with a single space.
(setq def (replace-regexp-in-string "[[:blank:]]+" " " def))
(if multi-line-footnote
(progn ;Goldmark
;; Goldmark's "PHP Markdown Extra: Footnotes" extension
;; supports multi-line footnotes --
;; https://github.com/yuin/goldmark/#footnotes-extension.
;; 2nd and further lines in a multi-line footnote need to
;; be indented by 4 spaces.
(setq def (replace-regexp-in-string "\n" "\n " def)))
(progn ;Blackfriday
;; Support multi-line footnote definitions by folding all
;; footnote definition lines into a single line as Blackfriday
;; does not support that.
(setq def (if is-cjk
(replace-regexp-in-string
"\n" " " ;If the footnote still has newlines, replace them with spaces
(replace-regexp-in-string
;; Do not insert spaces when joining newlines for
;; CJK languages.
"\\([[:multibyte:]]\\)[[:blank:]]*\n[[:blank:]]*\\([[:multibyte:]]\\)" "\\1\\2"
def))
(replace-regexp-in-string "\n" " " def)))

;; Replace multiple consecutive spaces with a single space.
(setq def (replace-regexp-in-string "[[:blank:]]+" " " def))))
(push (cons n def) fn-alist-stripped)
(setq n (1+ n))))
(when fn-alist-stripped
Expand Down
17 changes: 15 additions & 2 deletions ox-hugo.el
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,16 @@ export."
:type 'directory)
;;;###autoload (put 'org-hugo-base-dir 'safe-local-variable 'stringp)

(defcustom org-hugo-goldmark t
"When nil, enable the hacks necessary for Blackfriday Markdown
processing.
If using Hugo v0.60.0 (released Nov 2019), keep the default
value."
:group 'org-export-hugo
:type 'boolean)
;;;###autoload (put 'org-hugo-goldmark 'safe-local-variable 'booleanp)

(defcustom org-hugo-section "posts"
"Default section for Hugo posts.
Expand Down Expand Up @@ -801,6 +811,7 @@ newer."
(:hugo-section "HUGO_SECTION" nil org-hugo-section)
(:hugo-bundle "HUGO_BUNDLE" nil nil)
(:hugo-base-dir "HUGO_BASE_DIR" nil org-hugo-base-dir)
(:hugo-goldmark "HUGO_GOLDMARK" nil org-hugo-goldmark)
(:hugo-code-fence "HUGO_CODE_FENCE" nil t) ;Prefer to generate triple-backquoted Markdown code blocks by default.
(:hugo-use-code-for-kbd "HUGO_USE_CODE_FOR_KBD" nil org-hugo-use-code-for-kbd)
(:hugo-prefer-hyphen-in-tags "HUGO_PREFER_HYPHEN_IN_TAGS" nil org-hugo-prefer-hyphen-in-tags)
Expand Down Expand Up @@ -1990,7 +2001,8 @@ containing the TITLE's number."
"Return body of document after converting it to Hugo-compatible Markdown.
CONTENTS is the transcoded contents string. INFO is a plist
holding export options."
(let* ((toc-level (plist-get info :with-toc))
(let* ((goldmarkp (org-hugo--plist-get-true-p info :hugo-goldmark))
(toc-level (plist-get info :with-toc))
(toc-level (if (and toc-level
(not (wholenump toc-level)))
(plist-get info :headline-levels)
Expand All @@ -2007,7 +2019,7 @@ holding export options."
;; Make sure CONTENTS is separated from table of contents
;; and footnotes with at least a blank line.
"\n"
(org-blackfriday-footnote-section info (org-hugo--lang-cjk-p info))))))
(org-blackfriday-footnote-section info goldmarkp (org-hugo--lang-cjk-p info))))))

;;;; Keyword
(defun org-hugo-keyword (keyword contents info)
Expand Down Expand Up @@ -3714,6 +3726,7 @@ are \"toml\" and \"yaml\"."
"HUGO_SECTION*"
"HUGO_BUNDLE"
"HUGO_BASE_DIR"
"HUGO_GOLDMARK"
"HUGO_CODE_FENCE"
"HUGO_MENU"
"HUGO_CUSTOM_FRONT_MATTER"
Expand Down
35 changes: 33 additions & 2 deletions test/site/content-org/all-posts.org
Original file line number Diff line number Diff line change
Expand Up @@ -2730,11 +2730,36 @@ This is some text[fn:1][fn:2].
This is some text[fn:1].
This is some text[fn:1].
This is some text[fn:1].
** Multi-line footnote
** Multi-line footnote :multi_line:
*** Blackfriday :blackfriday:
:PROPERTIES:
:EXPORT_FILE_NAME: multi-line-footnote
:EXPORT_HUGO_GOLDMARK:
:END:
**** Multi-line footnote (Blackfriday)
:PROPERTIES:
:EXPORT_FILE_NAME: multi-line-footnote-blackfriday
:END:
This is some text[fn:3].
*** Goldmark :goldmark:
:PROPERTIES:
:EXPORT_HUGO_GOLDMARK: t
:END:
**** Multi-line footnote (Goldmark)
:PROPERTIES:
:EXPORT_FILE_NAME: multi-line-footnote-goldmark
:EXPORT_HUGO_ALIASES: multi-line-footnote
:END:
This is some text[fn:3].
**** Footnote with source block :src_block:
:PROPERTIES:
:EXPORT_FILE_NAME: footnote-with-src-block
:END:
#+begin_description
Exporting footnote with source block
#+end_description
{{{oxhugoissue(433)}}}

Testing code in a footnote with a ~#+begin_src~ directive.[fn:8]
** Multi-line footnote Japanese
:PROPERTIES:
:EXPORT_FILE_NAME: multi-line-footnote-japanese
Expand Down Expand Up @@ -7927,6 +7952,12 @@ Results from static site search implemented using /Fusejs/, /jquery/
and /mark.js/. -- [[https://gist.github.com/eddiewebb/735feb48f50f0ddd65ae5606a1cb41ae][Source]]
* Footnotes

[fn:8]
#+begin_src elisp
(emacs-version)
(org-version)
#+end_src

[fn:7] English on line 1
の Japanese on line 2

Expand Down
28 changes: 0 additions & 28 deletions test/site/content-org/issues.org
Original file line number Diff line number Diff line change
Expand Up @@ -107,34 +107,6 @@ rendering correctly at the moment --- {{{issue(382)}}}
\dot{z} & = -\beta z + xy
\end{align}
\right.\]
* Issue 433 -- Source block in a footnote :footnote:src_block:
:PROPERTIES:
:EXPORT_FILE_NAME: 443-src-block-in-footnote
:END:
#+begin_description
Source block in an Org footnote
#+end_description
{{{issue(433)}}}

Testing code in a footnote with a ~begin_src~ directive.[fn:1].

*This doesn't work because Hugo does not support having multi-line
content in footnotes.*

Due to that limitation, ~ox-hugo~ folds all footnote content onto a
single line.. and so the below Org footnote:

#+begin_src org
[fn:1]
,#+begin_src elisp
(emacs-version)
,#+end_src
#+end_src

gets exported as below in the Markdown footnote:
#+begin_src md
[^fn:1]: ```elisp (emacs-version) ```
#+end_src
* Issue 430 -- Disabling exporting of code blocks :babel:export:disable:
:PROPERTIES:
:EXPORT_FILE_NAME: 430-disabling-exporting-of-code-blocks
Expand Down
31 changes: 0 additions & 31 deletions test/site/content/issues/443-src-block-in-footnote.md

This file was deleted.

15 changes: 15 additions & 0 deletions test/site/content/posts/footnote-with-src-block.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
+++
title = "Footnote with source block"
description = "Exporting footnote with source block"
tags = ["footnote", "multi-line", "goldmark", "src-block"]
draft = false
+++

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

Testing code in a footnote with a `#+begin_src` directive.[^fn:1]

[^fn:1]: ```elisp
(emacs-version)
(org-version)
```
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
+++
title = "Multi-line footnote"
tags = ["footnote"]
title = "Multi-line footnote (Blackfriday)"
tags = ["footnote", "multi-line", "blackfriday"]
draft = false
+++

Expand Down
11 changes: 11 additions & 0 deletions test/site/content/posts/multi-line-footnote-goldmark.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
+++
title = "Multi-line footnote (Goldmark)"
aliases = ["/posts/multi-line-footnote"]
tags = ["footnote", "multi-line", "goldmark"]
draft = false
+++

This is some text[^fn:1].

[^fn:1]: This is a long footnote. It is so long that it gets auto-filled
over multiple lines. But even then it should be handled fine.
6 changes: 4 additions & 2 deletions test/site/content/posts/multi-line-footnote-japanese.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ but next line started with Japanese[^fn:2].
Here is a footnote entirely in English[^fn:3].

[^fn:1]: これはテストです.
[^fn:2]: English on line 1 の Japanese on line 2
[^fn:3]: This is a long footnote. It is so long that it gets auto-filled over multiple lines. But even then it should be handled fine.
[^fn:2]: English on line 1
の Japanese on line 2
[^fn:3]: This is a long footnote. It is so long that it gets auto-filled
over multiple lines. But even then it should be handled fine.
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,9 @@ Note
`pygmentsCodeFences` to `true` for syntax highlighting to work for
fenced code blocks.**

[^fn:1]: Even if the user has set the `HUGO_CODE_FENCE` value to `t` (via variable, keyword or subtree property), the Hugo `highlight` shortcode will be used automatically instead of code fences if either (i) the user has chosen to either show the line numbers, or (ii) has chosen to highlight lines of code (See the `ox-hugo` documentation on [Source Blocks](https://ox-hugo.scripter.co/doc/source-blocks)).
[^fn:1]: Even if the user has set the `HUGO_CODE_FENCE` value to `t`
(via variable, keyword or subtree property), the Hugo `highlight`
shortcode will be used automatically instead of code fences if either
(i) the user has chosen to either show the line numbers, or (ii) has
chosen to highlight lines of code (See the `ox-hugo` documentation on
[Source Blocks](https://ox-hugo.scripter.co/doc/source-blocks)).

0 comments on commit 1ea466d

Please sign in to comment.