Skip to content

Commit

Permalink
Merge pull request #639 from kaushalmodi/improve-bibliography-heading…
Browse files Browse the repository at this point in the history
…-injection

perf: Better way to auto-inject bibliography heading
  • Loading branch information
kaushalmodi authored May 20, 2022
2 parents 8914b75 + 22065f7 commit e63bb09
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 47 deletions.
19 changes: 1 addition & 18 deletions doc/ox-hugo-manual.org
Original file line number Diff line number Diff line change
Expand Up @@ -2909,7 +2909,7 @@ property list variable.
:PROPERTIES:
:CUSTOM_ID: org-hugo-citations-plist
:END:
This property list recognizes these properties:
This property list recognizes this property:

- :bibliography-section-heading :: /(string)/ Heading to insert before
the bibliography section. The default value is "References".
Expand All @@ -2932,15 +2932,6 @@ This property list recognizes these properties:
(with-eval-after-load 'ox-hugo
(plist-put org-hugo-citations-plist :bibliography-section-heading ""))
#+end_src
- :bibliography-section-regexp :: /(string)/ Regular expression to
find the beginning of the bibliography section. The default value
is a regular expression that matches the ~<div
class="csl-bib-body">~ HTML element exported by ~citeproc.el~.

#+begin_note
The default regular expression applies to bibliography insertion
done by Org Cite or [[* Org Ref Citations][Org Ref]] because both use ~citeproc.el~ for that.
#+end_note
**** Example
Minimal example with ~org-cite~ citations:

Expand Down Expand Up @@ -2986,14 +2977,6 @@ that."
(org-ref-process-buffer 'html)))
(add-to-list 'org-export-before-parsing-hook #'my/org-ref-process-buffer--html)))
#+end_src
**** Auto-inserting /Bibliography/ heading
A Markdown heading named "References" will be auto-inserted above
the ~org-ref~ ~[[bibliography:..]]~ link, which is typically present
at the end of a post.

