Skip to content

Commit 85fe5f4

Browse files
committed
markup: add --citeproc to pandoc converter
Adds the citeproc filter to the pandoc converter. There are several PRs for it this feature already. However, I think simply adding `--citeproc` is the cleanest way to enable this feature, with the option to flesh it out later, e.g., in gohugoio#7529. Some PRs and issues attempt adding more config options to Hugo which indirectly configure pandoc, but I think simply configuring Pandoc via Pandoc itself is simpler, as it is already possible with two YAML blocks -- one for Hugo, and one for Pandoc: --- title: This is the Hugo YAML block --- --- bibliography: assets/pandoc-yaml-block-bibliography.bib ... Document content with @citation! There are other useful options, e.g., gohugoio#4800 attempts to use `nocite`, which works out of the box with this PR: --- title: This is the Hugo YAML block --- --- bibliography: assets/pandoc-yaml-block-bibliography.bib nocite: | @* ... Document content with no citations but a full bibliography: ## Bibliography Other useful options are `csl: ...` and `link-citations: true`, which set the path to a custom CSL file and create HTML links between the references and the bibliography. The following issues and PRs are related: - Add support for parsing citations and Jupyter notebooks via Pandoc and/or Goldmark extension gohugoio#6101 Bundles multiple requests, this PR tackles citation parsing. - WIP: Bibliography with Pandoc gohugoio#4800 Passes the frontmatter to Pandoc and still uses `--filter pandoc-citeproc` instead of `--citeproc`. - Allow configuring Pandoc gohugoio#7529 That PR is much more extensive and might eventually supersede this PR, but I think --bibliography and --citeproc should be independent options (--bibliography should be optional and citeproc can always be specified). - Pandoc - allow citeproc extension to be invoked, with bibliography. gohugoio#8610 Similar to gohugoio#7529, gohugoio#8610 adds a new config option to Hugo. I think passing --citeproc and letting the users decide on the metadata they want to pass to pandoc is better, albeit uglier.
1 parent c1a8307 commit 85fe5f4

File tree

4 files changed

+156
-10
lines changed

4 files changed

+156
-10
lines changed

Diff for: docs/content/en/content-management/formats.md

+55-5
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,21 @@ Hugo passes reasonable default arguments to these external helpers by default:
4848

4949
- `asciidoctor`: `--no-header-footer -`
5050
- `rst2html`: `--leave-comments --initial-header-level=2`
51-
- `pandoc`: `--mathjax`
51+
- `pandoc`: `--mathjax --citeproc`
5252

5353
{{% warning "Performance of External Helpers" %}}
5454
Because additional formats are external commands, generation performance will rely heavily on the performance of the external tool you are using. As this feature is still in its infancy, feedback is welcome.
5555
{{% /warning %}}
5656

5757
### External Helper AsciiDoc
5858

