Skip to content

Commit

Permalink
More fixing of Pandoc output
Browse files Browse the repository at this point in the history
  • Loading branch information
kaushalmodi committed Jul 19, 2018
1 parent ac22a16 commit f2c1ab9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
35 changes: 26 additions & 9 deletions ox-hugo-pandoc-cite.el
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ The list of Pandoc specific meta-data is defined in
(delete-matching-lines regexp)))
(buffer-substring-no-properties (point-min) (point-max))))

(defun ox-hugo-pandoc-cite--fix-pandoc-output (content)
(defun ox-hugo-pandoc-cite--fix-pandoc-output (content loffset)
"Fix the Pandoc output CONTENT and return it.
Required fixes:
Expand All @@ -85,24 +85,37 @@ Required fixes:
- Replace \"::: {#ref-someref}\" with \"<a
id=\"ref-someref\"></a>\".
- Remove \"^:::$\""
- Remove \"^:::$\"
LOFFSET is the offset added to the base level of 1 for headings."
(with-temp-buffer
(insert content)
(let ((case-fold-search nil))
(let ((case-fold-search nil)
(level-mark (make-string (+ loffset 1) ?#)))
(goto-char (point-min))
(delete-matching-lines "^:::$")
;; Fix Hugo shortcodes.
(save-excursion
(let ((regexp (concat "{{\\\\<"
"\\(?1:\\(.\\|\n\\)+?\\)"
"\\(\\s-\\|\n\\)+"
"\\(?1:.+?\\)"
"\\(\\s-\\|\n\\)+"
"\\\\>}}")))
(while (re-search-forward regexp nil :noerror)
(replace-match "{{<\\1>}}" :fixedcase))))
(replace-match "{{< \\1 >}}" :fixedcase))))
;; Convert Pandoc ref ID style to HTML ID's.
(save-excursion
(let ((regexp "^::: {#ref-\\(.+?\\)}$"))
(while (re-search-forward regexp nil :noerror)
(replace-match "<a id=\"ref-\\1\"></a>" :fixedcase))))
(replace-match "<a id=\"ref-\\1\"></a>\n" :fixedcase))))
;; Replace "::: {#refs .references}" with a base-level
;; "References" heading in Markdown.
(save-excursion
(let ((regexp "^::: {#refs \\.references}$"))
;; There should be one-and-only-one replacement needed for
;; this.
(re-search-forward regexp nil :noerror)
(replace-match (concat level-mark " References {#references}\n"))))
(buffer-substring-no-properties (point-min) (point-max)))))

(defun ox-hugo-pandoc-cite--parse-citations-maybe (info)
Expand Down Expand Up @@ -160,8 +173,12 @@ OUTFILE is the Org exported file name."
fname))
bib-list-1)))))))
(if bib-list
(let ((fm (plist-get info :front-matter)))
;; (message "fm :: %S" fm)
(let ((fm (plist-get info :front-matter))
(loffset (string-to-number
(or (org-entry-get nil "EXPORT_HUGO_LEVEL_OFFSET" :inherit)
(plist-get info :hugo-level-offset)))))
;; (message "[ox-hugo parse citations] fm :: %S" fm)
;; (message "[ox-hugo parse citations] loffset :: %S" loffset)
(ox-hugo-pandoc-cite--run-pandoc outfile bib-list)
;; Prepend the original ox-hugo generated front-matter to
;; Pandoc output.
Expand All @@ -170,7 +187,7 @@ OUTFILE is the Org exported file name."
(insert-file-contents outfile)
(buffer-substring-no-properties
(point-min) (point-max))))
(contents-fixed (ox-hugo-pandoc-cite--fix-pandoc-output post-pandoc-contents))
(contents-fixed (ox-hugo-pandoc-cite--fix-pandoc-output post-pandoc-contents loffset))
(fm-plus-content (concat fm "\n" contents-fixed)))
(write-region fm-plus-content nil outfile)))
(message "[ox-hugo-pandoc-cite] No bibliography file was specified"))))
Expand Down
3 changes: 1 addition & 2 deletions test/site/content-org/all-posts.org
Original file line number Diff line number Diff line change
Expand Up @@ -4614,11 +4614,10 @@ Content in section 2.
#+begin_mdshortcode
Text containing *Markdown*
#+end_mdshortcode

Some text.
#+begin_myshortcode
Text not containing *Markdown*
#+end_myshortcode

**** COMMENT References
* Export Options :export_option:
** Table of Contents (TOC) :toc:
Expand Down
12 changes: 9 additions & 3 deletions test/site/content/posts/citations-example.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,30 @@ Content in section 2.

{{% mdshortcode %}} Text containing **Markdown** {{% /mdshortcode %}}

{{< myshortcode >}} Text not containing **Markdown** {{< /myshortcode
>}}
Some text.

{{< myshortcode >}} Text not containing **Markdown** {{< /myshortcode >}}

## References {#references}

::: {#refs .references}
<a id="ref-eilan2016"></a>

Eilan, Naomi. 2016. "You Me and the World." *Analysis* 76 (3): 311--24.

<a id="ref-giovanelli2016"></a>

Giovanelli, Marco. 2016. "\"\...But I Still Can't Get Rid of a Sense of
Artificiality\" the Reichenbach--Einstein Debate on the Geometrization
of the Electromagnetic Field." *Studies in History and Philosophy of
Science* 54: 35--51.

<a id="ref-loncar2016"></a>

Loncar, Samuel. 2016. "Why Listen to Philosophers? A Constructive
Critique of Disciplinary Philosophy." *Metaphilosophy* 47 (1): 3--25.

<a id="ref-thompson2016"></a>

Thompson, Morgan, Toni Adleberg, Sam Sims, and Eddy Nahmias. 2016. "Why
Do Women Leave Philosophy? Surveying Students at the Introductory
Level."
Expand Down

0 comments on commit f2c1ab9

Please sign in to comment.