This feature is controlled by the
{{{relref(~org-hugo-citations-plist~,org-cite-citations/#org-hugo-citations-plist)}}}
property list variable.
**** Example
Minimal example with ~org-ref~ citations:

Expand Down
55 changes: 28 additions & 27 deletions ox-hugo.el
Original file line number Diff line number Diff line change
Expand Up @@ -654,21 +654,14 @@ Some of the inbuilt functions that can be added to this list:
:group 'org-export-hugo
:type '(repeat function))

(defcustom org-hugo-citations-plist '(:bibliography-section-heading "References"
:bibliography-section-regexp "\n\n<style>\\(.\\|\n\\)*?<div class=\"csl-bib-body\">"
;; ^^^^ blank line before the <style> ..
;; .. <div class="csl-bib-body"> block
)
(defcustom org-hugo-citations-plist '(:bibliography-section-heading "References")
"Property list for storing default properties for citation exports.
Properties recognized in the PLIST:
- :bibliography-section-heading :: Heading to insert before the bibliography
section.
- :bibliography-section-regexp :: Regular expression to find the
beginning of the bibliography section.
Auto-detection of bibliography section requires installing the
`citations' package from Melpa and adding `#+cite_export: csl' at
the top of the Org file.
Expand Down Expand Up @@ -876,11 +869,7 @@ arguments of the ORIG-FUN.
This advice retains the `:hl_lines', `linenos' and
`:front_matter_extra' parameters, if added to any source block.
This parameter is used in `org-hugo-src-block'.
This advice is added to the ORIG-FUN only while an ox-hugo export
is in progress. See `org-hugo--before-export-function' and
`org-hugo--after-1-export-function'."
This parameter is used in `org-hugo-src-block'."
(let* ((param-keys-to-be-retained '(:hl_lines :linenos :front_matter_extra))
(info (car args))
(parameters (nth 2 info))
Expand Down Expand Up @@ -954,6 +943,25 @@ See `org-link-parameters' for details about PATH, DESC and FORMAT."
(when (member format '(md hugo))
(format "[%s](%s \"%s\")" desc link title))))

(defun org-hugo--org-cite-export-bibliography (orig-fun &rest args)
"Insert a heading before the exported bibliography.
ORIG-FUN is the original function `org-cite-export-bibliography'
that this function is designed to advice using `:around'. ARGS
are the arguments of the ORIG-FUN."
(let ((bib (apply orig-fun args)))
(when (org-string-nw-p bib)
;; Auto-inject Bibliography heading.
(let ((info (nth 2 args)) ;(org-cite-export-bibliography KEYWORD _ INFO)
(bib-heading (org-string-nw-p (plist-get org-hugo-citations-plist :bibliography-section-heading))))
(when bib-heading
(let* ((bib-heading (org-blackfriday--translate nil info bib-heading))
(loffset (string-to-number
(or (org-entry-get nil "EXPORT_HUGO_LEVEL_OFFSET" :inherit)
(plist-get info :hugo-level-offset))))
(level-mark (make-string (+ loffset 1) ?#)))
(format "%s %s\n\n%s" level-mark bib-heading bib)))))))

(defun org-hugo--before-export-function (subtreep)
"Function to be run before an ox-hugo export.
Expand All @@ -962,13 +970,18 @@ This function is called in the very beginning of
SUBTREEP is non-nil for subtree-based exports.
This function is used to advise few functions. Those advices are
effective only while an ox-hugo export is in progress because
they get removed later in `org-hugo--after-1-export-function'.
This is an internal function."
(unless subtreep
;; Reset the variables that are used only for subtree exports.
(setq org-hugo--subtree-coord nil))
(advice-add 'org-babel-exp-code :around #'org-hugo--org-babel-exp-code)
(advice-add 'org-babel--string-to-number :override #'org-hugo--org-babel--string-to-number)
(advice-add 'org-info-export :override #'org-hugo--org-info-export))
(advice-add 'org-info-export :override #'org-hugo--org-info-export)
(advice-add 'org-cite-export-bibliography :around #'org-hugo--org-cite-export-bibliography))

(defun org-hugo--after-1-export-function (info outfile)
"Function to be run after exporting one post.
Expand All @@ -984,6 +997,7 @@ INFO is a plist used as a communication channel.
OUTFILE is the Org exported file name.
This is an internal function."
(advice-remove 'org-cite-export-bibliography #'org-hugo--org-cite-export-bibliography)
(advice-remove 'org-info-export #'org-hugo--org-info-export)
(advice-remove 'org-babel--string-to-number #'org-hugo--org-babel--string-to-number)
(advice-remove 'org-babel-exp-code #'org-hugo--org-babel-exp-code)
Expand Down Expand Up @@ -2419,19 +2433,6 @@ holding export options."
"\\([[:space:]>]\\|\n\\)+\\([^-#`]\\)")
" \\2" contents)))

;; Auto-inject Bibliography heading.
(let ((bib-heading (org-string-nw-p (plist-get org-hugo-citations-plist :bibliography-section-heading))))
(when (and bib-heading (featurep 'citeproc))
(let* ((bib-heading (org-blackfriday--translate nil info bib-heading))
(bib-regexp (plist-get org-hugo-citations-plist :bibliography-section-regexp))
(loffset (string-to-number
(or (org-entry-get nil "EXPORT_HUGO_LEVEL_OFFSET" :inherit)
(plist-get info :hugo-level-offset))))
(level-mark (make-string (+ loffset 1) ?#)))
(setq contents (replace-regexp-in-string
bib-regexp
(format "\n\n%s %s\\&" level-mark bib-heading) contents)))))

;; (message "[org-hugo-inner-template DBG] toc-level: %s" toc-level)
(org-trim (concat
toc
Expand Down
2 changes: 0 additions & 2 deletions test/site/content/posts/citation-org-ref.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ draft = false

(<a href="#citeproc_bib_item_1">Org et al., 2021</a>)

## References

<style>.csl-entry{text-indent: -1.5em; margin-left: 1.5em;}</style><div class="csl-bib-body">
<div class="csl-entry"><a id="citeproc_bib_item_1"></a>org, m., Syntax, C., List, M., &#38; Effort, T. (2021). Elegant citations with org-mode. <i>Journal of Plain Text Formats</i>, <i>42</i>(1), 2–3.</div>
</div>
2 changes: 2 additions & 0 deletions test/site/content/posts/org-cite-basic-example.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,6 @@ Below Org snippet:

exports to:

## References

org, mode and Syntax, Citation and List, Mailing and Effort, Time (2021). _Elegant Citations with Org-Mode_, Journal of Plain Text Formats.

0 comments on commit e63bb09

Please sign in to comment.