59-
[AsciiDoc](https://github.com/asciidoc/asciidoc) implementation EOLs in Jan 2020 and is no longer supported.
60-
AsciiDoc development is being continued under [Asciidoctor](https://github.com/asciidoctor). The format AsciiDoc
59+
[AsciiDoc](https://github.com/asciidoc/asciidoc) implementation EOLs in Jan 2020 and is no longer supported.
60+
AsciiDoc development is being continued under [Asciidoctor](https://github.com/asciidoctor). The format AsciiDoc
6161
remains of course. Please continue with the implementation Asciidoctor.
6262

6363
### External Helper Asciidoctor
6464

65-
The Asciidoctor community offers a wide set of tools for the AsciiDoc format that can be installed additionally to Hugo.
65+
The Asciidoctor community offers a wide set of tools for the AsciiDoc format that can be installed additionally to Hugo.
6666
[See the Asciidoctor docs for installation instructions](https://asciidoctor.org/docs/install-toolchain/). Make sure that also all
6767
optional extensions like `asciidoctor-diagram` or `asciidoctor-html5s` are installed if required.
6868

@@ -110,13 +110,63 @@ Example of how to set extensions and attributes:
110110
my-attribute-name = "my value"
111111
```
112112

113-
In a complex Asciidoctor environment it is sometimes helpful to debug the exact call to your external helper with all
113+
In a complex Asciidoctor environment it is sometimes helpful to debug the exact call to your external helper with all
114114
parameters. Run Hugo with `-v`. You will get an output like
115115

116116
```
117117
INFO 2019/12/22 09:08:48 Rendering book-as-pdf.adoc with C:\Ruby26-x64\bin\asciidoctor.bat using asciidoc args [--no-header-footer -r asciidoctor-html5s -b html5s -r asciidoctor-diagram --base-dir D:\prototypes\hugo_asciidoc_ddd\docs -a outdir=D:\prototypes\hugo_asciidoc_ddd\build -] ...
118118
```
119119

120+
### External Helper Pandoc
121+
122+
[Pandoc](https://pandoc.org) is a universal document converter and can be used to convert markdown files.
123+
In Hugo, Pandoc can be used for LaTeX-style math (the `--mathjax` command line option is provided):
124+
125+
```
126+
---
127+
title: Math document
128+
---
129+
130+
Some inline math: $a^2 + b^2 = c^2$.
131+
```
132+
133+
This will render in your HTML as:
134+
135+
```
136+
<p>Some inline math: <span class="math inline">\(a^2 + b^2 = c^2\)</span></p>
137+
```
138+
You will have to [add MathJax](https://www.mathjax.org/#gettingstarted) to your template to properly render the math.
139+
140+
Additionally, Pandoc enables [citations](https://pandoc.org/MANUAL.html#extension-citations) using, e.g., [BibTeX files](https://en.wikibooks.org/wiki/LaTeX/Bibliography_Management#BibTeX):
141+
142+
```
143+
---
144+
title: Citation document
145+
---
146+
---
147+
bibliography: assets/bibliography.bib
148+
...
149+
This is a citation: @Doe2022
150+
```
151+
152+
Note that Hugo will **not** pass its metadata YAML block to Pandoc; however, it will pass the **second** meta data block, denoted with `---` and `...` to Pandoc. Thus, all pandoc settings should go there.
153+
154+
You can also add all elements from a bibliography file (without citing them first) using:
155+
156+
```
157+
---
158+
title: My Publications
159+
---
160+
---
161+
bibliography: assets/bibliography.bib
162+
nocite: |
163+
@*
164+
...
165+
```
166+
167+
It is also possible to provide a custom [CSL style](https://citationstyles.org/authors/) by passing `csl: path-to-style.csl` as a Pandoc option.
168+
169+
120170
## Learn Markdown
121171

122172
Markdown syntax is simple enough to learn in a single sitting. The following are excellent resources to get you up and running:

Diff for: markup/pandoc/convert.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (c *pandocConverter) getPandocContent(src []byte, ctx converter.DocumentCon
6464
" Leaving pandoc content unrendered.")
6565
return src, nil
6666
}
67-
args := []string{"--mathjax"}
67+
args := []string{"--mathjax", "--citeproc"}
6868
return internal.ExternallyRenderContent(c.cfg, ctx, src, binaryName, args)
6969
}
7070

Diff for: markup/pandoc/convert_test.go

+94-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
qt "github.com/frankban/quicktest"
2626
)
2727

28-
func TestConvert(t *testing.T) {
28+
func SetupTestConverter(t *testing.T) (*qt.C, converter.Converter) {
2929
if !Supports() {
3030
t.Skip("pandoc not installed")
3131
}
@@ -34,9 +34,99 @@ func TestConvert(t *testing.T) {
3434
sc.Exec.Allow = security.NewWhitelist("pandoc")
3535
p, err := Provider.New(converter.ProviderConfig{Exec: hexec.New(sc), Logger: loggers.NewErrorLogger()})
3636
c.Assert(err, qt.IsNil)
37-
conv, err := p.New(converter.DocumentContext{})
37+
ctx := converter.DocumentContext{Filename: "/tmp/hugo_pandoc_ddd/content/blog/pandoc/index.pandoc", DocumentName: "blog/pandoc/index.pandoc"}
38+
conv, err := p.New(ctx) // converter.DocumentContext{})
3839
c.Assert(err, qt.IsNil)
39-
b, err := conv.Convert(converter.RenderContext{Src: []byte("testContent")})
40+
return c, conv
41+
}
42+
43+
func RunConverterTest(t *testing.T, content string, expected string) {
44+
c, conv := SetupTestConverter(t)
45+
output, err := conv.Convert(converter.RenderContext{Src: []byte(content)})
4046
c.Assert(err, qt.IsNil)
41-
c.Assert(string(b.Bytes()), qt.Equals, "<p>testContent</p>\n")
47+
c.Assert(string(output.Bytes()), qt.Equals, expected)
48+
}
49+
50+
func TestConvert(t *testing.T) {
51+
RunConverterTest(t, "testContent", "<p>testContent</p>\n")
52+
}
53+
54+
func TestCiteprocWithHugoMeta(t *testing.T) {
55+
content := `
56+
---
57+
title: Test
58+
published: 2022-05-30
59+
---
60+
testContent
61+
`
62+
expected := "<p>testContent</p>\n"
63+
RunConverterTest(t, content, expected)
64+
}
65+
66+
func TestCiteprocWithPandocMeta(t *testing.T) {
67+
content := `
68+
---
69+
---
70+
---
71+
...
72+
testContent
73+
`
74+
expected := "<p>testContent</p>\n"
75+
RunConverterTest(t, content, expected)
76+
}
77+
78+
func TestCiteprocWithBibliography(t *testing.T) {
79+
content := `
80+
---
81+
---
82+
---
83+
bibliography: testdata/bibliography.bib
84+
...
85+
testContent
86+
`
87+
expected := "<p>testContent</p>\n"
88+
RunConverterTest(t, content, expected)
89+
}
90+
91+
func TestCiteprocWithExplicitCitation(t *testing.T) {
92+
content := `
93+
---
94+
---
95+
---
96+
bibliography: testdata/bibliography.bib
97+
...
98+
@Doe2022
99+
`
100+
expected := `<p><span class="citation" data-cites="Doe2022">Doe and Mustermann
101+
(2022)</span></p>
102+
<div id="refs" class="references csl-bib-body hanging-indent"
103+
role="doc-bibliography">
104+
<div id="ref-Doe2022" class="csl-entry" role="doc-biblioentry">
105+
Doe, Jane, and Max Mustermann. 2022. <span>“A Treatise on Hugo
106+
Tests.”</span> <em>Hugo Websites</em>.
107+
</div>
108+
</div>
109+
`
110+
RunConverterTest(t, content, expected)
111+
}
112+
113+
func TestCiteprocWithNocite(t *testing.T) {
114+
content := `
115+
---
116+
---
117+
---
118+
bibliography: testdata/bibliography.bib
119+
nocite: |
120+
@*
121+
...
122+
`
123+
expected := `<div id="refs" class="references csl-bib-body hanging-indent"
124+
role="doc-bibliography">
125+
<div id="ref-Doe2022" class="csl-entry" role="doc-biblioentry">
126+
Doe, Jane, and Max Mustermann. 2022. <span>“A Treatise on Hugo
127+
Tests.”</span> <em>Hugo Websites</em>.
128+
</div>
129+
</div>
130+
`
131+
RunConverterTest(t, content, expected)
42132
}

Diff for: markup/pandoc/testdata/bibliography.bib

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@article{Doe2022,
2+
author = "Jane Doe and Max Mustermann",
3+
title = "A Treatise on Hugo Tests",
4+
journal = "Hugo Websites",
5+
year = "2022",
6+
}

0 commit comments

Comments
 (0)