git.iko.soy/typeable/pandoc
on master help sign in
.circleci/config.yml+1−1
since splitsnapshots
unifiedsplit
@@ -2,7 +2,7 @@ version: "2.1"
2jobs:
2jobs:
3 build:
3 build:
4 macos:
4 macos:
5− xcode: 9.4.1
5+ xcode: 11.1.0
6 environment:
6 environment:
7 HOMEBREW_NO_AUTO_UPDATE: 1
7 HOMEBREW_NO_AUTO_UPDATE: 1
8 steps:
8 steps:
.editorconfig+8
@@ -8,6 +8,14 @@ indent_size = 2
8insert_final_newline = true
8insert_final_newline = true
9trim_trailing_whitespace = true
9trim_trailing_whitespace = true
10
10
11+[*.{zip,docx}]
12+charset =
13+end_of_line =
14+indent_style =
15+indent_size =
16+insert_final_newline =
17+trim_trailing_whitespace =
18+
11[*.{markdown,md}]
19[*.{markdown,md}]
12trim_trailing_whitespace = false
20trim_trailing_whitespace = false
13
21
.github/ISSUE_TEMPLATEdeleted−11
@@ -1,11 +0,0 @@
1−Asking a question?
2−Please use the pandoc-discuss mailing list instead:
3−https://groups.google.com/forum/#!forum/pandoc-discuss
4−This tracker is for bug reports and enhancement requests.
5−
6−Reporting a bug?
7−Please include your pandoc version and an example that allows us
8−to reproduce the problem (complete input, exact command line
9−used, expected and actual output). The smaller the example, the
10−better.
11−
.github/ISSUE_TEMPLATE/bug_report.mdnew file+25
@@ -0,0 +1,25 @@
1+---
2+name: Bug report
3+about: Create a report to help us improve
4+title: ''
5+labels: 'bug'
6+assignees: ''
7+
8+---
9+
10+<!--
11+Thank you for reporting an issue! Before you continue, please make sure that you have
12+
13+- searched the issue tracker for similar issues (including closed issues): https://github.com/jgm/pandoc/issues
14+- searched the pandoc-discuss mailing list for solutions: https://groups.google.com/forum/#!forum/pandoc-discuss
15+- reproduced your issue on the latest release of pandoc, or on https://pandoc.org/try
16+
17+Note that this bug tracker is for reporting bugs, not asking questions. For questions, use the pandoc-discuss mailing list.
18+-->
19+
20+**Explain the problem.**
21+Include the exact command line you used and all inputs necessary to reproduce the issue. Please create as minimal an example as possible, to help the maintainers isolate the problem. Explain the output you received and how it differs from what you expected.
22+
23+**Pandoc version?**
24+What version of pandoc are you using, on what OS?
25+
.github/ISSUE_TEMPLATE/config.ymlnew file+9
@@ -0,0 +1,9 @@
1+blank_issues_enabled: false
2+contact_links:
3+ - name: Ask a question
4+ url: https://groups.google.com/forum/#!forum/pandoc-discuss
5+ about: "Discussion forum for pandoc (may be used as an email list)"
6+ - name: Contributing Guidelines
7+ url: https://pandoc.org/CONTRIBUTING.html
8+ about: "Guidelines for submitting bug reports and code improvements"
9+
.github/ISSUE_TEMPLATE/feature_request.mdnew file+21
@@ -0,0 +1,21 @@
1+---
2+name: Feature request
3+about: Suggest an idea for this project
4+title: ''
5+labels: 'enhancement'
6+assignees: ''
7+
8+---
9+
10+<!--
11+Thank you for suggesting a feature! Before you continue, please make sure that you have
12+
13+- read the contributing guidelines: https://pandoc.org/CONTRIBUTING.html
14+- searched the issue tracker for similar issues (including closed issues): https://github.com/jgm/pandoc/issues
15+- searched the pandoc-discuss mailing list for relevant discussions: https://groups.google.com/forum/#!forum/pandoc-discuss
16+-->
17+
18+**Describe your proposed improvement and the problem it solves.**
19+
20+**Describe alternatives you've considered.**
21+
.github/workflows/ci.yml+92−8
@@ -45,23 +45,50 @@ jobs:
45 versions:
45 versions:
46 - ghc: '8.0.2'
46 - ghc: '8.0.2'
47 cabal: '2.4'
47 cabal: '2.4'
48+ prefix: 'v2-'
49+ cabalopts: ''
50+ testopts: ''
48 - ghc: '8.2.2'
51 - ghc: '8.2.2'
49 cabal: '2.4'
52 cabal: '2.4'
53+ prefix: 'v2-'
54+ cabalopts: ''
55+ testopts: ''
50 - ghc: '8.4.4'
56 - ghc: '8.4.4'
51 cabal: '2.4'
57 cabal: '2.4'
58+ prefix: 'v2-'
59+ cabalopts: '-f-embed_data_files'
60+ testopts: ''
52 - ghc: '8.6.5'
61 - ghc: '8.6.5'
53 cabal: '3.2'
62 cabal: '3.2'
63+ prefix: ''
64+ cabalopts: ''
65+ testopts: '--test-option=--hide-successes'
54 - ghc: '8.8.4'
66 - ghc: '8.8.4'
55 cabal: '3.2'
67 cabal: '3.2'
68+ prefix: ''
69+ cabalopts: ''
70+ testopts: '--test-option=--hide-successes'
56− - ghc: '8.10.2'
71+ - ghc: '8.10.4'
57 cabal: '3.2'
72 cabal: '3.2'
73+ prefix: ''
74+ cabalopts: '-ftrypandoc'
75+ testopts: '--test-option=--hide-successes'
76+ - ghc: '9.0.1'
77+ cabal: '3.4'
78+ prefix: ''
79+ cabalopts: ''
80+ testopts: '--test-option=--hide-successes'
58 steps:
81 steps:
59 - uses: actions/checkout@v2
82 - uses: actions/checkout@v2
60
83
84+ # needed by memory
85+ - name: Install numa
86+ run: sudo apt-get install libnuma-dev
87+
61 # need to install older cabal/ghc versions from ppa repository
88 # need to install older cabal/ghc versions from ppa repository
62
89
63 - name: Install recent cabal/ghc
90 - name: Install recent cabal/ghc
64− uses: actions/setup-haskell@v1.1.3
91+ uses: haskell/actions/setup@v1
65 with:
92 with:
66 ghc-version: ${{ matrix.versions.ghc }}
93 ghc-version: ${{ matrix.versions.ghc }}
67 cabal-version: ${{ matrix.versions.cabal }}
94 cabal-version: ${{ matrix.versions.cabal }}
@@ -88,14 +115,15 @@ jobs:
88
115
89 - name: Install dependencies
116 - name: Install dependencies
90 run: |
117 run: |
91− cabal v2-update
118+ cabal ${{ matrix.versions.prefix }}update
92− cabal v2-build --dependencies-only --enable-tests --disable-optimization
119+ cabal ${{ matrix.versions.prefix }}build ${{ matrix.versions.cabalopts }} --dependencies-only --enable-tests --disable-optimization
120+
93 - name: Build and test
121 - name: Build and test
94 run: |
122 run: |
95− cabal v2-build --enable-tests --disable-optimization 2>&1 | tee build.log
123+ cabal ${{ matrix.versions.prefix }}build ${{ matrix.versions.cabalopts }} --enable-tests --disable-optimization 2>&1 | tee build.log
96 # fail if warnings in local build
124 # fail if warnings in local build
97 ! grep -q ": *[Ww]arning:" build.log || exit 1
125 ! grep -q ": *[Ww]arning:" build.log || exit 1
98− cabal v2-test --disable-optimization
126+ cabal ${{ matrix.versions.prefix }}test ${{ matrix.versions.cabalopts }} --disable-optimization ${{ matrix.versions.testopts }}
99
127
100 windows:
128 windows:
101
129
@@ -105,7 +133,7 @@ jobs:
105 - uses: actions/checkout@v2
133 - uses: actions/checkout@v2
106
134
107 # We should have ghc 8.10, cabal, stack pre-installed
135 # We should have ghc 8.10, cabal, stack pre-installed
108− # - uses: actions/setup-haskell@v1.1.3
136+ # - uses: haskell/actions/setup@v1
109 # with:
137 # with:
110 # enable-stack: true
138 # enable-stack: true
111 # stack-version: 'latest'
139 # stack-version: 'latest'
@@ -154,7 +182,7 @@ jobs:
154 - uses: actions/checkout@v2
182 - uses: actions/checkout@v2
155
183
156 - name: Install recent cabal/ghc
184 - name: Install recent cabal/ghc
157− uses: actions/setup-haskell@v1.1.3
185+ uses: haskell/actions/setup@v1
158 with:
186 with:
159 ghc-version: ${{ matrix.versions.ghc }}
187 ghc-version: ${{ matrix.versions.ghc }}
160 cabal-version: ${{ matrix.versions.cabal }}
188 cabal-version: ${{ matrix.versions.cabal }}
@@ -189,3 +217,59 @@ jobs:
189 # fail if warnings in local build
217 # fail if warnings in local build
190 ! grep -q ": *[Ww]arning:" build.log || exit 1
218 ! grep -q ": *[Ww]arning:" build.log || exit 1
191 cabal v2-test --disable-optimization
219 cabal v2-test --disable-optimization
220+
221+ benchmark:
222+
223+ runs-on: ubuntu-18.04
224+ strategy:
225+ fail-fast: true
226+ matrix:
227+ versions:
228+ - ghc: '8.8.4'
229+ cabal: '3.2'
230+ - ghc: '8.10.2'
231+ cabal: '3.2'
232+ steps:
233+ - uses: actions/checkout@v2
234+
235+ # need to install older cabal/ghc versions from ppa repository
236+
237+ - name: Install recent cabal/ghc
238+ uses: haskell/actions/setup@v1
239+ with:
240+ ghc-version: ${{ matrix.versions.ghc }}
241+ cabal-version: ${{ matrix.versions.cabal }}
242+
243+ # declare/restore cached things
244+ # caching doesn't work for scheduled runs yet
245+ # https://github.com/actions/cache/issues/63
246+
247+ - name: Cache cabal global package db
248+ id: cabal-global
249+ uses: actions/cache@v2
250+ with:
251+ path: |
252+ ~/.cabal
253+ key: benchmark-${{ runner.os }}-${{ matrix.versions.ghc }}-${{ matrix.versions.cabal }}-cabal-global-${{ hashFiles('cabal.project') }}
254+
255+ - name: Cache cabal work
256+ id: cabal-local
257+ uses: actions/cache@v2
258+ with:
259+ path: |
260+ dist-newstyle
261+ key: benchmark-${{ runner.os }}-${{ matrix.versions.ghc }}-${{ matrix.versions.cabal }}-cabal-local
262+
263+ - name: Install dependencies
264+ run: |
265+ v2=$([ "${{ matrix.versions.cabal }}" = "2.2" ] && printf 'new' || printf 'v2')
266+ cabal $v2-update
267+ cabal $v2-build --dependencies-only --enable-optimization=1 --enable-benchmarks --disable-tests
268+
269+ - name: Build and test
270+ run: |
271+ v2=$([ "${{ matrix.versions.cabal }}" = "2.2" ] && printf 'new' || printf 'v2')
272+ cabal $v2-build --enable-optimization=1 --enable-benchmarks --disable-tests 2>&1 | tee build.log
273+ # fail if warnings in local build
274+ ! grep -q ": *[Ww]arning:" build.log || exit 1
275+ cabal $v2-bench --enable-optimization=1 --benchmark-options='--timeout=6 +RTS -T -RTS'
.github/workflows/nightly.yml+9−2
@@ -10,15 +10,22 @@ jobs:
10 runs-on: ubuntu-18.04
10 runs-on: ubuntu-18.04
11 steps:
11 steps:
12 - uses: actions/checkout@v2
12 - uses: actions/checkout@v2
13+
14+ - name: Install recent cabal/ghc
15+ uses: haskell/actions/setup@v1
16+ with:
17+ ghc-version: '8.10.3'
18+ cabal-version: '3.2'
19+
13 - name: Install dependencies
20 - name: Install dependencies
14 run: |
21 run: |
15 ghc --version
22 ghc --version
16 cabal --version
23 cabal --version
17 cabal v2-update
24 cabal v2-update
18− cabal v2-build --dependencies-only all
25+ cabal v2-build --dependencies-only
19 - name: Build
26 - name: Build
20 run: |
27 run: |
21− cabal v2-install all
28+ cabal v2-install
22 strip $HOME/.cabal/bin/pandoc
29 strip $HOME/.cabal/bin/pandoc
23 - name: Install artifact
30 - name: Install artifact
24 run: |
31 run: |
.github/workflows/release-candidate.yml−1
@@ -32,7 +32,6 @@ jobs:
32 - osarch: "windows-x86_64"
32 - osarch: "windows-x86_64"
33 arch: "x64"
33 arch: "x64"
34 stack_opts: "-j1"
34 stack_opts: "-j1"
35−
36 steps:
35 steps:
37 - uses: actions/checkout@v1
36 - uses: actions/checkout@v1
38 - name: Install dependencies
37 - name: Install dependencies
.hlint.yaml+5−5
@@ -9,11 +9,13 @@
9# Ignore some builtin hints
9# Ignore some builtin hints
10#
10#
11- ignore: {name: "Avoid lambda"}
11- ignore: {name: "Avoid lambda"}
12+- ignore: {name: "Use bimap"}
12- ignore: {name: "Eta reduce"}
13- ignore: {name: "Eta reduce"}
13- ignore: {name: "Evaluate"}
14- ignore: {name: "Evaluate"}
14- ignore: {name: "Reduce duplication"} # TODO: could be more fine-grained
15- ignore: {name: "Reduce duplication"} # TODO: could be more fine-grained
15- ignore: {name: "Use &&&"}
16- ignore: {name: "Use &&&"}
16- ignore: {name: "Use String"}
17- ignore: {name: "Use String"}
18+- ignore: {name: "Use camelCase"}
17- ignore: {name: "Use fmap"} # specific for GHC 7.8 compat
19- ignore: {name: "Use fmap"} # specific for GHC 7.8 compat
18- ignore: {name: "Use isDigit"}
20- ignore: {name: "Use isDigit"}
19
21
@@ -38,13 +40,10 @@
38 name: "Redundant return"
40 name: "Redundant return"
39 within: Text.Pandoc.Citeproc.BibTeX
41 within: Text.Pandoc.Citeproc.BibTeX
40
42
41−# TODO: check
43+# With recent hlint this rule has given false positives on TH
42- ignore:
44- ignore:
43 name: "Redundant bracket"
45 name: "Redundant bracket"
44− within:
46+
45− - Text.Pandoc.Citeproc
46− - Text.Pandoc.Citeproc.BibTeX
47− - Text.Pandoc.Citeproc.MetaValue
48
47
49- ignore:
48- ignore:
50 name: "Use <$>"
49 name: "Use <$>"
@@ -60,6 +59,7 @@
60 - Text.Pandoc.Citeproc
59 - Text.Pandoc.Citeproc
61 - Text.Pandoc.Extensions
60 - Text.Pandoc.Extensions
62 - Text.Pandoc.Lua.Marshaling.Version
61 - Text.Pandoc.Lua.Marshaling.Version
62+ - Text.Pandoc.Lua.Module.Pandoc
63 - Text.Pandoc.Lua.Module.Utils
63 - Text.Pandoc.Lua.Module.Utils
64 - Text.Pandoc.Readers.Odt.ContentReader
64 - Text.Pandoc.Readers.Odt.ContentReader
65 - Text.Pandoc.Readers.Odt.Namespaces
65 - Text.Pandoc.Readers.Odt.Namespaces
AUTHORS.md+18
@@ -20,6 +20,7 @@
20- Andreas Lööw
20- Andreas Lööw
21- Andrew Dunning
21- Andrew Dunning
22- Andy Morris
22- Andy Morris
23+- Anti-Distinctlyminty
23- Antoine Latter
24- Antoine Latter
24- Antonio Terceiro
25- Antonio Terceiro
25- Arata Mizuki
26- Arata Mizuki
@@ -35,6 +36,7 @@
35- Benoit Schweblin
36- Benoit Schweblin
36- Bjorn Buckwalter
37- Bjorn Buckwalter
37- Blake Eryx
38- Blake Eryx
39+- Bodigrim
38- Bradley Kuhn
40- Bradley Kuhn
39- Brent Yorgey
41- Brent Yorgey
40- Brian Leung
42- Brian Leung
@@ -45,6 +47,7 @@
45- Carlos Sosa
47- Carlos Sosa
46- Cécile Chemin
48- Cécile Chemin
47- Cédric Couralet
49- Cédric Couralet
50+- Charanjit Singh
48- Chris Black
51- Chris Black
49- Christian Conkle
52- Christian Conkle
50- Christian Despres
53- Christian Despres
@@ -75,8 +78,10 @@
75- Eigil Rischel
78- Eigil Rischel
76- Emanuel Evans
79- Emanuel Evans
77- Emerson Harkin
80- Emerson Harkin
81+- Emily Bourke
78- Emily Eisenberg
82- Emily Eisenberg
79- Eric Kow
83- Eric Kow
84+- Erik Rask
80- Eric Seidel
85- Eric Seidel
81- Eric Schrijver
86- Eric Schrijver
82- Ethan Riley
87- Ethan Riley
@@ -86,6 +91,7 @@
86- Florian Beeres
91- Florian Beeres
87- Florian Eitel
92- Florian Eitel
88- Florian Klink
93- Florian Klink
94+- Florian Kohrt
89- Francesco Occhipinti
95- Francesco Occhipinti
90- François Gannaz
96- François Gannaz
91- Frederik Elwert
97- Frederik Elwert
@@ -146,6 +152,7 @@
146- Josef Svenningsson
152- Josef Svenningsson
147- Joseph C. Sible
153- Joseph C. Sible
148- Julien Cretel
154- Julien Cretel
155+- Julien Dutant
149- Juliette Fourcot
156- Juliette Fourcot
150- Juliusz Gonera
157- Juliusz Gonera
151- Justin Bogner
158- Justin Bogner
@@ -163,6 +170,8 @@
163- Leif Metcalf
170- Leif Metcalf
164- Leonard Rosenthol
171- Leonard Rosenthol
165- Lila
172- Lila
173+- Loïc Grobol
174+- Lorenzo
166- Lucas Escot
175- Lucas Escot
167- Luke Plant
176- Luke Plant
168- Marc Schreiber
177- Marc Schreiber
@@ -196,10 +205,12 @@
196- Nathan Gass
205- Nathan Gass
197- Neil Mayhew
206- Neil Mayhew
198- Nick Bart
207- Nick Bart
208+- Nick Berendsen
199- Nick Fleisher
209- Nick Fleisher
200- Nicolas Kaiser
210- Nicolas Kaiser
201- Nikolay Yakimov
211- Nikolay Yakimov
202- Nils Carlson
212- Nils Carlson
213+- Nixon Enraght-Moony
203- Nokome Bentley
214- Nokome Bentley
204- Ole Martin Ruud
215- Ole Martin Ruud
205- Oliver Matthews
216- Oliver Matthews
@@ -224,6 +235,7 @@
224- Raymond Ehlers
235- Raymond Ehlers
225- Recai Oktaş
236- Recai Oktaş
226- Roland Hieber
237- Roland Hieber
238+- Roman Beránek
227- RyanGlScott
239- RyanGlScott
228- Salim B
240- Salim B
229- Sascha Wilde
241- Sascha Wilde
@@ -241,6 +253,7 @@
241- Stefan Dresselhaus
253- Stefan Dresselhaus
242- Sumit Sahrawat
254- Sumit Sahrawat
243- Tarik Graba
255- Tarik Graba
256+- Tatiana Porras
244- Thenaesh Elango
257- Thenaesh Elango
245- Thomas Hodgson
258- Thomas Hodgson
246- Thomas Weißschuh
259- Thomas Weißschuh
@@ -274,6 +287,7 @@
274- a-vrma
287- a-vrma
275- andrebauer
288- andrebauer
276- argent0
289- argent0
290+- badumont
277- blmage
291- blmage
278- bucklereed
292- bucklereed
279- bumper314
293- bumper314
@@ -289,8 +303,11 @@
289- kaizshang91
303- kaizshang91
290- lux-lth
304- lux-lth
291- lwolfsonkin
305- lwolfsonkin
306+- mbrackeantidot
292- nkalvi
307- nkalvi
293- niszet
308- niszet
309+- nuew
310+- obcat
294- oltolm
311- oltolm
295- oquechy
312- oquechy
296- quasicomputational
313- quasicomputational
@@ -302,6 +319,7 @@
302- shreevatsa.public
319- shreevatsa.public
303- takahashim
320- takahashim
304- taotieren
321- taotieren
322+- tecosaur
305- TEC
323- TEC
306- tgkokk
324- tgkokk
307- the-solipsist
325- the-solipsist
CONTRIBUTING.md+24−19
@@ -244,9 +244,16 @@ Or with stack:
244
244
245 stack test --test-arguments='-p markdown'
245 stack test --test-arguments='-p markdown'
246
246
247−It is often helpful to add `-j4` (run tests in parallel)
247+It is often helpful to add `-j4` (run tests in parallel) and
248−and `--hide-successes` (don't clutter output with successes)
248+`--hide-successes` (don't clutter output with successes) to the test
249−to the test arguments as well.
249+arguments as well. Collecting all options in a `cabal.project.local`
250+file in the project's root directory can help to keep `cabal`
251+commands short. E.g.:
252+
253+ flags: +embed_data_files
254+ tests: True
255+ test-show-details: direct
256+ test-options: -j4 --hide-successes
250
257
251If you add a new feature to pandoc, please add tests as well, following
258If you add a new feature to pandoc, please add tests as well, following
252the pattern of the existing tests. The test suite code is in
259the pattern of the existing tests. The test suite code is in
@@ -311,9 +318,6 @@ With stack:
311
318
312 stack bench
319 stack bench
313
320
314−You can also build `weigh-pandoc` (`stack build pandoc:weigh-pandoc`)
315−to get some statistics on memory usage. (Eventually this should
316−be incorporated into the benchmark suite.)
317
321
318Using the REPL
322Using the REPL
319--------------
323--------------
@@ -372,7 +376,7 @@ The code
372--------
376--------
373
377
374Pandoc has a publicly accessible git repository on
378Pandoc has a publicly accessible git repository on
375−GitHub: <http://github.com/jgm/pandoc>. To get a local copy of the source:
379+GitHub: <https://github.com/jgm/pandoc>. To get a local copy of the source:
376
380
377 git clone https://github.com/jgm/pandoc.git
381 git clone https://github.com/jgm/pandoc.git
378
382
@@ -382,7 +386,7 @@ the pandoc library is in `src/`, the source for the tests is in
382
386
383The modules `Text.Pandoc.Definition`, `Text.Pandoc.Builder`, and
387The modules `Text.Pandoc.Definition`, `Text.Pandoc.Builder`, and
384`Text.Pandoc.Generic` are in a separate library `pandoc-types`. The code can
388`Text.Pandoc.Generic` are in a separate library `pandoc-types`. The code can
385−be found in <http://github.com/jgm/pandoc-types>.
389+be found in <https://github.com/jgm/pandoc-types>.
386
390
387To build pandoc, you will need a working installation of the
391To build pandoc, you will need a working installation of the
388[Haskell platform].
392[Haskell platform].
@@ -402,11 +406,11 @@ The library is structured as follows:
402 tree automatically.
406 tree automatically.
403 - `Text.Pandoc.Readers.*` are the readers, and `Text.Pandoc.Writers.*`
407 - `Text.Pandoc.Readers.*` are the readers, and `Text.Pandoc.Writers.*`
404 are the writers.
408 are the writers.
405− - `Text.Pandoc.Biblio` is a utility module for formatting citations
409+ - `Text.Pandoc.Citeproc.*` contain the code for citation handling,
406− using citeproc-hs.
410+ including an interface to the [citeproc] library.
407 - `Text.Pandoc.Data` is used to embed data files when the `embed_data_files`
411 - `Text.Pandoc.Data` is used to embed data files when the `embed_data_files`
408− cabal flag is used. It is generated from `src/Text/Pandoc/Data.hsb` using
412+ cabal flag is used.
409− the preprocessor [hsb2hs].
413+ - `Text.Pandoc.Emoji` is a thin wrapper around [emojis].
410 - `Text.Pandoc.Highlighting` contains the interface to the
414 - `Text.Pandoc.Highlighting` contains the interface to the
411 skylighting library, which is used for code syntax highlighting.
415 skylighting library, which is used for code syntax highlighting.
412 - `Text.Pandoc.ImageSize` is a utility module containing functions for
416 - `Text.Pandoc.ImageSize` is a utility module containing functions for
@@ -417,7 +421,6 @@ The library is structured as follows:
417 - `Text.Pandoc.PDF` contains functions for producing a PDF from a
421 - `Text.Pandoc.PDF` contains functions for producing a PDF from a
418 LaTeX source.
422 LaTeX source.
419 - `Text.Pandoc.Parsing` contains parsing functions used in multiple readers.
423 - `Text.Pandoc.Parsing` contains parsing functions used in multiple readers.
420− - `Text.Pandoc.Pretty` is a pretty-printing library specialized to
421 the needs of pandoc.
424 the needs of pandoc.
422 - `Text.Pandoc.SelfContained` contains functions for making an HTML
425 - `Text.Pandoc.SelfContained` contains functions for making an HTML
423 file "self-contained," by importing remotely linked images, CSS,
426 file "self-contained," by importing remotely linked images, CSS,
@@ -445,14 +448,16 @@ you may want to consider submitting a pull request to the
445[closed issues]: https://github.com/jgm/pandoc/issues?q=is%3Aissue+is%3Aclosed
448[closed issues]: https://github.com/jgm/pandoc/issues?q=is%3Aissue+is%3Aclosed
446[latest released version]: https://github.com/jgm/pandoc/releases/latest
449[latest released version]: https://github.com/jgm/pandoc/releases/latest
447[Nightly builds]: https://github.com/jgm/pandoc/actions?query=workflow%3ANightly
450[Nightly builds]: https://github.com/jgm/pandoc/actions?query=workflow%3ANightly
448−[pandoc-discuss]: http://groups.google.com/group/pandoc-discuss
451+[pandoc-discuss]: https://groups.google.com/group/pandoc-discuss
449[issue tracker]: https://github.com/jgm/pandoc/issues
452[issue tracker]: https://github.com/jgm/pandoc/issues
450−[User's Guide]: http://pandoc.org/MANUAL.html
453+[User's Guide]: https://pandoc.org/MANUAL.html
451−[FAQs]: http://pandoc.org/faqs.html
454+[FAQs]: https://pandoc.org/faqs.html
452−[EditorConfig]: http://editorconfig.org/
455+[EditorConfig]: https://editorconfig.org/
453−[Haskell platform]: http://www.haskell.org/platform/
456+[Haskell platform]: https://www.haskell.org/platform/
454[hlint]: https://hackage.haskell.org/package/hlint
457[hlint]: https://hackage.haskell.org/package/hlint
458+[citeproc]: https://hackage.haskell.org/package/citeproc
459+[emojis]: https://hackage.haskell.org/package/emojis
455−[hsb2hs]: http://hackage.haskell.org/package/hsb2hs
460+[hsb2hs]: https://hackage.haskell.org/package/hsb2hs
456[pre-commit hook]: https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks
461[pre-commit hook]: https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks
457[GitHub labels]: https://github.com/jgm/pandoc/labels
462[GitHub labels]: https://github.com/jgm/pandoc/labels
458[good first issue]:https://github.com/jgm/pandoc/labels/good%20first%20issue
463[good first issue]:https://github.com/jgm/pandoc/labels/good%20first%20issue
COPYING.md+1−1
@@ -357,5 +357,5 @@ into proprietary programs. If your program is a subroutine library,
357you may consider it more useful to permit linking proprietary
357you may consider it more useful to permit linking proprietary
358applications with the library. If this is what you want to do, use the
358applications with the library. If this is what you want to do, use the
359[GNU Lesser General Public
359[GNU Lesser General Public
360−License](http://www.gnu.org/licenses/lgpl.html) instead of this
360+License](https://www.gnu.org/licenses/lgpl.html) instead of this
361License.
361License.
COPYRIGHT+3−3
@@ -22,11 +22,11 @@ The GNU General Public License is available in the file COPYING.md in
22the source distribution. On Debian systems, the complete text of the
22the source distribution. On Debian systems, the complete text of the
23GPL can be found in `/usr/share/common-licenses/GPL`.
23GPL can be found in `/usr/share/common-licenses/GPL`.
24
24
25−[GPL]: http://www.gnu.org/copyleft/gpl.html
25+[GPL]: https://www.gnu.org/copyleft/gpl.html
26
26
27Pandoc's complete source code is available from the [Pandoc home page].
27Pandoc's complete source code is available from the [Pandoc home page].
28
28
29−[Pandoc home page]: http://pandoc.org
29+[Pandoc home page]: https://pandoc.org
30
30
31Pandoc includes some code with different copyrights, or subject to different
31Pandoc includes some code with different copyrights, or subject to different
32licenses. The copyright and license statements for these sources are included
32licenses. The copyright and license statements for these sources are included
@@ -176,7 +176,7 @@ Released under the GNU General Public License version 2 or later.
176----------------------------------------------------------------------
176----------------------------------------------------------------------
177The dzslides template contains JavaScript and CSS from Paul Rouget's
177The dzslides template contains JavaScript and CSS from Paul Rouget's
178dzslides template.
178dzslides template.
179−http://github.com/paulrouget/dzslides
179+https://github.com/paulrouget/dzslides
180
180
181Released under the Do What the Fuck You Want To Public License.
181Released under the Do What the Fuck You Want To Public License.
182
182
INSTALL.md+18−15
@@ -34,6 +34,10 @@ without SVG support), [Python] (to use Pandoc filters), and
34
34
35 choco install rsvg-convert python miktex
35 choco install rsvg-convert python miktex
36
36
37+Using multiple installation methods can result in two separate
38+installations of pandoc; it is recommended to properly uninstall
39+pandoc before switching to an alternative installation method.
40+
37By default, Pandoc creates PDFs using LaTeX.
41By default, Pandoc creates PDFs using LaTeX.
38We recommend installing it via [MiKTeX].
42We recommend installing it via [MiKTeX].
39
43
@@ -45,7 +49,7 @@ by downloading [this script][uninstaller]
45and running it with `perl uninstall-pandoc.pl`.
49and running it with `perl uninstall-pandoc.pl`.
46
50
47Alternatively, you can install pandoc using
51Alternatively, you can install pandoc using
48−[Homebrew](http://brew.sh):
52+[Homebrew](https://brew.sh):
49
53
50 brew install pandoc
54 brew install pandoc
51
55
@@ -116,7 +120,7 @@ package repositories. For example, on Debian/Ubuntu,
116you can install it with `apt-get install haskell-platform`.
120you can install it with `apt-get install haskell-platform`.
117
121
118For PDF output, you'll need LaTeX. We recommend installing
122For PDF output, you'll need LaTeX. We recommend installing
119−[TeX Live](http://www.tug.org/texlive/) via your package
123+[TeX Live](https://www.tug.org/texlive/) via your package
120manager. (On Debian/Ubuntu, `apt-get install texlive`.)
124manager. (On Debian/Ubuntu, `apt-get install texlive`.)
121
125
122## Chrome OS
126## Chrome OS
@@ -238,7 +242,7 @@ The easiest way to build pandoc from source is to use [stack][stack]:
238
242
239 pandoc --help
243 pandoc --help
240
244
241− [Not sure where `$CABALDIR` is?](http://www.haskell.org/haskellwiki/Cabal-Install#The_cabal-install_configuration_file)
245+ [Not sure where `$CABALDIR` is?](https://wiki.haskell.org/Cabal-Install#The_cabal-install_configuration_file)
242
246
2435. By default `pandoc` uses the "i;unicode-casemap" method
2475. By default `pandoc` uses the "i;unicode-casemap" method
244 to sort bibliography entries (RFC 5051). If you would like to
248 to sort bibliography entries (RFC 5051). If you would like to
@@ -383,30 +387,29 @@ To run just the markdown benchmarks:
383
387
384
388
385[Arch]: https://www.archlinux.org/packages/community/x86_64/pandoc/
389[Arch]: https://www.archlinux.org/packages/community/x86_64/pandoc/
386−[Cabal User's Guide]: http://www.haskell.org/cabal/release/latest/doc/users-guide/builders.html#setup-configure-paths
390+[Cabal User's Guide]: https://cabal.readthedocs.io/
387[Debian]: https://packages.debian.org/pandoc
391[Debian]: https://packages.debian.org/pandoc
388[Fedora]: https://apps.fedoraproject.org/packages/pandoc
392[Fedora]: https://apps.fedoraproject.org/packages/pandoc
389−[FreeBSD ports]: http://www.freshports.org/textproc/hs-pandoc/
393+[FreeBSD ports]: https://www.freshports.org/textproc/hs-pandoc/
390−[GHC]: http://www.haskell.org/ghc/
394+[GHC]: https://www.haskell.org/ghc/
391−[GPL]: http://www.gnu.org/copyleft/gpl.html
392−[Haskell platform]: http://hackage.haskell.org/platform/
395+[Haskell platform]: https://hackage.haskell.org/platform/
393−[MacPorts]: http://trac.macports.org/browser/trunk/dports/textproc/pandoc/Portfile
396+[MacPorts]: https://trac.macports.org/browser/trunk/dports/textproc/pandoc/Portfile
394[MacTeX]: https://tug.org/mactex/
397[MacTeX]: https://tug.org/mactex/
395−[BasicTeX]: http://www.tug.org/mactex/morepackages.html
398+[BasicTeX]: https://www.tug.org/mactex/morepackages.html
396[LaTeX]: https://www.latex-project.org
399[LaTeX]: https://www.latex-project.org
397−[MiKTeX]: http://miktex.org/
400+[MiKTeX]: https://miktex.org/
398[librsvg]: https://wiki.gnome.org/Projects/LibRsvg
401[librsvg]: https://wiki.gnome.org/Projects/LibRsvg
399[Python]: https://www.python.org
402[Python]: https://www.python.org
400−[NetBSD]: http://pkgsrc.se/wip/pandoc
403+[NetBSD]: https://pkgsrc.se/wip/pandoc
401[NixOS]: https://nixos.org/nixos/packages.html
404[NixOS]: https://nixos.org/nixos/packages.html
402[Slackware]: https://www.slackbuilds.org/result/?search=pandoc&sv=
405[Slackware]: https://www.slackbuilds.org/result/?search=pandoc&sv=
403[Ubuntu]: https://packages.ubuntu.com/pandoc
406[Ubuntu]: https://packages.ubuntu.com/pandoc
404[download page]: https://github.com/jgm/pandoc/releases/latest
407[download page]: https://github.com/jgm/pandoc/releases/latest
405−[gentoo]: http://packages.gentoo.org/package/app-text/pandoc
408+[gentoo]: https://packages.gentoo.org/package/app-text/pandoc
406[haskell repository]: https://wiki.archlinux.org/index.php/Haskell_Package_Guidelines#.5Bhaskell.5D
409[haskell repository]: https://wiki.archlinux.org/index.php/Haskell_Package_Guidelines#.5Bhaskell.5D
407[openSUSE]: https://software.opensuse.org/package/pandoc
410[openSUSE]: https://software.opensuse.org/package/pandoc
408−[source tarball]: http://hackage.haskell.org/package/pandoc
411+[source tarball]: https://hackage.haskell.org/package/pandoc
409[stack]: https://docs.haskellstack.org/en/stable/install_and_upgrade.html
412[stack]: https://docs.haskellstack.org/en/stable/install_and_upgrade.html
410−[cabal-install]: http://hackage.haskell.org/package/cabal-install
413+[cabal-install]: https://hackage.haskell.org/package/cabal-install
411[Void]: https://voidlinux.org/
414[Void]: https://voidlinux.org/
412[uninstaller]: https://raw.githubusercontent.com/jgm/pandoc/master/macos/uninstall-pandoc.pl
415[uninstaller]: https://raw.githubusercontent.com/jgm/pandoc/master/macos/uninstall-pandoc.pl
MANUAL.txt+258−78
@@ -1,7 +1,7 @@
1---
1---
2title: Pandoc User's Guide
2title: Pandoc User's Guide
3author: John MacFarlane
3author: John MacFarlane
4−date: January 21, 2021
4+date: June 20, 2021
5---
5---
6
6
7# Synopsis
7# Synopsis
@@ -136,7 +136,7 @@ to output the generated LaTeX. You can then test it with `pdflatex test.tex`.
136When using LaTeX, the following packages need to be available
136When using LaTeX, the following packages need to be available
137(they are included with all recent versions of [TeX Live]):
137(they are included with all recent versions of [TeX Live]):
138[`amsfonts`], [`amsmath`], [`lm`], [`unicode-math`],
138[`amsfonts`], [`amsmath`], [`lm`], [`unicode-math`],
139−[`ifxetex`], [`ifluatex`], [`listings`] (if the
139+[`iftex`], [`listings`] (if the
140`--listings` option is used), [`fancyvrb`], [`longtable`],
140`--listings` option is used), [`fancyvrb`], [`longtable`],
141[`booktabs`], [`graphicx`] (if the document
141[`booktabs`], [`graphicx`] (if the document
142contains images), [`hyperref`], [`xcolor`],
142contains images), [`hyperref`], [`xcolor`],
@@ -180,8 +180,7 @@ footnotes in tables).
180[`graphicx`]: https://ctan.org/pkg/graphicx
180[`graphicx`]: https://ctan.org/pkg/graphicx
181[`grffile`]: https://ctan.org/pkg/grffile
181[`grffile`]: https://ctan.org/pkg/grffile
182[`hyperref`]: https://ctan.org/pkg/hyperref
182[`hyperref`]: https://ctan.org/pkg/hyperref
183−[`ifluatex`]: https://ctan.org/pkg/ifluatex
184−[`ifxetex`]: https://ctan.org/pkg/ifxetex
183+[`iftex`]: https://ctan.org/pkg/iftex
185[`listings`]: https://ctan.org/pkg/listings
184[`listings`]: https://ctan.org/pkg/listings
186[`lm`]: https://ctan.org/pkg/lm
185[`lm`]: https://ctan.org/pkg/lm
187[`longtable`]: https://ctan.org/pkg/longtable
186[`longtable`]: https://ctan.org/pkg/longtable
@@ -362,15 +361,15 @@ header when requesting a document from a URL:
362 will be used. On \*nix and macOS systems this will be the `pandoc`
361 will be used. On \*nix and macOS systems this will be the `pandoc`
363 subdirectory of the XDG data directory (by default,
362 subdirectory of the XDG data directory (by default,
364 `$HOME/.local/share`, overridable by setting the `XDG_DATA_HOME`
363 `$HOME/.local/share`, overridable by setting the `XDG_DATA_HOME`
365− environment variable). If that directory does not exist,
364+ environment variable). If that directory does not exist and
366− `$HOME/.pandoc` will be used (for backwards compatibility).
365+ `$HOME/.pandoc` exists, it will be used (for backwards compatibility).
367− In Windows the default user data directory is
366+ On Windows the default user data directory is
368 `C:\Users\USERNAME\AppData\Roaming\pandoc`.
367 `C:\Users\USERNAME\AppData\Roaming\pandoc`.
369 You can find the default user data directory on your system by
368 You can find the default user data directory on your system by
370 looking at the output of `pandoc --version`.
369 looking at the output of `pandoc --version`.
370+ Data files placed in this directory (for example, `reference.odt`,
371− A `reference.odt`, `reference.docx`, `epub.css`, `templates`,
371+ `reference.docx`, `epub.css`, `templates`) will override
372− `slidy`, `slideous`, or `s5` directory
373− placed in this directory will override pandoc's normal defaults.
372+ pandoc's normal defaults.
374
373
375`-d` *FILE*, `--defaults=`*FILE*
374`-d` *FILE*, `--defaults=`*FILE*
376
375
@@ -487,7 +486,7 @@ header when requesting a document from a URL:
487[Emacs Org mode]: https://orgmode.org
486[Emacs Org mode]: https://orgmode.org
488[AsciiDoc]: https://www.methods.co.nz/asciidoc/
487[AsciiDoc]: https://www.methods.co.nz/asciidoc/
489[AsciiDoctor]: https://asciidoctor.org/
488[AsciiDoctor]: https://asciidoctor.org/
490−[DZSlides]: http://paulrouget.com/dzslides/
489+[DZSlides]: https://paulrouget.com/dzslides/
491[Word docx]: https://en.wikipedia.org/wiki/Office_Open_XML
490[Word docx]: https://en.wikipedia.org/wiki/Office_Open_XML
492[PDF]: https://www.adobe.com/pdf/
491[PDF]: https://www.adobe.com/pdf/
493[reveal.js]: https://revealjs.com/
492[reveal.js]: https://revealjs.com/
@@ -676,12 +675,12 @@ header when requesting a document from a URL:
676: Extract images and other media contained in or linked from
675: Extract images and other media contained in or linked from
677 the source document to the path *DIR*, creating it if
676 the source document to the path *DIR*, creating it if
678 necessary, and adjust the images references in the document
677 necessary, and adjust the images references in the document
679− so they point to the extracted files. If the source format is
678+ so they point to the extracted files. Media are downloaded,
680− a binary container (docx, epub, or odt), the media is
679+ read from the file system, or extracted from a binary
681− extracted from the container and the original
680+ container (e.g. docx), as needed. The original file paths
682− filenames are used. Otherwise the media is read from the
681+ are used if they are relative paths not containing `..`.
683− file system or downloaded, and new filenames are constructed
682+ Otherwise filenames are constructed from the SHA1 hash of
684− based on SHA1 hashes of the contents.
683+ the contents.
685
684
686`--abbreviations=`*FILE*
685`--abbreviations=`*FILE*
687
686
@@ -692,9 +691,9 @@ header when requesting a document from a URL:
692 system default, use
691 system default, use
693 `pandoc --print-default-data-file=abbreviations`. The only
692 `pandoc --print-default-data-file=abbreviations`. The only
694 use pandoc makes of this list is in the Markdown reader.
693 use pandoc makes of this list is in the Markdown reader.
695− Strings ending in a period that are found in this list will
694+ Strings found in this list will be followed by a nonbreaking
696− be followed by a nonbreaking space, so that the period will
697− not produce sentence-ending space in formats like LaTeX.
695+ space, and the period will not produce sentence-ending space
696+ in formats like LaTeX. The strings may not contain spaces.
698
697
699[`pandocfilters`]: https://github.com/jgm/pandocfilters
698[`pandocfilters`]: https://github.com/jgm/pandocfilters
700[PHP]: https://github.com/vinai/pandocfilters-php
699[PHP]: https://github.com/vinai/pandocfilters-php
@@ -892,6 +891,11 @@ header when requesting a document from a URL:
892 will not be searched. For example:
891 will not be searched. For example:
893 `--resource-path=.:test` will search the working directory
892 `--resource-path=.:test` will search the working directory
894 and the `test` subdirectory, in that order.
893 and the `test` subdirectory, in that order.
894+ This option can be used repeatedly. Search path components
895+ that come later on the command line will be searched before
896+ those that come earlier, so
897+ `--resource-path foo:bar --resource-path baz:bim` is
898+ equivalent to `--resource-path baz:bim:foo:bar`.
895
899
896`--request-header=`*NAME*`:`*VAL*
900`--request-header=`*NAME*`:`*VAL*
897
901
@@ -1411,13 +1415,12 @@ results only for basic math, usually you will want to use
1411`--gladtex`
1415`--gladtex`
1412
1416
1413: Enclose TeX math in `<eq>` tags in HTML output. The resulting HTML
1417: Enclose TeX math in `<eq>` tags in HTML output. The resulting HTML
1414− can then be processed by [GladTeX] to produce images of the typeset
1418+ can then be processed by [GladTeX] to produce SVG images of the typeset
1415− formulas and an HTML file with links to these images.
1419+ formulas and an HTML file with these images embedded.
1416− So, the procedure is:
1417
1420
1418 pandoc -s --gladtex input.md -o myfile.htex
1421 pandoc -s --gladtex input.md -o myfile.htex
1419− gladtex -d myfile-images myfile.htex
1422+ gladtex -d image_dir myfile.htex
1420− # produces myfile.html and images in myfile-images
1423+ # produces myfile.html and images in image_dir
1421
1424
1422[MathML]: https://www.w3.org/Math/
1425[MathML]: https://www.w3.org/Math/
1423[MathJax]: https://www.mathjax.org
1426[MathJax]: https://www.mathjax.org
@@ -1465,6 +1468,7 @@ Nonzero exit codes have the following meanings:
1465 24 PandocCiteprocError
1468 24 PandocCiteprocError
1466 31 PandocEpubSubdirectoryError
1469 31 PandocEpubSubdirectoryError
1467 43 PandocPDFError
1470 43 PandocPDFError
1471+ 44 PandocXMLError
1468 47 PandocPDFProgramNotFoundError
1472 47 PandocPDFProgramNotFoundError
1469 61 PandocHttpError
1473 61 PandocHttpError
1470 62 PandocShouldNeverHappenError
1474 62 PandocShouldNeverHappenError
@@ -1477,6 +1481,7 @@ Nonzero exit codes have the following meanings:
1477 91 PandocMacroLoop
1481 91 PandocMacroLoop
1478 92 PandocUTF8DecodingError
1482 92 PandocUTF8DecodingError
1479 93 PandocIpynbDecodingError
1483 93 PandocIpynbDecodingError
1484+ 94 PandocUnsupportedCharsetError
1480 97 PandocCouldNotFindDataFileError
1485 97 PandocCouldNotFindDataFileError
1481 99 PandocResourceNotFound
1486 99 PandocResourceNotFound
1482----- ------------------------------------
1487----- ------------------------------------
@@ -1568,8 +1573,11 @@ data-dir:
1568verbosity: INFO
1573verbosity: INFO
1569log-file: log.json
1574log-file: log.json
1570
1575
1571−# citeproc, natbib, or biblatex
1576+# citeproc, natbib, or biblatex. This only affects LaTeX
1577+# output. If you want to use citeproc to format citations,
1578+# you should also set 'citeproc: true' (see above).
1572cite-method: citeproc
1579cite-method: citeproc
1580+
1573# part, chapter, section, or default:
1581# part, chapter, section, or default:
1574top-level-division: chapter
1582top-level-division: chapter
1575abbreviations:
1583abbreviations:
@@ -1668,6 +1676,34 @@ one line:
1668verbosity: INFO
1676verbosity: INFO
1669```
1677```
1670
1678
1679+In fields that expect a file path (or list of file paths), the
1680+following syntax may be used to interpolate environment variables:
1681+
1682+``` yaml
1683+csl: ${HOME}/mycsldir/special.csl
1684+```
1685+
1686+`${USERDATA}` may also be used; this will always resolve to the
1687+user data directory that is current when the defaults file is
1688+parsed, regardless of the setting of the environment
1689+variable `USERDATA`.
1690+
1691+`${.}` will resolve to the directory containing the default
1692+file itself. This allows you to refer to resources contained
1693+in that directory:
1694+
1695+``` yaml
1696+epub-cover-image: ${.}/cover.jpg
1697+epub-metadata: ${.}/meta.xml
1698+resource-path:
1699+- . # the working directory from which pandoc is run
1700+- ${.}/images # the images subdirectory of the directory
1701+ # containing this defaults file
1702+```
1703+
1704+This environment variable interpolation syntax *only* works in
1705+fields that expect file paths.
1706+
1671Default files can be placed in the `defaults` subdirectory of
1707Default files can be placed in the `defaults` subdirectory of
1672the user data directory and used from any directory. For
1708the user data directory and used from any directory. For
1673example, one could create a file specifying defaults for writing
1709example, one could create a file specifying defaults for writing
@@ -1895,7 +1931,8 @@ ${ styles.html() }
1895```
1931```
1896
1932
1897(If a partial is not found in the directory of the
1933(If a partial is not found in the directory of the
1934+template and the template path is given as a relative
1898−template, it will also be sought in the `templates`
1935+path, it will also be sought in the `templates`
1899subdirectory of the user data directory.)
1936subdirectory of the user data directory.)
1900
1937
1901Partials may optionally be applied to variables using
1938Partials may optionally be applied to variables using
@@ -2277,8 +2314,8 @@ math equations flush left using [YAML metadata](#layout) or with
2277
2314
2278These affect HTML output when [producing slide shows with pandoc].
2315These affect HTML output when [producing slide shows with pandoc].
2279
2316
2280−All [reveal.js configuration options] are available as variables.
2317+`institute`
2281−To turn off boolean flags that default to true in reveal.js, use `0`.
2318+: author affiliations: can be a list when there are multiple authors
2282
2319
2283`revealjs-url`
2320`revealjs-url`
2284: base URL for reveal.js documents (defaults to
2321: base URL for reveal.js documents (defaults to
@@ -2298,6 +2335,9 @@ To turn off boolean flags that default to true in reveal.js, use `0`.
2298: additional attributes for the title slide of reveal.js slide shows.
2335: additional attributes for the title slide of reveal.js slide shows.
2299 See [background in reveal.js and beamer] for an example.
2336 See [background in reveal.js and beamer] for an example.
2300
2337
2338+All [reveal.js configuration options] are available as variables.
2339+To turn off boolean flags that default to true in reveal.js, use `0`.
2340+
2301[reveal.js configuration options]: https://revealjs.com/config/
2341[reveal.js configuration options]: https://revealjs.com/config/
2302
2342
2303### Variables for Beamer slides
2343### Variables for Beamer slides
@@ -3477,6 +3517,9 @@ line must begin with a space.
3477 | 200 Main St.
3517 | 200 Main St.
3478 | Berkeley, CA 94718
3518 | Berkeley, CA 94718
3479
3519
3520+Inline formatting (such as emphasis) is allowed in the content,
3521+but not block-level formatting (such as block quotes or lists).
3522+
3480This syntax is borrowed from [reStructuredText].
3523This syntax is borrowed from [reStructuredText].
3481
3524
3482## Lists
3525## Lists
@@ -3711,7 +3754,7 @@ definition:
3711Note that space between items in a definition list is required.
3754Note that space between items in a definition list is required.
3712(A variant that loosens this requirement, but disallows "lazy"
3755(A variant that loosens this requirement, but disallows "lazy"
3713hard wrapping, can be activated with `compact_definition_lists`: see
3756hard wrapping, can be activated with `compact_definition_lists`: see
3714−[Non-pandoc extensions], below.)
3757+[Non-default extensions], below.)
3715
3758
3716[^3]: I have been influenced by the suggestions of [David
3759[^3]: I have been influenced by the suggestions of [David
3717 Wheeler](https://justatheory.com/2009/02/modest-markdown-proposal/).
3760 Wheeler](https://justatheory.com/2009/02/modest-markdown-proposal/).
@@ -4136,10 +4179,12 @@ option is used. All of the metadata will appear in a single block
4136at the beginning of the document.
4179at the beginning of the document.
4137
4180
4138Note that [YAML] escaping rules must be followed. Thus, for example,
4181Note that [YAML] escaping rules must be followed. Thus, for example,
4139−if a title contains a colon, it must be quoted. The pipe character
4182+if a title contains a colon, it must be quoted, and if it contains a
4183+backslash escape, then it must be ensured that it is not treated as a
4184+[YAML escape sequence]. The pipe character (`|`) can be used to begin
4140−(`|`) can be used to begin an indented block that will be interpreted
4185+an indented block that will be interpreted literally, without need for
4141−literally, without need for escaping. This form is necessary
4186+escaping. This form is necessary when the field contains blank lines
4142−when the field contains blank lines or block-level formatting:
4187+or block-level formatting:
4143
4188
4144 ---
4189 ---
4145 title: 'This is the title: it contains a colon'
4190 title: 'This is the title: it contains a colon'
@@ -4153,6 +4198,12 @@ when the field contains blank lines or block-level formatting:
4153 It consists of two paragraphs.
4198 It consists of two paragraphs.
4154 ...
4199 ...
4155
4200
4201+The literal block after the `|` must be indented relative to the
4202+line containing the `|`. If it is not, the YAML will be invalid
4203+and pandoc will not interpret it as metadata. For an overview
4204+of the complex rules governing YAML, see the [Wikipedia entry on
4205+YAML syntax].
4206+
4156Template variables will be set automatically from the metadata. Thus, for
4207Template variables will be set automatically from the metadata. Thus, for
4157example, in writing HTML, the variable `abstract` will be set to the HTML
4208example, in writing HTML, the variable `abstract` will be set to the HTML
4158equivalent of the Markdown in the `abstract` field:
4209equivalent of the Markdown in the `abstract` field:
@@ -4199,6 +4250,24 @@ will be interpreted as markdown. For example:
4199 \renewcommand{\section}[1]{\clearpage\oldsection{#1}}
4250 \renewcommand{\section}[1]{\clearpage\oldsection{#1}}
4200 ```
4251 ```
4201
4252
4253+Note: the `yaml_metadata_block` extension works with
4254+`commonmark` as well as `markdown` (and it is enabled by default
4255+in `gfm` and `commonmark_x`). However, in these formats the
4256+following restrictions apply:
4257+
4258+- The YAML metadata block must occur at the beginning of the
4259+ document (and there can be only one). If multiple files are
4260+ given as arguments to pandoc, only the first can be a YAML
4261+ metadata block.
4262+
4263+- The leaf nodes of the YAML structure are parsed in isolation from
4264+ each other and from the rest of the document. So, for
4265+ example, you can't use a reference link in these contexts
4266+ if the link definition is somewhere else in the document.
4267+
4268+[YAML escape sequence]: https://yaml.org/spec/1.2/spec.html#id2776092
4269+[Wikipedia entry on YAML syntax]: https://en.m.wikipedia.org/wiki/YAML#Syntax
4270+
4202## Backslash escapes
4271## Backslash escapes
4203
4272
4204#### Extension: `all_symbols_escapable` ####
4273#### Extension: `all_symbols_escapable` ####
@@ -4356,7 +4425,8 @@ you need to enclose text in literal `$` characters, backslash-escape
4356them and they won't be treated as math delimiters.
4425them and they won't be treated as math delimiters.
4357
4426
4358For display math, use `$$` delimiters. (In this case, the delimiters
4427For display math, use `$$` delimiters. (In this case, the delimiters
4359−may be separated from the formula by whitespace.)
4428+may be separated from the formula by whitespace. However, there can be
4429+no blank lines betwen the opening and closing `$$` delimiters.)
4360
4430
4361TeX math will be printed in all output formats. How it is rendered
4431TeX math will be printed in all output formats. How it is rendered
4362depends on the output format:
4432depends on the output format:
@@ -4890,37 +4960,68 @@ Inline and regular footnotes may be mixed freely.
4890
4960
4891#### Extension: `citations` ####
4961#### Extension: `citations` ####
4892
4962
4893−Markdown citations go inside square brackets and are separated
4963+To cite a bibliographic item with an identifier foo, use the
4894−by semicolons. Each citation must have a key, composed of '@' +
4964+syntax `@foo`. Normal citations should be included in square
4895−the citation identifier from the database, and may optionally
4965+brackets, with semicolons separating distinct items:
4896−have a prefix, a locator, and a suffix. The citation key must
4966+
4967+ Blah blah [@doe99; @smith2000; @smith2004].
4968+
4969+How this is rendered depends on the citation style. In an
4970+author-date style, it might render as
4971+
4972+ Blah blah (Doe 1999, Smith 2000, 2004).
4973+
4974+In a footnote style, it might render as
4975+
4976+ Blah blah.[^1]
4977+
4978+ [^1]: John Doe, "Frogs," *Journal of Amphibians* 44 (1999);
4979+ Susan Smith, "Flies," *Journal of Insects* (2000);
4980+ Susan Smith, "Bees," *Journal of Insects* (2004).
4981+
4982+See the [CSL user documentation] for more information about CSL
4983+styles and how they affect rendering.
4984+
4897−begin with a letter, digit, or `_`, and may contain
4985+Unless a citation key start with a letter, digit, or `_`,
4898−alphanumerics, `_`, and internal punctuation characters
4986+and contains only alphanumerics and internal punctuation
4899−(`:.#$%&-+?<>~/`). Here are some examples:
4987+characters (`:.#$%&-+?<>~/`), it must be surrounded
4900−
4988+by curly braces, which are not considered part of the key.
4901− Blah blah [see @doe99, pp. 33-35; also @smith04, chap. 1].
4989+In `@Foo_bar.baz.`, the key is `Foo_bar.baz`. The final
4902−
4990+period is not *internal* punctuation, so it is not included in
4903− Blah blah [@doe99, pp. 33-35, 38-39 and *passim*].
4991+the key. In `@{Foo_bar.baz.}`, the key is `Foo_bar.baz.`, including
4904−
4992+the final period. The curly braces are recommended if you use URLs as
4905− Blah blah [@smith04; @doe99].
4993+keys: `[@{https://example.com/bib?name=foobar&date=2000}, p. 33]`.
4906−
4994+
4995+Citation items may optionally include a prefix, a locator, and
4996+a suffix. In
4997+
4998+ Blah blah [see @doe99, pp. 33-35 and *passim*; @smith04, chap. 1].
4999+
5000+The first item (`doe99`) has prefix `see `, locator `pp. 33-35`,
5001+and suffix `and *passim*`. The second item (`smith04`) has
5002+locator `chap. 1` and no prefix or suffix.
5003+
5004+Pandoc uses some heuristics to separate the locator from the
5005+rest of the subject. It is sensitive to the locator terms
4907−`pandoc` detects locator terms in the [CSL locale files].
5006+defined in the [CSL locale files]. Either abbreviated or
4908−Either abbreviated or unabbreviated forms are accepted. In the `en-US`
5007+unabbreviated forms are accepted. In the `en-US` locale, locator
4909−locale, locator terms can be written in either singular or plural forms,
5008+terms can be written in either singular or plural forms, as
4910−as `book`, `bk.`/`bks.`; `chapter`, `chap.`/`chaps.`; `column`,
5009+`book`, `bk.`/`bks.`; `chapter`, `chap.`/`chaps.`; `column`,
4911−`col.`/`cols.`; `figure`, `fig.`/`figs.`; `folio`, `fol.`/`fols.`;
5010+`col.`/`cols.`; `figure`, `fig.`/`figs.`; `folio`,
4912−`number`, `no.`/`nos.`; `line`, `l.`/`ll.`; `note`, `n.`/`nn.`; `opus`,
5011+`fol.`/`fols.`; `number`, `no.`/`nos.`; `line`, `l.`/`ll.`;
5012+`note`, `n.`/`nn.`; `opus`, `op.`/`opp.`; `page`, `p.`/`pp.`;
4913−`op.`/`opp.`; `page`, `p.`/`pp.`; `paragraph`, `para.`/`paras.`; `part`,
5013+`paragraph`, `para.`/`paras.`; `part`, `pt.`/`pts.`; `section`,
4914−`pt.`/`pts.`; `section`, `sec.`/`secs.`; `sub verbo`, `s.v.`/`s.vv.`;
5014+`sec.`/`secs.`; `sub verbo`, `s.v.`/`s.vv.`; `verse`,
4915−`verse`, `v.`/`vv.`; `volume`, `vol.`/`vols.`; `¶`/`¶¶`; `§`/`§§`. If no
5015+`v.`/`vv.`; `volume`, `vol.`/`vols.`; `¶`/`¶¶`; `§`/`§§`. If no
4916locator term is used, "page" is assumed.
5016locator term is used, "page" is assumed.
4917
5017
4918−`pandoc` will use heuristics to distinguish the locator
5018+In complex cases, you can force something to be treated as
4919−from the suffix. In complex cases, the locator can be enclosed
4920−in curly braces:
5019+a locator by enclosing it in curly braces or prevent parsing
5020+the suffix as locator by prepending curly braces:
4921
5021
4922 [@smith{ii, A, D-Z}, with a suffix]
5022 [@smith{ii, A, D-Z}, with a suffix]
4923 [@smith, {pp. iv, vi-xi, (xv)-(xvii)} with suffix here]
5023 [@smith, {pp. iv, vi-xi, (xv)-(xvii)} with suffix here]
5024+ [@smith{}, 99 years later]
4924
5025
4925A minus sign (`-`) before the `@` will suppress mention of
5026A minus sign (`-`) before the `@` will suppress mention of
4926the author in the citation. This can be useful when the
5027the author in the citation. This can be useful when the
@@ -4928,13 +5029,26 @@ author is already mentioned in the text:
4928
5029
4929 Smith says blah [-@smith04].
5030 Smith says blah [-@smith04].
4930
5031
4931−You can also write an in-text citation, as follows:
5032+You can also write an author-in-text citation, by omitting the
5033+square brackets:
4932
5034
4933 @smith04 says blah.
5035 @smith04 says blah.
4934
5036
4935 @smith04 [p. 33] says blah.
5037 @smith04 [p. 33] says blah.
4936
5038
5039+This will cause the author's name to be rendered, followed by
5040+the bibliographical details. Use this form when you want to
5041+make the citation the subject of a sentence.
4937
5042
5043+When you are using a note style, it is usually better to let
5044+citeproc create the footnotes from citations rather than writing
5045+an explicit note. If you do write an explicit note that
5046+contains a citation, note that normal citations will be put in
5047+parentheses, while author-in-text citations will not. For
5048+this reason, it is sometimes preferable to use the
5049+author-in-text style inside notes when using a note style.
5050+
5051+[CSL user documentation]: https://citationstyles.org/authors/
4938[CSL]: https://docs.citationstyles.org/en/stable/specification.html
5052[CSL]: https://docs.citationstyles.org/en/stable/specification.html
4939[CSL markup specs]: https://docs.citationstyles.org/en/1.0/release-notes.html#rich-text-markup-within-fields
5053[CSL markup specs]: https://docs.citationstyles.org/en/1.0/release-notes.html#rich-text-markup-within-fields
4940[Chicago Manual of Style]: https://chicagomanualofstyle.org
5054[Chicago Manual of Style]: https://chicagomanualofstyle.org
@@ -4943,13 +5057,44 @@ You can also write an in-text citation, as follows:
4943[finding and editing styles]: https://citationstyles.org/authors/
5057[finding and editing styles]: https://citationstyles.org/authors/
4944[CSL locale files]: https://github.com/citation-style-language/locales
5058[CSL locale files]: https://github.com/citation-style-language/locales
4945
5059
4946−## Non-pandoc extensions
5060+## Non-default extensions
4947
5061
4948The following Markdown syntax extensions are not enabled by default
5062The following Markdown syntax extensions are not enabled by default
4949in pandoc, but may be enabled by adding `+EXTENSION` to the format
5063in pandoc, but may be enabled by adding `+EXTENSION` to the format
4950name, where `EXTENSION` is the name of the extension. Thus, for
5064name, where `EXTENSION` is the name of the extension. Thus, for
4951example, `markdown+hard_line_breaks` is Markdown with hard line breaks.
5065example, `markdown+hard_line_breaks` is Markdown with hard line breaks.
4952
5066
5067+#### Extension: `rebase_relative_paths` ####
5068+
5069+Rewrite relative paths for Markdown links and images, depending
5070+on the path of the file containing the link or image link. For
5071+each link or image, pandoc will compute the directory of the
5072+containing file, relative to the working directory, and prepend
5073+the resulting path to the link or image path.
5074+
5075+The use of this extension is best understood by example.
5076+Suppose you have a a subdirectory for each chapter of a book,
5077+`chap1`, `chap2`, `chap3`. Each contains a file `text.md` and a
5078+number of images used in the chapter. You would like to have
5079+`![image](spider.jpg)` in `chap1/text.md` refer to
5080+`chap1/spider.jpg` and `![image](spider.jpg)` in `chap2/text.md`
5081+refer to `chap2/spider.jpg`. To do this, use
5082+
5083+ pandoc chap*/*.md -f markdown+rebase_relative_paths
5084+
5085+Without this extension, you would have to use
5086+`![image](chap1/spider.jpg)` in `chap1/text.md` and
5087+`![image](chap2/spider.jpg)` in `chap2/text.md`. Links with
5088+relative paths will be rewritten in the same way as images.
5089+
5090+Absolute paths and URLs are not changed. Neither are empty
5091+paths or paths consisting entirely of a fragment, e.g., `#foo`.
5092+
5093+Note that relative paths in reference links and images will
5094+be rewritten relative to the file containing the link
5095+reference definition, not the file containing the reference link
5096+or image itself, if these differ.
5097+
4953#### Extension: `attributes` ####
5098#### Extension: `attributes` ####
4954
5099
4955Allows attributes to be attached to any inline or block-level
5100Allows attributes to be attached to any inline or block-level
@@ -5483,10 +5628,23 @@ A few other metadata fields affect bibliography formatting:
5483
5628
5484`lang`
5629`lang`
5485: The `lang` field will affect how the style is localized,
5630: The `lang` field will affect how the style is localized,
5486− for example in the translation of labels and the use
5631+ for example in the translation of labels, the use
5632+ of quotation marks, and the way items are sorted.
5487− of quotation marks. (For backwards compatibility,
5633+ (For backwards compatibility, `locale` may be used instead
5488− `locale` may be used instead of `lang`, but this use
5489− is deprecated.)
5634+ of `lang`, but this use is deprecated.)
5635+
5636+ A BCP 47 language tag is expected: for example, `en`,
5637+ `de`, `en-US`, `fr-CA`, `ug-Cyrl`. The unicode extension
5638+ syntax (after `-u-`) may be used to specify options for
5639+ collation (sorting) more precisely. Here are some examples:
5640+
5641+ - `zh-u-co-pinyin` -- Chinese with the Pinyin collation.
5642+ - `es-u-co-trad` -- Spanish with the traditional collation
5643+ (with `Ch` sorting after `C`).
5644+ - `fr-u-kb` -- French with "backwards" accent sorting
5645+ (with `coté` sorting after `côte`).
5646+ - `en-US-u-kf-upper` -- English with uppercase letters sorting
5647+ before lower (default is lower before upper).
5490
5648
5491`notes-after-punctuation`
5649`notes-after-punctuation`
5492: If true (the default), pandoc will put footnote citations
5650: If true (the default), pandoc will put footnote citations
@@ -5498,12 +5656,6 @@ A few other metadata fields affect bibliography formatting:
5498 punctuation.
5656 punctuation.
5499
5657
5500
5658
5501−
5502−
5503−
5504−
5505−
5506−
5507# Slide shows
5659# Slide shows
5508
5660
5509You can use pandoc to produce an HTML + JavaScript slide presentation
5661You can use pandoc to produce an HTML + JavaScript slide presentation
@@ -5596,7 +5748,11 @@ rules:
5596 * A heading at the slide level always starts a new slide.
5748 * A heading at the slide level always starts a new slide.
5597
5749
5598 * Headings *below* the slide level in the hierarchy create
5750 * Headings *below* the slide level in the hierarchy create
5599− headings *within* a slide.
5751+ headings *within* a slide. (In beamer, a "block" will be
5752+ created. If the heading has the class `example`, an
5753+ `exampleblock` environment will be used; if it has the class
5754+ `alert`, an `alertblock` will be used; otherwise a regular
5755+ `block` will be used.)
5600
5756
5601 * Headings *above* the slide level in the hierarchy create
5757 * Headings *above* the slide level in the hierarchy create
5602 "title slides," which just contain the section title
5758 "title slides," which just contain the section title
@@ -5929,6 +6085,15 @@ The following fields are recognized:
5929
6085
5930`rights`
6086`rights`
5931 ~ A string value.
6087 ~ A string value.
6088+
6089+`belongs-to-collection`
6090+ ~ A string value. identifies the name of a collection to which
6091+ the EPUB Publication belongs.
6092+
6093+`group-position`
6094+ ~ The `group-position` field indicates the numeric position in which
6095+ the EPUB Publication belongs relative to other works belonging to
6096+ the same `belongs-to-collection` field.
5932
6097
5933`cover-image`
6098`cover-image`
5934 ~ A string value (path to cover image).
6099 ~ A string value (path to cover image).
@@ -6165,7 +6330,7 @@ Then edit `my.theme` and use it like this:
6165If you are not satisfied with the built-in highlighting, or you
6330If you are not satisfied with the built-in highlighting, or you
6166want highlight a language that isn't supported, you can use the
6331want highlight a language that isn't supported, you can use the
6167`--syntax-definition` option to load a [KDE-style XML syntax definition
6332`--syntax-definition` option to load a [KDE-style XML syntax definition
6168−file](https://docs.kde.org/stable5/en/applications/katepart/highlight.html).
6333+file](https://docs.kde.org/stable5/en/kate/katepart/highlight.html).
6169Before writing your own, have a look at KDE's [repository of syntax
6334Before writing your own, have a look at KDE's [repository of syntax
6170definitions](https://github.com/KDE/syntax-highlighting/tree/master/data/syntax).
6335definitions](https://github.com/KDE/syntax-highlighting/tree/master/data/syntax).
6171
6336
@@ -6296,6 +6461,21 @@ subdirectory of your user data directory (see [Templates]).
6296
6461
6297[Lua]: https://www.lua.org
6462[Lua]: https://www.lua.org
6298
6463
6464+# Reproducible builds
6465+
6466+Some of the document formats pandoc targets (such as EPUB,
6467+docx, and ODT) include build timestamps in the generated document.
6468+That means that the files generated on successive builds will
6469+differ, even if the source does not. To avoid this, set the
6470+`SOURCE_DATE_EPOCH` environment variable, and the timestamp will
6471+be taken from it instead of the current time.
6472+`SOURCE_DATE_EPOCH` should contain an integer unix timestamp
6473+(specifying the number of second since midnight UTC January 1, 1970).
6474+
6475+Some document formats also include a unique identifier. For
6476+EPUB, this can be set explicitly by setting the `identifier`
6477+metadata field (see [EPUB Metadata], above).
6478+
6299# A note on security
6479# A note on security
6300
6480
6301If you use pandoc to convert user-contributed content in a web
6481If you use pandoc to convert user-contributed content in a web
@@ -6324,9 +6504,9 @@ application, here are some things to keep in mind:
63244. The HTML generated by pandoc is not guaranteed to be safe.
65044. The HTML generated by pandoc is not guaranteed to be safe.
6325 If `raw_html` is enabled for the Markdown input, users can
6505 If `raw_html` is enabled for the Markdown input, users can
6326 inject arbitrary HTML. Even if `raw_html` is disabled,
6506 inject arbitrary HTML. Even if `raw_html` is disabled,
6327− users can include dangerous content in attributes for
6507+ users can include dangerous content in URLs and attributes.
6328− headings, spans, and code blocks. To be safe, you should
6329− run all the generated HTML through an HTML sanitizer.
6508+ To be safe, you should run all the generated HTML through
6509+ an HTML sanitizer.
6330
6510
6331# Authors
6511# Authors
6332
6512
Makefile+32−11
@@ -2,17 +2,31 @@ version?=$(shell grep '^[Vv]ersion:' pandoc.cabal | awk '{print $$2;}')
2pandoc=$(shell find dist -name pandoc -type f -exec ls -t {} \; | head -1)
2pandoc=$(shell find dist -name pandoc -type f -exec ls -t {} \; | head -1)
3SOURCEFILES?=$(shell git ls-tree -r master --name-only | grep "\.hs$$")
3SOURCEFILES?=$(shell git ls-tree -r master --name-only | grep "\.hs$$")
4BRANCH?=master
4BRANCH?=master
5−RESOLVER?=lts-13
5+ARCH=$(shell uname -m)
6+DOCKERIMAGE=registry.gitlab.b-data.ch/ghc/ghc4pandoc:8.10.4
7+COMMIT=$(shell git rev-parse --short HEAD)
8+TIMESTAMP=$(shell date "+%Y%m%d_%H%M")
9+LATESTBENCH=$(word 1,$(shell ls -t bench_*.csv 2>/dev/null))
10+BASELINE?=$(LATESTBENCH)
11+ifeq ($(BASELINE),)
12+BASELINECMD=
13+else
14+BASELINECMD=--baseline $(BASELINE)
15+endif
6−GHCOPTS=-fdiagnostics-color=always
16+GHCOPTS=-fdiagnostics-color=always -j4 +RTS -A8m -RTS
7WEBSITE=../../web/pandoc.org
17WEBSITE=../../web/pandoc.org
8REVISION?=1
18REVISION?=1
19+# For gauge:
20+# BENCHARGS?=--small --ci=0.90 --match=pattern $(PATTERN)
21+# For tasty-bench:
22+BENCHARGS?=--csv bench_$(TIMESTAMP).csv $(BASELINECMD) --timeout=6 +RTS -T -RTS $(if $(PATTERN),--pattern "$(PATTERN)",)
9
23
10quick:
24quick:
11− stack install --ghc-options='$(GHCOPTS)' --install-ghc --flag 'pandoc:embed_data_files' --fast --test --ghc-options='-j +RTS -A64m -RTS' --test-arguments='-j4 --hide-successes $(TESTARGS)'
25+ stack install --ghc-options='$(GHCOPTS)' --install-ghc --flag 'pandoc:embed_data_files' --fast --test --ghc-options='$(GHCOPTS)' --test-arguments='-j4 --hide-successes $(TESTARGS)'
12
26
13quick-cabal:
27quick-cabal:
14 cabal new-configure . --ghc-options '$(GHCOPTS)' --disable-optimization --enable-tests
28 cabal new-configure . --ghc-options '$(GHCOPTS)' --disable-optimization --enable-tests
15− cabal new-build . --disable-optimization
29+ cabal new-build -j4 . --disable-optimization
16 cabal new-run test-pandoc --disable-optimization -- --hide-successes $(TESTARGS)
30 cabal new-run test-pandoc --disable-optimization -- --hide-successes $(TESTARGS)
17
31
18full-cabal:
32full-cabal:
@@ -21,7 +35,7 @@ full-cabal:
21 cabal new-run test-pandoc --disable-optimization -- --hide-successes $(TESTARGS)
35 cabal new-run test-pandoc --disable-optimization -- --hide-successes $(TESTARGS)
22
36
23full:
37full:
24− stack install --flag 'pandoc:embed_data_files' --flag 'pandoc:trypandoc' --bench --no-run-benchmarks --test --test-arguments='-j4 --hide-successes' --ghc-options '-Wall -Werror -fno-warn-unused-do-bind -O0 -j4 $(GHCOPTS)'
38+ stack install --flag 'pandoc:embed_data_files' --flag 'pandoc:trypandoc' --bench --no-run-benchmarks --test --test-arguments='-j4 --hide-successes' --ghc-options '-Wall -Werror -fno-warn-unused-do-bind -O0 $(GHCOPTS)'
25
39
26ghci:
40ghci:
27 stack ghci --flag 'pandoc:embed_data_files'
41 stack ghci --flag 'pandoc:embed_data_files'
@@ -37,11 +51,14 @@ test:
37ghcid:
51ghcid:
38 ghcid -c "stack repl --flag 'pandoc:embed_data_files'"
52 ghcid -c "stack repl --flag 'pandoc:embed_data_files'"
39
53
40−bench:
54+ghcid-test:
41− stack bench --benchmark-arguments='$(BENCHARGS)' --ghc-options '$(GHCOPTS)'
55+ ghcid -c "stack repl --ghc-options=-XNoImplicitPrelude --flag 'pandoc:embed_data_files' --ghci-options=-fobject-code pandoc:lib pandoc:test-pandoc"
42
56
43−weigh:
57+bench:
58+ stack bench \
44− stack build --ghc-options '$(GHCOPTS)' pandoc:weigh-pandoc && stack exec weigh-pandoc
59+ --ghc-options '$(GHCOPTS)' \
60+ --benchmark-arguments='$(BENCHARGS)' 2>&1 | \
61+ tee "bench_latest.txt"
45
62
46reformat:
63reformat:
47 for f in $(SOURCEFILES); do echo $$f; stylish-haskell -i $$f ; done
64 for f in $(SOURCEFILES); do echo $$f; stylish-haskell -i $$f ; done
@@ -73,10 +90,14 @@ checkdocs:
73debpkg: man/pandoc.1
90debpkg: man/pandoc.1
74 docker run -v `pwd`:/mnt \
91 docker run -v `pwd`:/mnt \
75 -v `pwd`/linux/artifacts:/artifacts \
92 -v `pwd`/linux/artifacts:/artifacts \
93+ --user $(id -u):$(id -g) \
76 -e REVISION=$(REVISION) \
94 -e REVISION=$(REVISION) \
77 -w /mnt \
95 -w /mnt \
78− utdemir/ghc-musl:v12-libgmp-ghc8101 bash \
96+ --memory=0 \
97+ --rm \
98+ $(DOCKERIMAGE) \
99+ bash \
79− /mnt/linux/make_artifacts.sh
100+ /mnt/linux/make_artifacts.sh 2>&1 > docker.log
80
101
81man/pandoc.1: MANUAL.txt man/pandoc.1.before man/pandoc.1.after
102man/pandoc.1: MANUAL.txt man/pandoc.1.before man/pandoc.1.after
82 pandoc $< -f markdown -t man -s \
103 pandoc $< -f markdown -t man -s \
README.md+6−6
@@ -8,10 +8,10 @@ pandoc --lua-filter tools/update-readme.lua README.template -o README.md
8[![github
8[![github
9release](https://img.shields.io/github/release/jgm/pandoc.svg?label=current+release)](https://github.com/jgm/pandoc/releases)
9release](https://img.shields.io/github/release/jgm/pandoc.svg?label=current+release)](https://github.com/jgm/pandoc/releases)
10[![hackage
10[![hackage
11−release](https://img.shields.io/hackage/v/pandoc.svg?label=hackage)](http://hackage.haskell.org/package/pandoc)
11+release](https://img.shields.io/hackage/v/pandoc.svg?label=hackage)](https://hackage.haskell.org/package/pandoc)
12−[![homebrew](https://img.shields.io/homebrew/v/pandoc.svg)](http://brewformulas.org/Pandoc)
12+[![homebrew](https://img.shields.io/homebrew/v/pandoc.svg)](https://formulae.brew.sh/formula/pandoc)
13[![stackage LTS
13[![stackage LTS
14−package](http://stackage.org/package/pandoc/badge/lts)](http://stackage.org/lts/package/pandoc)
14+package](https://stackage.org/package/pandoc/badge/lts)](https://www.stackage.org/lts/package/pandoc-types)
15[![CI
15[![CI
16tests](https://github.com/jgm/pandoc/workflows/CI%20tests/badge.svg)](https://github.com/jgm/pandoc/actions)
16tests](https://github.com/jgm/pandoc/workflows/CI%20tests/badge.svg)](https://github.com/jgm/pandoc/actions)
17[![license](https://img.shields.io/badge/license-GPLv2+-lightgray.svg)](https://www.gnu.org/licenses/gpl.html)
17[![license](https://img.shields.io/badge/license-GPLv2+-lightgray.svg)](https://www.gnu.org/licenses/gpl.html)
@@ -20,7 +20,7 @@ groups](https://img.shields.io/badge/pandoc-discuss-red.svg?style=social)](https
20
20
21## The universal markup converter
21## The universal markup converter
22
22
23−Pandoc is a [Haskell](http://haskell.org) library for converting from
23+Pandoc is a [Haskell](https://haskell.org) library for converting from
24one markup format to another, and a command-line tool that uses this
24one markup format to another, and a command-line tool that uses this
25library. It can convert *from*
25library. It can convert *from*
26
26
@@ -175,7 +175,7 @@ It can convert *to*
175 and JavaScript slide show)
175 and JavaScript slide show)
176- `slidy` ([Slidy](https://www.w3.org/Talks/Tools/Slidy2/) HTML and
176- `slidy` ([Slidy](https://www.w3.org/Talks/Tools/Slidy2/) HTML and
177 JavaScript slide show)
177 JavaScript slide show)
178−- `dzslides` ([DZSlides](http://paulrouget.com/dzslides/) HTML5 +
178+- `dzslides` ([DZSlides](https://paulrouget.com/dzslides/) HTML5 +
179 JavaScript slide show),
179 JavaScript slide show),
180- `revealjs` ([reveal.js](https://revealjs.com/) HTML5 + JavaScript
180- `revealjs` ([reveal.js](https://revealjs.com/) HTML5 + JavaScript
181 slide show)
181 slide show)
@@ -239,6 +239,6 @@ opening a new issue.
239## License
239## License
240
240
241© 2006-2021 John MacFarlane (jgm@berkeley.edu). Released under the
241© 2006-2021 John MacFarlane (jgm@berkeley.edu). Released under the
242−[GPL](http://www.gnu.org/copyleft/gpl.html "GNU General Public License"),
242+[GPL](https://www.gnu.org/licenses/old-licenses/gpl-2.0.html "GNU General Public License"),
243version 2 or greater. This software carries no warranty of any kind.
243version 2 or greater. This software carries no warranty of any kind.
244(See COPYRIGHT for full copyright and warranty notices.)
244(See COPYRIGHT for full copyright and warranty notices.)
README.template+5−5
@@ -7,9 +7,9 @@ Pandoc
7======
7======
8
8
9[![github release](https://img.shields.io/github/release/jgm/pandoc.svg?label=current+release)](https://github.com/jgm/pandoc/releases)
9[![github release](https://img.shields.io/github/release/jgm/pandoc.svg?label=current+release)](https://github.com/jgm/pandoc/releases)
10−[![hackage release](https://img.shields.io/hackage/v/pandoc.svg?label=hackage)](http://hackage.haskell.org/package/pandoc)
10+[![hackage release](https://img.shields.io/hackage/v/pandoc.svg?label=hackage)](https://hackage.haskell.org/package/pandoc)
11−[![homebrew](https://img.shields.io/homebrew/v/pandoc.svg)](http://brewformulas.org/Pandoc)
11+[![homebrew](https://img.shields.io/homebrew/v/pandoc.svg)](https://formulae.brew.sh/formula/pandoc)
12−[![stackage LTS package](http://stackage.org/package/pandoc/badge/lts)](http://stackage.org/lts/package/pandoc)
12+[![stackage LTS package](https://stackage.org/package/pandoc/badge/lts)](https://www.stackage.org/lts/package/pandoc-types)
13[![CI tests](https://github.com/jgm/pandoc/workflows/CI%20tests/badge.svg)](https://github.com/jgm/pandoc/actions)
13[![CI tests](https://github.com/jgm/pandoc/workflows/CI%20tests/badge.svg)](https://github.com/jgm/pandoc/actions)
14[![license](https://img.shields.io/badge/license-GPLv2+-lightgray.svg)](https://www.gnu.org/licenses/gpl.html)
14[![license](https://img.shields.io/badge/license-GPLv2+-lightgray.svg)](https://www.gnu.org/licenses/gpl.html)
15[![pandoc-discuss on google groups](https://img.shields.io/badge/pandoc-discuss-red.svg?style=social)](https://groups.google.com/forum/#!forum/pandoc-discuss)
15[![pandoc-discuss on google groups](https://img.shields.io/badge/pandoc-discuss-red.svg?style=social)](https://groups.google.com/forum/#!forum/pandoc-discuss)
@@ -85,5 +85,5 @@ License
85[GPL], version 2 or greater. This software carries no warranty of
85[GPL], version 2 or greater. This software carries no warranty of
86any kind. (See COPYRIGHT for full copyright and warranty notices.)
86any kind. (See COPYRIGHT for full copyright and warranty notices.)
87
87
88−[GPL]: http://www.gnu.org/copyleft/gpl.html "GNU General Public License"
88+[GPL]: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html "GNU General Public License"
89−[Haskell]: http://haskell.org
89+[Haskell]: https://haskell.org
RELEASE-CHECKLIST+1
@@ -4,6 +4,7 @@
4[ ] Finalize changelog
4[ ] Finalize changelog
5[ ] push release candidate branch rc/VERSION and (if it builds
5[ ] push release candidate branch rc/VERSION and (if it builds
6 successfully) download artifacts
6 successfully) download artifacts
7+[ ] run tools/build-arm.sh to create and download arm64 linux package
7[ ] download macos artifacts from CircleCI
8[ ] download macos artifacts from CircleCI
8[ ] Use 'make' in macos and windows artifacts to sign code
9[ ] Use 'make' in macos and windows artifacts to sign code
9[ ] make update-website
10[ ] make update-website
SECURITY.mdnew file+12
@@ -0,0 +1,12 @@
1+# Security Policy
2+
3+## Supported Versions
4+
5+Only the most recent version of pandoc is supported with security updates.
6+
7+## Reporting a Vulnerability
8+
9+To report a vulnerability, email the maintainer, jgm@berkeley.edu.
10+But first please read the section of the manual entitled
11+[A note on security](https://pandoc.org/MANUAL.html#a-note-on-security),
12+which describes some security guarantees pandoc does NOT make.
app/pandoc.hs−2
@@ -1,4 +1,3 @@
1−{-# LANGUAGE NoImplicitPrelude #-}
2{- |
1{- |
3 Module : Main
2 Module : Main
4 Copyright : Copyright (C) 2006-2021 John MacFarlane
3 Copyright : Copyright (C) 2006-2021 John MacFarlane
@@ -12,7 +11,6 @@ Parses command-line options and calls the appropriate readers and
12writers.
11writers.
13-}
12-}
14module Main where
13module Main where
15−import Prelude
16import qualified Control.Exception as E
14import qualified Control.Exception as E
17import Text.Pandoc.App (convertWithOpts, defaultOpts, options, parseOptions)
15import Text.Pandoc.App (convertWithOpts, defaultOpts, options, parseOptions)
18import Text.Pandoc.Error (handleError)
16import Text.Pandoc.Error (handleError)
benchmark/benchmark-pandoc.hs+64−83
@@ -1,4 +1,3 @@
1−{-# LANGUAGE NoImplicitPrelude #-}
2{-# LANGUAGE OverloadedStrings #-}
1{-# LANGUAGE OverloadedStrings #-}
3{-
2{-
4Copyright (C) 2012-2021 John MacFarlane <jgm@berkeley.edu>
3Copyright (C) 2012-2021 John MacFarlane <jgm@berkeley.edu>
@@ -17,106 +16,88 @@ You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
16along with this program; if not, write to the Free Software
18Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19-}
18-}
20−import Prelude
21import Text.Pandoc
19import Text.Pandoc
22import Text.Pandoc.MIME
20import Text.Pandoc.MIME
21+import Control.DeepSeq (force)
23−import Control.Monad.Except (throwError, liftIO)
22+import Control.Monad.Except (throwError)
24import qualified Text.Pandoc.UTF8 as UTF8
23import qualified Text.Pandoc.UTF8 as UTF8
25import qualified Data.ByteString as B
24import qualified Data.ByteString as B
26import qualified Data.Text as T
25import qualified Data.Text as T
27−import Criterion.Main
26+import Test.Tasty.Bench
28−import Criterion.Types (Config(..))
27+-- import Gauge
29−import Data.List (intersect)
30−import Data.Maybe (mapMaybe, catMaybes)
31−import System.Environment (getArgs)
32import qualified Data.ByteString.Lazy as BL
28import qualified Data.ByteString.Lazy as BL
33−
29+import Data.Maybe (mapMaybe)
34−data Input = InputText {unInputText :: T.Text}
30+import Data.List (sortOn)
35− | InputBS {unInputBS :: BL.ByteString}
36
31
37readerBench :: Pandoc
32readerBench :: Pandoc
38 -> T.Text
33 -> T.Text
39− -> IO (Maybe Benchmark)
34+ -> Maybe Benchmark
40−readerBench doc name = do
35+readerBench _ name
41− let (rdr, rexts) = either (error . show) id . runPure $ getReader name
36+ | name `elem` ["bibtex", "biblatex", "csljson"] = Nothing
42− res <- runIO $ do
37+readerBench doc name = either (const Nothing) Just $
38+ runPure $ do
39+ (rdr, rexts) <- getReader name
43− (wtr, wexts) <- getWriter name
40+ (wtr, wexts) <- getWriter name
44− case (rdr, wtr) of
41+ case (rdr, wtr) of
45− (TextReader r, TextWriter w) -> do
42+ (TextReader r, TextWriter w) -> do
46− setResourcePath ["./test"]
47− inp <- w def{ writerWrapText = WrapAuto
43+ inp <- w def{ writerWrapText = WrapAuto
48− , writerExtensions = wexts } doc
44+ , writerExtensions = wexts } doc
49− return (r def{ readerExtensions = rexts } . unInputText, InputText inp)
45+ return $ bench (T.unpack name) $
46+ nf (either (error . show) id . runPure . r def) inp
50− (ByteStringReader r, ByteStringWriter w) -> do
47+ (ByteStringReader r, ByteStringWriter w) -> do
51− setResourcePath ["./test"]
52− tmpl <- Just <$> compileDefaultTemplate name
48+ tmpl <- Just <$> compileDefaultTemplate name
53− inp <- w def{ writerWrapText = WrapAuto
49+ inp <- w def{ writerWrapText = WrapAuto
54− , writerExtensions = wexts
50+ , writerExtensions = wexts
55− , writerTemplate = tmpl } doc
51+ , writerTemplate = tmpl } doc
56− liftIO $ BL.writeFile "/tmp/test.odt" inp
52+ return $ bench (T.unpack name) $
53+ nf (either (error . show) id .
57− return (r def{ readerExtensions = rexts } . unInputBS, InputBS inp)
54+ runPure . r def{readerExtensions = rexts}) inp
58− _ -> throwError $ PandocSomeError $ "text/bytestring format mismatch: "
55+ _ -> throwError $ PandocSomeError $ "text/bytestring format mismatch: "
59− <> name
56+ <> name
60− return $ case res of
61− Right (readerFun, inp) ->
62− Just $ bench (T.unpack $ name <> " reader")
63− $ nf (\i -> either (error . show) id $ runPure (readerFun i))
64− inp
65− Left _ -> Nothing
66
57
67getImages :: IO [(FilePath, MimeType, BL.ByteString)]
58getImages :: IO [(FilePath, MimeType, BL.ByteString)]
68getImages = do
59getImages = do
69− ll <- BL.readFile "test/lalune.jpg"
60+ ll <- B.readFile "test/lalune.jpg"
70− mv <- BL.readFile "test/movie.jpg"
61+ mv <- B.readFile "test/movie.jpg"
71− return [("lalune.jpg", "image/jpg", ll)
62+ return [("lalune.jpg", "image/jpg", BL.fromStrict ll)
72− ,("movie.jpg", "image/jpg", mv)]
63+ ,("movie.jpg", "image/jpg", BL.fromStrict mv)]
73
64
65+writerBench :: [(FilePath, MimeType, BL.ByteString)]
74−writerBench :: Pandoc
66+ -> Pandoc
75 -> T.Text
67 -> T.Text
76 -> Maybe Benchmark
68 -> Maybe Benchmark
77−writerBench doc name =
69+writerBench _ _ name
70+ | name `elem` ["bibtex", "biblatex", "csljson"] = Nothing
71+writerBench imgs doc name = either (const Nothing) Just $
72+ runPure $ do
73+ (wtr, wexts) <- getWriter name
78− case res of
74+ case wtr of
79− Right writerFun ->
75+ TextWriter writerFun ->
80− Just $ env getImages $ \imgs ->
76+ return $ bench (T.unpack name)
77+ $ nf (\d -> either (error . show) id $
78+ runPure $ do
79+ mapM_ (\(fp,mt,bs) -> insertMedia fp (Just mt) bs) imgs
80+ writerFun def{ writerExtensions = wexts} d)
81+ doc
82+ ByteStringWriter writerFun ->
81− bench (T.unpack $ name <> " writer")
83+ return $ bench (T.unpack name)
82 $ nf (\d -> either (error . show) id $
84 $ nf (\d -> either (error . show) id $
83− runPure (do mapM_
85+ runPure $ do
84− (\(fp, mt, bs) ->
85− insertMedia fp (Just mt) bs)
86+ mapM_ (\(fp,mt,bs) -> insertMedia fp (Just mt) bs) imgs
86− imgs
87− writerFun d)) doc
88− Left _ -> Nothing
89− where res = runPure $ do
90− (wtr, wexts) <- getWriter name
91− case wtr of
92− TextWriter w ->
93− return $ w def{ writerExtensions = wexts }
87+ writerFun def{ writerExtensions = wexts} d)
94− _ -> throwError $ PandocSomeError
88+ doc
95− $ "could not get text writer for " <> name
96
89
97main :: IO ()
90main :: IO ()
98main = do
91main = do
99− args <- filter (\x -> T.take 1 x /= "-") . fmap T.pack <$> getArgs
100− print args
101− let matchReader (n, _) =
102− null args || ("reader" `elem` args && n `elem` args)
103− matchWriter (n, TextWriter _) =
104− null args || ("writer" `elem` args && n `elem` args)
105− matchWriter _ = False
106− allWriters = map fst (writers :: [(T.Text, Writer PandocPure)])
107− matchedReaders = map fst (filter matchReader readers
108− :: [(T.Text, Reader PandocPure)])
109− matchedWriters = map fst (filter matchWriter writers
110− :: [(T.Text, Writer PandocPure)])
111 inp <- UTF8.toText <$> B.readFile "test/testsuite.txt"
92 inp <- UTF8.toText <$> B.readFile "test/testsuite.txt"
112 let opts = def
93 let opts = def
113− let doc = either (error . show) id $ runPure $ readMarkdown opts inp
94+ let doc = either (error . show) force $ runPure $ readMarkdown opts inp
114− readerBs <- fmap catMaybes
95+ defaultMain
96+ [ env getImages $ \imgs ->
97+ bgroup "writers" $ mapMaybe (writerBench imgs doc . fst)
98+ (sortOn fst
99+ writers :: [(T.Text, Writer PandocPure)])
115− $ mapM (readerBench doc)
100+ , bgroup "readers" $ mapMaybe (readerBench doc . fst)
116− $ filter (/="haddock")
101+ (sortOn fst
117− (matchedReaders `intersect` allWriters)
102+ readers :: [(T.Text, Reader PandocPure)])
118− -- we need the corresponding writer to generate
103+ ]
119− -- input for the reader
120− let writerBs = mapMaybe (writerBench doc) matchedWriters
121− defaultMainWith defaultConfig{ timeLimit = 6.0 }
122− (writerBs ++ readerBs)
benchmark/weigh-pandoc.hsdeleted−50
@@ -1,50 +0,0 @@
1−{-# LANGUAGE NoImplicitPrelude #-}
2−{-# LANGUAGE OverloadedStrings #-}
3−{- |
4− Module : Main
5− Copyright : © 2016-2021 John MacFarlane <jgm@berkeley.edu>
6− License : GNU GPL, version 2 or above
7−
8− Maintainer : John MacFarlane <jgm@berkeley.edu>
9− Stability : alpha
10− Portability : portable
11−
12−Benchmarks to determine resource use of readers and writers.
13−-}
14−import Prelude
15−import Weigh
16−import Text.Pandoc
17−import Data.Text (Text, unpack)
18−
19−main :: IO ()
20−main = do
21− doc <- read <$> readFile "test/testsuite.native"
22− mainWith $ do
23− func "Pandoc document" id doc
24− mapM_
25− (\(n,r) -> weighReader doc n (either (error . show) id . runPure . r def{readerExtensions = pandocExtensions}))
26− [("markdown", readMarkdown)
27− ,("html", readHtml)
28− ,("docbook", readDocBook)
29− ,("latex", readLaTeX)
30− ,("commonmark", readCommonMark)
31− ]
32− mapM_
33− (\(n,w) -> weighWriter doc n (either (error . show) id . runPure . w def))
34− [("markdown", writeMarkdown)
35− ,("html", writeHtml5String)
36− ,("docbook", writeDocbook5)
37− ,("latex", writeLaTeX)
38− ,("commonmark", writeCommonMark)
39− ]
40−
41−weighWriter :: Pandoc -> String -> (Pandoc -> Text) -> Weigh ()
42−weighWriter doc name writer = func (name ++ " writer") writer doc
43−
44−weighReader :: Pandoc -> Text -> (Text -> Pandoc) -> Weigh ()
45−weighReader doc name reader =
46− case lookup name writers of
47− Just (TextWriter writer) ->
48− let inp = either (error . show) id $ runPure $ writer def{ writerWrapText = WrapAuto} doc
49− in func (unpack $ name <> " reader") reader inp
50− _ -> return () -- no writer for reader
cabal.project+9−8
@@ -1,12 +1,13 @@
1packages: pandoc.cabal
1packages: pandoc.cabal
2−
3tests: True
2tests: True
3+flags: +embed_data_files
4
4
5−package pandoc
5+source-repository-package
6− flags: +embed_data_files -trypandoc
6+ type: git
7− ghc-options: -j +RTS -A64m -RTS
7+ location: https://github.com/jgm/doctemplates
8+ tag: 428c26d5303cf7a2b1051fe1ffd9aafe9ba71c81
8
9
9−-- source-repository-package
10+source-repository-package
10−-- type: git
11+ type: git
11−-- location: https://github.com/jgm/citeproc
12+ location: https://github.com/jgm/citeproc
12−-- tag: d44e24696ab444090d0e63e321c3a573f68b2e74
13+ tag: 4ce0501cd6f9c86eee087afcf878c256e49b9615
changelog.md+1234−2
@@ -1,5 +1,1234 @@
1# Revision history for pandoc
1# Revision history for pandoc
2
2
3+## pandoc 2.14.0.3 (2021-06-22)
4+
5+ * Text.Pandoc.MediaBag `insertMediaBag`: ensure we get a sane mediaPath
6+ for URLs (#7391). In earlier 2.14.x versions, we'd get
7+ incorrect paths for resources downloaded from URLs when the
8+ media are extracted (including in PDF production).
9+ * Text.Pandoc.Parsing: improve `emailAddress` (#7398).
10+ Previously the parser would accept characters in domains
11+ that are illegal in domains, and this sometimes caused it
12+ to gobble bits of the following text.
13+ * txt2tags reader: modify the email address parser so
14+ it still includes form parameters, even after the change to
15+ `emailAddress` in Text.Pandoc.Parsing.
16+ * Text.Pandoc.Readers.Metadata: Fix regression with comment-only YAML
17+ metadata blocks (#7400).
18+ * reveal.js writer and template: better handling of options. Previously
19+ it was impossible to specify false values for options that default to
20+ true (e.g. `center`); setting the option to false just caused the portion
21+ of the template setting the option to be omitted. Now we prepopulate
22+ all the variables with their default values, including them all
23+ unconditionally and allowing them to be overridden.
24+ * Markdown writer: Fix regression in code blocks with attributes (#7397).
25+ Code blocks with a single class but nonempty attributes
26+ were having attributes drop as a result of #7242.
27+ * LaTeX writer:
28+ + Add strut at end of minipage if it contains line breaks.
29+ Without them, the last line is not as tall as it should be in
30+ some cases.
31+ + Always use a minipage for cells with line breaks, when
32+ width information is available (#7393). Otherwise the way we treat them
33+ can lead to content that overflows a cell.
34+ + Use `\strut` instead of `~` before `\\` in empty line.
35+ * Use lts-18.0 stack resolver.
36+ * Require skylighting 0.10.5.2 (adding support for Swift).
37+ * Require commonmark 0.2.1.
38+ * Rephrase section on unsafe HTML in manual.
39+ * Create SECURITY.md
40+
41+## pandoc 2.14.0.2 (2021-06-13)
42+
43+ * Fix MediaBag regressions (#7345). iIn the 2.14 release `--extract-media`
44+ stopped working as before; there could be mismatches between the
45+ paths in the rendered document and the extracted media.
46+ This patch makes several changes that restore the earlier behavior
47+ (while keeping the same API). The `mediaPath` in 2.14 was always
48+ constructed from the SHA1 hash of the media contents. Now, we
49+ preserve the original path unless it's an absolute path or contains
50+ `..` segments (in that case we use a path based on the SHA1 hash of
51+ the contents).
52+
53+ In Text.Pandoc.MediaBag, `mediaDirectory` and `mediaItems` now use the
54+ `mediaPath`, rather than the mediabag key, for the first component of the
55+ tuple. This makes more sense, I think, and fits with the documentation of
56+ these functions; eventually, though, we should rework the API so that
57+ `mediaItems` returns both the keys and the MediaItems.
58+
59+ In Text.Pandoc.Class.IO, rewriting of source paths in `extractMedia` has
60+ been fixed.
61+
62+ In Text.Pandoc.Class.PandocMonad, `fillMediaBag` has been modified so that
63+ it doesn't modify image paths (that was part of the problem in #7345).
64+
65+ We now do path normalization (e.g. `\` separators on Windows) in
66+ writing the media.
67+
68+ * Text.Pandoc.PDF:
69+
70+ + Text.Pandoc.PDF: Fix regression in 2.14 for generation of PDFs with
71+ SVGs (#7344).
72+ + Only print relevant part of environment on `--verbose`. Since
73+ `--verbose` output might be put in an issue, we want to avoid
74+ spilling out secrets in environment variables.
75+
76+ * Markdown reader: fix pipe table regression in 2.11.4 (#7343).
77+ Previously pipe tables with empty headers (that is, a header
78+ line with all empty cells) would be rendered as headerless
79+ tables. This broke in 2.11.4. The fix here is to produce an
80+ AST with an empty table head when a pipe table has all empty
81+ header cells.
82+
83+ * LaTeX reader: don't allow optional `*` on symbol control sequences
84+ (#7340). Generally we allow optional starred variants of LaTeX commands
85+ (since many allow them, and if we don't accept these explicitly,
86+ ignoring the star usually gives acceptable results). But we
87+ don't want to do this for `\(*\)` and similar cases.
88+
89+ * Docx reader: handle absolute URIs in Relationship Target (#7374).
90+
91+ * Docx writer: fix handling of empty table headers (Albert Krewinkel,
92+ #7369). A table header which does not contain any cells is now treated as
93+ an empty header.
94+
95+ * LaTeX writer: Fix regression in table header position (#7347).
96+ In recent versions the table headers were no longer bottom-aligned
97+ (if more than one line). This patch fixes that by using minipages
98+ for table headers in non-simple tables.
99+
100+ * CommonMark writer:
101+
102+ + Do not use simple class for fenced-divs (Jan Tojnar, amends #7242.)
103+ + Do not throw away attributes when `Ext_attributes` is enabled.
104+ `Ext_attributes` covers at least the following:
105+ `Ext_fenced_code_attributes`, `Ext_header_attributes`,
106+ `Ext_inline_code_attributes`, `Ext_link_attributes`.
107+
108+ * Markdown writer:
109+
110+ + Allow `pipe_tables` to be disabled for commonmark formats
111+ (`commonmark_x`, `gfm`) (#7375).
112+ + Re-use functions from Text.Pandoc.Markdown.Inline (Jan Tojnar).
113+
114+ * DocBook writer: Remove non-existent admonitions (Jan Tojnar).
115+ `attention`, `error` and `hint` are reStructuredText specific.
116+
117+ * HTML writer: Don't omit width attribute on div (#7342).
118+
119+ * Text.Pandoc.MIME, `extensionFromMimeType`: add a few special cases.
120+ When we do a reverse lookup in the MIME table, we just get the
121+ last match, so when the same mime type is associated with several
122+ different extensions, we sometimes got weird results, e.g. `.vs`
123+ for `text/plain`. These special cases help us get the most standard
124+ extensions for mime types like `text/plain`.
125+
126+ * Lua utils: fix handling of table headers in `from_simple_table` (Albert
127+ Krewinkel, #7369). Passing an empty list of header cells now results
128+ in an empty table header.
129+
130+ * Text.Pandoc.Citeproc:
131+
132+ + Avoid duplicate classes and attributes on references div.
133+ + Fix regression in citeproc processing (#7376). If inline
134+ references are used (in the metadata `references` field), we
135+ should still only include in the bibliography items that are
136+ actually cited (unless `nocite` is used).
137+
138+ * Require citeproc 0.4.0.1. This fixes a bug which led to doubled
139+ "et al." in some (rare) circumstances.
140+
141+ * MANUAL.txt:
142+
143+ + Mention GladTeX for EPUB export (Sebastian Humenda).
144+ This updates the manual and the web site about the GladTeX usage.
145+ + More details and a useful link for YAML syntax.
146+
147+ * CONTRIBUTING.md: update modules overview (Albert Krewinkel).
148+
149+ * using-the-pandoc-api.md: switch from String to Text (Albert Krewinkel).
150+
151+
152+## pandoc 2.14.0.1 (2021-06-01)
153+
154+ * Commonmark reader: Fix regression in 2.14 with YAML metdata block parsing,
155+ which could cause the document body to be omitted after metadata (#7339).
156+
157+ * HTML reader: fix column width regression in 2.14 (#7334).
158+ Column widths specified with a style attribute were off by a factor of 100.
159+
160+ * Markdown reader: in `rebasePaths`, check for both Windows and Posix
161+ absolute paths. Previously Windows pandoc was treating
162+ `/foo/bar.jpg` as non-absolute.
163+
164+ * Text.Pandoc.Logging: In rendering `LoadedResource`, use relative paths.
165+
166+ * Docx writer: fix regression on captions (#7328). The "Table Caption"
167+ style was no longer getting applied. (It was overwritten by "Compact.")
168+
169+ * Use commonmark-extensions 0.2.1.2
170+
171+## pandoc 2.14 (2021-05-28)
172+
173+ * Change reader types, allowing better tracking of source positions
174+ [API change]. Previously, when multiple file arguments were provided,
175+ pandoc simply concatenated them and passed the contents to the readers,
176+ which took a Text argument. As a result, the readers had no way of knowing
177+ which file was the source of any particular bit of text. This meant that
178+ we couldn't report accurate source positions on errors or include accurate
179+ source positions as attributes in the AST. More seriously, it meant that
180+ we couldn't resolve resource paths relative to the files containing them
181+ (see e.g. #5501, #6632, #6384, #3752).
182+
183+ * Add `rebase_relative_paths` extension (#3752). When enabled, this
184+ extension rewrites relative image and link paths by prepending
185+ the (relative) directory of the containing file. This
186+ behavior is useful when your input sources are split
187+ into multiple files, across several directories, with files
188+ referring to images stored in the same directory. The
189+ extension can be enabled for all markdown and commonmark-based formats.
190+
191+ * Add Text.Pandoc.Sources (exported module), with a `Sources` type and a
192+ `ToSources` class. A `Sources` wraps a list of `(SourcePos, Text)` pairs
193+ [API change]. A parsec `Stream` instance is provided for `Sources`. The
194+ module also exports versions of parsec's `satisfy` and other Char parsers
195+ that track source positions accurately from a `Sources` stream (or any
196+ instance of the new `UpdateSourcePos` class).
197+
198+ * Text.Pandoc.Parsing
199+
200+ + Export the modified Char parsers defined in Text.Pandoc.Sources
201+ instead of the ones parsec provides. Modified parsers to use a
202+ `Sources` as stream [API change].
203+ + Improve include file functions [API change]. Remove old
204+ `insertIncludedFileF`. Give `insertIncludedFile` a more general type,
205+ allowing it to be used where `insertIncludedFileF` was.
206+ + Add parameter to the `citeKey` parser from
207+ Text.Pandoc.Parsing, which controls whether the `@{..}`
208+ syntax is allowed [API change].
209+
210+ * Text.Pandoc.Error: Modified the constructor `PandocParsecError` to take a
211+ `Sources` rather than a `Text` as first argument, so parse error locations
212+ can be accurately reported.
213+
214+ * Fix source position reporting for YAML bibliographies (#7273).
215+
216+ * Issue error message when reader or writer format is malformed
217+ (#7231). Previously we exited with an error status but (due to a bug)
218+ no message.
219+
220+ * Smarter smart quotes (#7216, #2103). Treat a leading `"` with no
221+ closing `"` as a left curly quote. This supports the practice, in
222+ fiction, of continuing paragraphs quoting the same speaker without an
223+ end quote. It also helps with quotes that break over lines in line blocks.
224+
225+ * Markdown reader:
226+
227+ + Use MetaInlines not MetaBlocks for multimarkdown metadata fields.
228+ This gives better results in converting to e.g. pandoc markdown.
229+ + Implement curly-brace syntax for Markdown citation keys (#6026).
230+ The change provides a way to use citation keys that contain
231+ special characters not usable with the standard citation key syntax.
232+ Example: `@{foo_bar{x}'}` for the key `foo_bar{x}`. It also allows
233+ separating citation keys from immediately following text, e.g. `@{foo}A`.
234+
235+ * RST reader:
236+
237+ + Seek include files in the directory of the file
238+ containing the include directive, as RST requires (#6632).
239+ + Use `insertIncludedFile` from Text.Pandoc.Parsing
240+ instead of reproducing much of its code.
241+
242+ * Org reader: Resolve org includes relative to the directory containing the
243+ file containing the INCLUDE directive (#5501).
244+
245+ * ODT reader: Treat tabs as spaces (#7185, niszet).
246+
247+ * Docx reader:
248+
249+ + Add handling of vml image objects (#7257, mbrackeantidot).
250+ + Support new table features (Emily Bourke, #6316): column
251+ spans, row spans, multiple header rows, table description
252+ (parsed as a simple caption), captions, column widths.
253+
254+ * LaTeX reader:
255+
256+ + Improved siunitx support (#6658, #6620).
257+ + Better support for `\xspace` (#7299).
258+ + Improve parsing of `\def` macros. We previously set "verbatim mode"
259+ even for parsing the initial `\def`; this caused problems
260+ for `\def` nested inside another `\def`.
261+ + Implement `\newif`.
262+
263+ * ConTeXt writer: improve ordered lists (#5016, Denis Maier).
264+ Change ordered list from itemize to enumerate. Add new
265+ itemgroup for ordered lists. Remove manual insertion of
266+ width attributes. Use tabular figures in ordered list
267+ enumerators.
268+
269+ * HTML reader:
270+
271+ + Don't fail on unmatched closing "script" tag (Albert Krenkel, #7282).
272+ + Keep h1 tags as normal headers (#2293, Albert Krewinkel).
273+ The tags `<title>` and `<h1 class="title">` often contain the same
274+ information, so the latter was dropped from the document. However, as
275+ this can lead to loss of information, the heading is now always
276+ retained. Use `--shift-heading-level-by=-1` to turn the `<h1>`
277+ into the document title, or a filter to restore the previous behavior.
278+ + Handle relative lengths (e.g. `2*`) in HTML column widths (#4063).
279+ See <https://www.w3.org/TR/html4/types.html#h-6.6>.
280+
281+ * DocBook/JATS readers:
282+
283+ + Fix mathml regression caused by the switch in XML libraries (#7173).
284+ + Fix "phrase" in DocBook: take classes from "role" not "class" (#7195).
285+
286+ * DocBook reader: ensure that first and last names are separated (#6541).
287+
288+ * Jira reader (Albert Krewinkel, #7218):
289+
290+ + Support "smart" links: `[alias|https://example.com|smart-card]` syntax.
291+ + Allow spaces and most unicode characters in attachment links.
292+ + No longer require a newline character after `{noformat}`.
293+ + Only allow URI path segment characters in bare links.
294+ + The `file:` schema is no longer allowed in bare links; these
295+ rarely make sense.
296+
297+ * Plain writer: handle superscript unicode minus (#7276).
298+
299+ * LaTeX writer:
300+
301+ + Better handling of line breaks in simple tables (#7272).
302+ Now we also handle the case where they're embedded in other elements,
303+ e.g. spans.
304+ + For beamer output, support `exampleblock` and `alertblock` (#7278).
305+ A block will be rendered as an `exampleblock` if the heading
306+ has class `example` and an `alertblock` if it has class `alert`.
307+ + Separate successive quote chars with thin space (#6958,
308+ Albert Krewinkel). Successive quote characters are separated with
309+ a thin space to improve readability and to prevent unwanted ligatures.
310+ Detection of these quotes sometimes had failed if the second quote
311+ was nested in a span element.
312+ + Separate successive quote chars with thin space (#6958, Albert
313+ Krewinkel).
314+
315+ * EPUB Writer: Fix belongs-to-collection XML id choice (#7267, nuew).
316+ The epub writer previously used the same XML id for both the book
317+ identifier and the epub collection. This causes an error on epubcheck.
318+
319+ * BibTeX/BibLaTeX writer: Handle `annote` field (#7266).
320+
321+ * ZimWiki writer: allow links and emphasis in headers (#6605,
322+ Albert Krewinkel).
323+
324+ * ConTeXt writer:
325+
326+ + Support blank lines in line blocks (#6564, Albert Krewinkel,
327+ thanks to @denismaier).
328+ + Use span identifiers as reference anchors (#7246, Albert Krewinkel).
329+
330+ * HTML writer:
331+
332+ + Keep attributes from code nested below `pre` tag (#7221,
333+ Albert Krewinkel). If a code block is defined with `<pre><code
334+ class="language-x">…</code></pre>`, where the `<pre>` element has no
335+ attributes, then the attributes from the `<code>` element are used
336+ instead. Any leading `language-` prefix is dropped in the code's
337+ `class` attribute are dropped to improve syntax highlighting.
338+ + Ensure headings only have valid attribs in HTML4 (#5944, Albert
339+ Krewinkel).
340+ + Parse `<header>` as a Div (Albert Krewinkel).
341+
342+ * Org writer:
343+
344+ + Inline latex envs need newlines (#7252, tecosaur).
345+ As specified in https://orgmode.org/manual/LaTeX-fragments.html, an
346+ inline \begin{}...\end{} LaTeX block must start on a new line.
347+ + Use LaTeX style maths deliminators (#7196, tecosaur).
348+
349+ * JATS writer (Albert Krewinkel):
350+
351+ + Use either styled-content or named-content for spans (#7211).
352+ If the element has a content-type attribute, or at least one class,
353+ then that value is used as `content-type` and the span is put inside
354+ a `<named-content>` element. Otherwise a `<styled-content>` element
355+ is used instead.
356+ + Reduce unnecessary use of `<p>` elements for wrapping (#7227).
357+ The `<p>` element is used for wrapping in cases were the contents
358+ would otherwise not be allowed in a certain context. Unnecessary
359+ wrapping is avoided, especially around quotes (`<disp-quote>` elements).
360+ + Convert spans to `<named-content>` elements (#7211). Spans with
361+ attributes are converted to `<named-content>` elements instead of
362+ being wrapped with `<milestone-start/>` and `<milestone-end>`
363+ elements. Milestone elements are not allowed in documents using the
364+ articleauthoring tag set, so this change ensures the creation of valid
365+ documents.
366+ + Add footnote number as label in backmatter (#7210). Footnotes in the
367+ backmatter are given the footnote's number as a label. The
368+ articleauthoring output is unaffected from this change, as footnotes
369+ are placed inline there.
370+ + Escape disallows chars in identifiers. XML identifiers must start
371+ with an underscore or letter, and can contain only a limited set
372+ of punctuation characters. Any IDs not adhering to these rules are
373+ rewritten by writing the offending characters as `Uxxxx`,
374+ where `xxxx` is the character's hex code.
375+
376+ * Jira writer: use `{color}` when span has a color attribute
377+ (Albert Krewinkel, tarleb/jira-wiki-markup#10).
378+
379+ * Docx writer:
380+
381+ + Autoset table width if no column has an explicit width (Albert
382+ Krewinkel).
383+ + Extract Table handling into separate module (Albert Krewinkel).
384+ + Support colspans and rowspans in tables (Albert Krewinkel, #6315).
385+ + Support multirow table headers (Albert Krewinkel).
386+ + Improve integration of settings from reference.docx (#1209).
387+ This change allows users to create a reference.docx that
388+ sets `w:proofState` for spelling or grammar to `dirty`,
389+ so that spell/grammar checking will be triggered on the
390+ generated docx.
391+ + Copy over more settings from reference.docx (#7240). From settings.xml
392+ in the reference-doc, we now include: `zoom`, `embedSystemFonts`,
393+ `doNotTrackMoves`, `defaultTabStop`, `drawingGridHorizontalSpacing`,
394+ `drawingGridVerticalSpacing`, `displayHorizontalDrawingGridEvery`,
395+ `displayVerticalDrawingGridEvery`, `characterSpacingControl`,
396+ `savePreviewPicture`, `mathPr`, `themeFontLang`, `decimalSymbol`,
397+ `listSeparator`, `autoHyphenation`, `compat`.
398+ + Set zoom to 100% by default in settings.xml.
399+ + Align math options more with current Word defaults (e.g. Cambria Math
400+ font).
401+ + Remove `rsid`s from default settings.xml. Word will add these
402+ when revisions are made.
403+
404+ * Ms writer: Handle tables with multiple paragraphs (#7288).
405+ Previously they overflowed the table cell width. We now set line lengths
406+ per-cell and restore them after the table has been written.
407+
408+ * Markdown writer:
409+
410+ + Use cleaner braceless syntax for code blocks with a
411+ single class (#7242, Jan Tojnar).
412+ + Add quotes properly in markdown YAML metadata fields (#7245).
413+ This fixes a bug, which caused the writer to look at the *last*
414+ rather than the *first* character in determining whether quotes
415+ were needed. So we got spurious quotes in some cases and
416+ didn't get necessary quotes in others.
417+ + Use `@{..}` syntax for citations when needed.
418+ + Use fewer unneeded escapes for `#` (see #6259).
419+ + Improve escaping of `@`. We need to escape literal `@` before
420+ `{` because of the new citation syntax.
421+
422+ * Commonmark writer: Use backslash escapes for `<` and `|`...
423+ instead of entities (#7208).
424+
425+ * Powerpoint writer: allow `monofont` to be specified in metadata
426+ (#7187).
427+
428+ * LaTeX template:
429+
430+ + Use non-starred names for xcolor color names (#6109).
431+ This should make svgnames and x11names work properly.
432+ + Fix bad vertical spacing after bibliography (#7234, badumont).
433+ + List of figures before list of tables (#7235, Julien Dutant).
434+ + Move CSL macro definitions before header-includes so they can be
435+ overridden (#7286).
436+ + Improve treatment of CSL `entry-spacing` (#7296).
437+ Previously with the default template settings (`indent` variable
438+ not set), we would get interparagraph spaces separating bib
439+ entries even with `entry-spacing="0"`. On the other hand,
440+ setting `entry-spacing="2"` gave ridiculously large spacing.
441+ This change makes the spacing caused by `entry-spacing` a multiple
442+ of `\parskip` by default, which gives aesthetically reasonable
443+ output. Those who want a larger or smaller unit (e.g. because
444+ they use `indent` which sets `\parskip` to 0) may
445+ `\setlength{\cslentryspacingunit}{10pt}` in header-includes
446+ to override the defaults.
447+ + Move title, author, date up to top of preamble (#7295).
448+ This allows header-includes to use them, and puts them
449+ in a position where you can see them immediately.
450+ + Define commands for zero width non-joiner character
451+ (#6639, Albert Krewinkel). The zero-width non-joiner character
452+ is used to avoid ligatures (e.g. in German).
453+
454+ * ConTeXt template:
455+
456+ + Define `enumerate` itemgroup (#5016, Denis Maier).
457+ + List of figures before list of tables (#7235, Julien Dutant).
458+
459+ * reveal.js template:
460+
461+ + Support `toc-title` (#7171, Florian Kohrt).
462+ + Use `hash: true` by default rather than `history: true` (#6968).
463+
464+ * HTML-based slide shows: add support for `institute` (#7289, Thomas
465+ Hodgson).
466+
467+ * Text.Pandoc.Extensions: Add constructor `Ext_rebase_relative_paths` to
468+ `Extensions` [API change].
469+
470+ * Text.Pandoc.XML.Light: add Eq, Ord instances for Content,
471+ Element, Attr, CDataKind [API change].
472+
473+ * Text.Pandoc.MediaBag:
474+
475+ + Change type to use a `Text` key instead of `[FilePath]`.
476+ We normalize the path and use `/` separators for consistency.
477+ + Export `MediaItem` type [API change].
478+ + Change `MediaBag` type to a map from Text to MediaItem [API change].
479+ + `lookupMedia` now returns a `MediaItem` [API change].
480+ + Change `insertMedia` so it sets the `mediaPath` to a filename based on
481+ the SHA1 hash of the contents. This will be used when contents
482+ are extracted.
483+
484+ * Text.Pandoc.Class.PandocMonad:
485+
486+ + Remove `fetchMediaResource` [API change]. Use `fetchItem` to get
487+ resources in `fillMediaBag`.
488+ + Add informational message in `downloadOrRead` indicating what path
489+ local resources have been loaded from.
490+
491+ * Text.Pandoc.Logging:
492+
493+ + Remove single quotes around paths in messages.
494+ + Add LoadedResource constructor to LogMessage [API change].
495+ This is for INFO-level messages telling where image data has been
496+ loaded from. (This can vary because of the resource path.)
497+
498+ * Text.Pandoc.Asciify: simplify code and export `toAsciiText` [API change].
499+ Instead of encoding a giant (and incomplete) map, we now
500+ just use unicode-transforms to normalize the text to
501+ a canonical decomposition, and manipulate the result.
502+
503+ * App: allow tabs expansion even if file-scope is used (Albert Krewinkel,
504+ #6709). Tabs in plain-text inputs are now handled correctly, even if
505+ the `--file-scope` flag is used.
506+
507+ * Add new internal module Text.Pandoc.Writers.GridTable (Albert Krewinkel).
508+
509+ * Text.Pandoc.Highlighting: Change type of `languagesByExtension`, adding
510+ a parameter for a `SyntaxMap` [API change] (Jan Tojnar, #7241).
511+ Languages defined using `--syntax-definition` were not recognized by
512+ `languagesByExtension`. This patch corrects that, allowing the writers
513+ to see all custom definitions. The LaTeX writer still uses the default
514+ syntax map, but that's okay in that context, since
515+ `--syntax-definition` won't create new listings styles.
516+
517+ * Text.Pandoc.Citeproc:
518+
519+ + Ensure that CSL-related attributes are passed on to a Div with id
520+ 'refs'. Otherwise things like `entry-spacing` won't work when
521+ such Divs are used.
522+ + Use metadata's `lang` for the lang parameter of citeproc, overriding
523+ `localeLanguage`.
524+ + Recognize locators spelled with a capital letter (#7323).
525+ + Add a comma and a space in front of the suffix if it doesn't start
526+ with space or punctuation (#7324).
527+ + Don't detect math elements as locators (#7321).
528+
529+ * Remove Text.Pandoc.BCP47 module [API change]. Use types and functions
530+ from UnicodeCollation.Lang instead. This is a richer implementation
531+ of BCP 47.
532+
533+ * Text.Pandoc.Shared:
534+
535+ + Fix regression in grid tables for wide characters (#7214).
536+ In the translation from String to Text, a char-width-sensitive
537+ `splitAt'` was dropped. This commit reinstates it and uses it to make
538+ `splitTextByInstances` char-width sensitive.
539+ + Add `getLang` (formerly in the now-removed BCP47) [API change].
540+
541+ * Text.Pandoc.SelfContained: use `application/octet-stream`
542+ for unknown mime types instead of halting with an error (#7202).
543+
544+ * Lua filters: respect Inlines/Blocks filter functions in `pandoc.walk_*`
545+ (Albert Krewinkel).
546+
547+ * Add text as build-depend for trypandoc (#7193, Roman Beránek).
548+
549+ * Bump upper-bounds for network-uri, time, attoparsec.
550+
551+ * Use citeproc 0.4.
552+
553+ * Use texmath 0.12.3.
554+
555+ * Use jira-wiki-markup 1.3.5 (Albert Krewinkel).
556+
557+ * Require latest skylighting (fixes a bug in XML syntax highlighting).
558+
559+ * Use latest xml-conduit.
560+
561+ * Use latest commonmark, commonmark-extensions, commonmark-pandoc.
562+
563+ * Use haddock-library-1.10.0 (Albert Krewinkel).
564+
565+ * Allow compilation with base 4.15 (Albert Krewinkel).
566+
567+ * MANUAL:
568+
569+ + Add information about `lang` and bibliography sorting.
570+ + Add info about YAML escape sequences, link to spec (#7152,
571+ Albert Krewinkel).
572+ + Note that `institute` variable works for HTML-based slides.
573+ + Update documentation on citation syntax.
574+ + Add citation example for locators and suffixes (Tristan Stenner)
575+
576+ * Updated and fixed typos in documentation (Charanjit Singh,
577+ Anti-Distinctlyminty, Tatiana Porras, obcat).
578+
579+ * Add instructions for installing pandoc-types before compiling filter.
580+
581+ * INSTALL: add note that parallel installations should be avoided
582+ (#6865).
583+
584+ * Remove `biblatex-nussbaum.md` test. It is basically the same
585+ as `biblaetx-quotes.md`.
586+
587+ * Command tests: fail if a file contains no tests---and fix a
588+ test that failed in that way!
589+
590+ * Use smaller images in tests, reducing the size of the source tarball by 8 MB.
591+
592+
593+## pandoc 2.13 (2021-03-21)
594+
595+ * Support `yaml_metadata_block` extension for `commonmark`, `gfm` (#6537).
596+ This supported is a bit more limited than with pandoc's
597+ `markdown`. The YAML block must be the first thing in the input,
598+ and the leaf notes are parsed in isolation from the rest of
599+ the document. So, for example, you can't use reference
600+ links if the references are defined later in the document.
601+
602+ * Fix fallback to default partials when custom templates are used.
603+ If the directory containing a template does not contain the partial,
604+ it should be sought in the default templates, but this was not
605+ working properly (#7164).
606+
607+ * Handle `nocite` better with `--biblatex` and `--natbib` (#4585).
608+ Previously the nocite metadata field was ignored with these formats.
609+ Now it populates a `nocite-ids` template variable and causes a
610+ `\nocite` command to be issued.
611+
612+ * Text.Pandoc.Citeproc: apply `fixLinks` correctly (#7130). This is code
613+ that incorporates a prefix like `https://doi.org/` into a following link
614+ when appropriate.
615+
616+ * Text.Pandoc.Shared:
617+
618+ + Remove `backslashEscapes`, `escapeStringUsing` [API change]. Replace
619+ these inefficient association list lookups with more efficient escaping
620+ functions in the writers that used them (for a 10-25% performance boost
621+ in org, haddock, rtf, texinfo writers).
622+ + Remove `ToString`, `ToText` typeclasses [API change]. These were needed
623+ for the transition from String to Text, but they are no longer used and
624+ may clash with other things.
625+ + Simplify `compactDL`.
626+
627+ * Text.Pandoc.Parsing:
628+
629+ + Change type of `readWithM` so that it is no longer polymorphic
630+ [API change]. The `ToText` class has been removed, and now that we've
631+ completed the transition to Text we no longer need this to operate
632+ on Strings.
633+ + Remove `F` type synonym [API change]. Muse and Org were defining their
634+ own `F` anyway.
635+
636+ * Text.Pandoc.Readers.Metadata:
637+
638+ + Export `yamlMetaBlock` [API change].
639+ + Make `yamlBsToMeta`, `yamlBsToRefs` polymorphic on the parser state
640+ [API change].
641+
642+ * Markdown reader: Fix regression with `tex_math_backslash` (#7155).
643+
644+ * MediaWiki reader: Allow block-level content in notes (ref) (#7145).
645+
646+ * Jira reader (Albert Krewinkel):
647+
648+ + Fixed parsing of autolinks (i.e., of bare URLs in the text).
649+ Previously an autolink would take up the rest of a line, as spaces
650+ were allowed characters in these items.
651+ + Emoji character sequences no longer cause parsing failures. This was
652+ due to missing backtracking when emoji parsing fails.
653+ + Mark divs created from panels with class "panel".
654+
655+ * RST reader: fix logic for ending comments (#7134). Previously comments
656+ sometimes got extended too far.
657+
658+ * DocBook writer: include Header attributes as XML attributes on
659+ section (Erik Rask). Attributes with key names that are not allowed
660+ as XML attributes are dropped, as are attributes with invalid values
661+ and `xml:id` (DocBook 5) and `id` (DocBook 4).
662+
663+ * Docx writer:
664+
665+ + Make `nsid` in `abstractNum` deterministic. Previously we assigned
666+ a random number, but we don't need random values, so now we just
667+ assign a value based on the list marker.
668+ + Use integral values for `w:tblW` (#7141).
669+
670+ * Jira writer (Albert Krewinkel):
671+
672+ + Block quotes are only rendered as `bq.` if they do not contain a
673+ linebreak.
674+ + Jira writer: improve div/panel handling. Include div attributes in
675+ panels, always render divs with class `panel` as panels, and
676+ avoid nesting of panels.
677+
678+ * HTML writer: Add warnings on duplicate attribute values.
679+ This prevents emitting invalid HTML. Ultimately it would be good to
680+ prevent this in the types themselves, but this is better for now.
681+
682+ * Org writer: Prevent unintended creation of ordered list items (#7132,
683+ Albert Krewinkel). Adjust line wrapping if default wrapping would cause
684+ a line to be read as an ordered list item.
685+
686+ * JATS templates: support 'equal-contrib' attrib for authors (Albert
687+ Krewinkel). Authors who contributed equally to a paper may be marked
688+ with `equal-contrib`.
689+
690+ * reveal.js template: replace JS comment with HTML (#7154, Florian Kohrt).
691+
692+ * Text.Pandoc.Logging: Add `DuplicateAttribute` constructor to `LogMessage`.
693+ [API change]
694+
695+ * Use `-j4` for linux release build. This speeds up the build dramatically
696+ on arm.
697+
698+ * cabal.project: remove ghcoptions. Move flags to top level, so they can
699+ be set differently on the command line.
700+
701+ * Require latest texmath, skylighting, citeproc, jira-wiki-markup.
702+ (The latest skylighting fixes a bad bug with Haskell syntax highlighting.)
703+ Narrow version bounds for texmath, skylighting, and citeproc, since
704+ the test output depend on them.
705+
706+ * Use doclayout 0.3.0.2. This significantly reduces the time and memory
707+ needed to compile pandoc.
708+
709+ * Use `foldl'` instead of `foldl` everywhere.
710+
711+ * Update bounds for random (#7156, Alexey Kuleshevich).
712+
713+ * Remove uses of some partial functions.
714+
715+ * Don't bake in a larger stack size for the executable.
716+
717+ * Test improvements:
718+
719+ + Use `getExecutablePath` from base, avoiding the dependency on
720+ `executable-path`.
721+ + Factor out `setupEnvironment` in Helpers, to avoid code duplication.
722+ + Fix finding of data files by setting teh `pandoc_datadir` environment
723+ variable when we shell out to pandoc. This avoids the need to use
724+ `--data-dir` for the tests, which caused problems finding `pandoc.lua`
725+ when compiling without the `embed_data_files` flag (#7163).
726+
727+ * Benchmark improvements:
728+
729+ + Build `+RTS -A8m -RTS` into default ghc-options for benchmark.
730+ This is necessary to get accurate benchmark results; otherwise we
731+ are largely measuring garbage collecting, some not related to the
732+ current benchmark.
733+ + Allow specifying BASELINE file in 'make bench' for comparison
734+ (otherwise the latest benchmark is chosen by default).
735+ + Force `readFile` in benchmarks early (Bodigrim).
736+
737+ * CONTRIBUTING: suggest using a `cabal.project.local` file (#7153,
738+ Albert Krewinkel).
739+
740+ * Add ghcid-test to Makefile. This loads the test suite in ghcid.
741+
742+
743+## pandoc 2.12 (2021-03-08)
744+
745+ * `--resource-path` now accumulates if specified multiple
746+ times (#6152). Resource paths specified later on the command line are
747+ prepended to those specified earlier. Thus,
748+ `--resource-path foo --resource-path bar:baz` is equivalent to
749+ `--resource-path bar:bas:foo`. (The previous behavior was
750+ for the last `--resource-path` to replace all the rest.)
751+ `resource-path` in defaults files behaves the same way: it
752+ will be prepended to the resource path set by earlier
753+ command line options or defaults files. This change
754+ facilitates the use of multiple defaults files: each can
755+ specify a directory containing resources it refers to
756+ without clobbering the resource paths set by the others.
757+
758+ * Allow defaults files to refer to the home directory, the
759+ user data directory, and the directory containing the defaults file
760+ itself (#5871, #5982, #5977). In fields that expect file paths
761+ (and only in these fields),
762+
763+ + `${VARIABLE}` will expand to the value of the environment variable
764+ `VARIABLE` (and in particular `${HOME}` will expand to the path
765+ of the home directory). A warning will be raised for undefined
766+ variables.
767+ + `${USERDATA}` will expand to the path of the user data
768+ directory in force when the defaults file is being processed.
769+ + `${.}` will expand to the directory containing the defaults file.
770+ (This allows default files to be placed in a directory containing
771+ resources they make use of.)
772+
773+ * When downloading content from URL arguments, be sensitive to
774+ the character encoding (#5600). We can properly handle UTF-8 and latin1
775+ (ISO-8859-1); for others we raise an error. Fall back to latin1 if
776+ no charset is given in the mime type and UTF-8 decoding fails.
777+
778+ * Allow abbreviations that don't end in a period to be
779+ specified using `--abbreviations` (#7124).
780+
781+ * Add new unexported module Text.Pandoc.XML.Light, as well
782+ as Text.Pandoc.XML.Light.Types, Text.Pantoc.XML.Light.Proc,
783+ Text.Pandoc.XML.Light.Output. (Closes #6001, #6565, #7091).
784+
785+ This module exports definitions of `Element` and `Content`
786+ that are isomorphic to xml-light's, but with Text
787+ instead of String. This allows us to keep most of the code in existing
788+ readers that use xml-light, but avoid lots of unnecessary allocation.
789+
790+ We also add versions of the functions from xml-light's
791+ Text.XML.Light.Output and Text.XML.Light.Proc that operate on our
792+ modified XML types, and functions that convert xml-light types to our
793+ types (since some of our dependencies, like texmath, use xml-light).
794+
795+ We export functions that use xml-conduit's parser to produce an
796+ `Element` or `[Content]`. This allows existing pandoc code to use
797+ a better parser without much modification.
798+
799+ The new parser is used in all places where xml-light's parser was
800+ previously used. Benchmarks show a significant performance improvement
801+ in parsing XML-based formats (with docbook, opml, jats, and docx
802+ almost twice as fast, odt and fb2 more than twice as fast).
803+
804+ In addition, the new parser gives us better error reporting than
805+ xml-light. We report XML errors, when possible, using the new
806+ `PandocXMLError` constructor in `PandocError`.
807+
808+ These changes revealed the need for some changes in the tests. The
809+ docbook-reader.docbook test lacked definitions for the entities it used;
810+ these have been added. And the docx golden tests have been updated,
811+ because the new parser does not preserve the order of attributes.
812+
813+ * DocBook reader:
814+
815+ + Avoid expensive tree normalization step, as it is not necessary
816+ with the new XML parser.
817+ + Support `informalfigure` (#7079) (Nils Carlson).
818+
819+ * Docx reader:
820+
821+ + Use Map instead of list for Namespaces. This gives a speedup of
822+ about 5-10%. With this and the XML parsing changes, the docx reader
823+ is now about twice as fast as in the previous release.
824+
825+ * HTML reader:
826+
827+ + Small performance tweaks.
828+ + Also, remove exported class `NamedTag(..)` [API change]. This was just
829+ intended to smooth over the transition from String to Text and is no
830+ longer needed.
831+ + As a result, the functions `isInlineTag` and `isBlockTag`
832+ are no longer polymorphic; they apply to a `Tag Text` [API change].
833+ + Do a lookahead to find the right parser to use. This takes
834+ benchmarks from 34ms to 23ms, with less allocation.
835+ + Fix bad handling of empty `src` attribute in `iframe` (#7099).
836+ If `src` is empty, we simply skip the `iframe`.
837+ If `src` is invalid or cannot be fetched, we issue a warning
838+ nd skip instead of failing with an error.
839+
840+ * JATS reader:
841+
842+ + Avoid tree normalization, which is no longer necessary given the
843+ new XML parser.
844+
845+ * LaTeX reader:
846+
847+ + Don't export `tokenize`, `untokenize` [API change]. These are internal
848+ implementation details, which were only exported for testing.
849+ They don't belong in the public API.
850+ + Improved efficiency of the parser. With these changes the reader
851+ is almost twice as fast as in the last release in our benchmarks.
852+ + Code cleanup, removing some unnecessary things.
853+ + Rewrite `withRaw` so it doesn't rely on fragile assumptions
854+ about token positions (which break when macros are expanded)
855+ (#7092). This requires the addition of `sEnableWithRaw` and
856+ `sRawTokens` in `LaTeXState`, and a new combinator `disablingWithRaw`
857+ to disable collecting of raw tokens in certain contexts.
858+ Add `parseFromToks` to Text.Pandoc.Readers.LaTeX.Parsing.
859+ Fix parsing of single character tokens so it doesn't mess
860+ up the new raw token collecting. These changes slightly increase
861+ allocations and have a small performance impact.
862+ + Handle some bibtex/biblatex-specific commands that used to be
863+ dealt with in pandoc-citeproc (#7049).
864+ + Optimize `satisfyTok`, avoiding unnecessary macro expansion steps.
865+ Benchmarks after this change show 2/3 of the run time and 2/3 of the
866+ allocation of the Feb. 10 benchmarks.
867+ + Removed `sExpanded` in state. This isn't actually needed and checking
868+ it doesn't change anything.
869+ + Improve `braced'`. Remove the parameter, have it parse the
870+ opening brace, and make it more efficient.
871+ + Factor out pieces of the LaTeX reader to make the module smaller.
872+ This reduces memory demands when compiling. Created
873+ Text.Pandoc.Readers.{LaTeX,Math,Citation,Table,Macro,Inline}.
874+ Changed Text.Pandoc.Readers.LaTeX.SIunitx to export a command map
875+ instead of individual commands.
876+ + Handle table cells containing `&` in `\verb` (#7129).
877+
878+ * Make Text.Pandoc.Readers.LaTeX.Types an unexported module [API change].
879+
880+ * Markdown reader:
881+
882+ + Improved handling of mmd link attributes in references (#7080).
883+ Previously they only worked for links that had titles.
884+ + Improved efficiency of the parser (benchmarks show a 15% speedup).
885+
886+ * OPML reader:
887+
888+ + Avoid tree normalization, which is no longer necessary with the
889+ new XML parser.
890+
891+ * ODT reader:
892+
893+ + Finer-grained errors on parse failure (#7091).
894+ + Give more information if the zip container can't be unpacked.
895+
896+ * Org reader:
897+
898+ + Support `task_lists` extension (Albert Krewinkel, #6336).
899+ + Fix bug in org-ref citation parsing (Albert Krewinkel, #7101).
900+ The org-ref syntax allows to list multiple citations separated by
901+ comma. Previously commas were accepted as part of the citation id,
902+ so all citation lists were parsed as one single citation.
903+
904+ * RST reader:
905+
906+ + Use `getTimestamp` instead of `getCurrentTime` to fetch timestamp.
907+ Setting `SOURCE_DATE_EPOCH` will allow reproducible builds.
908+ + RST reader: fix handling of header in CSV tables (#7064).
909+ The interpretation of this line is not affected by the delim option.
910+
911+ * Jira reader:
912+
913+ + Modified the Doc parser to skip leading blank lines. This fixes
914+ parsing of documents which start with multiple blank lines (Albert
915+ Krewinkel, #7095).
916+ + Prevent URLs within link aliases to be treated as autolinks (Albert
917+ Krewinkel, #6944).
918+
919+ * Text.Pandoc.Shared
920+
921+ + Remove formerly exported functions that are no longer used in the
922+ code base: `splitByIndices`, `splitStringByIndicies`, `substitute`,
923+ and `underlineSpan` (which had been deprecated in April 2020)
924+ [API change].
925+ + Export `handleTaskListItem` (Albert Krewinkel) [API change].
926+ + Change `defaultUserDataDirs` to `defaultUserDataDir` [API
927+ change]. We determine what is the default user data directory
928+ by seeing whether the XDG directory and/or legacy
929+ directory exist.
930+
931+ * BibTeX writer:
932+
933+ + BibTeX writer: use doclayout and doctemplate. This change allows
934+ bibtex/biblatex output to wrap as other formats do,
935+ depending on the settings of `--wrap` and `--columns` (#7068).
936+
937+ * CSL JSON writer:
938+
939+ + Output `[]` if no references in input, instead of raising a
940+ PandocAppError as before.
941+
942+ * Docx writer:
943+
944+ + Use `getTimestamp` instead of `getCurrentTime` for timestamp.
945+ Setting `SOURCE_DATE_EPOCH` will allow reproducible builds.
946+
947+ * EPUB writer:
948+
949+ + Use `getTimestamp` instead of `getCurrentTime` for timestamp.
950+ Setting `SOURCE_DATE_EPOCH` will allow reproducible builds (#7093).
951+ This does not suffice to fully enable reproducible in EPUB, since
952+ a unique id is still being generated for each build.
953+ + Support `belongs-to-collection` metadata (#7063) (Nick Berendsen).
954+
955+ * JATS writer:
956+
957+ + Escape special chars in reference elements (Albert Krewinkel).
958+ Prevents the generation of invalid markup if a citation element
959+ contains an ampersand or another character with a special meaning
960+ in XML.
961+
962+ * Jira writer:
963+
964+ + Use Span identifiers as anchors (Albert Krewinkel).
965+ + Use `{noformat}` instead of `{code}` for unknown languages (Albert
966+ Krewinkel). Code blocks which are not marked as a language supported
967+ by Jira are rendered as preformatted text via `{noformat}` blocks.
968+
969+ * LaTeX writer:
970+
971+ + Adjust hypertargets to beginnings of paragraphs (#7078).
972+ Use `\vadjust pre` so that the hypertarget takes you to the beginning
973+ of the paragraph rather than one line down.
974+ This makes a particular difference for links to citations using
975+ `--citeproc` and `link-citations: true`.
976+ + Change BCP47 lang tag from `jp` to `ja` (Mauro Bieg, #7047).
977+ + Use function instead of map for accent lookup (should be
978+ more efficient).
979+ + Split the module to make it easier to compile on low-memory
980+ systems: added Text.Pandoc.Writers.LaTeX.{Util,Citation,Lang}.
981+
982+ * Markdown writer:
983+
984+ + Handle math right before digit. We insert an HTML comment to
985+ avoid a `$` right before a digit, which pandoc will not recognize
986+ as a math delimiter.
987+ + Split the module to make it easier to compile on low-memory
988+ systems: added Text.Pandoc.Writers.Markdown.{Types,Inline}.
989+
990+ * ODT writer:
991+
992+ + Use `getTimestamp` instead of `getCurrentTime` for timestamp.
993+ Setting `SOURCE_DATE_EPOCH` will allow reproducible builds.
994+ + Update default ODT style (Lorenzo). Previously, the "First paragraph"
995+ style inherited from "Standard" but not from "Text body." Now
996+ it is adjusted to inherit from "Text body", to avoid some ugly
997+ spacing issues. It may be necessary to update a custom `reference.odt`
998+ in light of this change.
999+
1000+ * Org writer:
1001+
1002+ + Support `task_lists` extension (Albert Krewinkel, #6336).
1003+
1004+ * Pptx writer:
1005+
1006+ + Use `getTimestamp` instead of `getCurrentTime` for timestamp.
1007+ Setting `SOURCE_DATE_EPOCH` will allow reproducible builds.
1008+
1009+ * JATS templates: tag `author.name` as `string-name` (Albert Krewinkel).
1010+ The partitioning the components of a name into surname, given names,
1011+ etc. is not always possible or not available. Using `author.name`
1012+ allows to give the full name as a fallback to be used when
1013+ `author.surname` is not available.
1014+
1015+ * Add default templates for bibtex and biblatex, so that
1016+ the variables `header-include`, `include-before`, `include-after`
1017+ (or alternatively the command line options
1018+ `--include-in-header`, `--include-before-body`, `--include-after-body`)
1019+ may be used.
1020+
1021+ * LaTeX template:
1022+
1023+ + Update to iftex package (#7073) (Andrew Dunning)
1024+ + Wrap url colours in braces (#7121) (Loïc Grobol).
1025+
1026+ * revealjs template: Add 'center' option for vertical slide centering.
1027+ (maurerle, #7104).
1028+
1029+ * Text.Pandoc.XML: Improve efficiency of `fromEntities`.
1030+
1031+ * Text.Pandoc.MIME
1032+
1033+ + Add exported function `getCharset` [API change].
1034+
1035+ * Text.Pandoc.UTF8: change IO functions to return Text, not String
1036+ [API change]. This affects `readFile`, `getContents`, `writeFileWith`,
1037+ `writeFile`, `putStrWith`, `putStr`, `putStrLnWith`, `putStrLn`.
1038+ `hPutStrWith`, `hPutStr`, `hPutStrLnWith`, `hPutStrLn`, `hGetContents`.
1039+ This avoids the need to uselessly create a linked list of characters
1040+ when emiting output.
1041+
1042+ * Text.Pandoc.App
1043+
1044+ + Add `parseOptionsFromArgs` [API change, new exported function].
1045+ + Add fields for CSL options to `Opt` [API change]:
1046+ `optCSL`, `optbibliography`, `optCitationAbbreviations`.
1047+
1048+ * Text.Pandoc.Citeproc.BibTeX
1049+
1050+ + `Text.Pandoc.Citeproc.writeBibTeXString` now returns
1051+ `Doc Text` instead of `Text` (#7068).
1052+ + Correctly handle `pages` (= `page` in CSL) (#7067).
1053+ + Correctly handle BibLaTeX `langid` (= `language` in CSL, #7067).
1054+ + In BibTeX output, protect foreign titles since there's no language
1055+ field (#7067).
1056+ + Clean up BibTeX parsing (#7049). Previously there was a messy code
1057+ path that gave strange results in some cases, not passing through raw
1058+ tex but trying to extract a string content. This was an artefact of
1059+ trying to handle some special bibtex-specific commands in the BibTeX
1060+ reader. Now we just handle these in the LaTeX reader and simplify
1061+ parsing in the BibTeX reader. This does mean that more raw tex will
1062+ be passed through (and currently this is not sensitive to the
1063+ `raw_tex` extension; this should be fixed).
1064+
1065+ * Text.Pandoc.Citeproc.MetaValue
1066+
1067+ + Correctly parse "raw" date value in markdown references metadata.
1068+ (See jgm/citeproc#53.)
1069+
1070+ * Text.Pandoc.Citeproc
1071+
1072+ + Use https URLs for links (Salim B, #7122).
1073+
1074+ * Text.Pandoc.Class
1075+
1076+ + Add `getTimestamp` [API change]. This attempts to read the
1077+ `SOURCE_DATE_EPOCH` environment variable and parse a UTC time
1078+ from it (treating it as a unix date stamp, see
1079+ https://reproducible-builds.org/specs/source-date-epoch/). If the
1080+ variable is not set or can't be parsed as a unix date stamp, then the
1081+ function returns the current date.
1082+
1083+ * Text.Pandoc.Error
1084+
1085+ + Add `PandocUnsupportedCharsetError` constructor for
1086+ `PandocError` [API change].
1087+ + Export `renderError` [API change].
1088+ + Refactor `handleError` to use `renderError`. This allows us render
1089+ error messages without exiting.
1090+
1091+ * Text.Pandoc.Extensions
1092+
1093+ + `Ext_task_lists` is now supported by org (and turned
1094+ on by default) (Albert Krewinkel, #6336).
1095+ + Remove `Ext_fenced_code_attributes` from allowed commonmark attributes
1096+ (#7097). This attribute was listed as allowed, but it didn't actually
1097+ do anything. Use `attributes` for code attributes and more.
1098+
1099+ * Lua subsystem:
1100+
1101+ + Always load built-in Lua scripts from default data-dir (Albert
1102+ Krewinkel). The Lua modules `pandoc` and `pandoc.List` are now always
1103+ loaded from the system's default data directory. Loading from a
1104+ different directory by overriding the default path, e.g. via
1105+ `--data-dir`, is no longer supported to avoid unexpected behavior
1106+ and to address security concerns.
1107+ + Add module "pandoc.path" (Albert Krewinkel, #6001, #6565).
1108+ The module allows to work with file paths in a convenient and
1109+ platform-independent manner.
1110+ + Use strict evaluation when retrieving AST value from the stack
1111+ (Albert Krewinkel, #6674).
1112+
1113+ * Text.Pandoc.PDF
1114+
1115+ + Disable `smart` extension when building PDF via LaTeX.
1116+ This is to prevent accidental creation of ligatures like
1117+ `` ?` `` and `` !` `` (especially in languages with quotations like
1118+ German), and similar ligature issues. (See jgm/citeproc#54.)
1119+
1120+ * Text.Pandoc.CSV:
1121+
1122+ + Fix parsing of unquoted values (#7112). Previously we didn't allow
1123+ unescaped quotes in unquoted values, but they are allowed
1124+ in CSV.
1125+
1126+ * Test suite:
1127+
1128+ + Use a more robust method for testing the executable. Many
1129+ of our tests require running the pandoc executable. This
1130+ is problematic for a few different reasons. First,
1131+ cabal-install will sometimes run the test suite after
1132+ building the library but before building the executable,
1133+ which means the executable isn't in place for the tests.
1134+ One can work around that by first building, then building
1135+ and running the tests, but that's fragile. Second, we
1136+ have to find the executable. So far, we've done that using
1137+ a function `findPandoc` that attempts to locate it
1138+ relative to the test executable (which can be located
1139+ using findExecutablePath). But the logic here is delicate
1140+ and work with every combination of options. To solve both
1141+ problems, we add an `--emulate` option to the
1142+ `test-pandoc` executable. When `--emulate` occurs as the
1143+ first argument passed to `test-pandoc`, the program simply
1144+ emulates the regular pandoc executable, using the rest of
1145+ the arguments (after `--emulate`). Thus, `test-pandoc
1146+ --emulate -f markdown -t latex` is just like `pandoc -f
1147+ markdown -t latex`. Since all the work is done by library
1148+ functions, implementing this emulation just takes a couple
1149+ lines of code and should be entirely reliable. With this
1150+ change, we can test the pandoc executable by running the
1151+ test program itself (locatable using `findExecutablePath`)
1152+ with the `--emulate` option. This removes the need for the
1153+ fragile `findPandoc` step, and it means we can run our
1154+ integration tests even when we're just building the
1155+ library, not the executable. [Note: part of this change
1156+ involved simplifying some complex handling to set
1157+ environment variables for dynamic library paths. I have
1158+ tested a build with `--enable-dynamic-executable`, and it
1159+ works, but further testing may be needed.]
1160+ + Print accurate location if a test fails (Albert
1161+ Krewinkel). Ensures that tasty-hunit reports the location
1162+ of the failing test instead of the location of the helper
1163+ `test` function.
1164+
1165+ * Documentation: Update URLs and use `https` where possible (#7122,
1166+ Salim B).
1167+
1168+ * Add `doc/libraries.md`, a description of libraries that support pandoc.
1169+
1170+ * MANUAL.txt
1171+
1172+ + MANUAL: block-level formatting is not allowed in line blocks (#7107).
1173+ + Clarify `tex_math_dollars` extension. Note that no blank lines
1174+ are allowed between the delimiters in display math.
1175+ + Add MANUAL section on reproducible builds.
1176+ + Document no template fallback for absolute path (#7077, Nixon
1177+ Enraght-Moony.)
1178+ + Improve docs for cite-method.
1179+ + Update README and man page.
1180+
1181+ * Makefile: in `make bench`, create CSV files for comparison and compare
1182+ against previous benchmark run. Add timestamp to CSV filenames.
1183+
1184+ * cabal.project: don't explicitly set -trypandoc.
1185+ If we do, this can't be overridden on the cabal command line.
1186+
1187+ * doc/lua-filters.md: improve documentation for
1188+ `pandoc.mediabag.insert`, `pandoc.mediabag.fetch`,
1189+ `directory`, `normalize` (Albert Krewinkel).
1190+
1191+ * Allow base64-bytestring-1.2.* (Dmitrii Kovanikov)
1192+
1193+ * Require jira-wiki-markup 1.3.3 (Albert Krewinkel)
1194+
1195+ * Require citeproc 0.3.0.8, which correctly titlecases when titles
1196+ contain non-ASCII characters.
1197+
1198+ * Use skylighting 0.10.4. This version of skylighting uses xml-conduit
1199+ rather than hxt. This speeds up parsing of XML syntax definitions
1200+ fourfold, and removes four packages from pandoc's dependency graph:
1201+ hxt-charproperties, hxt-unicode, hxt-regex-xmlschema, hxt.
1202+
1203+ * Add script `tools/parseTimings.pl` to help pin down which
1204+ modules take the most time and memory to compile.
1205+
1206+ * Avoid unnecessary use of NoImplicitPrelude pragma (#7089) (Albert
1207+ Krewinkel)
1208+
1209+ * Benchmarks
1210+
1211+ + Use the lighter-weight tasty-bench instead of criterion.
1212+ + Run writer benchmarks for binary formats too.
1213+ + Alphabetize benchmarks.
1214+ + Don't run benchmarks for bibliography formats
1215+ (yet; we need a special input for them).
1216+ + Show allocation data
1217+ + Clean up benchmark code.
1218+ + Allow specifying patterns using `-p blah'.
1219+
1220+ * trypandoc: add 2 second timeout.
1221+
1222+ * Use `-split-sections` in creating linux release binary.
1223+ This reduces executable size significantly (by about 30%).
1224+
1225+ * Remove `weigh-pandoc`. It's not really useful any more, now that our
1226+ regular benchmarks include data on allocation.
1227+
1228+ * Improve linux package build process and add script to
1229+ automate building an arm64 binary package.
1230+
1231+
3## pandoc 2.11.4 (2021-01-22)
1232## pandoc 2.11.4 (2021-01-22)
4
1233
5 * Add `biblatex`, `bibtex` as output formats (closes #7040).
1234 * Add `biblatex`, `bibtex` as output formats (closes #7040).
@@ -591,8 +1820,11 @@
591 translated to `C:\/foo/bar`, which caused problems.
1820 translated to `C:\/foo/bar`, which caused problems.
592 With this fix, the backslashes are removed.
1821 With this fix, the backslashes are removed.
593
1822
1823+ * Text.Pandoc.Logging:
1824+
594− * Text.Pandoc.Logging: Add constructor `ATXHeadingInLHS` constructor
1825+ + Add constructor `ATXHeadingInLHS` to `LogMessage` [API change].
1826+ + Add constructor `EnvironmentVariableUndefined` to
595− to `LogMessage` [API change].
1827+ `LogMessage` [API change].
596
1828
597 * Fix error that is given when people specify `doc` output (#6834,
1829 * Fix error that is given when people specify `doc` output (#6834,
598 gison93).
1830 gison93).
data/docx/word/settings.xml+6−16
@@ -1,6 +1,6 @@
1<?xml version="1.0" encoding="UTF-8"?>
1<?xml version="1.0" encoding="UTF-8"?>
2<w:settings xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:sl="http://schemas.openxmlformats.org/schemaLibrary/2006/main">
2<w:settings xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:sl="http://schemas.openxmlformats.org/schemaLibrary/2006/main">
3− <w:zoom w:percent="90" />
3+ <w:zoom w:percent="100" />
4 <w:embedSystemFonts />
4 <w:embedSystemFonts />
5 <w:proofState w:spelling="clean" w:grammar="clean" />
5 <w:proofState w:spelling="clean" w:grammar="clean" />
6 <w:stylePaneFormatFilter w:val="0004" />
6 <w:stylePaneFormatFilter w:val="0004" />
@@ -17,31 +17,21 @@
17 <w:characterSpacingControl w:val="doNotCompress" />
17 <w:characterSpacingControl w:val="doNotCompress" />
18 <w:savePreviewPicture />
18 <w:savePreviewPicture />
19 <w:rsids>
19 <w:rsids>
20− <w:rsidRoot w:val="00590D07" />
21− <w:rsid w:val="00011C8B" />
22− <w:rsid w:val="004E29B3" />
23− <w:rsid w:val="00590D07" />
24− <w:rsid w:val="00784D58" />
25− <w:rsid w:val="008D6863" />
26− <w:rsid w:val="00B86B75" />
27− <w:rsid w:val="00BC48D5" />
28− <w:rsid w:val="00C36279" />
29− <w:rsid w:val="00E315A3" />
30 </w:rsids>
20 </w:rsids>
31 <m:mathPr>
21 <m:mathPr>
32− <m:mathFont m:val="Lucida Grande" />
22+ <m:mathFont m:val="Cambria Math" />
33 <m:brkBin m:val="before" />
23 <m:brkBin m:val="before" />
34 <m:brkBinSub m:val="--" />
24 <m:brkBinSub m:val="--" />
35− <m:smallFrac m:val="false" />
25+ <m:smallFrac m:val="0" />
36− <m:dispDef m:val="false" />
26+ <m:dispDef />
37 <m:lMargin m:val="0" />
27 <m:lMargin m:val="0" />
38 <m:rMargin m:val="0" />
28 <m:rMargin m:val="0" />
39 <m:wrapRight />
29 <m:wrapRight />
40 <m:intLim m:val="subSup" />
30 <m:intLim m:val="subSup" />
41− <m:naryLim m:val="subSup" />
31+ <m:naryLim m:val="undOvr" />
42 </m:mathPr>
32 </m:mathPr>
43 <w:themeFontLang w:val="en-US" />
33 <w:themeFontLang w:val="en-US" />
44 <w:clrSchemeMapping w:bg1="light1" w:t1="dark1" w:bg2="light2" w:t2="dark2" w:accent1="accent1" w:accent2="accent2" w:accent3="accent3" w:accent4="accent4" w:accent5="accent5" w:accent6="accent6" w:hyperlink="hyperlink" w:followedHyperlink="followedHyperlink" />
34 <w:clrSchemeMapping w:bg1="light1" w:t1="dark1" w:bg2="light2" w:t2="dark2" w:accent1="accent1" w:accent2="accent2" w:accent3="accent3" w:accent4="accent4" w:accent5="accent5" w:accent6="accent6" w:hyperlink="hyperlink" w:followedHyperlink="followedHyperlink" />
45 <w:decimalSymbol w:val="." />
35 <w:decimalSymbol w:val="." />
46 <w:listSeparator w:val="," />
36 <w:listSeparator w:val="," />
47−</w:settings>
37+</w:settings>
data/odt/styles.xml+1−1
@@ -350,7 +350,7 @@ xmlns:css3t="http://www.w3.org/TR/css3-text/" office:version="1.2">
350 </style:style>
350 </style:style>
351 <style:style style:name="First_20_paragraph"
351 <style:style style:name="First_20_paragraph"
352 style:display-name="First paragraph" style:family="paragraph"
352 style:display-name="First paragraph" style:family="paragraph"
353− style:parent-style-name="Standard"
353+ style:parent-style-name="Text_20_body"
354 style:next-style-name="Text_20_body" style:class="text" />
354 style:next-style-name="Text_20_body" style:class="text" />
355 <style:style style:name="Numbering_20_Symbols"
355 <style:style style:name="Numbering_20_Symbols"
356 style:display-name="Numbering Symbols" style:family="text" />
356 style:display-name="Numbering Symbols" style:family="text" />
data/templates/article.jats_publishing+6−4
@@ -30,10 +30,10 @@ $if(journal.abbrev-title)$
30$endif$
30$endif$
31</journal-title-group>
31</journal-title-group>
32$if(journal.pissn)$
32$if(journal.pissn)$
33−<issn pub-type="ppub">$journal.pissn$</issn>
33+<issn publication-format="print">$journal.pissn$</issn>
34$endif$
34$endif$
35$if(journal.eissn)$
35$if(journal.eissn)$
36−<issn pub-type="epub">$journal.eissn$</issn>
36+<issn publication-format="electronic">$journal.eissn$</issn>
37$endif$
37$endif$
38$-- At least one issn element is required; use empty issn as fallback
38$-- At least one issn element is required; use empty issn as fallback
39$if(journal.pissn)$
39$if(journal.pissn)$
@@ -86,7 +86,7 @@ $endif$
86$if(author)$
86$if(author)$
87<contrib-group>
87<contrib-group>
88$for(author)$
88$for(author)$
89−<contrib contrib-type="author">
89+<contrib contrib-type="author"$if(author.equal-contrib)$ equal-contrib="true"$endif$>
90$if(author.orcid)$
90$if(author.orcid)$
91<contrib-id contrib-id-type="orcid">$author.orcid$</contrib-id>
91<contrib-id contrib-id-type="orcid">$author.orcid$</contrib-id>
92$endif$
92$endif$
@@ -95,6 +95,8 @@ $if(author.surname)$
95<surname>$author.surname$</surname>
95<surname>$author.surname$</surname>
96<given-names>$author.given-names$</given-names>
96<given-names>$author.given-names$</given-names>
97</name>
97</name>
98+$elseif(author.name)$
99+<string-name>$author.name$</string-name>
98$else$
100$else$
99<string-name>$author$</string-name>
101<string-name>$author$</string-name>
100$endif$
102$endif$
@@ -128,7 +130,7 @@ $endif$
128</author-notes>
130</author-notes>
129$endif$
131$endif$
130$if(date)$
132$if(date)$
131−<pub-date pub-type="epub"$if(date.iso-8601)$ iso-8601-date="$date.iso-8601$"$endif$>
133+<pub-date date-type="$if(date.type)$$date.type$$else$pub$endif$" publication-format="electronic"$if(date.iso-8601)$ iso-8601-date="$date.iso-8601$"$endif$>
132$if(date.day)$
134$if(date.day)$
133<day>$date.day$</day>
135<day>$date.day$</day>
134$endif$
136$endif$
data/templates/default.biblatexnew file+10
@@ -0,0 +1,10 @@
1+$for(header-includes)$
2+$header-includes$
3+$endfor$
4+$for(include-before)$
5+$include-before$
6+$endfor$
7+$body$
8+$for(include-after)$
9+$include-after$
10+$endfor$
data/templates/default.bibtexnew file+10
@@ -0,0 +1,10 @@
1+$for(header-includes)$
2+$header-includes$
3+$endfor$
4+$for(include-before)$
5+$include-before$
6+$endfor$
7+$body$
8+$for(include-after)$
9+$include-after$
10+$endfor$
data/templates/default.context+7−3
@@ -51,6 +51,7 @@ $endif$
51
51
52% use microtypography
52% use microtypography
53\definefontfeature[default][default][script=latn, protrusion=quality, expansion=quality, itlc=yes, textitalics=yes, onum=yes, pnum=yes]
53\definefontfeature[default][default][script=latn, protrusion=quality, expansion=quality, itlc=yes, textitalics=yes, onum=yes, pnum=yes]
54+\definefontfeature[default:tnum][default][tnum=yes, pnum=no]
54\definefontfeature[smallcaps][script=latn, protrusion=quality, expansion=quality, smcp=yes, onum=yes, pnum=yes]
55\definefontfeature[smallcaps][script=latn, protrusion=quality, expansion=quality, smcp=yes, onum=yes, pnum=yes]
55\setupalign[hz,hanging]
56\setupalign[hz,hanging]
56\setupitaliccorrection[global, always]
57\setupitaliccorrection[global, always]
@@ -97,6 +98,9 @@ $endif$
97\setupitemize[autointro] % prevent orphan list intro
98\setupitemize[autointro] % prevent orphan list intro
98\setupitemize[indentnext=no]
99\setupitemize[indentnext=no]
99
100
101+\defineitemgroup[enumerate]
102+\setupenumerate[each][fit][itemalign=left,distance=.5em,style={\feature[+][default:tnum]}]
103+
100\setupfloat[figure][default={here,nonumber}]
104\setupfloat[figure][default={here,nonumber}]
101\setupfloat[table][default={here,nonumber}]
105\setupfloat[table][default={here,nonumber}]
102
106
@@ -160,12 +164,12 @@ $endfor$
160$if(toc)$
164$if(toc)$
161\completecontent
165\completecontent
162$endif$
166$endif$
163−$if(lot)$
164−\completelistoftables
165−$endif$
166$if(lof)$
167$if(lof)$
167\completelistoffigures
168\completelistoffigures
168$endif$
169$endif$
170+$if(lot)$
171+\completelistoftables
172+$endif$
169
173
170$body$
174$body$
171
175
data/templates/default.dzslides+1−1
@@ -171,7 +171,7 @@ $if(subtitle)$
171 <h1 class="subtitle">$subtitle$</h1>
171 <h1 class="subtitle">$subtitle$</h1>
172$endif$
172$endif$
173 <footer>
173 <footer>
174− $if(author)$<span class="author">$for(author)$$author$$sep$, $endfor$</span>$endif$ · $if(date)$<span class="date">$date$</span>$endif$
174+ $if(author)$<span class="author">$for(author)$$author$$sep$, $endfor$</span> · $endif$$if(institute)$<span class="institute">$for(institute)$$institute$$sep$, $endfor$</span> · $endif$$if(date)$<span class="date">$date$</span>$endif$
175 </footer>
175 </footer>
176</section>
176</section>
177$endif$
177$endif$
data/templates/default.jats_articleauthoring+3−1
@@ -19,7 +19,7 @@ $endif$
19$if(author)$
19$if(author)$
20<contrib-group>
20<contrib-group>
21$for(author)$
21$for(author)$
22−<contrib contrib-type="author">
22+<contrib contrib-type="author"$if(author.equal-contrib)$ equal-contrib="true"$endif$>
23$if(author.orcid)$
23$if(author.orcid)$
24<contrib-id contrib-id-type="orcid">$author.orcid$</contrib-id>
24<contrib-id contrib-id-type="orcid">$author.orcid$</contrib-id>
25$endif$
25$endif$
@@ -28,6 +28,8 @@ $if(author.surname)$
28<surname>$author.surname$</surname>
28<surname>$author.surname$</surname>
29<given-names>$author.given-names$</given-names>
29<given-names>$author.given-names$</given-names>
30</name>
30</name>
31+$elseif(author.name)$
32+<string-name>$author.name$</string-name>
31$else$
33$else$
32<string-name>$author$</string-name>
34<string-name>$author$</string-name>
33$endif$
35$endif$
data/templates/default.latex+65−40
@@ -2,7 +2,7 @@
2\PassOptionsToPackage{unicode$for(hyperrefoptions)$,$hyperrefoptions$$endfor$}{hyperref}
2\PassOptionsToPackage{unicode$for(hyperrefoptions)$,$hyperrefoptions$$endfor$}{hyperref}
3\PassOptionsToPackage{hyphens}{url}
3\PassOptionsToPackage{hyphens}{url}
4$if(colorlinks)$
4$if(colorlinks)$
5−\PassOptionsToPackage{dvipsnames,svgnames*,x11names*}{xcolor}
5+\PassOptionsToPackage{dvipsnames,svgnames,x11names}{xcolor}
6$endif$
6$endif$
7$if(dir)$
7$if(dir)$
8$if(latex-dir-rtl)$
8$if(latex-dir-rtl)$
@@ -98,14 +98,14 @@ $endif$
98$if(linestretch)$
98$if(linestretch)$
99\usepackage{setspace}
99\usepackage{setspace}
100$endif$
100$endif$
101−\usepackage{ifxetex,ifluatex}
101+\usepackage{iftex}
102−\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
102+\ifPDFTeX
103 \usepackage[$if(fontenc)$$fontenc$$else$T1$endif$]{fontenc}
103 \usepackage[$if(fontenc)$$fontenc$$else$T1$endif$]{fontenc}
104 \usepackage[utf8]{inputenc}
104 \usepackage[utf8]{inputenc}
105 \usepackage{textcomp} % provide euro and other symbols
105 \usepackage{textcomp} % provide euro and other symbols
106\else % if luatex or xetex
106\else % if luatex or xetex
107$if(mathspec)$
107$if(mathspec)$
108− \ifxetex
108+ \ifXeTeX
109 \usepackage{mathspec}
109 \usepackage{mathspec}
110 \else
110 \else
111 \usepackage{unicode-math}
111 \usepackage{unicode-math}
@@ -129,7 +129,7 @@ $for(fontfamilies)$
129$endfor$
129$endfor$
130$if(mathfont)$
130$if(mathfont)$
131$if(mathspec)$
131$if(mathspec)$
132− \ifxetex
132+ \ifXeTeX
133 \setmathfont(Digits,Latin,Greek)[$for(mathfontoptions)$$mathfontoptions$$sep$,$endfor$]{$mathfont$}
133 \setmathfont(Digits,Latin,Greek)[$for(mathfontoptions)$$mathfontoptions$$sep$,$endfor$]{$mathfont$}
134 \else
134 \else
135 \setmathfont[$for(mathfontoptions)$$mathfontoptions$$sep$,$endfor$]{$mathfont$}
135 \setmathfont[$for(mathfontoptions)$$mathfontoptions$$sep$,$endfor$]{$mathfont$}
@@ -139,23 +139,42 @@ $else$
139$endif$
139$endif$
140$endif$
140$endif$
141$if(CJKmainfont)$
141$if(CJKmainfont)$
142− \ifxetex
142+ \ifXeTeX
143 \usepackage{xeCJK}
143 \usepackage{xeCJK}
144 \setCJKmainfont[$for(CJKoptions)$$CJKoptions$$sep$,$endfor$]{$CJKmainfont$}
144 \setCJKmainfont[$for(CJKoptions)$$CJKoptions$$sep$,$endfor$]{$CJKmainfont$}
145 \fi
145 \fi
146$endif$
146$endif$
147$if(luatexjapresetoptions)$
147$if(luatexjapresetoptions)$
148− \ifluatex
148+ \ifLuaTeX
149 \usepackage[$for(luatexjapresetoptions)$$luatexjapresetoptions$$sep$,$endfor$]{luatexja-preset}
149 \usepackage[$for(luatexjapresetoptions)$$luatexjapresetoptions$$sep$,$endfor$]{luatexja-preset}
150 \fi
150 \fi
151$endif$
151$endif$
152$if(CJKmainfont)$
152$if(CJKmainfont)$
153− \ifluatex
153+ \ifLuaTeX
154 \usepackage[$for(luatexjafontspecoptions)$$luatexjafontspecoptions$$sep$,$endfor$]{luatexja-fontspec}
154 \usepackage[$for(luatexjafontspecoptions)$$luatexjafontspecoptions$$sep$,$endfor$]{luatexja-fontspec}
155 \setmainjfont[$for(CJKoptions)$$CJKoptions$$sep$,$endfor$]{$CJKmainfont$}
155 \setmainjfont[$for(CJKoptions)$$CJKoptions$$sep$,$endfor$]{$CJKmainfont$}
156 \fi
156 \fi
157$endif$
157$endif$
158\fi
158\fi
159+$if(zero-width-non-joiner)$
160+%% Support for zero-width non-joiner characters.
161+\makeatletter
162+\def\zerowidthnonjoiner{%
163+ % Prevent ligatures and adjust kerning, but still support hyphenating.
164+ \texorpdfstring{%
165+ \textormath{\nobreak\discretionary{-}{}{\kern.03em}%
166+ \ifvmode\else\nobreak\hskip\z@skip\fi}{}%
167+ }{}%
168+}
169+\makeatother
170+\ifPDFTeX
171+ \DeclareUnicodeCharacter{200C}{\zerowidthnonjoiner}
172+\else
173+ \catcode`^^^^200c=\active
174+ \protected\def ^^^^200c{\zerowidthnonjoiner}
175+\fi
176+%% End of ZWNJ support
177+$endif$
159$if(beamer)$
178$if(beamer)$
160$if(theme)$
179$if(theme)$
161\usetheme[$for(themeoptions)$$themeoptions$$sep$,$endfor$]{$theme$}
180\usetheme[$for(themeoptions)$$themeoptions$$sep$,$endfor$]{$theme$}
@@ -219,10 +238,10 @@ $if(keywords)$
219$endif$
238$endif$
220$if(colorlinks)$
239$if(colorlinks)$
221 colorlinks=true,
240 colorlinks=true,
222− linkcolor=$if(linkcolor)$$linkcolor$$else$Maroon$endif$,
241+ linkcolor={$if(linkcolor)$$linkcolor$$else$Maroon$endif$},
223− filecolor=$if(filecolor)$$filecolor$$else$Maroon$endif$,
242+ filecolor={$if(filecolor)$$filecolor$$else$Maroon$endif$},
224− citecolor=$if(citecolor)$$citecolor$$else$Blue$endif$,
243+ citecolor={$if(citecolor)$$citecolor$$else$Blue$endif$},
225− urlcolor=$if(urlcolor)$$urlcolor$$else$Blue$endif$,
244+ urlcolor={$if(urlcolor)$$urlcolor$$else$Blue$endif$},
226$else$
245$else$
227 hidelinks,
246 hidelinks,
228$endif$
247$endif$
@@ -325,11 +344,36 @@ $endif$
325$if(pagestyle)$
344$if(pagestyle)$
326\pagestyle{$pagestyle$}
345\pagestyle{$pagestyle$}
327$endif$
346$endif$
347+$if(csl-refs)$
348+\newlength{\cslhangindent}
349+\setlength{\cslhangindent}{1.5em}
350+\newlength{\csllabelwidth}
351+\setlength{\csllabelwidth}{3em}
352+\newlength{\cslentryspacingunit} % times entry-spacing
353+\setlength{\cslentryspacingunit}{\parskip}
354+\newenvironment{CSLReferences}[2] % #1 hanging-ident, #2 entry spacing
355+ {% don't indent paragraphs
356+ \setlength{\parindent}{0pt}
357+ % turn on hanging indent if param 1 is 1
358+ \ifodd #1
359+ \let\oldpar\par
360+ \def\par{\hangindent=\cslhangindent\oldpar}
361+ \fi
362+ % set entry spacing
363+ \setlength{\parskip}{#2\cslentryspacingunit}
364+ }%
365+ {}
366+\usepackage{calc}
367+\newcommand{\CSLBlock}[1]{#1\hfill\break}
368+\newcommand{\CSLLeftMargin}[1]{\parbox[t]{\csllabelwidth}{#1}}
369+\newcommand{\CSLRightInline}[1]{\parbox[t]{\linewidth - \csllabelwidth}{#1}\break}
370+\newcommand{\CSLIndent}[1]{\hspace{\cslhangindent}#1}
371+$endif$
328$for(header-includes)$
372$for(header-includes)$
329$header-includes$
373$header-includes$
330$endfor$
374$endfor$
331$if(lang)$
375$if(lang)$
332−\ifxetex
376+\ifXeTeX
333 % Load polyglossia as late as possible: uses bidi with RTL langages (e.g. Hebrew, Arabic)
377 % Load polyglossia as late as possible: uses bidi with RTL langages (e.g. Hebrew, Arabic)
334 \usepackage{polyglossia}
378 \usepackage{polyglossia}
335 \setmainlanguage[$for(polyglossia-lang.options)$$polyglossia-lang.options$$sep$,$endfor$]{$polyglossia-lang.name$}
379 \setmainlanguage[$for(polyglossia-lang.options)$$polyglossia-lang.options$$sep$,$endfor$]{$polyglossia-lang.name$}
@@ -346,15 +390,15 @@ $if(babel-newcommands)$
346$endif$
390$endif$
347\fi
391\fi
348$endif$
392$endif$
349−\ifluatex
393+\ifLuaTeX
350 \usepackage{selnolig} % disable illegal ligatures
394 \usepackage{selnolig} % disable illegal ligatures
351\fi
395\fi
352$if(dir)$
396$if(dir)$
353−\ifxetex
397+\ifXeTeX
354 % Load bidi as late as possible as it modifies e.g. graphicx
398 % Load bidi as late as possible as it modifies e.g. graphicx
355 \usepackage{bidi}
399 \usepackage{bidi}
356\fi
400\fi
357−\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
401+\ifPDFTeX
358 \TeXXeTstate=1
402 \TeXXeTstate=1
359 \newcommand{\RL}[1]{\beginR #1\endR}
403 \newcommand{\RL}[1]{\beginR #1\endR}
360 \newcommand{\LR}[1]{\beginL #1\endL}
404 \newcommand{\LR}[1]{\beginL #1\endL}
@@ -372,27 +416,8 @@ $for(bibliography)$
372\addbibresource{$bibliography$}
416\addbibresource{$bibliography$}
373$endfor$
417$endfor$
374$endif$
418$endif$
375−$if(csl-refs)$
419+$if(nocite-ids)$
376−\newlength{\cslhangindent}
420+\nocite{$for(nocite-ids)$$it$$sep$, $endfor$}
377−\setlength{\cslhangindent}{1.5em}
378−\newlength{\csllabelwidth}
379−\setlength{\csllabelwidth}{3em}
380−\newenvironment{CSLReferences}[2] % #1 hanging-ident, #2 entry spacing
381− {% don't indent paragraphs
382− \setlength{\parindent}{0pt}
383− % turn on hanging indent if param 1 is 1
384− \ifodd #1 \everypar{\setlength{\hangindent}{\cslhangindent}}\ignorespaces\fi
385− % set entry spacing
386− \ifnum #2 > 0
387− \setlength{\parskip}{#2\baselineskip}
388− \fi
389− }%
390− {}
391−\usepackage{calc}
392−\newcommand{\CSLBlock}[1]{#1\hfill\break}
393−\newcommand{\CSLLeftMargin}[1]{\parbox[t]{\csllabelwidth}{#1}}
394−\newcommand{\CSLRightInline}[1]{\parbox[t]{\linewidth - \csllabelwidth}{#1}\break}
395−\newcommand{\CSLIndent}[1]{\hspace{\cslhangindent}#1}
396$endif$
421$endif$
397$if(csquotes)$
422$if(csquotes)$
398\usepackage{csquotes}
423\usepackage{csquotes}
@@ -469,12 +494,12 @@ $endif$
469}
494}
470$endif$
495$endif$
471$endif$
496$endif$
472−$if(lot)$
473−\listoftables
474−$endif$
475$if(lof)$
497$if(lof)$
476\listoffigures
498\listoffigures
477$endif$
499$endif$
500+$if(lot)$
501+\listoftables
502+$endif$
478$if(linestretch)$
503$if(linestretch)$
479\setstretch{$linestretch$}
504\setstretch{$linestretch$}
480$endif$
505$endif$
data/templates/default.revealjs+79−125
@@ -52,6 +52,9 @@ $endif$
52$for(author)$
52$for(author)$
53 <p class="author">$author$</p>
53 <p class="author">$author$</p>
54$endfor$
54$endfor$
55+$for(institute)$
56+ <p class="institute">$institute$</p>
57+$endfor$
55$if(date)$
58$if(date)$
56 <p class="date">$date$</p>
59 <p class="date">$date$</p>
57$endif$
60$endif$
@@ -59,7 +62,12 @@ $endif$
59$endif$
62$endif$
60$if(toc)$
63$if(toc)$
61<section id="$idprefix$TOC">
64<section id="$idprefix$TOC">
65+<nav role="doc-toc">
66+$if(toc-title)$
67+<h2 id="$idprefix$toc-title">$toc-title$</h2>
68+$endif$
62$table-of-contents$
69$table-of-contents$
70+</nav>
63</section>
71</section>
64$endif$
72$endif$
65
73
@@ -69,7 +77,7 @@ $body$
69
77
70 <script src="$revealjs-url$/dist/reveal.js"></script>
78 <script src="$revealjs-url$/dist/reveal.js"></script>
71
79
72− // reveal.js plugins
80+ <!-- reveal.js plugins -->
73 <script src="$revealjs-url$/plugin/notes/notes.js"></script>
81 <script src="$revealjs-url$/plugin/notes/notes.js"></script>
74 <script src="$revealjs-url$/plugin/search/search.js"></script>
82 <script src="$revealjs-url$/plugin/search/search.js"></script>
75 <script src="$revealjs-url$/plugin/zoom/zoom.js"></script>
83 <script src="$revealjs-url$/plugin/zoom/zoom.js"></script>
@@ -82,252 +90,198 @@ $endif$
82 // Full list of configuration options available at:
90 // Full list of configuration options available at:
83 // https://revealjs.com/config/
91 // https://revealjs.com/config/
84 Reveal.initialize({
92 Reveal.initialize({
85−$if(controls)$
86 // Display controls in the bottom right corner
93 // Display controls in the bottom right corner
87 controls: $controls$,
94 controls: $controls$,
88−$endif$
95+
89−$if(controlsTutorial)$
90 // Help the user learn the controls by providing hints, for example by
96 // Help the user learn the controls by providing hints, for example by
91 // bouncing the down arrow when they first encounter a vertical slide
97 // bouncing the down arrow when they first encounter a vertical slide
92 controlsTutorial: $controlsTutorial$,
98 controlsTutorial: $controlsTutorial$,
93−$endif$
99+
94−$if(controlsLayout)$
95 // Determines where controls appear, "edges" or "bottom-right"
100 // Determines where controls appear, "edges" or "bottom-right"
96 controlsLayout: '$controlsLayout$',
101 controlsLayout: '$controlsLayout$',
97−$endif$
102+
98−$if(controlsBackArrows)$
99 // Visibility rule for backwards navigation arrows; "faded", "hidden"
103 // Visibility rule for backwards navigation arrows; "faded", "hidden"
100 // or "visible"
104 // or "visible"
101 controlsBackArrows: '$controlsBackArrows$',
105 controlsBackArrows: '$controlsBackArrows$',
102−$endif$
106+
103−$if(progress)$
104 // Display a presentation progress bar
107 // Display a presentation progress bar
105 progress: $progress$,
108 progress: $progress$,
106−$endif$
109+
107−$if(slideNumber)$
108 // Display the page number of the current slide
110 // Display the page number of the current slide
109 slideNumber: $slideNumber$,
111 slideNumber: $slideNumber$,
110−$endif$
112+
111−$if(showSlideNumber)$
112 // 'all', 'print', or 'speaker'
113 // 'all', 'print', or 'speaker'
113 showSlideNumber: '$showSlideNumber$',
114 showSlideNumber: '$showSlideNumber$',
114−$endif$
115+
115−$if(hash)$
116 // Add the current slide number to the URL hash so that reloading the
116 // Add the current slide number to the URL hash so that reloading the
117 // page/copying the URL will return you to the same slide
117 // page/copying the URL will return you to the same slide
118 hash: $hash$,
118 hash: $hash$,
119−$endif$
119+
120−$if(hashOneBasedIndex)$
121 // Start with 1 for the hash rather than 0
120 // Start with 1 for the hash rather than 0
122 hashOneBasedIndex: $hashOneBasedIndex$,
121 hashOneBasedIndex: $hashOneBasedIndex$,
123−$endif$
122+
123+ // Flags if we should monitor the hash and change slides accordingly
124+ respondToHashChanges: $respondToHashChanges$,
125+
124 // Push each slide change to the browser history
126 // Push each slide change to the browser history
125− history: $if(history)$$history$$else$true$endif$,
127+ history: $history$,
126−$if(keyboard)$
128+
127 // Enable keyboard shortcuts for navigation
129 // Enable keyboard shortcuts for navigation
128 keyboard: $keyboard$,
130 keyboard: $keyboard$,
129−$endif$
131+
130−$if(overview)$
131 // Enable the slide overview mode
132 // Enable the slide overview mode
132 overview: $overview$,
133 overview: $overview$,
133−$endif$
134+
134−$if(center)$
135+ // Disables the default reveal.js slide layout (scaling and centering)
136+ // so that you can use custom CSS layout
137+ disableLayout: false,
138+
135 // Vertical centering of slides
139 // Vertical centering of slides
136 center: $center$,
140 center: $center$,
137−$endif$
141+
138−$if(touch)$
139 // Enables touch navigation on devices with touch input
142 // Enables touch navigation on devices with touch input
140 touch: $touch$,
143 touch: $touch$,
141−$endif$
144+
142−$if(loop)$
143 // Loop the presentation
145 // Loop the presentation
144 loop: $loop$,
146 loop: $loop$,
145−$endif$
147+
146−$if(rtl)$
147 // Change the presentation direction to be RTL
148 // Change the presentation direction to be RTL
148 rtl: $rtl$,
149 rtl: $rtl$,
149−$endif$
150+
150−$if(navigationMode)$
151 // see https://revealjs.com/vertical-slides/#navigation-mode
151 // see https://revealjs.com/vertical-slides/#navigation-mode
152 navigationMode: '$navigationMode$',
152 navigationMode: '$navigationMode$',
153−$endif$
153+
154−$if(shuffle)$
155 // Randomizes the order of slides each time the presentation loads
154 // Randomizes the order of slides each time the presentation loads
156 shuffle: $shuffle$,
155 shuffle: $shuffle$,
157−$endif$
156+
158−$if(fragments)$
159 // Turns fragments on and off globally
157 // Turns fragments on and off globally
160 fragments: $fragments$,
158 fragments: $fragments$,
161−$endif$
159+
162−$if(fragmentInURL)$
163 // Flags whether to include the current fragment in the URL,
160 // Flags whether to include the current fragment in the URL,
164 // so that reloading brings you to the same fragment position
161 // so that reloading brings you to the same fragment position
165 fragmentInURL: $fragmentInURL$,
162 fragmentInURL: $fragmentInURL$,
166−$endif$
163+
167−$if(embedded)$
168 // Flags if the presentation is running in an embedded mode,
164 // Flags if the presentation is running in an embedded mode,
169 // i.e. contained within a limited portion of the screen
165 // i.e. contained within a limited portion of the screen
170 embedded: $embedded$,
166 embedded: $embedded$,
171−$endif$
167+
172−$if(help)$
173 // Flags if we should show a help overlay when the questionmark
168 // Flags if we should show a help overlay when the questionmark
174 // key is pressed
169 // key is pressed
175 help: $help$,
170 help: $help$,
176−$endif$
171+
177−$if(pause)$
178− // Flags if it should be possible to pause the presentation (blackout)
172+ // Flags if it should be possible to pause the presentation (blackout)
179− pause: $pause$,
173+ pause: $pause$,
180−$endif$
174+
181−$if(showNotes)$
182 // Flags if speaker notes should be visible to all viewers
175 // Flags if speaker notes should be visible to all viewers
183 showNotes: $showNotes$,
176 showNotes: $showNotes$,
184−$endif$
177+
185−$if(autoPlayMedia)$
186− // Global override for autoplaying embedded media (video/audio/iframe)
178+ // Global override for autoplaying embedded media (null/true/false)
187− // - null: Media will only autoplay if data-autoplay is present
188− // - true: All media will autoplay, regardless of individual setting
189− // - false: No media will autoplay, regardless of individual setting
190 autoPlayMedia: $autoPlayMedia$,
179 autoPlayMedia: $autoPlayMedia$,
191−$endif$
180+
192−$if(preloadIframes)$
193− // Global override for preloading lazy-loaded iframes
181+ // Global override for preloading lazy-loaded iframes (null/true/false)
194− // - null: Iframes with data-src AND data-preload will be loaded when within
195− // the viewDistance, iframes with only data-src will be loaded when visible
196− // - true: All iframes with data-src will be loaded when within the viewDistance
197− // - false: All iframes with data-src will be loaded only when visible
198 preloadIframes: $preloadIframes$,
182 preloadIframes: $preloadIframes$,
199−$endif$
183+
200−$if(autoSlide)$
201 // Number of milliseconds between automatically proceeding to the
184 // Number of milliseconds between automatically proceeding to the
202 // next slide, disabled when set to 0, this value can be overwritten
185 // next slide, disabled when set to 0, this value can be overwritten
203 // by using a data-autoslide attribute on your slides
186 // by using a data-autoslide attribute on your slides
204 autoSlide: $autoSlide$,
187 autoSlide: $autoSlide$,
205−$endif$
188+
206−$if(autoSlideStoppable)$
207 // Stop auto-sliding after user input
189 // Stop auto-sliding after user input
208 autoSlideStoppable: $autoSlideStoppable$,
190 autoSlideStoppable: $autoSlideStoppable$,
209−$endif$
191+
210−$if(autoSlideMethod)$
211 // Use this method for navigation when auto-sliding
192 // Use this method for navigation when auto-sliding
212 autoSlideMethod: $autoSlideMethod$,
193 autoSlideMethod: $autoSlideMethod$,
213−$endif$
194+
214−$if(defaultTiming)$
215 // Specify the average time in seconds that you think you will spend
195 // Specify the average time in seconds that you think you will spend
216 // presenting each slide. This is used to show a pacing timer in the
196 // presenting each slide. This is used to show a pacing timer in the
217 // speaker view
197 // speaker view
218 defaultTiming: $defaultTiming$,
198 defaultTiming: $defaultTiming$,
219−$endif$
199+
220−$if(totalTime)$
221− // Specify the total time in seconds that is available to
222− // present. If this is set to a nonzero value, the pacing
223− // timer will work out the time available for each slide,
224− // instead of using the defaultTiming value
225− totalTime: $totalTime$,
226−$endif$
227−$if(minimumTimePerSlide)$
228− // Specify the minimum amount of time you want to allot to
229− // each slide, if using the totalTime calculation method. If
230− // the automated time allocation causes slide pacing to fall
231− // below this threshold, then you will see an alert in the
232− // speaker notes window
233− minimumTimePerSlide: $minimumTimePerSlide$,
234−$endif$
235−$if(mouseWheel)$
236 // Enable slide navigation via mouse wheel
200 // Enable slide navigation via mouse wheel
237 mouseWheel: $mouseWheel$,
201 mouseWheel: $mouseWheel$,
238−$endif$
202+
239−$if(rollingLinks)$
203+ // The display mode that will be used to show slides
240− // Apply a 3D roll to links on hover
204+ display: '$display$',
241− rollingLinks: $rollingLinks$,
205+
242−$endif$
243−$if(hideInactiveCursor)$
244 // Hide cursor if inactive
206 // Hide cursor if inactive
245 hideInactiveCursor: $hideInactiveCursor$,
207 hideInactiveCursor: $hideInactiveCursor$,
246−$endif$
208+
247−$if(hideCursorTime)$
248 // Time before the cursor is hidden (in ms)
209 // Time before the cursor is hidden (in ms)
249 hideCursorTime: $hideCursorTime$,
210 hideCursorTime: $hideCursorTime$,
250−$endif$
211+
251−$if(hideAddressBar)$
252− // Hides the address bar on mobile devices
253− hideAddressBar: $hideAddressBar$,
254−$endif$
255−$if(previewLinks)$
256 // Opens links in an iframe preview overlay
212 // Opens links in an iframe preview overlay
257 previewLinks: $previewLinks$,
213 previewLinks: $previewLinks$,
258−$endif$
214+
259−$if(transition)$
260− // Transition style
261− transition: '$transition$', // none/fade/slide/convex/concave/zoom
215+ // Transition style (none/fade/slide/convex/concave/zoom)
262−$endif$
216+ transition: '$transition$',
263−$if(transitionSpeed)$
217+
264− // Transition speed
218+ // Transition speed (default/fast/slow)
265− transitionSpeed: '$transitionSpeed$', // default/fast/slow
219+ transitionSpeed: '$transitionSpeed$',
266−$endif$
220+
267−$if(backgroundTransition)$
268 // Transition style for full page slide backgrounds
221 // Transition style for full page slide backgrounds
269− backgroundTransition: '$backgroundTransition$', // none/fade/slide/convex/concave/zoom
222+ // (none/fade/slide/convex/concave/zoom)
270−$endif$
223+ backgroundTransition: '$backgroundTransition$',
271−$if(viewDistance)$
224+
272 // Number of slides away from the current that are visible
225 // Number of slides away from the current that are visible
273 viewDistance: $viewDistance$,
226 viewDistance: $viewDistance$,
274−$endif$
227+
275−$if(mobileViewDistance)$
276 // Number of slides away from the current that are visible on mobile
228 // Number of slides away from the current that are visible on mobile
277 // devices. It is advisable to set this to a lower number than
229 // devices. It is advisable to set this to a lower number than
278 // viewDistance in order to save resources.
230 // viewDistance in order to save resources.
279 mobileViewDistance: $mobileViewDistance$,
231 mobileViewDistance: $mobileViewDistance$,
280−$endif$
281$if(parallaxBackgroundImage)$
232$if(parallaxBackgroundImage)$
233+
282 // Parallax background image
234 // Parallax background image
283 parallaxBackgroundImage: '$parallaxBackgroundImage$', // e.g. "'https://s3.amazonaws.com/hakim-static/reveal-js/reveal-parallax-1.jpg'"
235 parallaxBackgroundImage: '$parallaxBackgroundImage$', // e.g. "'https://s3.amazonaws.com/hakim-static/reveal-js/reveal-parallax-1.jpg'"
284$else$
236$else$
285$if(background-image)$
237$if(background-image)$
238+
286 // Parallax background image
239 // Parallax background image
287 parallaxBackgroundImage: '$background-image$', // e.g. "'https://s3.amazonaws.com/hakim-static/reveal-js/reveal-parallax-1.jpg'"
240 parallaxBackgroundImage: '$background-image$', // e.g. "'https://s3.amazonaws.com/hakim-static/reveal-js/reveal-parallax-1.jpg'"
288$endif$
241$endif$
289$endif$
242$endif$
290$if(parallaxBackgroundSize)$
243$if(parallaxBackgroundSize)$
244+
291 // Parallax background size
245 // Parallax background size
292 parallaxBackgroundSize: '$parallaxBackgroundSize$', // CSS syntax, e.g. "2100px 900px"
246 parallaxBackgroundSize: '$parallaxBackgroundSize$', // CSS syntax, e.g. "2100px 900px"
293$endif$
247$endif$
294$if(parallaxBackgroundHorizontal)$
248$if(parallaxBackgroundHorizontal)$
249+
295 // Amount to move parallax background (horizontal and vertical) on slide change
250 // Amount to move parallax background (horizontal and vertical) on slide change
296 // Number, e.g. 100
251 // Number, e.g. 100
297 parallaxBackgroundHorizontal: $parallaxBackgroundHorizontal$,
252 parallaxBackgroundHorizontal: $parallaxBackgroundHorizontal$,
298$endif$
253$endif$
299$if(parallaxBackgroundVertical)$
254$if(parallaxBackgroundVertical)$
255+
300 parallaxBackgroundVertical: $parallaxBackgroundVertical$,
256 parallaxBackgroundVertical: $parallaxBackgroundVertical$,
301$endif$
257$endif$
302$if(width)$
258$if(width)$
259+
303 // The "normal" size of the presentation, aspect ratio will be preserved
260 // The "normal" size of the presentation, aspect ratio will be preserved
304 // when the presentation is scaled to fit different resolutions. Can be
261 // when the presentation is scaled to fit different resolutions. Can be
305 // specified using percentage units.
262 // specified using percentage units.
306 width: $width$,
263 width: $width$,
307$endif$
264$endif$
308$if(height)$
265$if(height)$
266+
309 height: $height$,
267 height: $height$,
310$endif$
268$endif$
311$if(margin)$
269$if(margin)$
270+
312 // Factor of the display size that should remain empty around the content
271 // Factor of the display size that should remain empty around the content
313 margin: $margin$,
272 margin: $margin$,
314$endif$
273$endif$
315$if(minScale)$
274$if(minScale)$
275+
316 // Bounds for smallest/largest possible scale to apply to content
276 // Bounds for smallest/largest possible scale to apply to content
317 minScale: $minScale$,
277 minScale: $minScale$,
318$endif$
278$endif$
319$if(maxScale)$
279$if(maxScale)$
280+
320 maxScale: $maxScale$,
281 maxScale: $maxScale$,
321$endif$
282$endif$
322−$if(zoomKey)$
323− // Modifier key used to click-zoom to part of the slide
324− zoomKey: '$zoomKey$',
325−$endif$
326−$if(display)$
327− // The display mode that will be used to show slides
328− display: '$display$',
329−$endif$
330$if(mathjax)$
283$if(mathjax)$
284+
331 math: {
285 math: {
332 mathjax: '$mathjaxurl$',
286 mathjax: '$mathjaxurl$',
333 config: 'TeX-AMS_HTML-full',
287 config: 'TeX-AMS_HTML-full',
data/templates/default.s5+3
@@ -61,6 +61,9 @@ $endif$
61$if(author)$
61$if(author)$
62 <h3 class="author">$for(author)$$author$$sep$<br/>$endfor$</h3>
62 <h3 class="author">$for(author)$$author$$sep$<br/>$endfor$</h3>
63$endif$
63$endif$
64+$if(institute)$
65+ <h3 class="institute">$for(institute)$$institute$$sep$<br/>$endfor$</h3>
66+$endif$
64$if(date)$
67$if(date)$
65 <h4 class="date">$date$</h4>
68 <h4 class="date">$date$</h4>
66$endif$
69$endif$
data/templates/default.slideous+7
@@ -63,9 +63,16 @@ $if(title)$
63$if(subtitle)$
63$if(subtitle)$
64 <h1 class="subtitle">$subtitle$</h1>
64 <h1 class="subtitle">$subtitle$</h1>
65$endif$
65$endif$
66+$if(author)$
66 <p class="author">
67 <p class="author">
67$for(author)$$author$$sep$<br/>$endfor$
68$for(author)$$author$$sep$<br/>$endfor$
68 </p>
69 </p>
70+$endif$
71+$if(institute)$
72+ <p class="institute">
73+$for(institute)$$institute$$sep$<br/>$endfor$
74+ </p>
75+$endif$
69$if(date)$
76$if(date)$
70 <p class="date">$date$</p>
77 <p class="date">$date$</p>
71$endif$
78$endif$
data/templates/default.slidy+5
@@ -52,6 +52,11 @@ $if(author)$
52$for(author)$$author$$sep$<br/>$endfor$
52$for(author)$$author$$sep$<br/>$endfor$
53 </p>
53 </p>
54$endif$
54$endif$
55+$if(institute)$
56+ <p class="institute">
57+$for(institute)$$institute$$sep$<br/>$endfor$
58+ </p>
59+$endif$
55$if(date)$
60$if(date)$
56 <p class="date">$date$</p>
61 <p class="date">$date$</p>
57$endif$
62$endif$
data/templates/styles.html+2−1
@@ -14,7 +14,7 @@ body {
14 padding-top: $if(margin-top)$$margin-top$$else$50px$endif$;
14 padding-top: $if(margin-top)$$margin-top$$else$50px$endif$;
15 padding-bottom: $if(margin-bottom)$$margin-bottom$$else$50px$endif$;
15 padding-bottom: $if(margin-bottom)$$margin-bottom$$else$50px$endif$;
16 hyphens: auto;
16 hyphens: auto;
17− word-wrap: break-word;
17+ overflow-wrap: break-word;
18 text-rendering: optimizeLegibility;
18 text-rendering: optimizeLegibility;
19 font-kerning: normal;
19 font-kerning: normal;
20}
20}
@@ -93,6 +93,7 @@ $endif$
93pre code {
93pre code {
94 padding: 0;
94 padding: 0;
95 overflow: visible;
95 overflow: visible;
96+ overflow-wrap: normal;
96}
97}
97.sourceCode {
98.sourceCode {
98 background-color: transparent;
99 background-color: transparent;
doc/epub.md+9−2
@@ -141,8 +141,15 @@ Pandoc has an EPUB3 writer. It renders LaTeX math into MathML, which
141EPUB3 readers are supposed to support (but unfortunately few do).
141EPUB3 readers are supposed to support (but unfortunately few do).
142
142
143Of course, this isn't much help if you want EPUB2 output (`pandoc -t epub2`)
143Of course, this isn't much help if you want EPUB2 output (`pandoc -t epub2`)
144−or target readers that don't support MathML. Then you should try using the
144+or target readers that don't support MathML. Then you have two options:
145+
146+1. Use the [`--webtex`](https://pandoc.org/MANUAL.html#option--webtex) option,
145−`--webtex` option, which will use a web service to convert the TeX to an image.
147+ which will use a web service to convert the TeX to an image.
148+2. Use the [`--gladtex`](https://pandoc.org/MANUAL.html#option--gladtex) option
149+ to convert maths into SVG images on your local machine.
150+
151+Both GladTeX and WebTeX add the LaTeX source of the formula as alternative text
152+of the image, increasing accessibility for blind users.
146
153
147[KindleGen]: https://www.amazon.com/gp/feature.html?ie=UTF8&docId=1000765211
154[KindleGen]: https://www.amazon.com/gp/feature.html?ie=UTF8&docId=1000765211
148[EPUB]: https://en.wikipedia.org/wiki/EPUB
155[EPUB]: https://en.wikipedia.org/wiki/EPUB
doc/faqs.mdnew file+128
@@ -0,0 +1,128 @@
1+% FAQs
2+
3+::: faqs
4+
5+## How can I convert a whole directory of files from Markdown to RTF?
6+
7+On linux or OSX:
8+
9+ for f in *.txt; do pandoc "$f" -s -o "${f%.txt}.rtf"; done
10+
11+
12+In Windows Powershell:
13+
14+ gci -r -i *.txt |foreach{$rtf=$_.directoryname+"\"+$_.basename+".rtf";pandoc -f markdown -s $_.name -o $rtf}
15+
16+## I used pandoc to convert a document to ICML (or OPML or RTF), and when I try to open it I'm told it's invalid. What have I done wrong?
17+
18+Be sure to use the `-s` or `--standalone` flag, or you just get a
19+fragment, not a full document with the required header:
20+
21+ pandoc -s -f markdown -t icml -o my.icml my.md
22+
23+## I get a blank document when I try to convert a markdown document in Chinese to PDF.
24+
25+By default, pandoc uses pdflatex to generate the PDF, and pdflatex
26+doesn't handle Chinese characters. But you can change the default to
27+use xelatex instead. You should also make sure you're using a font
28+with Chinese glyphs. For example:
29+
30+ pandoc -o c.pdf --pdf-engine=xelatex -V mainfont='Adobe Ming Std'
31+
32+## The Windows installer does a single user install, rather than installing pandoc for all users. How can I install pandoc for all users?
33+
34+Run the following command as admin:
35+
36+ msiexec /i pandoc-VERSION.msi ALLUSERS=1
37+
38+This will put pandoc in `C:\Program Files\Pandoc`.
39+You can install Pandoc to a different directory by setting APPLICATIONFOLDER parameter,
40+for example:
41+
42+ msiexec /i pandoc-1.11.1.msi ALLUSERS=1 APPLICATIONFOLDER="C:\Pandoc"
43+
44+## How do I change the margins in PDF output?
45+
46+The option
47+
48+ -V geometry:margin=1in
49+
50+will set the margins to one inch on each side. If you don't want uniform
51+margins, you can do something like
52+
53+ -V geometry:"top=2cm, bottom=1.5cm, left=1cm, right=1cm"
54+
55+Or
56+
57+ -V geometry:"left=3cm, width=10cm"
58+
59+For more options, see the documentation for the LaTeX [geometry
60+package](https://www.ctan.org/pkg/geometry).
61+
62+## How does pandoc compare to multimarkdown?
63+
64+Here is a [wiki
65+page](https://github.com/jgm/pandoc/wiki/Pandoc-vs-Multimarkdown)
66+comparing the two.
67+
68+## When I specify an image width of 50% and convert to LaTeX, pandoc sets the height to textheight and the aspect ratio isn't preserved. How can I prevent this?
69+
70+For example, if you convert an image with `{width="50%"}`, the LaTeX produced
71+will be `\includegraphics[width=0.5\textwidth,height=\textheight]`.
72+
73+This output presupposes the following code in pandoc's default latex
74+template:
75+
76+```
77+% Scale images if necessary, so that they will not overflow the page
78+% margins by default, and it is still possible to overwrite the defaults
79+% using explicit options in \includegraphics[width, height, ...]{}
80+\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
81+```
82+
83+If you don't have this in your custom template, you should
84+add it. If we didn't set the `height` explicitly in this way,
85+the image would not be resized correctly unless it was
86+being resized to smaller than its original size.
87+
88+## Pandoc sometimes uses too much memory. How can I limit the memory used by pandoc?
89+
90+`pandoc +RTS -M30m -RTS` will limit heap memory to 30MB.
91+When converting a document requires more than this, an out of
92+memory error will be issued.
93+
94+## When using `--include-in-header` with PDF or LaTeX output, how do I reference tex declarations coming after `$header-includes$` in the default template?
95+
96+For various reasons, the `$header-includes$` are not at the very
97+end of the LaTeX preamble. This poses a problem when the code
98+you are inserting depends on declarations in the preamble coming
99+after the `$header-includes$` location. For example, you might
100+want to reference the `\author` and `\title` metadata values
101+(set at the very bottom of the preamble) and present them in
102+margins. In that case you can wrap your code in `etoolbox`'s
103+`\AtEndPreamble`. The technique is demonstrated in [this
104+gist](https://gist.github.com/JohnLukeBentley/9dda6166b9ee5c4127afd2b8cd16b70a).
105+When using `\AtEndPreamble`, keep any `makeatletter` or
106+`makeatother` outside of the `\AtEndPreamble`, as shown in the
107+example.
108+
109+## How can I convert PDFs to other formats using pandoc?
110+
111+You can't. You can try opening the PDF in Word or Google Docs
112+and saving in a format from which pandoc can convert directly.
113+
114+## Do I really need to install a 1 GB TeX installation to produce a PDF using pandoc?
115+
116+No. You can get by with a relatively small TeX installation,
117+for example, by starting with MacTeX's Basic TeX distribution
118+and using the `tlmgr` tool to install a few packages required by pandoc
119+(see https://pandoc.org/MANUAL.html#creating-a-pdf).
120+
121+Or, you can produce PDFs via HTML and `wkhtmltopdf`,
122+or via groff ms and `pdfroff`. (These don't produce as nice
123+topography as TeX, particularly when it comes to math, but they
124+may be fine for many purposes.)
125+
126+
127+:::
128+
doc/filters.md+2
@@ -394,6 +394,8 @@ automatically assign it `Just` the target format or `Nothing`.
394
394
395We compile our script:
395We compile our script:
396
396
397+ # first, make sure pandoc-types is installed:
398+ cabal install --lib pandoc-types --package-env .
397 ghc --make handleRuby
399 ghc --make handleRuby
398
400
399Then run it:
401Then run it:
doc/jats.md+31−7
@@ -33,6 +33,11 @@ Metadata Values
33
33
34 See [`<given-names>`][elem:given-names].
34 See [`<given-names>`][elem:given-names].
35
35
36+ `name`
37+ : full name of the author; included only as a fallback if
38+ `author.surname` is not available. Tagged with
39+ [`<string-name>`][elem:string-name].
40+
36 `email`
41 `email`
37 : the contributor's email address.
42 : the contributor's email address.
38
43
@@ -45,6 +50,13 @@ Metadata Values
45 list must also occur as the `id` of an affiliation listed in
50 list must also occur as the `id` of an affiliation listed in
46 the top-level `affiliation` list.
51 the top-level `affiliation` list.
47
52
53+ `equal-contrib`
54+ : boolean attribute used to mark authors who contributed
55+ equally to the work. The
56+ [`equal-contrib`][attr:equal-contrib] attribute is added
57+ to the author's [`<contrib>`] element if this is set to a
58+ truthy value.
59+
48 `cor-id`
60 `cor-id`
49 : identifier linking to the contributor's correspondence
61 : identifier linking to the contributor's correspondence
50 information. The info itself must be stored in as an item in
62 information. The info itself must be stored in as an item in
@@ -167,12 +179,13 @@ Metadata Values
167 pass these components directly.
179 pass these components directly.
168
180
169 The publication date is recorded in the document via the
181 The publication date is recorded in the document via the
170− [`<pub-date>`][elem:pub-date] element and its sub-elements. The
182+ [`<pub-date>`] element and its sub-elements. The
171− [`pub-type`][attr:pub-type] attribute is always set to `epub`.
183+ [`publication-format`][attr:publication-format] attribute is
184+ always set to `electronic`.
172
185
173 `iso-8601`
186 `iso-8601`
174 : ISO-8601 representation of the publication date. Used as the
187 : ISO-8601 representation of the publication date. Used as the
175− value of the [`pub-date`][elem:pub-date] element's
188+ value of the [`<pub-date>`] element's
176 [`iso-8601-date`][attr:iso-8601-date] attribute.
189 [`iso-8601-date`][attr:iso-8601-date] attribute.
177
190
178 This value is set automatically if pandoc can parse the
191 This value is set automatically if pandoc can parse the
@@ -186,6 +199,12 @@ Metadata Values
186 The values are set automatically if pandoc can parse the
199 The values are set automatically if pandoc can parse the
187 `date` value as a date.
200 `date` value as a date.
188
201
202+ `type`
203+ : The type of event marked by this date. The value is set as
204+ the [`date-type`][attr:date-type] attribute on the
205+ [`<pub-date>`] element and defaults to "pub" if not
206+ specified.
207+
189`article`
208`article`
190: information concerning the article that identifies or describes
209: information concerning the article that identifies or describes
191 it. The key-value pairs within this map are typically used
210 it. The key-value pairs within this map are typically used
@@ -295,12 +314,14 @@ Metadata Values
295 `pissn`
314 `pissn`
296 : ISSN identifier of the publication's print version. Used as
315 : ISSN identifier of the publication's print version. Used as
297 content of the [`<issn>`][elem:issn] element with the
316 content of the [`<issn>`][elem:issn] element with the
298− [`pub-type`][attr:pub-type] attribute set to `ppub`.
317+ [`publication-format`][attr:publication-format] attribute
318+ set to `print`.
299
319
300 `eissn`
320 `eissn`
301 : ISSN identifier of the publication's electronic version.
321 : ISSN identifier of the publication's electronic version.
302 Used as content of the [`<issn>`][elem:issn] element with
322 Used as content of the [`<issn>`][elem:issn] element with
323+ the [`publication-format`][attr:publication-format]
303− the [`pub-type`][attr:pub-type] attribute set to `epub`.
324+ attribute set to `electronic`.
304
325
305 `publisher-name`
326 `publisher-name`
306 : name of the publishing entity (person, company, or other).
327 : name of the publishing entity (person, company, or other).
@@ -340,6 +361,8 @@ Required metadata values:
340
361
341[Ringgold]: https://ringgold.com/
362[Ringgold]: https://ringgold.com/
342[attr:content-type]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/attribute/content-type.html
363[attr:content-type]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/attribute/content-type.html
364+[attr:date-type]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/attribute/date-type.html
365+[attr:equal-contrib]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/attribute/equal-contrib.html
343[attr:fn-type]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/attribute/fn-type.html
366[attr:fn-type]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/attribute/fn-type.html
344[attr:institution-id-type]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/attribute/institution-id-type.html
367[attr:institution-id-type]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/attribute/institution-id-type.html
345[attr:iso-8601-date]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/attribute/iso-8601-date.html
368[attr:iso-8601-date]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/attribute/iso-8601-date.html
@@ -347,7 +370,7 @@ Required metadata values:
347[attr:kwd-group-type]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/attribute/kwd-group-type.html
370[attr:kwd-group-type]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/attribute/kwd-group-type.html
348[attr:license-type]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/attribute/license-type.html
371[attr:license-type]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/attribute/license-type.html
349[attr:pub-id-type]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/attribute/pub-id-type.html
372[attr:pub-id-type]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/attribute/pub-id-type.html
350−[attr:pub-type]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/attribute/pub-type.html
373+[attr:publication-format]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/attribute/publication-format.html
351[attr:ref-type]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/attribute/ref-type.html
374[attr:ref-type]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/attribute/ref-type.html
352[attr:rid]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/attribute/rid.html
375[attr:rid]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/attribute/rid.html
353[attr:subj-group-type]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/attribute/subj-group-type.html
376[attr:subj-group-type]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/attribute/subj-group-type.html
@@ -372,7 +395,6 @@ Required metadata values:
372[elem:license]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/element/license.html
395[elem:license]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/element/license.html
373[elem:notes]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/element/notes.html
396[elem:notes]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/element/notes.html
374[elem:permissions]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/element/permissions.html
397[elem:permissions]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/element/permissions.html
375−[elem:pub-date]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/element/pub-date.html
376[elem:publisher-loc]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/element/publisher-loc.html
398[elem:publisher-loc]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/element/publisher-loc.html
377[elem:publisher-name]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/element/publisher-name.html
399[elem:publisher-name]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/element/publisher-name.html
378[elem:string-name]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/element/string-name.html
400[elem:string-name]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/element/string-name.html
@@ -384,7 +406,9 @@ Required metadata values:
384[`<addr-line>`]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/element/addr-line.html
406[`<addr-line>`]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/element/addr-line.html
385[`<aff>`]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/element/aff.html
407[`<aff>`]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/element/aff.html
386[`<city>`]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/element/city.html
408[`<city>`]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/element/city.html
409+[`<contrib>`]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/element/contrib.html
387[`<country>`]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/element/country.html
410[`<country>`]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/element/country.html
388[`<institution-id>`]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/element/institution-id.html
411[`<institution-id>`]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/element/institution-id.html
389[`<institution-wrap>`]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/element/institution-wrap.html
412[`<institution-wrap>`]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/element/institution-wrap.html
390[`<institution>`]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/element/institution.html
413[`<institution>`]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/element/institution.html
414+[`<pub-date>`]: https://jats.nlm.nih.gov/publishing/tag-library/1.2/element/pub-date.html
doc/libraries.mdnew file+63
@@ -0,0 +1,63 @@
1+---
2+title: The pandoc family of libraries
3+author: John MacFarlane
4+---
5+
6+The following Haskell libraries have been developed to support
7+pandoc:
8+
9+[skylighting-core] and [skylighting]
10+: Syntax highlighting engine supporting over 140 languages.
11+
12+[citeproc]
13+: Citation processing using CSL stylesheets.
14+
15+[texmath]
16+: Conversion of math between tex, Word equation, MathML, and GNU eqn.
17+
18+[unicode-collation]
19+: Proper Unicode collation (sorting).
20+
21+[doclayout]
22+: Combinators for laying out a textual document, with support
23+ for line wrapping, tabular layout, and more.
24+
25+[doctemplates]
26+: Supports pandoc's templates.
27+
28+[commonmark], [commonmark-extensions], and [commonmark-pandoc]
29+: Efficient, standards-compliant parser for commonmark and extensions.
30+
31+[ipynb]
32+: Representation of Jupyter notebooks and conversion to and
33+ from JSON.
34+
35+[zip-archive]
36+: A pure zip file creator and extractor, used by pandoc for
37+ docx, ODT, and EPUB.
38+
39+[rfc5051]
40+: Simple unicode collation (used for citation sorting).
41+
42+[emojis]
43+: Conversion between emoji characters and aliases.
44+
45+[jira-wiki-markup]
46+: Support for parsing Jira wiki syntax.
47+
48+[skylighting]: https://hackage.haskell.org/package/skylighting
49+[skylighting-core]: https://hackage.haskell.org/package/skylighting-core
50+[citeproc]: https://hackage.haskell.org/package/citeproc
51+[texmath]: https://hackage.haskell.org/package/texmath
52+[doclayout]: https://hackage.haskell.org/package/doclayout
53+[doctemplates]: https://hackage.haskell.org/package/doctemplates
54+[commonmark]: https://hackage.haskell.org/package/commonmark
55+[commonmark-extensions]: https://hackage.haskell.org/package/commonmark-extensions
56+[commonmark-pandoc]: https://hackage.haskell.org/package/commonmark-pandoc
57+[ipynb]: https://hackage.haskell.org/package/ipynb
58+[zip-archive]: https://hackage.haskell.org/package/zip-archive
59+[rfc5051]: https://hackage.haskell.org/package/rfc5051
60+[emojis]: https://hackage.haskell.org/package/emojis
61+[jira-wiki-markup]: https://hackage.haskell.org/package/jira-wiki-markup
62+[unicode-collation]: https://hackage.haskell.org/package/unicode-collation
63+
doc/lua-filters.md+192−6
@@ -109,7 +109,7 @@ element filtering function. In other words, filter entries will
109be called for each corresponding element in the document,
109be called for each corresponding element in the document,
110getting the respective element as input.
110getting the respective element as input.
111
111
112−The return of a filter function must one of the following:
112+The return of a filter function must be one of the following:
113
113
114- nil: this means that the object should remain unchanged.
114- nil: this means that the object should remain unchanged.
115- a pandoc object: this must be of the same type as the input
115- a pandoc object: this must be of the same type as the input
@@ -3084,7 +3084,8 @@ Clear-out the media bag, deleting all items.
3084
3084
3085`insert (filepath, mime_type, contents)`
3085`insert (filepath, mime_type, contents)`
3086
3086
3087−Adds a new entry to pandoc's media bag.
3087+Adds a new entry to pandoc's media bag. Replaces any existing
3088+mediabag entry with the same `filepath`.
3088
3089
3089Parameters:
3090Parameters:
3090
3091
@@ -3092,7 +3093,7 @@ Parameters:
3092: filename and path relative to the output folder.
3093: filename and path relative to the output folder.
3093
3094
3094`mime_type`:
3095`mime_type`:
3095−: the file's MIME type
3096+: the file's MIME type; use `nil` if unknown or unavailable.
3096
3097
3097`contents`:
3098`contents`:
3098: the binary contents of the file.
3099: the binary contents of the file.
@@ -3176,12 +3177,22 @@ Usage:
3176
3177
3177### fetch {#pandoc.mediabag.fetch}
3178### fetch {#pandoc.mediabag.fetch}
3178
3179
3179−`fetch (source, base_url)`
3180+`fetch (source)`
3180
3181
3181Fetches the given source from a URL or local file. Returns two
3182Fetches the given source from a URL or local file. Returns two
3182values: the contents of the file and the MIME type (or an empty
3183values: the contents of the file and the MIME type (or an empty
3183string).
3184string).
3184
3185
3186+The function will first try to retrieve `source` from the
3187+mediabag; if that fails, it will try to download it or read it
3188+from the local file system while respecting pandoc's "resource
3189+path" setting.
3190+
3191+Parameters:
3192+
3193+`source`:
3194+: path to a resource; either a local file path or URI
3195+
3185Returns:
3196Returns:
3186
3197
3187- the entries MIME type, or nil if the file was not found.
3198- the entries MIME type, or nil if the file was not found.
@@ -3190,11 +3201,11 @@ Returns:
3190Usage:
3201Usage:
3191
3202
3192 local diagram_url = "https://pandoc.org/diagram.jpg"
3203 local diagram_url = "https://pandoc.org/diagram.jpg"
3193− local mt, contents = pandoc.mediabag.fetch(diagram_url, ".")
3204+ local mt, contents = pandoc.mediabag.fetch(diagram_url)
3194
3205
3195# Module pandoc.List
3206# Module pandoc.List
3196
3207
3197−The this module defines pandoc's list type. It comes with useful
3208+This module defines pandoc's list type. It comes with useful
3198methods and convenience functions.
3209methods and convenience functions.
3199
3210
3200## Constructor
3211## Constructor
@@ -3376,6 +3387,181 @@ methods and convenience functions.
3376 `comp`:
3387 `comp`:
3377 : Comparison function as described above.
3388 : Comparison function as described above.
3378
3389
3390+# Module pandoc.path
3391+
3392+Module for file path manipulations.
3393+
3394+## Static Fields {#pandoc.path-fields}
3395+
3396+### separator {#pandoc.path.separator}
3397+
3398+The character that separates directories.
3399+
3400+### search_path_separator {#pandoc.path.search_path_separator}
3401+
3402+The character that is used to separate the entries in the `PATH`
3403+environment variable.
3404+
3405+## Functions {#pandoc.path-functions}
3406+
3407+### directory (filepath) {#pandoc.path.directory}
3408+
3409+Gets the directory name, i.e., removes the last directory
3410+separator and everything after from the given path.
3411+
3412+Parameters:
3413+
3414+filepath
3415+: path (string)
3416+
3417+Returns:
3418+
3419+- The filepath up to the last directory separator. (string)
3420+
3421+### filename (filepath) {#pandoc.path.filename}
3422+
3423+Get the file name.
3424+
3425+Parameters:
3426+
3427+filepath
3428+: path (string)
3429+
3430+Returns:
3431+
3432+- File name part of the input path. (string)
3433+
3434+### is_absolute (filepath) {#pandoc.path.is_absolute}
3435+
3436+Checks whether a path is absolute, i.e. not fixed to a root.
3437+
3438+Parameters:
3439+
3440+filepath
3441+: path (string)
3442+
3443+Returns:
3444+
3445+- `true` iff `filepath` is an absolute path, `false` otherwise.
3446+ (boolean)
3447+
3448+### is_relative (filepath) {#pandoc.path.is_relative}
3449+
3450+Checks whether a path is relative or fixed to a root.
3451+
3452+Parameters:
3453+
3454+filepath
3455+: path (string)
3456+
3457+Returns:
3458+
3459+- `true` iff `filepath` is a relative path, `false` otherwise.
3460+ (boolean)
3461+
3462+### join (filepaths) {#pandoc.path.join}
3463+
3464+Join path elements back together by the directory separator.
3465+
3466+Parameters:
3467+
3468+filepaths
3469+: path components (list of strings)
3470+
3471+Returns:
3472+
3473+- The joined path. (string)
3474+
3475+### make_relative (path, root[, unsafe]) {#pandoc.path.make_relative}
3476+
3477+Contract a filename, based on a relative path. Note that the
3478+resulting path will usually not introduce `..` paths, as the
3479+presence of symlinks means `../b` may not reach `a/b` if it starts
3480+from `a/c`. For a worked example see [this blog
3481+post](https://neilmitchell.blogspot.co.uk/2015/10/filepaths-are-subtle-symlinks-are-hard.html).
3482+
3483+Set `unsafe` to a truthy value to a allow `..` in paths.
3484+
3485+Parameters:
3486+
3487+path
3488+: path to be made relative (string)
3489+
3490+root
3491+: root path (string)
3492+
3493+unsafe
3494+: whether to allow `..` in the result. (boolean)
3495+
3496+Returns:
3497+
3498+- contracted filename (string)
3499+
3500+### normalize (filepath) {#pandoc.path.normalize}
3501+
3502+Normalizes a path.
3503+
3504+- `//` makes sense only as part of a (Windows) network drive;
3505+ elsewhere, multiple slashes are reduced to a single
3506+ `path.separator` (platform dependent).
3507+- `/` becomes `path.separator` (platform dependent)
3508+- `./` -\> ''
3509+- an empty path becomes `.`
3510+
3511+Parameters:
3512+
3513+filepath
3514+: path (string)
3515+
3516+Returns:
3517+
3518+- The normalized path. (string)
3519+
3520+### split (filepath) {#pandoc.path.split}
3521+
3522+Splits a path by the directory separator.
3523+
3524+Parameters:
3525+
3526+filepath
3527+: path (string)
3528+
3529+Returns:
3530+
3531+- List of all path components. (list of strings)
3532+
3533+### split_extension (filepath) {#pandoc.path.split_extension}
3534+
3535+Splits the last extension from a file path and returns the parts. The
3536+extension, if present, includes the leading separator; if the path has
3537+no extension, then the empty string is returned as the extension.
3538+
3539+Parameters:
3540+
3541+filepath
3542+: path (string)
3543+
3544+Returns:
3545+
3546+- filepath without extension (string)
3547+
3548+- extension or empty string (string)
3549+
3550+### split_search_path (search_path) {#pandoc.path.split_search_path}
3551+
3552+Takes a string and splits it on the `search_path_separator` character.
3553+Blank items are ignored on Windows, and converted to `.` on Posix. On
3554+Windows path elements are stripped of quotes.
3555+
3556+Parameters:
3557+
3558+search_path
3559+: platform-specific search path (string)
3560+
3561+Returns:
3562+
3563+- list of directories in search path (list of strings)
3564+
3379# Module pandoc.system
3565# Module pandoc.system
3380
3566
3381Access to system information and functionality.
3567Access to system information and functionality.
doc/org.md+16−2
@@ -25,8 +25,7 @@ The following export keywords are supported:
25- LANGUAGE: document language; included as plain-text metadata
25- LANGUAGE: document language; included as plain-text metadata
26 field `lang`. The value should be a [BCP47 language tag].
26 field `lang`. The value should be a [BCP47 language tag].
27
27
28−- SELECT_TAGS: tags which select a tree for export. Currently
28+- SELECT_TAGS: tags which select a tree for export.
29− *unsupported*.
30
29
31- EXCLUDE\_TAGS: tags which prevent a subtree from being
30- EXCLUDE\_TAGS: tags which prevent a subtree from being
32 exported. Fully supported.
31 exported. Fully supported.
@@ -37,6 +36,21 @@ The following export keywords are supported:
37 defaults to stdout unless a target has to be given as a command
36 defaults to stdout unless a target has to be given as a command
38 line option.
37 line option.
39
38
39+::: {.alert .alert-info}
40+Pandoc tries to be compatible with org-mode when exporting an org document. If
41+you find some behavior confusing, please do refer to org-mode
42+[Export-Settings](https://orgmode.org/manual/Export-Settings.html)
43+documentation. For example, a common confusion
44+([#3214](https://github.com/jgm/pandoc/issues/3214 "Problem with headers lower
45+then 3 in org-mode reader"), [#5169](https://github.com/jgm/pandoc/issues/5169
46+"org mode headings past level three converted to numbered outline list"),
47+[#6145](https://github.com/jgm/pandoc/issues/6145 "Headers 4 levels deep render
48+differently"), [#7236](https://github.com/jgm/pandoc/issues/7236 "In Org mode,
49+Header with level > 3 are not recognized as headers")) is treatment of headers
50+with level > 3 differently because org-mode sets `org-export-headline-levels`
51+(configurable with `#+OPTIONS: H:3`) to 3 by default.
52+:::
53+
40[BCP47 language tag]: https://tools.ietf.org/html/bcp47
54[BCP47 language tag]: https://tools.ietf.org/html/bcp47
41
55
42Format-specific options
56Format-specific options
doc/using-the-pandoc-api.md+25−12
@@ -14,7 +14,7 @@ and types is available at
14Pandoc is structured as a set of *readers*, which translate
14Pandoc is structured as a set of *readers*, which translate
15various input formats into an abstract syntax tree (the
15various input formats into an abstract syntax tree (the
16Pandoc AST) representing a structured document, and a set of
16Pandoc AST) representing a structured document, and a set of
17−*writers*, which render this AST into various input formats.
17+*writers*, which render this AST into various output formats.
18Pictorially:
18Pictorially:
19
19
20```
20```
@@ -90,8 +90,14 @@ Some notes:
90Let's look at the types of `readMarkdown` and `writeRST`:
90Let's look at the types of `readMarkdown` and `writeRST`:
91
91
92```haskell
92```haskell
93−readMarkdown :: PandocMonad m => ReaderOptions -> Text -> m Pandoc
93+readMarkdown :: (PandocMonad m, ToSources a)
94+ => ReaderOptions
95+ -> a
96+ -> m Pandoc
94−writeRST :: PandocMonad m => WriterOptions -> Pandoc -> m Text
97+writeRST :: PandocMonad m
98+ => WriterOptions
99+ -> Pandoc
100+ -> m Text
95```
101```
96
102
97The `PandocMonad m =>` part is a typeclass constraint.
103The `PandocMonad m =>` part is a typeclass constraint.
@@ -133,7 +139,7 @@ report :: PandocMonad m => LogMessage -> m ()
133-- | Fetch an image or other item from the local filesystem or the net.
139-- | Fetch an image or other item from the local filesystem or the net.
134-- Returns raw content and maybe mime type.
140-- Returns raw content and maybe mime type.
135fetchItem :: PandocMonad m
141fetchItem :: PandocMonad m
136− => String
142+ => Text
137 -> m (B.ByteString, Maybe MimeType)
143 -> m (B.ByteString, Maybe MimeType)
138
144
139-- Set the resource path searched by 'fetchItem'.
145-- Set the resource path searched by 'fetchItem'.
@@ -155,6 +161,13 @@ Note that `PandocIO` is an instance of `MonadIO`, so you can
155use `liftIO` to perform arbitrary IO operations inside a pandoc
161use `liftIO` to perform arbitrary IO operations inside a pandoc
156conversion chain.
162conversion chain.
157
163
164+`readMarkdown` is polymorphic in its second argument, which
165+can be any type that is an instance of the `ToSources`
166+typeclass. You can use `Text`, as in the example above.
167+But you can also use `[(FilePath, Text)]`, if the input comes
168+from multiple files and you want to track source positions
169+accurately.
170+
158# Options
171# Options
159
172
160The first argument of each reader or writer is for
173The first argument of each reader or writer is for
@@ -200,8 +213,8 @@ of the Monoid typeclass and can easily be concatenated:
200import Text.Pandoc.Builder
213import Text.Pandoc.Builder
201
214
202mydoc :: Pandoc
215mydoc :: Pandoc
203−mydoc = doc $ header 1 (text "Hello!")
216+mydoc = doc $ header 1 (text (T.pack "Hello!"))
204− <> para (emph (text "hello world") <> text ".")
217+ <> para (emph (text (T.pack "hello world")) <> text (T.pack "."))
205
218
206main :: IO ()
219main :: IO ()
207main = print mydoc
220main = print mydoc
@@ -248,16 +261,16 @@ import qualified Data.Text as T
248import Data.List (intersperse)
261import Data.List (intersperse)
249
262
250data Station = Station{
263data Station = Station{
251− address :: String
264+ address :: T.Text
252− , name :: String
265+ , name :: T.Text
253− , cardsAccepted :: [String]
266+ , cardsAccepted :: [T.Text]
254 } deriving Show
267 } deriving Show
255
268
256instance FromJSON Station where
269instance FromJSON Station where
257 parseJSON (Object v) = Station <$>
270 parseJSON (Object v) = Station <$>
258 v .: "street_address" <*>
271 v .: "street_address" <*>
259 v .: "station_name" <*>
272 v .: "station_name" <*>
260− (words <$> (v .:? "cards_accepted" .!= ""))
273+ (T.words <$> (v .:? "cards_accepted" .!= ""))
261 parseJSON _ = mzero
274 parseJSON _ = mzero
262
275
263createLetter :: [Station] -> Pandoc
276createLetter :: [Station] -> Pandoc
@@ -315,7 +328,7 @@ users to override the system defaults. If you want to disable
315this behavior, use `setUserDataDir Nothing`.
328this behavior, use `setUserDataDir Nothing`.
316
329
317To render a template, use `renderTemplate'`, which takes two
330To render a template, use `renderTemplate'`, which takes two
318−arguments, a template (String) and a context (any instance
331+arguments, a template (Text) and a context (any instance
319of ToJSON). If you want to create a context from the metadata
332of ToJSON). If you want to create a context from the metadata
320part of a Pandoc document, use `metaToJSON'` from
333part of a Pandoc document, use `metaToJSON'` from
321[Text.Pandoc.Writers.Shared]. If you also want to incorporate
334[Text.Pandoc.Writers.Shared]. If you also want to incorporate
@@ -413,7 +426,7 @@ concatenated together. Here's an example that returns a
413list of the URLs linked to in a document:
426list of the URLs linked to in a document:
414
427
415```haskell
428```haskell
416−listURLs :: Pandoc -> [String]
429+listURLs :: Pandoc -> [Text]
417listURLs = query urls
430listURLs = query urls
418 where urls (Link _ _ (src, _)) = [src]
431 where urls (Link _ _ (src, _)) = [src]
419 urls _ = []
432 urls _ = []
linux/make_artifacts.sh+19−7
@@ -2,13 +2,23 @@ set -e
2
2
3MACHINE=$(uname -m)
3MACHINE=$(uname -m)
4case "$MACHINE" in
4case "$MACHINE" in
5− x86_64) ARCHITECTURE=amd64;;
5+ x86_64) ARCHITECTURE=amd64;;
6− i686) ARCHITECTURE=i386;;
6+ i686) ARCHITECTURE=i386;;
7− i386) ARCHITECTURE=i386;;
7+ i386) ARCHITECTURE=i386;;
8+ aarch64) ARCHITECTURE=arm64;;
9+ *) ARCHITECTURE=unknown;;
8esac
10esac
9
11
10ARTIFACTS="${ARTIFACTS:-/artifacts}"
12ARTIFACTS="${ARTIFACTS:-/artifacts}"
11
13
14+# This is our sentinel that tells us when we're done.
15+rm -f $ARTIFACTS/DONE
16+
17+clean_up() {
18+ echo "All done!" > "$ARTIFACTS/DONE"
19+}
20+trap clean_up EXIT
21+
12# build binaries
22# build binaries
13
23
14cabal --version
24cabal --version
@@ -16,9 +26,9 @@ ghc --version
16
26
17cabal v2-update
27cabal v2-update
18cabal v2-clean
28cabal v2-clean
19−cabal v2-configure --enable-tests -f-export-dynamic -fembed_data_files --enable-executable-static --ghc-options '-optc-Os -optl=-pthread' pandoc
29+cabal v2-configure --enable-tests -f-export-dynamic -fembed_data_files --enable-executable-static --ghc-options '-j4 +RTS -A256m -RTS -split-sections -optc-Os -optl=-pthread' pandoc
20−cabal v2-build
30+cabal v2-build -j4
21−cabal v2-test -j1
31+cabal v2-test -j4
22for f in $(find dist-newstyle -name 'pandoc' -type f -perm /400); do cp $f /artifacts/; done
32for f in $(find dist-newstyle -name 'pandoc' -type f -perm /400); do cp $f /artifacts/; done
23
33
24# make deb
34# make deb
@@ -68,5 +78,7 @@ mv pandoc $TARGET/bin
68strip $TARGET/bin/pandoc
78strip $TARGET/bin/pandoc
69gzip -9 $TARGET/share/man/man1/pandoc.1
79gzip -9 $TARGET/share/man/man1/pandoc.1
70
80
71−tar cvzf $TARGET-linux-amd64.tar.gz $TARGET
81+tar cvzf $TARGET-linux-$ARCHITECTURE.tar.gz $TARGET
72rm -r $TARGET
82rm -r $TARGET
83+
84+exit 0
man/pandoc.1+287−62
@@ -1,7 +1,7 @@
1'\" t
1'\" t
2−.\" Automatically generated by Pandoc 2.11.4
2+.\" Automatically generated by Pandoc 2.14.0.1
3.\"
3.\"
4−.TH "Pandoc User\[cq]s Guide" "" "January 21, 2021" "pandoc 2.11.4" ""
4+.TH "Pandoc User\[cq]s Guide" "" "June 20, 2021" "pandoc 2.14.0.3" ""
5.hy
5.hy
6.SH NAME
6.SH NAME
7pandoc - general markup converter
7pandoc - general markup converter
@@ -172,13 +172,12 @@ You can then test it with \f[C]pdflatex test.tex\f[R].
172When using LaTeX, the following packages need to be available (they are
172When using LaTeX, the following packages need to be available (they are
173included with all recent versions of TeX Live): \f[C]amsfonts\f[R],
173included with all recent versions of TeX Live): \f[C]amsfonts\f[R],
174\f[C]amsmath\f[R], \f[C]lm\f[R], \f[C]unicode-math\f[R],
174\f[C]amsmath\f[R], \f[C]lm\f[R], \f[C]unicode-math\f[R],
175−\f[C]ifxetex\f[R], \f[C]ifluatex\f[R], \f[C]listings\f[R] (if the
175+\f[C]iftex\f[R], \f[C]listings\f[R] (if the \f[C]--listings\f[R] option
176−\f[C]--listings\f[R] option is used), \f[C]fancyvrb\f[R],
177−\f[C]longtable\f[R], \f[C]booktabs\f[R], \f[C]graphicx\f[R] (if the
176+is used), \f[C]fancyvrb\f[R], \f[C]longtable\f[R], \f[C]booktabs\f[R],
178−document contains images), \f[C]hyperref\f[R], \f[C]xcolor\f[R],
177+\f[C]graphicx\f[R] (if the document contains images),
178+\f[C]hyperref\f[R], \f[C]xcolor\f[R], \f[C]ulem\f[R], \f[C]geometry\f[R]
179−\f[C]ulem\f[R], \f[C]geometry\f[R] (with the \f[C]geometry\f[R] variable
179+(with the \f[C]geometry\f[R] variable set), \f[C]setspace\f[R] (with
180−set), \f[C]setspace\f[R] (with \f[C]linestretch\f[R]), and
181−\f[C]babel\f[R] (with \f[C]lang\f[R]).
180+\f[C]linestretch\f[R]), and \f[C]babel\f[R] (with \f[C]lang\f[R]).
182The use of \f[C]xelatex\f[R] or \f[C]lualatex\f[R] as the PDF engine
181The use of \f[C]xelatex\f[R] or \f[C]lualatex\f[R] as the PDF engine
183requires \f[C]fontspec\f[R].
182requires \f[C]fontspec\f[R].
184\f[C]lualatex\f[R] uses \f[C]selnolig\f[R].
183\f[C]lualatex\f[R] uses \f[C]selnolig\f[R].
@@ -458,16 +457,15 @@ On *nix and macOS systems this will be the \f[C]pandoc\f[R] subdirectory
458of the XDG data directory (by default, \f[C]$HOME/.local/share\f[R],
457of the XDG data directory (by default, \f[C]$HOME/.local/share\f[R],
459overridable by setting the \f[C]XDG_DATA_HOME\f[R] environment
458overridable by setting the \f[C]XDG_DATA_HOME\f[R] environment
460variable).
459variable).
461−If that directory does not exist, \f[C]$HOME/.pandoc\f[R] will be used
460+If that directory does not exist and \f[C]$HOME/.pandoc\f[R] exists, it
462−(for backwards compatibility).
461+will be used (for backwards compatibility).
463−In Windows the default user data directory is
462+On Windows the default user data directory is
464\f[C]C:\[rs]Users\[rs]USERNAME\[rs]AppData\[rs]Roaming\[rs]pandoc\f[R].
463\f[C]C:\[rs]Users\[rs]USERNAME\[rs]AppData\[rs]Roaming\[rs]pandoc\f[R].
465You can find the default user data directory on your system by looking
464You can find the default user data directory on your system by looking
466at the output of \f[C]pandoc --version\f[R].
465at the output of \f[C]pandoc --version\f[R].
466+Data files placed in this directory (for example,
467−A \f[C]reference.odt\f[R], \f[C]reference.docx\f[R], \f[C]epub.css\f[R],
467+\f[C]reference.odt\f[R], \f[C]reference.docx\f[R], \f[C]epub.css\f[R],
468−\f[C]templates\f[R], \f[C]slidy\f[R], \f[C]slideous\f[R], or
469−\f[C]s5\f[R] directory placed in this directory will override
470−pandoc\[cq]s normal defaults.
468+\f[C]templates\f[R]) will override pandoc\[cq]s normal defaults.
471.TP
469.TP
472\f[B]\f[CB]-d\f[B]\f[R] \f[I]FILE\f[R], \f[B]\f[CB]--defaults=\f[B]\f[R]\f[I]FILE\f[R]
470\f[B]\f[CB]-d\f[B]\f[R] \f[I]FILE\f[R], \f[B]\f[CB]--defaults=\f[B]\f[R]\f[I]FILE\f[R]
473Specify a set of default option settings.
471Specify a set of default option settings.
@@ -718,11 +716,11 @@ Extract images and other media contained in or linked from the source
718document to the path \f[I]DIR\f[R], creating it if necessary, and adjust
716document to the path \f[I]DIR\f[R], creating it if necessary, and adjust
719the images references in the document so they point to the extracted
717the images references in the document so they point to the extracted
720files.
718files.
719+Media are downloaded, read from the file system, or extracted from a
721−If the source format is a binary container (docx, epub, or odt), the
720+binary container (e.g.\ docx), as needed.
722−media is extracted from the container and the original filenames are
721+The original file paths are used if they are relative paths not
723−used.
722+containing \f[C]..\f[R].
724−Otherwise the media is read from the file system or downloaded, and new
725−filenames are constructed based on SHA1 hashes of the contents.
723+Otherwise filenames are constructed from the SHA1 hash of the contents.
726.TP
724.TP
727\f[B]\f[CB]--abbreviations=\f[B]\f[R]\f[I]FILE\f[R]
725\f[B]\f[CB]--abbreviations=\f[B]\f[R]\f[I]FILE\f[R]
728Specifies a custom abbreviations file, with abbreviations one to a line.
726Specifies a custom abbreviations file, with abbreviations one to a line.
@@ -732,9 +730,9 @@ system default.
732To see the system default, use
730To see the system default, use
733\f[C]pandoc --print-default-data-file=abbreviations\f[R].
731\f[C]pandoc --print-default-data-file=abbreviations\f[R].
734The only use pandoc makes of this list is in the Markdown reader.
732The only use pandoc makes of this list is in the Markdown reader.
735−Strings ending in a period that are found in this list will be followed
733+Strings found in this list will be followed by a nonbreaking space, and
736−by a nonbreaking space, so that the period will not produce
737−sentence-ending space in formats like LaTeX.
734+the period will not produce sentence-ending space in formats like LaTeX.
735+The strings may not contain spaces.
738.SS General writer options
736.SS General writer options
739.TP
737.TP
740\f[B]\f[CB]-s\f[B]\f[R], \f[B]\f[CB]--standalone\f[B]\f[R]
738\f[B]\f[CB]-s\f[B]\f[R], \f[B]\f[CB]--standalone\f[B]\f[R]
@@ -925,6 +923,11 @@ Note that, if \f[C]--resource-path\f[R] is specified, the working
925directory must be explicitly listed or it will not be searched.
923directory must be explicitly listed or it will not be searched.
926For example: \f[C]--resource-path=.:test\f[R] will search the working
924For example: \f[C]--resource-path=.:test\f[R] will search the working
927directory and the \f[C]test\f[R] subdirectory, in that order.
925directory and the \f[C]test\f[R] subdirectory, in that order.
926+This option can be used repeatedly.
927+Search path components that come later on the command line will be
928+searched before those that come earlier, so
929+\f[C]--resource-path foo:bar --resource-path baz:bim\f[R] is equivalent
930+to \f[C]--resource-path baz:bim:foo:bar\f[R].
928.TP
931.TP
929\f[B]\f[CB]--request-header=\f[B]\f[R]\f[I]NAME\f[R]\f[B]\f[CB]:\f[B]\f[R]\f[I]VAL\f[R]
932\f[B]\f[CB]--request-header=\f[B]\f[R]\f[I]NAME\f[R]\f[B]\f[CB]:\f[B]\f[R]\f[I]VAL\f[R]
930Set the request header \f[I]NAME\f[R] to the value \f[I]VAL\f[R] when
933Set the request header \f[I]NAME\f[R] to the value \f[I]VAL\f[R] when
@@ -1501,16 +1504,16 @@ inserted.
1501.TP
1504.TP
1502\f[B]\f[CB]--gladtex\f[B]\f[R]
1505\f[B]\f[CB]--gladtex\f[B]\f[R]
1503Enclose TeX math in \f[C]<eq>\f[R] tags in HTML output.
1506Enclose TeX math in \f[C]<eq>\f[R] tags in HTML output.
1504−The resulting HTML can then be processed by GladTeX to produce images of
1507+The resulting HTML can then be processed by GladTeX to produce SVG
1505−the typeset formulas and an HTML file with links to these images.
1508+images of the typeset formulas and an HTML file with these images
1506−So, the procedure is:
1509+embedded.
1507.RS
1510.RS
1508.IP
1511.IP
1509.nf
1512.nf
1510\f[C]
1513\f[C]
1511pandoc -s --gladtex input.md -o myfile.htex
1514pandoc -s --gladtex input.md -o myfile.htex
1512−gladtex -d myfile-images myfile.htex
1515+gladtex -d image_dir myfile.htex
1513−# produces myfile.html and images in myfile-images
1516+# produces myfile.html and images in image_dir
1514\f[R]
1517\f[R]
1515.fi
1518.fi
1516.RE
1519.RE
@@ -1614,6 +1617,11 @@ T}@T{
1614PandocPDFError
1617PandocPDFError
1615T}
1618T}
1616T{
1619T{
1620+44
1621+T}@T{
1622+PandocXMLError
1623+T}
1624+T{
161747
162547
1618T}@T{
1626T}@T{
1619PandocPDFProgramNotFoundError
1627PandocPDFProgramNotFoundError
@@ -1674,6 +1682,11 @@ T}@T{
1674PandocIpynbDecodingError
1682PandocIpynbDecodingError
1675T}
1683T}
1676T{
1684T{
1685+94
1686+T}@T{
1687+PandocUnsupportedCharsetError
1688+T}
1689+T{
167797
169097
1678T}@T{
1691T}@T{
1679PandocCouldNotFindDataFileError
1692PandocCouldNotFindDataFileError
@@ -1773,8 +1786,11 @@ data-dir:
1773verbosity: INFO
1786verbosity: INFO
1774log-file: log.json
1787log-file: log.json
1775
1788
1776−# citeproc, natbib, or biblatex
1789+# citeproc, natbib, or biblatex. This only affects LaTeX
1790+# output. If you want to use citeproc to format citations,
1791+# you should also set \[aq]citeproc: true\[aq] (see above).
1777cite-method: citeproc
1792cite-method: citeproc
1793+
1778# part, chapter, section, or default:
1794# part, chapter, section, or default:
1779top-level-division: chapter
1795top-level-division: chapter
1780abbreviations:
1796abbreviations:
@@ -1875,6 +1891,38 @@ verbosity: INFO
1875\f[R]
1891\f[R]
1876.fi
1892.fi
1877.PP
1893.PP
1894+In fields that expect a file path (or list of file paths), the following
1895+syntax may be used to interpolate environment variables:
1896+.IP
1897+.nf
1898+\f[C]
1899+csl: ${HOME}/mycsldir/special.csl
1900+\f[R]
1901+.fi
1902+.PP
1903+\f[C]${USERDATA}\f[R] may also be used; this will always resolve to the
1904+user data directory that is current when the defaults file is parsed,
1905+regardless of the setting of the environment variable
1906+\f[C]USERDATA\f[R].
1907+.PP
1908+\f[C]${.}\f[R] will resolve to the directory containing the default file
1909+itself.
1910+This allows you to refer to resources contained in that directory:
1911+.IP
1912+.nf
1913+\f[C]
1914+epub-cover-image: ${.}/cover.jpg
1915+epub-metadata: ${.}/meta.xml
1916+resource-path:
1917+- . # the working directory from which pandoc is run
1918+- ${.}/images # the images subdirectory of the directory
1919+ # containing this defaults file
1920+\f[R]
1921+.fi
1922+.PP
1923+This environment variable interpolation syntax \f[I]only\f[R] works in
1924+fields that expect file paths.
1925+.PP
1878Default files can be placed in the \f[C]defaults\f[R] subdirectory of
1926Default files can be placed in the \f[C]defaults\f[R] subdirectory of
1879the user data directory and used from any directory.
1927the user data directory and used from any directory.
1880For example, one could create a file specifying defaults for writing
1928For example, one could create a file specifying defaults for writing
@@ -2134,9 +2182,9 @@ ${ styles.html() }
2134\f[R]
2182\f[R]
2135.fi
2183.fi
2136.PP
2184.PP
2137−(If a partial is not found in the directory of the template, it will
2185+(If a partial is not found in the directory of the template and the
2186+template path is given as a relative path, it will also be sought in the
2138−also be sought in the \f[C]templates\f[R] subdirectory of the user data
2187+\f[C]templates\f[R] subdirectory of the user data directory.)
2139−directory.)
2140.PP
2188.PP
2141Partials may optionally be applied to variables using a colon:
2189Partials may optionally be applied to variables using a colon:
2142.IP
2190.IP
@@ -2543,10 +2591,9 @@ YAML metadata or with \f[C]-M classoption=fleqn\f[R].
2543.SS Variables for HTML slides
2591.SS Variables for HTML slides
2544.PP
2592.PP
2545These affect HTML output when [producing slide shows with pandoc].
2593These affect HTML output when [producing slide shows with pandoc].
2546−.PP
2594+.TP
2547−All reveal.js configuration options are available as variables.
2595+\f[B]\f[CB]institute\f[B]\f[R]
2548−To turn off boolean flags that default to true in reveal.js, use
2596+author affiliations: can be a list when there are multiple authors
2549−\f[C]0\f[R].
2550.TP
2597.TP
2551\f[B]\f[CB]revealjs-url\f[B]\f[R]
2598\f[B]\f[CB]revealjs-url\f[B]\f[R]
2552base URL for reveal.js documents (defaults to
2599base URL for reveal.js documents (defaults to
@@ -2565,6 +2612,10 @@ base URL for Slideous documents (defaults to \f[C]slideous\f[R])
2565\f[B]\f[CB]title-slide-attributes\f[B]\f[R]
2612\f[B]\f[CB]title-slide-attributes\f[B]\f[R]
2566additional attributes for the title slide of reveal.js slide shows.
2613additional attributes for the title slide of reveal.js slide shows.
2567See background in reveal.js and beamer for an example.
2614See background in reveal.js and beamer for an example.
2615+.PP
2616+All reveal.js configuration options are available as variables.
2617+To turn off boolean flags that default to true in reveal.js, use
2618+\f[C]0\f[R].
2568.SS Variables for Beamer slides
2619.SS Variables for Beamer slides
2569.PP
2620.PP
2570These variables change the appearance of PDF slides using
2621These variables change the appearance of PDF slides using
@@ -3928,6 +3979,9 @@ begin with a space.
3928\f[R]
3979\f[R]
3929.fi
3980.fi
3930.PP
3981.PP
3982+Inline formatting (such as emphasis) is allowed in the content, but not
3983+block-level formatting (such as block quotes or lists).
3984+.PP
3931This syntax is borrowed from reStructuredText.
3985This syntax is borrowed from reStructuredText.
3932.SS Lists
3986.SS Lists
3933.SS Bullet lists
3987.SS Bullet lists
@@ -4221,7 +4275,7 @@ Term 2
4221Note that space between items in a definition list is required.
4275Note that space between items in a definition list is required.
4222(A variant that loosens this requirement, but disallows \[lq]lazy\[rq]
4276(A variant that loosens this requirement, but disallows \[lq]lazy\[rq]
4223hard wrapping, can be activated with \f[C]compact_definition_lists\f[R]:
4277hard wrapping, can be activated with \f[C]compact_definition_lists\f[R]:
4224−see Non-pandoc extensions, below.)
4278+see Non-default extensions, below.)
4225.SS Numbered example lists
4279.SS Numbered example lists
4226.SS Extension: \f[C]example_lists\f[R]
4280.SS Extension: \f[C]example_lists\f[R]
4227.PP
4281.PP
@@ -4742,7 +4796,9 @@ All of the metadata will appear in a single block at the beginning of
4742the document.
4796the document.
4743.PP
4797.PP
4744Note that YAML escaping rules must be followed.
4798Note that YAML escaping rules must be followed.
4745−Thus, for example, if a title contains a colon, it must be quoted.
4799+Thus, for example, if a title contains a colon, it must be quoted, and
4800+if it contains a backslash escape, then it must be ensured that it is
4801+not treated as a YAML escape sequence.
4746The pipe character (\f[C]|\f[R]) can be used to begin an indented block
4802The pipe character (\f[C]|\f[R]) can be used to begin an indented block
4747that will be interpreted literally, without need for escaping.
4803that will be interpreted literally, without need for escaping.
4748This form is necessary when the field contains blank lines or
4804This form is necessary when the field contains blank lines or
@@ -4764,6 +4820,13 @@ abstract: |
4764\f[R]
4820\f[R]
4765.fi
4821.fi
4766.PP
4822.PP
4823+The literal block after the \f[C]|\f[R] must be indented relative to the
4824+line containing the \f[C]|\f[R].
4825+If it is not, the YAML will be invalid and pandoc will not interpret it
4826+as metadata.
4827+For an overview of the complex rules governing YAML, see the Wikipedia
4828+entry on YAML syntax.
4829+.PP
4767Template variables will be set automatically from the metadata.
4830Template variables will be set automatically from the metadata.
4768Thus, for example, in writing HTML, the variable \f[C]abstract\f[R] will
4831Thus, for example, in writing HTML, the variable \f[C]abstract\f[R] will
4769be set to the HTML equivalent of the Markdown in the \f[C]abstract\f[R]
4832be set to the HTML equivalent of the Markdown in the \f[C]abstract\f[R]
@@ -4829,6 +4892,21 @@ header-includes:
4829 \[ga]\[ga]\[ga]
4892 \[ga]\[ga]\[ga]
4830\f[R]
4893\f[R]
4831.fi
4894.fi
4895+.PP
4896+Note: the \f[C]yaml_metadata_block\f[R] extension works with
4897+\f[C]commonmark\f[R] as well as \f[C]markdown\f[R] (and it is enabled by
4898+default in \f[C]gfm\f[R] and \f[C]commonmark_x\f[R]).
4899+However, in these formats the following restrictions apply:
4900+.IP \[bu] 2
4901+The YAML metadata block must occur at the beginning of the document (and
4902+there can be only one).
4903+If multiple files are given as arguments to pandoc, only the first can
4904+be a YAML metadata block.
4905+.IP \[bu] 2
4906+The leaf nodes of the YAML structure are parsed in isolation from each
4907+other and from the rest of the document.
4908+So, for example, you can\[cq]t use a reference link in these contexts if
4909+the link definition is somewhere else in the document.
4832.SS Backslash escapes
4910.SS Backslash escapes
4833.SS Extension: \f[C]all_symbols_escapable\f[R]
4911.SS Extension: \f[C]all_symbols_escapable\f[R]
4834.PP
4912.PP
@@ -5048,7 +5126,9 @@ delimiters.
5048.PP
5126.PP
5049For display math, use \f[C]$$\f[R] delimiters.
5127For display math, use \f[C]$$\f[R] delimiters.
5050(In this case, the delimiters may be separated from the formula by
5128(In this case, the delimiters may be separated from the formula by
5051−whitespace.)
5129+whitespace.
5130+However, there can be no blank lines betwen the opening and closing
5131+\f[C]$$\f[R] delimiters.)
5052.PP
5132.PP
5053TeX math will be printed in all output formats.
5133TeX math will be printed in all output formats.
5054How it is rendered depends on the output format:
5134How it is rendered depends on the output format:
@@ -5707,27 +5787,70 @@ Inline and regular footnotes may be mixed freely.
5707.SS Citation syntax
5787.SS Citation syntax
5708.SS Extension: \f[C]citations\f[R]
5788.SS Extension: \f[C]citations\f[R]
5709.PP
5789.PP
5710−Markdown citations go inside square brackets and are separated by
5790+To cite a bibliographic item with an identifier foo, use the syntax
5711−semicolons.
5791+\f[C]\[at]foo\f[R].
5712−Each citation must have a key, composed of `\[at]' + the citation
5792+Normal citations should be included in square brackets, with semicolons
5713−identifier from the database, and may optionally have a prefix, a
5793+separating distinct items:
5714−locator, and a suffix.
5715−The citation key must begin with a letter, digit, or \f[C]_\f[R], and
5716−may contain alphanumerics, \f[C]_\f[R], and internal punctuation
5717−characters (\f[C]:.#$%&-+?<>\[ti]/\f[R]).
5718−Here are some examples:
5719.IP
5794.IP
5720.nf
5795.nf
5721\f[C]
5796\f[C]
5722−Blah blah [see \[at]doe99, pp. 33-35; also \[at]smith04, chap. 1].
5797+Blah blah [\[at]doe99; \[at]smith2000; \[at]smith2004].
5723−
5798+\f[R]
5724−Blah blah [\[at]doe99, pp. 33-35, 38-39 and *passim*].
5799+.fi
5800+.PP
5801+How this is rendered depends on the citation style.
5802+In an author-date style, it might render as
5803+.IP
5804+.nf
5805+\f[C]
5806+Blah blah (Doe 1999, Smith 2000, 2004).
5807+\f[R]
5808+.fi
5809+.PP
5810+In a footnote style, it might render as
5811+.IP
5812+.nf
5813+\f[C]
5814+Blah blah.[\[ha]1]
5725
5815
5726−Blah blah [\[at]smith04; \[at]doe99].
5816+[\[ha]1]: John Doe, \[dq]Frogs,\[dq] *Journal of Amphibians* 44 (1999);
5817+Susan Smith, \[dq]Flies,\[dq] *Journal of Insects* (2000);
5818+Susan Smith, \[dq]Bees,\[dq] *Journal of Insects* (2004).
5727\f[R]
5819\f[R]
5728.fi
5820.fi
5729.PP
5821.PP
5822+See the CSL user documentation for more information about CSL styles and
5823+how they affect rendering.
5824+.PP
5825+Unless a citation key start with a letter, digit, or \f[C]_\f[R], and
5826+contains only alphanumerics and internal punctuation characters
5827+(\f[C]:.#$%&-+?<>\[ti]/\f[R]), it must be surrounded by curly braces,
5828+which are not considered part of the key.
5829+In \f[C]\[at]Foo_bar.baz.\f[R], the key is \f[C]Foo_bar.baz\f[R].
5830+The final period is not \f[I]internal\f[R] punctuation, so it is not
5831+included in the key.
5832+In \f[C]\[at]{Foo_bar.baz.}\f[R], the key is \f[C]Foo_bar.baz.\f[R],
5833+including the final period.
5834+The curly braces are recommended if you use URLs as keys:
5835+\f[C][\[at]{https://example.com/bib?name=foobar&date=2000}, p. 33]\f[R].
5836+.PP
5837+Citation items may optionally include a prefix, a locator, and a suffix.
5838+In
5839+.IP
5840+.nf
5841+\f[C]
5842+Blah blah [see \[at]doe99, pp. 33-35 and *passim*; \[at]smith04, chap. 1].
5843+\f[R]
5844+.fi
5845+.PP
5846+The first item (\f[C]doe99\f[R]) has prefix \f[C]see\f[R], locator
5847+\f[C]pp. 33-35\f[R], and suffix \f[C]and *passim*\f[R].
5848+The second item (\f[C]smith04\f[R]) has locator \f[C]chap. 1\f[R] and no
5849+prefix or suffix.
5850+.PP
5851+Pandoc uses some heuristics to separate the locator from the rest of the
5852+subject.
5730−\f[C]pandoc\f[R] detects locator terms in the CSL locale files.
5853+It is sensitive to the locator terms defined in the CSL locale files.
5731Either abbreviated or unabbreviated forms are accepted.
5854Either abbreviated or unabbreviated forms are accepted.
5732In the \f[C]en-US\f[R] locale, locator terms can be written in either
5855In the \f[C]en-US\f[R] locale, locator terms can be written in either
5733singular or plural forms, as \f[C]book\f[R],
5856singular or plural forms, as \f[C]book\f[R],
@@ -5749,14 +5872,15 @@ singular or plural forms, as \f[C]book\f[R],
5749\f[C]\[sc]\f[R]/\f[C]\[sc]\[sc]\f[R].
5872\f[C]\[sc]\f[R]/\f[C]\[sc]\[sc]\f[R].
5750If no locator term is used, \[lq]page\[rq] is assumed.
5873If no locator term is used, \[lq]page\[rq] is assumed.
5751.PP
5874.PP
5752−\f[C]pandoc\f[R] will use heuristics to distinguish the locator from the
5875+In complex cases, you can force something to be treated as a locator by
5753−suffix.
5876+enclosing it in curly braces or prevent parsing the suffix as locator by
5754−In complex cases, the locator can be enclosed in curly braces:
5877+prepending curly braces:
5755.IP
5878.IP
5756.nf
5879.nf
5757\f[C]
5880\f[C]
5758[\[at]smith{ii, A, D-Z}, with a suffix]
5881[\[at]smith{ii, A, D-Z}, with a suffix]
5759[\[at]smith, {pp. iv, vi-xi, (xv)-(xvii)} with suffix here]
5882[\[at]smith, {pp. iv, vi-xi, (xv)-(xvii)} with suffix here]
5883+[\[at]smith{}, 99 years later]
5760\f[R]
5884\f[R]
5761.fi
5885.fi
5762.PP
5886.PP
@@ -5770,7 +5894,8 @@ Smith says blah [-\[at]smith04].
5770\f[R]
5894\f[R]
5771.fi
5895.fi
5772.PP
5896.PP
5773−You can also write an in-text citation, as follows:
5897+You can also write an author-in-text citation, by omitting the square
5898+brackets:
5774.IP
5899.IP
5775.nf
5900.nf
5776\f[C]
5901\f[C]
@@ -5779,13 +5904,64 @@ You can also write an in-text citation, as follows:
5779\[at]smith04 [p. 33] says blah.
5904\[at]smith04 [p. 33] says blah.
5780\f[R]
5905\f[R]
5781.fi
5906.fi
5907+.PP
5908+This will cause the author\[cq]s name to be rendered, followed by the
5909+bibliographical details.
5910+Use this form when you want to make the citation the subject of a
5911+sentence.
5912+.PP
5913+When you are using a note style, it is usually better to let citeproc
5914+create the footnotes from citations rather than writing an explicit
5915+note.
5916+If you do write an explicit note that contains a citation, note that
5917+normal citations will be put in parentheses, while author-in-text
5918+citations will not.
5919+For this reason, it is sometimes preferable to use the author-in-text
5920+style inside notes when using a note style.
5782−.SS Non-pandoc extensions
5921+.SS Non-default extensions
5783.PP
5922.PP
5784The following Markdown syntax extensions are not enabled by default in
5923The following Markdown syntax extensions are not enabled by default in
5785pandoc, but may be enabled by adding \f[C]+EXTENSION\f[R] to the format
5924pandoc, but may be enabled by adding \f[C]+EXTENSION\f[R] to the format
5786name, where \f[C]EXTENSION\f[R] is the name of the extension.
5925name, where \f[C]EXTENSION\f[R] is the name of the extension.
5787Thus, for example, \f[C]markdown+hard_line_breaks\f[R] is Markdown with
5926Thus, for example, \f[C]markdown+hard_line_breaks\f[R] is Markdown with
5788hard line breaks.
5927hard line breaks.
5928+.SS Extension: \f[C]rebase_relative_paths\f[R]
5929+.PP
5930+Rewrite relative paths for Markdown links and images, depending on the
5931+path of the file containing the link or image link.
5932+For each link or image, pandoc will compute the directory of the
5933+containing file, relative to the working directory, and prepend the
5934+resulting path to the link or image path.
5935+.PP
5936+The use of this extension is best understood by example.
5937+Suppose you have a a subdirectory for each chapter of a book,
5938+\f[C]chap1\f[R], \f[C]chap2\f[R], \f[C]chap3\f[R].
5939+Each contains a file \f[C]text.md\f[R] and a number of images used in
5940+the chapter.
5941+You would like to have \f[C]![image](spider.jpg)\f[R] in
5942+\f[C]chap1/text.md\f[R] refer to \f[C]chap1/spider.jpg\f[R] and
5943+\f[C]![image](spider.jpg)\f[R] in \f[C]chap2/text.md\f[R] refer to
5944+\f[C]chap2/spider.jpg\f[R].
5945+To do this, use
5946+.IP
5947+.nf
5948+\f[C]
5949+pandoc chap*/*.md -f markdown+rebase_relative_paths
5950+\f[R]
5951+.fi
5952+.PP
5953+Without this extension, you would have to use
5954+\f[C]![image](chap1/spider.jpg)\f[R] in \f[C]chap1/text.md\f[R] and
5955+\f[C]![image](chap2/spider.jpg)\f[R] in \f[C]chap2/text.md\f[R].
5956+Links with relative paths will be rewritten in the same way as images.
5957+.PP
5958+Absolute paths and URLs are not changed.
5959+Neither are empty paths or paths consisting entirely of a fragment,
5960+e.g., \f[C]#foo\f[R].
5961+.PP
5962+Note that relative paths in reference links and images will be rewritten
5963+relative to the file containing the link reference definition, not the
5964+file containing the reference link or image itself, if these differ.
5789.SS Extension: \f[C]attributes\f[R]
5965.SS Extension: \f[C]attributes\f[R]
5790.PP
5966.PP
5791Allows attributes to be attached to any inline or block-level element.
5967Allows attributes to be attached to any inline or block-level element.
@@ -6415,9 +6591,29 @@ entries (for author-date and numerical styles only).
6415.TP
6591.TP
6416\f[B]\f[CB]lang\f[B]\f[R]
6592\f[B]\f[CB]lang\f[B]\f[R]
6417The \f[C]lang\f[R] field will affect how the style is localized, for
6593The \f[C]lang\f[R] field will affect how the style is localized, for
6418−example in the translation of labels and the use of quotation marks.
6594+example in the translation of labels, the use of quotation marks, and
6595+the way items are sorted.
6419(For backwards compatibility, \f[C]locale\f[R] may be used instead of
6596(For backwards compatibility, \f[C]locale\f[R] may be used instead of
6420\f[C]lang\f[R], but this use is deprecated.)
6597\f[C]lang\f[R], but this use is deprecated.)
6598+.RS
6599+.PP
6600+A BCP 47 language tag is expected: for example, \f[C]en\f[R],
6601+\f[C]de\f[R], \f[C]en-US\f[R], \f[C]fr-CA\f[R], \f[C]ug-Cyrl\f[R].
6602+The unicode extension syntax (after \f[C]-u-\f[R]) may be used to
6603+specify options for collation (sorting) more precisely.
6604+Here are some examples:
6605+.IP \[bu] 2
6606+\f[C]zh-u-co-pinyin\f[R] \[en] Chinese with the Pinyin collation.
6607+.IP \[bu] 2
6608+\f[C]es-u-co-trad\f[R] \[en] Spanish with the traditional collation
6609+(with \f[C]Ch\f[R] sorting after \f[C]C\f[R]).
6610+.IP \[bu] 2
6611+\f[C]fr-u-kb\f[R] \[en] French with \[lq]backwards\[rq] accent sorting
6612+(with \f[C]cot\['e]\f[R] sorting after \f[C]c\[^o]te\f[R]).
6613+.IP \[bu] 2
6614+\f[C]en-US-u-kf-upper\f[R] \[en] English with uppercase letters sorting
6615+before lower (default is lower before upper).
6616+.RE
6421.TP
6617.TP
6422\f[B]\f[CB]notes-after-punctuation\f[B]\f[R]
6618\f[B]\f[CB]notes-after-punctuation\f[B]\f[R]
6423If true (the default), pandoc will put footnote citations after
6619If true (the default), pandoc will put footnote citations after
@@ -6538,6 +6734,11 @@ A heading at the slide level always starts a new slide.
6538.IP \[bu] 2
6734.IP \[bu] 2
6539Headings \f[I]below\f[R] the slide level in the hierarchy create
6735Headings \f[I]below\f[R] the slide level in the hierarchy create
6540headings \f[I]within\f[R] a slide.
6736headings \f[I]within\f[R] a slide.
6737+(In beamer, a \[lq]block\[rq] will be created.
6738+If the heading has the class \f[C]example\f[R], an
6739+\f[C]exampleblock\f[R] environment will be used; if it has the class
6740+\f[C]alert\f[R], an \f[C]alertblock\f[R] will be used; otherwise a
6741+regular \f[C]block\f[R] will be used.)
6541.IP \[bu] 2
6742.IP \[bu] 2
6542Headings \f[I]above\f[R] the slide level in the hierarchy create
6743Headings \f[I]above\f[R] the slide level in the hierarchy create
6543\[lq]title slides,\[rq] which just contain the section title and help to
6744\[lq]title slides,\[rq] which just contain the section title and help to
@@ -6938,6 +7139,16 @@ A string value.
6938\f[B]\f[CB]rights\f[B]\f[R]
7139\f[B]\f[CB]rights\f[B]\f[R]
6939A string value.
7140A string value.
6940.TP
7141.TP
7142+\f[B]\f[CB]belongs-to-collection\f[B]\f[R]
7143+A string value.
7144+identifies the name of a collection to which the EPUB Publication
7145+belongs.
7146+.TP
7147+\f[B]\f[CB]group-position\f[B]\f[R]
7148+The \f[C]group-position\f[R] field indicates the numeric position in
7149+which the EPUB Publication belongs relative to other works belonging to
7150+the same \f[C]belongs-to-collection\f[R] field.
7151+.TP
6941\f[B]\f[CB]cover-image\f[B]\f[R]
7152\f[B]\f[CB]cover-image\f[B]\f[R]
6942A string value (path to cover image).
7153A string value (path to cover image).
6943.TP
7154.TP
@@ -7453,6 +7664,20 @@ need to specify a template manually using \f[C]--template\f[R] or add a
7453new default template with the name
7664new default template with the name
7454\f[C]default.NAME_OF_CUSTOM_WRITER.lua\f[R] to the \f[C]templates\f[R]
7665\f[C]default.NAME_OF_CUSTOM_WRITER.lua\f[R] to the \f[C]templates\f[R]
7455subdirectory of your user data directory (see Templates).
7666subdirectory of your user data directory (see Templates).
7667+.SH REPRODUCIBLE BUILDS
7668+.PP
7669+Some of the document formats pandoc targets (such as EPUB, docx, and
7670+ODT) include build timestamps in the generated document.
7671+That means that the files generated on successive builds will differ,
7672+even if the source does not.
7673+To avoid this, set the \f[C]SOURCE_DATE_EPOCH\f[R] environment variable,
7674+and the timestamp will be taken from it instead of the current time.
7675+\f[C]SOURCE_DATE_EPOCH\f[R] should contain an integer unix timestamp
7676+(specifying the number of second since midnight UTC January 1, 1970).
7677+.PP
7678+Some document formats also include a unique identifier.
7679+For EPUB, this can be set explicitly by setting the \f[C]identifier\f[R]
7680+metadata field (see EPUB Metadata, above).
7456.SH A NOTE ON SECURITY
7681.SH A NOTE ON SECURITY
7457.PP
7682.PP
7458If you use pandoc to convert user-contributed content in a web
7683If you use pandoc to convert user-contributed content in a web
@@ -7483,7 +7708,7 @@ The HTML generated by pandoc is not guaranteed to be safe.
7483If \f[C]raw_html\f[R] is enabled for the Markdown input, users can
7708If \f[C]raw_html\f[R] is enabled for the Markdown input, users can
7484inject arbitrary HTML.
7709inject arbitrary HTML.
7485Even if \f[C]raw_html\f[R] is disabled, users can include dangerous
7710Even if \f[C]raw_html\f[R] is disabled, users can include dangerous
7486−content in attributes for headings, spans, and code blocks.
7711+content in URLs and attributes.
7487To be safe, you should run all the generated HTML through an HTML
7712To be safe, you should run all the generated HTML through an HTML
7488sanitizer.
7713sanitizer.
7489.SH AUTHORS
7714.SH AUTHORS
@@ -7495,4 +7720,4 @@ This software carries no warranty of any kind.
7495of contributors, see the file AUTHORS.md in the pandoc source code.
7720of contributors, see the file AUTHORS.md in the pandoc source code.
7496.PP
7721.PP
7497The Pandoc source code and all documentation may be downloaded
7722The Pandoc source code and all documentation may be downloaded
7498−from <http://pandoc.org>.
7723+from <https://pandoc.org>.
man/pandoc.1.after+1−1
@@ -1,3 +1,3 @@
1.PP
1.PP
2The Pandoc source code and all documentation may be downloaded
2The Pandoc source code and all documentation may be downloaded
3−from <http://pandoc.org>.
3+from <https://pandoc.org>.
pandoc.cabal+100−158
@@ -1,6 +1,6 @@
1cabal-version: 2.2
1cabal-version: 2.2
2name: pandoc
2name: pandoc
3−version: 2.11.4
3+version: 2.14.0.3
4build-type: Simple
4build-type: Simple
5license: GPL-2.0-or-later
5license: GPL-2.0-or-later
6license-file: COPYING.md
6license-file: COPYING.md
@@ -12,7 +12,7 @@ stability: alpha
12homepage: https://pandoc.org
12homepage: https://pandoc.org
13category: Text
13category: Text
14tested-with: GHC == 8.2.2, GHC == 8.4.4, GHC == 8.6.5,
14tested-with: GHC == 8.2.2, GHC == 8.4.4, GHC == 8.6.5,
15− GHC == 8.8.4, GHC == 8.10.2
15+ GHC == 8.8.4, GHC == 8.10.2, GHC == 9.0.1
16synopsis: Conversion between markup formats
16synopsis: Conversion between markup formats
17description: Pandoc is a Haskell library for converting from one markup
17description: Pandoc is a Haskell library for converting from one markup
18 format to another, and a command-line tool that uses
18 format to another, and a command-line tool that uses
@@ -56,6 +56,8 @@ data-files:
56 data/templates/default.icml
56 data/templates/default.icml
57 data/templates/default.opml
57 data/templates/default.opml
58 data/templates/default.latex
58 data/templates/default.latex
59+ data/templates/default.bibtex
60+ data/templates/default.biblatex
59 data/templates/default.context
61 data/templates/default.context
60 data/templates/default.texinfo
62 data/templates/default.texinfo
61 data/templates/default.jira
63 data/templates/default.jira
@@ -206,12 +208,16 @@ extra-source-files:
206 test/command/*.md
208 test/command/*.md
207 test/command/*.csl
209 test/command/*.csl
208 test/command/biblio.bib
210 test/command/biblio.bib
209− test/command/biblatex-examples.bib
211+ test/command/averroes.bib
210 test/command/A.txt
212 test/command/A.txt
211 test/command/B.txt
213 test/command/B.txt
212 test/command/C.txt
214 test/command/C.txt
213 test/command/D.txt
215 test/command/D.txt
214 test/command/01.csv
216 test/command/01.csv
217+ test/command/chap1/spider.png
218+ test/command/chap2/spider.png
219+ test/command/chap1/text.md
220+ test/command/chap2/text.md
215 test/command/defaults1.yaml
221 test/command/defaults1.yaml
216 test/command/defaults2.yaml
222 test/command/defaults2.yaml
217 test/command/defaults3.yaml
223 test/command/defaults3.yaml
@@ -394,103 +400,116 @@ flag trypandoc
394 Description: Build trypandoc cgi executable.
400 Description: Build trypandoc cgi executable.
395 Default: False
401 Default: False
396
402
403+common common-options
404+ default-language: Haskell2010
405+ build-depends: base >= 4.9 && < 5
406+ ghc-options: -Wall -fno-warn-unused-do-bind
407+ -Wincomplete-record-updates
408+ -Wnoncanonical-monad-instances
409+
410+ if impl(ghc < 8.4)
411+ hs-source-dirs: prelude
412+ other-modules: Prelude
413+ build-depends: base-compat >= 0.9
414+ other-extensions: NoImplicitPrelude
415+
416+ if os(windows)
417+ cpp-options: -D_WINDOWS
418+
419+ -- Later:
420+ -- -Wpartial-fields (currently used in Powerpoint writer)
421+ -- -Wmissing-export-lists (currently some Odt modules violate this)
422+ -- -Wredundant-constraints (problematic if we want to support older base)
423+ if impl(ghc >= 8.2)
424+ ghc-options: -Wcpp-undef
425+ if impl(ghc >= 8.4)
426+ ghc-options: -Wincomplete-uni-patterns
427+ -Widentities
428+ -fhide-source-paths
429+
430+common common-executable
431+ import: common-options
432+ build-depends: pandoc
433+ ghc-options: -rtsopts -with-rtsopts=-A8m -threaded
434+
435+
397library
436library
437+ import: common-options
398− build-depends: base >= 4.9 && < 5,
438+ build-depends: Glob >= 0.7 && < 0.11,
399− Glob >= 0.7 && < 0.11,
400 HTTP >= 4000.0.5 && < 4000.4,
439 HTTP >= 4000.0.5 && < 4000.4,
401 HsYAML >= 0.2 && < 0.3,
440 HsYAML >= 0.2 && < 0.3,
402 JuicyPixels >= 3.1.6.1 && < 3.4,
441 JuicyPixels >= 3.1.6.1 && < 3.4,
403 SHA >= 1.6 && < 1.7,
442 SHA >= 1.6 && < 1.7,
404 aeson >= 0.7 && < 1.6,
443 aeson >= 0.7 && < 1.6,
405 aeson-pretty >= 0.8.5 && < 0.9,
444 aeson-pretty >= 0.8.5 && < 0.9,
445+ array >= 0.5 && < 0.6,
406− attoparsec >= 0.12 && < 0.14,
446+ attoparsec >= 0.12 && < 0.15,
407− base64-bytestring >= 0.1 && < 1.2,
447+ base64-bytestring >= 0.1 && < 1.3,
408 binary >= 0.7 && < 0.11,
448 binary >= 0.7 && < 0.11,
409 blaze-html >= 0.9 && < 0.10,
449 blaze-html >= 0.9 && < 0.10,
410 blaze-markup >= 0.8 && < 0.9,
450 blaze-markup >= 0.8 && < 0.9,
411 bytestring >= 0.9 && < 0.12,
451 bytestring >= 0.9 && < 0.12,
412 case-insensitive >= 1.2 && < 1.3,
452 case-insensitive >= 1.2 && < 1.3,
413− citeproc >= 0.3.0.5 && < 0.4,
453+ citeproc >= 0.4.0.1 && < 0.4.1,
414− commonmark >= 0.1.1.3 && < 0.2,
454+ commonmark >= 0.2.1 && < 0.3,
415− commonmark-extensions >= 0.2.0.4 && < 0.3,
455+ commonmark-extensions >= 0.2.1.2 && < 0.3,
416− commonmark-pandoc >= 0.2 && < 0.3,
456+ commonmark-pandoc >= 0.2.1.1 && < 0.3,
417 connection >= 0.3.1,
457 connection >= 0.3.1,
418 containers >= 0.4.2.1 && < 0.7,
458 containers >= 0.4.2.1 && < 0.7,
419 data-default >= 0.4 && < 0.8,
459 data-default >= 0.4 && < 0.8,
420 deepseq >= 1.3 && < 1.5,
460 deepseq >= 1.3 && < 1.5,
421 directory >= 1.2.3 && < 1.4,
461 directory >= 1.2.3 && < 1.4,
422− doclayout >= 0.3 && < 0.4,
462+ doclayout >= 0.3.0.1 && < 0.4,
423 doctemplates >= 0.9 && < 0.10,
463 doctemplates >= 0.9 && < 0.10,
424 emojis >= 0.1 && < 0.2,
464 emojis >= 0.1 && < 0.2,
425 exceptions >= 0.8 && < 0.11,
465 exceptions >= 0.8 && < 0.11,
426 file-embed >= 0.0 && < 0.1,
466 file-embed >= 0.0 && < 0.1,
427 filepath >= 1.1 && < 1.5,
467 filepath >= 1.1 && < 1.5,
428− haddock-library >= 1.8 && < 1.10,
468+ haddock-library >= 1.10 && < 1.11,
429 hslua >= 1.1 && < 1.4,
469 hslua >= 1.1 && < 1.4,
470+ hslua-module-path >= 0.1.0 && < 0.2.0,
430 hslua-module-system >= 0.2 && < 0.3,
471 hslua-module-system >= 0.2 && < 0.3,
431 hslua-module-text >= 0.2.1 && < 0.4,
472 hslua-module-text >= 0.2.1 && < 0.4,
432 http-client >= 0.4.30 && < 0.8,
473 http-client >= 0.4.30 && < 0.8,
433 http-client-tls >= 0.2.4 && < 0.4,
474 http-client-tls >= 0.2.4 && < 0.4,
434 http-types >= 0.8 && < 0.13,
475 http-types >= 0.8 && < 0.13,
435 ipynb >= 0.1 && < 0.2,
476 ipynb >= 0.1 && < 0.2,
436− jira-wiki-markup >= 1.3.2 && < 1.4,
477+ jira-wiki-markup >= 1.4 && < 1.5,
437 mtl >= 2.2 && < 2.3,
478 mtl >= 2.2 && < 2.3,
438 network >= 2.6,
479 network >= 2.6,
439− network-uri >= 2.6 && < 2.7,
480+ network-uri >= 2.6 && < 2.8,
440 pandoc-types >= 1.22 && < 1.23,
481 pandoc-types >= 1.22 && < 1.23,
441 parsec >= 3.1 && < 3.2,
482 parsec >= 3.1 && < 3.2,
442 process >= 1.2.3 && < 1.7,
483 process >= 1.2.3 && < 1.7,
443− random >= 1 && < 1.2,
484+ random >= 1 && < 1.3,
444− safe >= 0.3 && < 0.4,
485+ safe >= 0.3.18 && < 0.4,
445 scientific >= 0.3 && < 0.4,
486 scientific >= 0.3 && < 0.4,
446− skylighting >= 0.10.2 && < 0.11,
487+ skylighting >= 0.10.5.2 && < 0.10.6,
447− skylighting-core >= 0.10.2 && < 0.11,
488+ skylighting-core >= 0.10.5.2 && < 0.10.6,
448 split >= 0.2 && < 0.3,
489 split >= 0.2 && < 0.3,
449 syb >= 0.1 && < 0.8,
490 syb >= 0.1 && < 0.8,
450 tagsoup >= 0.14.6 && < 0.15,
491 tagsoup >= 0.14.6 && < 0.15,
451 temporary >= 1.1 && < 1.4,
492 temporary >= 1.1 && < 1.4,
452− texmath >= 0.12.1 && < 0.13,
493+ texmath >= 0.12.3 && < 0.12.4,
453 text >= 1.1.1.0 && < 1.3,
494 text >= 1.1.1.0 && < 1.3,
454 text-conversions >= 0.3 && < 0.4,
495 text-conversions >= 0.3 && < 0.4,
455− time >= 1.5 && < 1.10,
496+ time >= 1.5 && < 1.12,
456 unicode-transforms >= 0.3 && < 0.4,
497 unicode-transforms >= 0.3 && < 0.4,
457 unordered-containers >= 0.2 && < 0.3,
498 unordered-containers >= 0.2 && < 0.3,
458 xml >= 1.3.12 && < 1.4,
499 xml >= 1.3.12 && < 1.4,
500+ xml-conduit >= 1.9.1.1 && < 1.10,
501+ unicode-collation >= 0.1.1 && < 0.2,
459 zip-archive >= 0.2.3.4 && < 0.5,
502 zip-archive >= 0.2.3.4 && < 0.5,
460 zlib >= 0.5 && < 0.7
503 zlib >= 0.5 && < 0.7
461 if os(windows) && arch(i386)
504 if os(windows) && arch(i386)
462 build-depends: basement >= 0.0.10,
505 build-depends: basement >= 0.0.10,
463 foundation >= 0.0.23
506 foundation >= 0.0.23
464 -- basement 0.0.9 won't build on 32-bit windows.
507 -- basement 0.0.9 won't build on 32-bit windows.
465− if impl(ghc < 8.4)
466− hs-source-dirs: prelude
467− other-modules: Prelude
468− build-depends: base-compat >= 0.9
469− if os(windows)
508+ if !os(windows)
470− cpp-options: -D_WINDOWS
471− else
472 build-depends: unix >= 2.4 && < 2.8
509 build-depends: unix >= 2.4 && < 2.8
473 if flag(embed_data_files)
510 if flag(embed_data_files)
474 cpp-options: -DEMBED_DATA_FILES
511 cpp-options: -DEMBED_DATA_FILES
475 other-modules: Text.Pandoc.Data
512 other-modules: Text.Pandoc.Data
476− if os(windows)
477− cpp-options: -D_WINDOWS
478− ghc-options: -Wall -fno-warn-unused-do-bind
479− -Wincomplete-record-updates
480− -Wnoncanonical-monad-instances
481− -- Later:
482− -- -Wpartial-fields (currently used in Powerpoint writer)
483− -- -Wmissing-export-lists (currently some Odt modules violate this)
484− -- -Wredundant-constraints (problematic if we want to support older base)
485− if impl(ghc >= 8.2)
486− ghc-options: -Wcpp-undef
487− if impl(ghc >= 8.4)
488− ghc-options: -Wincomplete-uni-patterns
489− -Widentities
490− -fhide-source-paths
491−
492− default-language: Haskell2010
493− other-extensions: NoImplicitPrelude
494 hs-source-dirs: src
513 hs-source-dirs: src
495
514
496 exposed-modules: Text.Pandoc,
515 exposed-modules: Text.Pandoc,
@@ -498,13 +517,13 @@ library
498 Text.Pandoc.Options,
517 Text.Pandoc.Options,
499 Text.Pandoc.Extensions,
518 Text.Pandoc.Extensions,
500 Text.Pandoc.Shared,
519 Text.Pandoc.Shared,
520+ Text.Pandoc.Sources,
501 Text.Pandoc.MediaBag,
521 Text.Pandoc.MediaBag,
502 Text.Pandoc.Error,
522 Text.Pandoc.Error,
503 Text.Pandoc.Filter,
523 Text.Pandoc.Filter,
504 Text.Pandoc.Readers,
524 Text.Pandoc.Readers,
505 Text.Pandoc.Readers.HTML,
525 Text.Pandoc.Readers.HTML,
506 Text.Pandoc.Readers.LaTeX,
526 Text.Pandoc.Readers.LaTeX,
507− Text.Pandoc.Readers.LaTeX.Types,
508 Text.Pandoc.Readers.Markdown,
527 Text.Pandoc.Readers.Markdown,
509 Text.Pandoc.Readers.CommonMark,
528 Text.Pandoc.Readers.CommonMark,
510 Text.Pandoc.Readers.Creole,
529 Text.Pandoc.Readers.Creole,
@@ -588,7 +607,6 @@ library
588 Text.Pandoc.Asciify,
607 Text.Pandoc.Asciify,
589 Text.Pandoc.Emoji,
608 Text.Pandoc.Emoji,
590 Text.Pandoc.ImageSize,
609 Text.Pandoc.ImageSize,
591− Text.Pandoc.BCP47,
592 Text.Pandoc.Class,
610 Text.Pandoc.Class,
593 Text.Pandoc.Citeproc
611 Text.Pandoc.Citeproc
594 other-modules: Text.Pandoc.App.CommandLineOptions,
612 other-modules: Text.Pandoc.App.CommandLineOptions,
@@ -613,9 +631,15 @@ library
613 Text.Pandoc.Readers.HTML.Table,
631 Text.Pandoc.Readers.HTML.Table,
614 Text.Pandoc.Readers.HTML.TagCategories,
632 Text.Pandoc.Readers.HTML.TagCategories,
615 Text.Pandoc.Readers.HTML.Types,
633 Text.Pandoc.Readers.HTML.Types,
616− Text.Pandoc.Readers.LaTeX.Parsing,
634+ Text.Pandoc.Readers.LaTeX.Inline,
635+ Text.Pandoc.Readers.LaTeX.Citation,
617 Text.Pandoc.Readers.LaTeX.Lang,
636 Text.Pandoc.Readers.LaTeX.Lang,
637+ Text.Pandoc.Readers.LaTeX.Macro,
638+ Text.Pandoc.Readers.LaTeX.Math,
639+ Text.Pandoc.Readers.LaTeX.Parsing,
618 Text.Pandoc.Readers.LaTeX.SIunitx,
640 Text.Pandoc.Readers.LaTeX.SIunitx,
641+ Text.Pandoc.Readers.LaTeX.Table,
642+ Text.Pandoc.Readers.LaTeX.Types,
619 Text.Pandoc.Readers.Odt.Base,
643 Text.Pandoc.Readers.Odt.Base,
620 Text.Pandoc.Readers.Odt.Namespaces,
644 Text.Pandoc.Readers.Odt.Namespaces,
621 Text.Pandoc.Readers.Odt.StyleReader,
645 Text.Pandoc.Readers.Odt.StyleReader,
@@ -639,13 +663,21 @@ library
639 Text.Pandoc.Readers.Metadata,
663 Text.Pandoc.Readers.Metadata,
640 Text.Pandoc.Readers.Roff,
664 Text.Pandoc.Readers.Roff,
641 Text.Pandoc.Writers.Docx.StyleMap,
665 Text.Pandoc.Writers.Docx.StyleMap,
666+ Text.Pandoc.Writers.Docx.Table,
667+ Text.Pandoc.Writers.Docx.Types,
668+ Text.Pandoc.Writers.GridTable
642 Text.Pandoc.Writers.JATS.References,
669 Text.Pandoc.Writers.JATS.References,
643 Text.Pandoc.Writers.JATS.Table,
670 Text.Pandoc.Writers.JATS.Table,
644 Text.Pandoc.Writers.JATS.Types,
671 Text.Pandoc.Writers.JATS.Types,
645 Text.Pandoc.Writers.LaTeX.Caption,
672 Text.Pandoc.Writers.LaTeX.Caption,
646 Text.Pandoc.Writers.LaTeX.Notes,
673 Text.Pandoc.Writers.LaTeX.Notes,
647 Text.Pandoc.Writers.LaTeX.Table,
674 Text.Pandoc.Writers.LaTeX.Table,
675+ Text.Pandoc.Writers.LaTeX.Lang,
648 Text.Pandoc.Writers.LaTeX.Types,
676 Text.Pandoc.Writers.LaTeX.Types,
677+ Text.Pandoc.Writers.LaTeX.Citation,
678+ Text.Pandoc.Writers.LaTeX.Util,
679+ Text.Pandoc.Writers.Markdown.Types,
680+ Text.Pandoc.Writers.Markdown.Inline,
649 Text.Pandoc.Writers.Roff,
681 Text.Pandoc.Writers.Roff,
650 Text.Pandoc.Writers.Powerpoint.Presentation,
682 Text.Pandoc.Writers.Powerpoint.Presentation,
651 Text.Pandoc.Writers.Powerpoint.Output,
683 Text.Pandoc.Writers.Powerpoint.Output,
@@ -673,6 +705,10 @@ library
673 Text.Pandoc.Lua.PandocLua,
705 Text.Pandoc.Lua.PandocLua,
674 Text.Pandoc.Lua.Util,
706 Text.Pandoc.Lua.Util,
675 Text.Pandoc.Lua.Walk,
707 Text.Pandoc.Lua.Walk,
708+ Text.Pandoc.XML.Light,
709+ Text.Pandoc.XML.Light.Types,
710+ Text.Pandoc.XML.Light.Proc,
711+ Text.Pandoc.XML.Light.Output,
676 Text.Pandoc.CSS,
712 Text.Pandoc.CSS,
677 Text.Pandoc.CSV,
713 Text.Pandoc.CSV,
678 Text.Pandoc.RoffChar,
714 Text.Pandoc.RoffChar,
@@ -691,116 +727,50 @@ library
691 buildable: True
727 buildable: True
692
728
693executable pandoc
729executable pandoc
694− build-depends: pandoc, base >= 4.8 && < 5
730+ import: common-executable
695− if impl(ghc < 8.4)
696− hs-source-dirs: prelude
697− other-modules: Prelude
698− build-depends: base-compat >= 0.9
699− default-language: Haskell2010
700− other-extensions: NoImplicitPrelude
701 hs-source-dirs: app
731 hs-source-dirs: app
702 main-is: pandoc.hs
732 main-is: pandoc.hs
703 buildable: True
733 buildable: True
704 other-modules: Paths_pandoc
734 other-modules: Paths_pandoc
705− ghc-options: -rtsopts -with-rtsopts=-K16m -threaded
706− -Wall -fno-warn-unused-do-bind
707− -Wincomplete-record-updates
708− -Wnoncanonical-monad-instances
709− if impl(ghc >= 8.2)
710− ghc-options: -Wcpp-undef
711− if impl(ghc >= 8.4)
712− ghc-options: -Wincomplete-uni-patterns
713− -Widentities
714− -fhide-source-paths
715
735
716executable trypandoc
736executable trypandoc
737+ import: common-executable
717 main-is: trypandoc.hs
738 main-is: trypandoc.hs
718 hs-source-dirs: trypandoc
739 hs-source-dirs: trypandoc
719− default-language: Haskell2010
720− other-extensions: NoImplicitPrelude
721 if flag(trypandoc)
740 if flag(trypandoc)
722− build-depends: base, aeson, pandoc,
741+ build-depends: aeson, http-types, text, wai >= 0.3, wai-extra >= 3.0.24
723− text, wai-extra, wai >= 0.3, http-types
724 buildable: True
742 buildable: True
725 else
743 else
726 buildable: False
744 buildable: False
727− if impl(ghc < 8.4)
728− hs-source-dirs: prelude
729− other-modules: Prelude
730− build-depends: base-compat >= 0.9
731− ghc-options: -rtsopts -with-rtsopts=-K16m -threaded
732− -Wall -fno-warn-unused-do-bind
733− -Wincomplete-record-updates
734− -Wnoncanonical-monad-instances
735− if impl(ghc >= 8.2)
736− ghc-options: -Wcpp-undef
737− if impl(ghc >= 8.4)
738− ghc-options: -Wincomplete-uni-patterns
739− -Widentities
740− -fhide-source-paths
741−
742−benchmark weigh-pandoc
743− type: exitcode-stdio-1.0
744− main-is: weigh-pandoc.hs
745− hs-source-dirs: benchmark
746− build-depends: pandoc,
747− base >= 4.8 && < 5,
748− mtl >= 2.2 && < 2.3,
749− text,
750− weigh >= 0.0 && < 0.1,
751− if impl(ghc < 8.4)
752− hs-source-dirs: prelude
753− other-modules: Prelude
754− build-depends: base-compat >= 0.9
755− default-language: Haskell2010
756− other-extensions: NoImplicitPrelude
757− ghc-options: -rtsopts -with-rtsopts=-K16m -threaded
758− -Wall -fno-warn-unused-do-bind
759− -Wincomplete-record-updates
760− -Wnoncanonical-monad-instances
761− if impl(ghc >= 8.2)
762− ghc-options: -Wcpp-undef
763− if impl(ghc >= 8.4)
764− ghc-options: -Wincomplete-uni-patterns
765− -Widentities
766− -fhide-source-paths
767−
768
745
769test-suite test-pandoc
746test-suite test-pandoc
747+ import: common-executable
770 type: exitcode-stdio-1.0
748 type: exitcode-stdio-1.0
771 main-is: test-pandoc.hs
749 main-is: test-pandoc.hs
772 hs-source-dirs: test
750 hs-source-dirs: test
773 build-depends: pandoc,
751 build-depends: pandoc,
774− base >= 4.8 && < 5,
775 Diff >= 0.2 && < 0.5,
752 Diff >= 0.2 && < 0.5,
776 Glob >= 0.7 && < 0.11,
753 Glob >= 0.7 && < 0.11,
777 QuickCheck >= 2.4 && < 2.15,
754 QuickCheck >= 2.4 && < 2.15,
778− base64-bytestring >= 0.1 && < 1.2,
779 bytestring >= 0.9 && < 0.12,
755 bytestring >= 0.9 && < 0.12,
780 containers >= 0.4.2.1 && < 0.7,
756 containers >= 0.4.2.1 && < 0.7,
781 directory >= 1.2.3 && < 1.4,
757 directory >= 1.2.3 && < 1.4,
782 doctemplates >= 0.9 && < 0.10,
758 doctemplates >= 0.9 && < 0.10,
783 exceptions >= 0.8 && < 0.11,
759 exceptions >= 0.8 && < 0.11,
784− executable-path >= 0.0 && < 0.1,
785 filepath >= 1.1 && < 1.5,
760 filepath >= 1.1 && < 1.5,
786 hslua >= 1.1 && < 1.4,
761 hslua >= 1.1 && < 1.4,
787 mtl >= 2.2 && < 2.3,
762 mtl >= 2.2 && < 2.3,
788 pandoc-types >= 1.22 && < 1.23,
763 pandoc-types >= 1.22 && < 1.23,
789 process >= 1.2.3 && < 1.7,
764 process >= 1.2.3 && < 1.7,
790− tasty >= 0.11 && < 1.4,
765+ tasty >= 0.11 && < 1.5,
791 tasty-golden >= 2.3 && < 2.4,
766 tasty-golden >= 2.3 && < 2.4,
792 tasty-hunit >= 0.9 && < 0.11,
767 tasty-hunit >= 0.9 && < 0.11,
793 tasty-lua >= 0.2 && < 0.3,
768 tasty-lua >= 0.2 && < 0.3,
794 tasty-quickcheck >= 0.8 && < 0.11,
769 tasty-quickcheck >= 0.8 && < 0.11,
795− temporary >= 1.1 && < 1.4,
796− text >= 1.1.1.0 && < 1.3,
770+ text >= 1.1.1.0 && < 1.3,
797− time >= 1.5 && < 1.10,
771+ time >= 1.5 && < 1.12,
798 xml >= 1.3.12 && < 1.4,
772 xml >= 1.3.12 && < 1.4,
799 zip-archive >= 0.2.3.4 && < 0.5
773 zip-archive >= 0.2.3.4 && < 0.5
800− if impl(ghc < 8.4)
801− hs-source-dirs: prelude
802− other-modules: Prelude
803− build-depends: base-compat >= 0.9
804 other-modules: Tests.Old
774 other-modules: Tests.Old
805 Tests.Command
775 Tests.Command
806 Tests.Helpers
776 Tests.Helpers
@@ -856,47 +826,19 @@ test-suite test-pandoc
856 Tests.Writers.OOXML
826 Tests.Writers.OOXML
857 Tests.Writers.Ms
827 Tests.Writers.Ms
858 Tests.Writers.AnnotatedTable
828 Tests.Writers.AnnotatedTable
859− if os(windows)
860− cpp-options: -D_WINDOWS
861− default-language: Haskell2010
862− other-extensions: NoImplicitPrelude
863− ghc-options: -rtsopts -with-rtsopts=-K16m -threaded
864− -Wall -fno-warn-unused-do-bind
865− -Wincomplete-record-updates
866− -Wnoncanonical-monad-instances
867− if impl(ghc >= 8.2)
868− ghc-options: -Wcpp-undef
869− if impl(ghc >= 8.4)
870− ghc-options: -Wincomplete-uni-patterns
871− -Widentities
872− -fhide-source-paths
873−
874
829
875benchmark benchmark-pandoc
830benchmark benchmark-pandoc
831+ import: common-executable
876 type: exitcode-stdio-1.0
832 type: exitcode-stdio-1.0
877 main-is: benchmark-pandoc.hs
833 main-is: benchmark-pandoc.hs
878 hs-source-dirs: benchmark
834 hs-source-dirs: benchmark
879− build-depends: pandoc,
880− base >= 4.8 && < 5,
881− bytestring,
835+ build-depends: bytestring,
882 containers,
836 containers,
883− criterion >= 1.0 && < 1.6,
837+ -- gauge >= 0.2 && < 0.3,
838+ tasty-bench >= 0.2 && <= 0.3,
884 mtl >= 2.2 && < 2.3,
839 mtl >= 2.2 && < 2.3,
885 text >= 1.1.1.0 && < 1.3,
840 text >= 1.1.1.0 && < 1.3,
886− time
841+ time,
887− if impl(ghc < 8.4)
842+ deepseq
888− hs-source-dirs: prelude
843+ -- we increase heap size to avoid benchmarking garbage collection:
889− other-modules: Prelude
890− build-depends: base-compat >= 0.9
891− default-language: Haskell2010
892− other-extensions: NoImplicitPrelude
893− ghc-options: -rtsopts -with-rtsopts=-K16m -threaded
844+ ghc-options: -rtsopts -with-rtsopts=-A8m -threaded
894− -Wall -fno-warn-unused-do-bind
895− -Wincomplete-record-updates
896− -Wnoncanonical-monad-instances
897− if impl(ghc >= 8.2)
898− ghc-options: -Wcpp-undef
899− if impl(ghc >= 8.4)
900− ghc-options: -Wincomplete-uni-patterns
901− -Widentities
902− -fhide-source-paths
shell.nixnew file+106
@@ -0,0 +1,106 @@
1+{nixpkgs ? import <nixpkgs> {} }:
2+let
3+ inherit (nixpkgs) pkgs;
4+ inherit (pkgs) haskellPackages;
5+
6+ haskellDeps = ps: with ps; [
7+ Diff
8+ Glob
9+ HTTP
10+ HTTP
11+ HsYAML
12+ JuicyPixels
13+ QuickCheck
14+ SHA
15+ aeson
16+ aeson-pretty
17+ attoparsec
18+ base
19+ base-compat
20+ base64-bytestring
21+ binary
22+ blaze-html
23+ blaze-markup
24+ bytestring
25+ case-insensitive
26+ citeproc
27+ commonmark
28+ commonmark-extensions
29+ commonmark-pandoc
30+ conduit-extra
31+ connection
32+ connection
33+ containers
34+ data-default
35+ deepseq
36+ directory
37+ doclayout
38+ doctemplates
39+ emojis
40+ exceptions
41+ file-embed
42+ filepath
43+ haddock-library
44+ hsc2hs
45+ hslua
46+ hslua-module-system
47+ hslua-module-text
48+ http-client
49+ http-client
50+ http-client-tls
51+ http-client-tls
52+ http-types
53+ ipynb
54+ jira-wiki-markup
55+ mtl
56+ network
57+ network
58+ network-uri
59+ pandoc-types
60+ parsec
61+ process
62+ random
63+ safe
64+ scientific
65+ skylighting
66+ skylighting-core
67+ socks
68+ split
69+ streaming-commons
70+ syb
71+ tagsoup
72+ tasty
73+ tasty-golden
74+ tasty-hunit
75+ tasty-lua
76+ tasty-quickcheck
77+ temporary
78+ texmath
79+ text
80+ text-conversions
81+ time
82+ tls
83+ unicode-transforms
84+ unordered-containers
85+ weigh
86+ xml
87+ xml-conduit
88+ zip-archive
89+ zlib
90+ ];
91+
92+ ghc = haskellPackages.ghcWithPackages haskellDeps;
93+
94+ nixPackages = [
95+ pkgs.zlib
96+ ghc
97+ pkgs.gdb
98+ haskellPackages.ghcid
99+ haskellPackages.cabal2nix
100+ haskellPackages.cabal-install
101+ ];
102+in
103+pkgs.stdenv.mkDerivation {
104+ name = "env";
105+ buildInputs = nixPackages;
106+}
src/Text/Pandoc/App.hs+63−28
@@ -1,3 +1,4 @@
1+{-# LANGUAGE LambdaCase #-}
1{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE CPP #-}
3{-# LANGUAGE CPP #-}
3{-# LANGUAGE ScopedTypeVariables #-}
4{-# LANGUAGE ScopedTypeVariables #-}
@@ -19,6 +20,7 @@ module Text.Pandoc.App (
19 , Filter(..)
20 , Filter(..)
20 , defaultOpts
21 , defaultOpts
21 , parseOptions
22 , parseOptions
23+ , parseOptionsFromArgs
22 , options
24 , options
23 , applyFilters
25 , applyFilters
24 ) where
26 ) where
@@ -27,6 +29,7 @@ import Control.Monad ( (>=>), when )
27import Control.Monad.Trans ( MonadIO(..) )
29import Control.Monad.Trans ( MonadIO(..) )
28import Control.Monad.Except (throwError)
30import Control.Monad.Except (throwError)
29import qualified Data.ByteString as BS
31import qualified Data.ByteString as BS
32+import qualified Data.ByteString.Char8 as B8
30import qualified Data.ByteString.Lazy as BL
33import qualified Data.ByteString.Lazy as BL
31import Data.Char (toLower)
34import Data.Char (toLower)
32import Data.Maybe (fromMaybe, isJust, isNothing)
35import Data.Maybe (fromMaybe, isJust, isNothing)
@@ -44,18 +47,21 @@ import System.FilePath ( takeBaseName, takeExtension )
44import System.IO (nativeNewline, stdout)
47import System.IO (nativeNewline, stdout)
45import qualified System.IO as IO (Newline (..))
48import qualified System.IO as IO (Newline (..))
46import Text.Pandoc
49import Text.Pandoc
50+import Text.Pandoc.Builder (setMeta)
51+import Text.Pandoc.MIME (getCharset)
47import Text.Pandoc.App.FormatHeuristics (formatFromFilePaths)
52import Text.Pandoc.App.FormatHeuristics (formatFromFilePaths)
48import Text.Pandoc.App.Opt (Opt (..), LineEnding (..), defaultOpts,
53import Text.Pandoc.App.Opt (Opt (..), LineEnding (..), defaultOpts,
49− IpynbOutput (..) )
54+ IpynbOutput (..))
50−import Text.Pandoc.App.CommandLineOptions (parseOptions, options)
55+import Text.Pandoc.App.CommandLineOptions (parseOptions, parseOptionsFromArgs,
56+ options)
51import Text.Pandoc.App.OutputSettings (OutputSettings (..), optToOutputSettings)
57import Text.Pandoc.App.OutputSettings (OutputSettings (..), optToOutputSettings)
52−import Text.Pandoc.BCP47 (Lang (..), parseBCP47)
58+import Text.Collate.Lang (Lang (..), parseLang)
53import Text.Pandoc.Filter (Filter (JSONFilter, LuaFilter), applyFilters)
59import Text.Pandoc.Filter (Filter (JSONFilter, LuaFilter), applyFilters)
54import Text.Pandoc.PDF (makePDF)
60import Text.Pandoc.PDF (makePDF)
55import Text.Pandoc.SelfContained (makeSelfContained)
61import Text.Pandoc.SelfContained (makeSelfContained)
56import Text.Pandoc.Shared (eastAsianLineBreakFilter, stripEmptyParagraphs,
62import Text.Pandoc.Shared (eastAsianLineBreakFilter, stripEmptyParagraphs,
57 headerShift, isURI, tabFilter, uriPathToPath, filterIpynbOutput,
63 headerShift, isURI, tabFilter, uriPathToPath, filterIpynbOutput,
58− defaultUserDataDirs, tshow, findM)
64+ defaultUserDataDir, tshow)
59import Text.Pandoc.Writers.Shared (lookupMetaString)
65import Text.Pandoc.Writers.Shared (lookupMetaString)
60import Text.Pandoc.Readers.Markdown (yamlToMeta)
66import Text.Pandoc.Readers.Markdown (yamlToMeta)
61import qualified Text.Pandoc.UTF8 as UTF8
67import qualified Text.Pandoc.UTF8 as UTF8
@@ -66,25 +72,29 @@ import System.Posix.Terminal (queryTerminal)
66
72
67convertWithOpts :: Opt -> IO ()
73convertWithOpts :: Opt -> IO ()
68convertWithOpts opts = do
74convertWithOpts opts = do
75+ datadir <- case optDataDir opts of
76+ Nothing -> do
77+ d <- defaultUserDataDir
78+ exists <- doesDirectoryExist d
79+ return $ if exists
80+ then Just d
81+ else Nothing
82+ Just _ -> return $ optDataDir opts
83+
69 let outputFile = fromMaybe "-" (optOutputFile opts)
84 let outputFile = fromMaybe "-" (optOutputFile opts)
70 let filters = optFilters opts
85 let filters = optFilters opts
71 let verbosity = optVerbosity opts
86 let verbosity = optVerbosity opts
72
87
73 when (optDumpArgs opts) $
88 when (optDumpArgs opts) $
74− do UTF8.hPutStrLn stdout outputFile
89+ do UTF8.hPutStrLn stdout (T.pack outputFile)
90+ mapM_ (UTF8.hPutStrLn stdout . T.pack)
75− mapM_ (UTF8.hPutStrLn stdout) (fromMaybe ["-"] $ optInputFiles opts)
91+ (fromMaybe ["-"] $ optInputFiles opts)
76 exitSuccess
92 exitSuccess
77
93
78 let sources = case optInputFiles opts of
94 let sources = case optInputFiles opts of
79 Just xs | not (optIgnoreArgs opts) -> xs
95 Just xs | not (optIgnoreArgs opts) -> xs
80 _ -> ["-"]
96 _ -> ["-"]
81
97
82− datadir <- case optDataDir opts of
83− Nothing -> do
84− ds <- defaultUserDataDirs
85− findM doesDirectoryExist ds
86− Just _ -> return $ optDataDir opts
87−
88 let runIO' :: PandocIO a -> IO a
98 let runIO' :: PandocIO a -> IO a
89 runIO' f = do
99 runIO' f = do
90 (res, reports) <- runIOorExplode $ do
100 (res, reports) <- runIOorExplode $ do
@@ -150,9 +160,11 @@ convertWithOpts opts = do
150 else optTabStop opts)
160 else optTabStop opts)
151
161
152
162
153− let readSources :: [FilePath] -> PandocIO Text
163+ let readSources :: [FilePath] -> PandocIO [(FilePath, Text)]
154− readSources srcs = convertTabs . T.intercalate (T.pack "\n") <$>
164+ readSources srcs =
165+ mapM (\fp -> do
155− mapM readSource srcs
166+ t <- readSource fp
167+ return (if fp == "-" then "" else fp, convertTabs t)) srcs
156
168
157
169
158 outputSettings <- optToOutputSettings opts
170 outputSettings <- optToOutputSettings opts
@@ -190,8 +202,8 @@ convertWithOpts opts = do
190 Just f -> readFileStrict f
202 Just f -> readFileStrict f
191
203
192 case lookupMetaString "lang" (optMetadata opts) of
204 case lookupMetaString "lang" (optMetadata opts) of
193− "" -> setTranslations $ Lang "en" "" "US" []
205+ "" -> setTranslations $ Lang "en" Nothing (Just "US") [] [] []
194− l -> case parseBCP47 l of
206+ l -> case parseLang l of
195 Left _ -> report $ InvalidLang l
207 Left _ -> report $ InvalidLang l
196 Right l' -> setTranslations l'
208 Right l' -> setTranslations l'
197
209
@@ -245,13 +257,17 @@ convertWithOpts opts = do
245 let sourceToDoc :: [FilePath] -> PandocIO Pandoc
257 let sourceToDoc :: [FilePath] -> PandocIO Pandoc
246 sourceToDoc sources' =
258 sourceToDoc sources' =
247 case reader of
259 case reader of
248− TextReader r
260+ TextReader r
249− | optFileScope opts || readerNameBase == "json" ->
261+ | readerNameBase == "json" ->
262+ mconcat <$> mapM (readSource >=> r readerOpts) sources'
263+ | optFileScope opts ->
264+ -- Read source and convert tabs (see #6709)
265+ let readSource' = fmap convertTabs . readSource
250− mconcat <$> mapM (readSource >=> r readerOpts) sources'
266+ in mconcat <$> mapM (readSource' >=> r readerOpts) sources'
251− | otherwise ->
267+ | otherwise ->
252− readSources sources' >>= r readerOpts
268+ readSources sources' >>= r readerOpts
253− ByteStringReader r ->
269+ ByteStringReader r ->
254− mconcat <$> mapM (readFile' >=> r readerOpts) sources'
270+ mconcat <$> mapM (readFile' >=> r readerOpts) sources'
255
271
256
272
257 when (readerNameBase == "markdown_github" ||
273 when (readerNameBase == "markdown_github" ||
@@ -269,12 +285,21 @@ convertWithOpts opts = do
269 report $ Deprecated "pandoc-citeproc filter"
285 report $ Deprecated "pandoc-citeproc filter"
270 "Use --citeproc instead."
286 "Use --citeproc instead."
271
287
288+ let cslMetadata =
289+ maybe id (setMeta "csl") (optCSL opts) .
290+ (case optBibliography opts of
291+ [] -> id
292+ xs -> setMeta "bibliography" xs) .
293+ maybe id (setMeta "citation-abbreviations")
294+ (optCitationAbbreviations opts) $ mempty
295+
272 doc <- sourceToDoc sources >>=
296 doc <- sourceToDoc sources >>=
273 ( (if isJust (optExtractMedia opts)
297 ( (if isJust (optExtractMedia opts)
274 then fillMediaBag
298 then fillMediaBag
275 else return)
299 else return)
276 >=> return . adjustMetadata (metadataFromFile <>)
300 >=> return . adjustMetadata (metadataFromFile <>)
277 >=> return . adjustMetadata (<> optMetadata opts)
301 >=> return . adjustMetadata (<> optMetadata opts)
302+ >=> return . adjustMetadata (<> cslMetadata)
278 >=> applyTransforms transforms
303 >=> applyTransforms transforms
279 >=> applyFilters readerOpts filters [T.unpack format]
304 >=> applyFilters readerOpts filters [T.unpack format]
280 >=> maybe return extractMedia (optExtractMedia opts)
305 >=> maybe return extractMedia (optExtractMedia opts)
@@ -341,7 +366,18 @@ readSource src = case parseURI src of
341 _ -> PandocAppError (tshow e))
366 _ -> PandocAppError (tshow e))
342
367
343readURI :: FilePath -> PandocIO Text
368readURI :: FilePath -> PandocIO Text
369+readURI src = do
344−readURI src = UTF8.toText . fst <$> openURL (T.pack src)
370+ (bs, mt) <- openURL (T.pack src)
371+ case mt >>= getCharset of
372+ Just "UTF-8" -> return $ UTF8.toText bs
373+ Just "ISO-8859-1" -> return $ T.pack $ B8.unpack bs
374+ Just charset -> throwError $ PandocUnsupportedCharsetError charset
375+ Nothing -> liftIO $ -- try first as UTF-8, then as latin1
376+ E.catch (return $! UTF8.toText bs)
377+ (\case
378+ TSE.DecodeError{} ->
379+ return $ T.pack $ B8.unpack bs
380+ e -> E.throwIO e)
345
381
346readFile' :: MonadIO m => FilePath -> m BL.ByteString
382readFile' :: MonadIO m => FilePath -> m BL.ByteString
347readFile' "-" = liftIO BL.getContents
383readFile' "-" = liftIO BL.getContents
@@ -352,6 +388,5 @@ writeFnBinary "-" = liftIO . BL.putStr
352writeFnBinary f = liftIO . BL.writeFile (UTF8.encodePath f)
388writeFnBinary f = liftIO . BL.writeFile (UTF8.encodePath f)
353
389
354writerFn :: MonadIO m => IO.Newline -> FilePath -> Text -> m ()
390writerFn :: MonadIO m => IO.Newline -> FilePath -> Text -> m ()
355−-- TODO this implementation isn't maximally efficient:
356−writerFn eol "-" = liftIO . UTF8.putStrWith eol . T.unpack
391+writerFn eol "-" = liftIO . UTF8.putStrWith eol
357−writerFn eol f = liftIO . UTF8.writeFileWith eol f . T.unpack
392+writerFn eol f = liftIO . UTF8.writeFileWith eol f
src/Text/Pandoc/App/CommandLineOptions.hs+54−25
@@ -17,6 +17,7 @@ Does a pandoc conversion based on command-line options.
17-}
17-}
18module Text.Pandoc.App.CommandLineOptions (
18module Text.Pandoc.App.CommandLineOptions (
19 parseOptions
19 parseOptions
20+ , parseOptionsFromArgs
20 , options
21 , options
21 , engines
22 , engines
22 , lookupHighlightStyle
23 , lookupHighlightStyle
@@ -30,7 +31,7 @@ import Data.Aeson.Encode.Pretty (encodePretty', Config(..), keyOrder,
30 defConfig, Indent(..), NumberFormat(..))
31 defConfig, Indent(..), NumberFormat(..))
31import Data.Bifunctor (second)
32import Data.Bifunctor (second)
32import Data.Char (toLower)
33import Data.Char (toLower)
33−import Data.List (intercalate, sort)
34+import Data.List (intercalate, sort, foldl')
34#ifdef _WINDOWS
35#ifdef _WINDOWS
35#if MIN_VERSION_base(4,12,0)
36#if MIN_VERSION_base(4,12,0)
36import Data.List (isPrefixOf)
37import Data.List (isPrefixOf)
@@ -47,12 +48,13 @@ import System.FilePath
47import System.IO (stdout)
48import System.IO (stdout)
48import Text.DocTemplates (Context (..), ToContext (toVal), Val (..))
49import Text.DocTemplates (Context (..), ToContext (toVal), Val (..))
49import Text.Pandoc
50import Text.Pandoc
51+import Text.Pandoc.Builder (setMeta)
50import Text.Pandoc.App.Opt (Opt (..), LineEnding (..), IpynbOutput (..),
52import Text.Pandoc.App.Opt (Opt (..), LineEnding (..), IpynbOutput (..),
51− DefaultsState (..), addMeta, applyDefaults,
53+ DefaultsState (..), applyDefaults,
52 fullDefaultsPath)
54 fullDefaultsPath)
53import Text.Pandoc.Filter (Filter (..))
55import Text.Pandoc.Filter (Filter (..))
54import Text.Pandoc.Highlighting (highlightingStyles)
56import Text.Pandoc.Highlighting (highlightingStyles)
55−import Text.Pandoc.Shared (ordNub, elemText, safeStrRead, defaultUserDataDirs)
57+import Text.Pandoc.Shared (ordNub, elemText, safeStrRead, defaultUserDataDir)
56import Text.Printf
58import Text.Printf
57
59
58#ifdef EMBED_DATA_FILES
60#ifdef EMBED_DATA_FILES
@@ -73,9 +75,13 @@ parseOptions :: [OptDescr (Opt -> IO Opt)] -> Opt -> IO Opt
73parseOptions options' defaults = do
75parseOptions options' defaults = do
74 rawArgs <- map UTF8.decodeArg <$> getArgs
76 rawArgs <- map UTF8.decodeArg <$> getArgs
75 prg <- getProgName
77 prg <- getProgName
78+ parseOptionsFromArgs options' defaults prg rawArgs
76
79
80+parseOptionsFromArgs
81+ :: [OptDescr (Opt -> IO Opt)] -> Opt -> String -> [String] -> IO Opt
82+parseOptionsFromArgs options' defaults prg rawArgs = do
77 let (actions, args, unrecognizedOpts, errors) =
83 let (actions, args, unrecognizedOpts, errors) =
78− getOpt' Permute options' rawArgs
84+ getOpt' Permute options' (map UTF8.decodeArg rawArgs)
79
85
80 let unknownOptionErrors =
86 let unknownOptionErrors =
81 foldr (handleUnrecognizedOption . takeWhile (/= '=')) []
87 foldr (handleUnrecognizedOption . takeWhile (/= '=')) []
@@ -87,7 +93,7 @@ parseOptions options' defaults = do
87 ("Try " ++ prg ++ " --help for more information.")
93 ("Try " ++ prg ++ " --help for more information.")
88
94
89 -- thread option data structure through all supplied option actions
95 -- thread option data structure through all supplied option actions
90− opts <- foldl (>>=) (return defaults) actions
96+ opts <- foldl' (>>=) (return defaults) actions
91 let mbArgs = case args of
97 let mbArgs = case args of
92 [] -> Nothing
98 [] -> Nothing
93 xs -> Just xs
99 xs -> Just xs
@@ -282,7 +288,8 @@ options =
282 , Option "" ["resource-path"]
288 , Option "" ["resource-path"]
283 (ReqArg
289 (ReqArg
284 (\arg opt -> return opt { optResourcePath =
290 (\arg opt -> return opt { optResourcePath =
285− splitSearchPath arg })
291+ splitSearchPath arg ++
292+ optResourcePath opt })
286 "SEARCHPATH")
293 "SEARCHPATH")
287 "" -- "Paths to search for images and other resources"
294 "" -- "Paths to search for images and other resources"
288
295
@@ -807,10 +814,10 @@ options =
807 map (\c -> ['-',c]) shorts ++
814 map (\c -> ['-',c]) shorts ++
808 map ("--" ++) longs
815 map ("--" ++) longs
809 let allopts = unwords (concatMap optnames options)
816 let allopts = unwords (concatMap optnames options)
810− UTF8.hPutStrLn stdout $ printf tpl allopts
817+ UTF8.hPutStrLn stdout $ T.pack $ printf tpl allopts
811− (unwords readersNames)
818+ (T.unpack $ T.unwords readersNames)
812− (unwords writersNames)
819+ (T.unpack $ T.unwords writersNames)
813− (unwords $ map (T.unpack . fst) highlightingStyles)
820+ (T.unpack $ T.unwords $ map fst highlightingStyles)
814 (unwords datafiles)
821 (unwords datafiles)
815 exitSuccess ))
822 exitSuccess ))
816 "" -- "Print bash completion script"
823 "" -- "Print bash completion script"
@@ -849,7 +856,7 @@ options =
849 else if extensionEnabled x allExts
856 else if extensionEnabled x allExts
850 then '-'
857 then '-'
851 else ' ') : drop 4 (show x)
858 else ' ') : drop 4 (show x)
852− mapM_ (UTF8.hPutStrLn stdout . showExt)
859+ mapM_ (UTF8.hPutStrLn stdout . T.pack . showExt)
853 [ex | ex <- extList, extensionEnabled ex allExts]
860 [ex | ex <- extList, extensionEnabled ex allExts]
854 exitSuccess )
861 exitSuccess )
855 "FORMAT")
862 "FORMAT")
@@ -863,14 +870,14 @@ options =
863 , sShortname s `notElem`
870 , sShortname s `notElem`
864 [T.pack "Alert", T.pack "Alert_indent"]
871 [T.pack "Alert", T.pack "Alert_indent"]
865 ]
872 ]
866− mapM_ (UTF8.hPutStrLn stdout) (sort langs)
873+ mapM_ (UTF8.hPutStrLn stdout . T.pack) (sort langs)
867 exitSuccess ))
874 exitSuccess ))
868 ""
875 ""
869
876
870 , Option "" ["list-highlight-styles"]
877 , Option "" ["list-highlight-styles"]
871 (NoArg
878 (NoArg
872 (\_ -> do
879 (\_ -> do
873− mapM_ (UTF8.hPutStrLn stdout . T.unpack . fst) highlightingStyles
880+ mapM_ (UTF8.hPutStrLn stdout . fst) highlightingStyles
874 exitSuccess ))
881 exitSuccess ))
875 ""
882 ""
876
883
@@ -888,7 +895,7 @@ options =
888 | T.null t -> -- e.g. for docx, odt, json:
895 | T.null t -> -- e.g. for docx, odt, json:
889 E.throwIO $ PandocCouldNotFindDataFileError $ T.pack
896 E.throwIO $ PandocCouldNotFindDataFileError $ T.pack
890 ("templates/default." ++ arg)
897 ("templates/default." ++ arg)
891− | otherwise -> write . T.unpack $ t
898+ | otherwise -> write t
892 Left e -> E.throwIO e
899 Left e -> E.throwIO e
893 exitSuccess)
900 exitSuccess)
894 "FORMAT")
901 "FORMAT")
@@ -934,12 +941,13 @@ options =
934 (NoArg
941 (NoArg
935 (\_ -> do
942 (\_ -> do
936 prg <- getProgName
943 prg <- getProgName
937− defaultDatadirs <- defaultUserDataDirs
944+ defaultDatadir <- defaultUserDataDir
945+ UTF8.hPutStrLn stdout
946+ $ T.pack
938− UTF8.hPutStrLn stdout (prg ++ " " ++ T.unpack pandocVersion ++
947+ $ prg ++ " " ++ T.unpack pandocVersion ++
939− compileInfo ++
948+ compileInfo ++
940− "\nUser data directory: " ++
949+ "\nUser data directory: " ++ defaultDatadir ++
941− intercalate " or " defaultDatadirs ++
942− ('\n':copyrightMessage))
950+ ('\n':copyrightMessage)
943 exitSuccess ))
951 exitSuccess ))
944 "" -- "Print version"
952 "" -- "Print version"
945
953
@@ -947,7 +955,7 @@ options =
947 (NoArg
955 (NoArg
948 (\_ -> do
956 (\_ -> do
949 prg <- getProgName
957 prg <- getProgName
950− UTF8.hPutStr stdout (usageMessage prg options)
958+ UTF8.hPutStr stdout (T.pack $ usageMessage prg options)
951 exitSuccess ))
959 exitSuccess ))
952 "" -- "Show help"
960 "" -- "Show help"
953 ]
961 ]
@@ -1008,12 +1016,12 @@ handleUnrecognizedOption "-R" = handleUnrecognizedOption "--parse-raw"
1008handleUnrecognizedOption x =
1016handleUnrecognizedOption x =
1009 (("Unknown option " ++ x ++ ".") :)
1017 (("Unknown option " ++ x ++ ".") :)
1010
1018
1011−readersNames :: [String]
1019+readersNames :: [Text]
1012−readersNames = sort (map (T.unpack . fst) (readers :: [(Text, Reader PandocIO)]))
1020+readersNames = sort (map fst (readers :: [(Text, Reader PandocIO)]))
1013
1021
1014−writersNames :: [String]
1022+writersNames :: [Text]
1015writersNames = sort
1023writersNames = sort
1016− ("pdf" : map (T.unpack . fst) (writers :: [(Text, Writer PandocIO)]))
1024+ ("pdf" : map fst (writers :: [(Text, Writer PandocIO)]))
1017
1025
1018splitField :: String -> (String, String)
1026splitField :: String -> (String, String)
1019splitField = second (tailDef "true") . break (`elemText` ":=")
1027splitField = second (tailDef "true") . break (`elemText` ":=")
@@ -1046,6 +1054,27 @@ setVariable key val (Context ctx) = Context $ M.alter go key ctx
1046 go (Just (ListVal xs)) = Just $ ListVal $ xs ++ [toVal val]
1054 go (Just (ListVal xs)) = Just $ ListVal $ xs ++ [toVal val]
1047 go (Just x) = Just $ ListVal [x, toVal val]
1055 go (Just x) = Just $ ListVal [x, toVal val]
1048
1056
1057+addMeta :: String -> String -> Meta -> Meta
1058+addMeta k v meta =
1059+ case lookupMeta k' meta of
1060+ Nothing -> setMeta k' v' meta
1061+ Just (MetaList xs) ->
1062+ setMeta k' (MetaList (xs ++ [v'])) meta
1063+ Just x -> setMeta k' (MetaList [x, v']) meta
1064+ where
1065+ v' = readMetaValue v
1066+ k' = T.pack k
1067+
1068+readMetaValue :: String -> MetaValue
1069+readMetaValue s
1070+ | s == "true" = MetaBool True
1071+ | s == "True" = MetaBool True
1072+ | s == "TRUE" = MetaBool True
1073+ | s == "false" = MetaBool False
1074+ | s == "False" = MetaBool False
1075+ | s == "FALSE" = MetaBool False
1076+ | otherwise = MetaString $ T.pack s
1077+
1049-- On Windows with ghc 8.6+, we need to rewrite paths
1078-- On Windows with ghc 8.6+, we need to rewrite paths
1050-- beginning with \\ to \\?\UNC\. -- See #5127.
1079-- beginning with \\ to \\?\UNC\. -- See #5127.
1051normalizePath :: FilePath -> FilePath
1080normalizePath :: FilePath -> FilePath
src/Text/Pandoc/App/FormatHeuristics.hs+13−7
@@ -15,18 +15,24 @@ module Text.Pandoc.App.FormatHeuristics
15 ) where
15 ) where
16
16
17import Data.Char (toLower)
17import Data.Char (toLower)
18+import Data.Foldable (asum)
18import Data.Text (Text)
19import Data.Text (Text)
19import System.FilePath (takeExtension)
20import System.FilePath (takeExtension)
20
21
21−-- Determine default format based on file extensions.
22+-- | Determines default format based on file extensions; uses the format
23+-- of the first extension that's associated with a format.
24+--
25+-- Examples:
26+--
27+-- > formatFromFilePaths ["text.unknown", "no-extension"]
28+-- Nothing
29+--
30+-- > formatFromFilePaths ["my.md", "other.rst"]
31+-- Just "markdown"
22formatFromFilePaths :: [FilePath] -> Maybe Text
32formatFromFilePaths :: [FilePath] -> Maybe Text
23−formatFromFilePaths [] = Nothing
33+formatFromFilePaths = asum . map formatFromFilePath
24−formatFromFilePaths (x:xs) =
25− case formatFromFilePath x of
26− Just f -> Just f
27− Nothing -> formatFromFilePaths xs
28
34
29−-- Determine format based on file extension
35+-- | Determines format based on file extension.
30formatFromFilePath :: FilePath -> Maybe Text
36formatFromFilePath :: FilePath -> Maybe Text
31formatFromFilePath x =
37formatFromFilePath x =
32 case takeExtension (map toLower x) of
38 case takeExtension (map toLower x) of
src/Text/Pandoc/App/Opt.hs+145−57
@@ -22,28 +22,29 @@ module Text.Pandoc.App.Opt (
22 , IpynbOutput (..)
22 , IpynbOutput (..)
23 , DefaultsState (..)
23 , DefaultsState (..)
24 , defaultOpts
24 , defaultOpts
25− , addMeta
26 , applyDefaults
25 , applyDefaults
27 , fullDefaultsPath
26 , fullDefaultsPath
28 ) where
27 ) where
29import Control.Monad.Except (MonadIO, liftIO, throwError, (>=>), foldM)
28import Control.Monad.Except (MonadIO, liftIO, throwError, (>=>), foldM)
30import Control.Monad.State.Strict (StateT, modify, gets)
29import Control.Monad.State.Strict (StateT, modify, gets)
31−import System.FilePath ( addExtension, (</>), takeExtension )
30+import System.FilePath ( addExtension, (</>), takeExtension, takeDirectory )
31+import System.Directory ( canonicalizePath )
32import Data.Char (isLower, toLower)
32import Data.Char (isLower, toLower)
33import Data.Maybe (fromMaybe)
33import Data.Maybe (fromMaybe)
34import GHC.Generics hiding (Meta)
34import GHC.Generics hiding (Meta)
35−import Text.Pandoc.Builder (setMeta)
36import Text.Pandoc.Filter (Filter (..))
35import Text.Pandoc.Filter (Filter (..))
37−import Text.Pandoc.Logging (Verbosity (WARNING))
36+import Text.Pandoc.Logging (Verbosity (WARNING), LogMessage(..))
38import Text.Pandoc.Options (TopLevelDivision (TopLevelDefault),
37import Text.Pandoc.Options (TopLevelDivision (TopLevelDefault),
39 TrackChanges (AcceptChanges),
38 TrackChanges (AcceptChanges),
40 WrapOption (WrapAuto), HTMLMathMethod (PlainMath),
39 WrapOption (WrapAuto), HTMLMathMethod (PlainMath),
41 ReferenceLocation (EndOfDocument),
40 ReferenceLocation (EndOfDocument),
42 ObfuscationMethod (NoObfuscation),
41 ObfuscationMethod (NoObfuscation),
43 CiteMethod (Citeproc))
42 CiteMethod (Citeproc))
44−import Text.Pandoc.Class (readFileLazy, fileExists, setVerbosity, PandocMonad)
43+import Text.Pandoc.Class (readFileLazy, fileExists, setVerbosity, report,
44+ PandocMonad(lookupEnv), getUserDataDir)
45import Text.Pandoc.Error (PandocError (PandocParseError, PandocSomeError))
45import Text.Pandoc.Error (PandocError (PandocParseError, PandocSomeError))
46−import Text.Pandoc.Shared (camelCaseStrToHyphenated, defaultUserDataDirs, findM, ordNub)
46+import Text.Pandoc.Shared (camelCaseStrToHyphenated, defaultUserDataDir,
47+ findM, ordNub)
47import qualified Text.Pandoc.Parsing as P
48import qualified Text.Pandoc.Parsing as P
48import Text.Pandoc.Readers.Metadata (yamlMap)
49import Text.Pandoc.Readers.Metadata (yamlMap)
49import Text.Pandoc.Class.PandocPure
50import Text.Pandoc.Class.PandocPure
@@ -52,7 +53,7 @@ import Data.Text (Text, unpack)
52import Data.Default (def)
53import Data.Default (def)
53import qualified Data.Text as T
54import qualified Data.Text as T
54import qualified Data.Map as M
55import qualified Data.Map as M
55−import Text.Pandoc.Definition (Meta(..), MetaValue(..), lookupMeta)
56+import Text.Pandoc.Definition (Meta(..), MetaValue(..))
56import Data.Aeson (defaultOptions, Options(..))
57import Data.Aeson (defaultOptions, Options(..))
57import Data.Aeson.TH (deriveJSON)
58import Data.Aeson.TH (deriveJSON)
58import Control.Applicative ((<|>))
59import Control.Applicative ((<|>))
@@ -156,6 +157,9 @@ data Opt = Opt
156 , optNoCheckCertificate :: Bool -- ^ Disable certificate validation
157 , optNoCheckCertificate :: Bool -- ^ Disable certificate validation
157 , optEol :: LineEnding -- ^ Style of line-endings to use
158 , optEol :: LineEnding -- ^ Style of line-endings to use
158 , optStripComments :: Bool -- ^ Skip HTML comments
159 , optStripComments :: Bool -- ^ Skip HTML comments
160+ , optCSL :: Maybe FilePath -- ^ CSL stylesheet
161+ , optBibliography :: [FilePath] -- ^ Bibliography files
162+ , optCitationAbbreviations :: Maybe FilePath -- ^ Citation abbreviations
159 } deriving (Generic, Show)
163 } deriving (Generic, Show)
160
164
161instance FromYAML (Opt -> Opt) where
165instance FromYAML (Opt -> Opt) where
@@ -175,17 +179,128 @@ instance (PandocMonad m, MonadIO m)
175 dataDir <- case M.lookup "data-dir" opts of
179 dataDir <- case M.lookup "data-dir" opts of
176 Nothing -> return Nothing
180 Nothing -> return Nothing
177 Just v -> Just . unpack <$> parseYAML v
181 Just v -> Just . unpack <$> parseYAML v
178− f <- parseOptions $ M.toList m
182+ f <- parseOptions (M.toList m)
179 case M.lookup "defaults" opts of
183 case M.lookup "defaults" opts of
180 Just v -> do
184 Just v -> do
181 g <- parseDefaults v dataDir
185 g <- parseDefaults v dataDir
182− return $ g >=> f
186+ return $ g >=> f >=> resolveVarsInOpt
183− Nothing -> return f
187+ Nothing -> return $ f >=> resolveVarsInOpt
184 where
188 where
185 toText (Scalar _ (SStr s)) = s
189 toText (Scalar _ (SStr s)) = s
186 toText _ = ""
190 toText _ = ""
187 parseYAML n = failAtNode n "Expected a mapping"
191 parseYAML n = failAtNode n "Expected a mapping"
188
192
193+resolveVarsInOpt :: forall m. (PandocMonad m, MonadIO m)
194+ => Opt -> StateT DefaultsState m Opt
195+resolveVarsInOpt
196+ opt@Opt
197+ { optTemplate = oTemplate
198+ , optMetadataFiles = oMetadataFiles
199+ , optOutputFile = oOutputFile
200+ , optInputFiles = oInputFiles
201+ , optSyntaxDefinitions = oSyntaxDefinitions
202+ , optAbbreviations = oAbbreviations
203+ , optReferenceDoc = oReferenceDoc
204+ , optEpubMetadata = oEpubMetadata
205+ , optEpubFonts = oEpubFonts
206+ , optEpubCoverImage = oEpubCoverImage
207+ , optLogFile = oLogFile
208+ , optFilters = oFilters
209+ , optDataDir = oDataDir
210+ , optExtractMedia = oExtractMedia
211+ , optCss = oCss
212+ , optIncludeBeforeBody = oIncludeBeforeBody
213+ , optIncludeAfterBody = oIncludeAfterBody
214+ , optIncludeInHeader = oIncludeInHeader
215+ , optResourcePath = oResourcePath
216+ , optCSL = oCSL
217+ , optBibliography = oBibliography
218+ , optCitationAbbreviations = oCitationAbbreviations
219+ }
220+ = do
221+ oTemplate' <- mapM resolveVars oTemplate
222+ oMetadataFiles' <- mapM resolveVars oMetadataFiles
223+ oOutputFile' <- mapM resolveVars oOutputFile
224+ oInputFiles' <- mapM (mapM resolveVars) oInputFiles
225+ oSyntaxDefinitions' <- mapM resolveVars oSyntaxDefinitions
226+ oAbbreviations' <- mapM resolveVars oAbbreviations
227+ oReferenceDoc' <- mapM resolveVars oReferenceDoc
228+ oEpubMetadata' <- mapM resolveVars oEpubMetadata
229+ oEpubFonts' <- mapM resolveVars oEpubFonts
230+ oEpubCoverImage' <- mapM resolveVars oEpubCoverImage
231+ oLogFile' <- mapM resolveVars oLogFile
232+ oFilters' <- mapM resolveVarsInFilter oFilters
233+ oDataDir' <- mapM resolveVars oDataDir
234+ oExtractMedia' <- mapM resolveVars oExtractMedia
235+ oCss' <- mapM resolveVars oCss
236+ oIncludeBeforeBody' <- mapM resolveVars oIncludeBeforeBody
237+ oIncludeAfterBody' <- mapM resolveVars oIncludeAfterBody
238+ oIncludeInHeader' <- mapM resolveVars oIncludeInHeader
239+ oResourcePath' <- mapM resolveVars oResourcePath
240+ oCSL' <- mapM resolveVars oCSL
241+ oBibliography' <- mapM resolveVars oBibliography
242+ oCitationAbbreviations' <- mapM resolveVars oCitationAbbreviations
243+ return opt{ optTemplate = oTemplate'
244+ , optMetadataFiles = oMetadataFiles'
245+ , optOutputFile = oOutputFile'
246+ , optInputFiles = oInputFiles'
247+ , optSyntaxDefinitions = oSyntaxDefinitions'
248+ , optAbbreviations = oAbbreviations'
249+ , optReferenceDoc = oReferenceDoc'
250+ , optEpubMetadata = oEpubMetadata'
251+ , optEpubFonts = oEpubFonts'
252+ , optEpubCoverImage = oEpubCoverImage'
253+ , optLogFile = oLogFile'
254+ , optFilters = oFilters'
255+ , optDataDir = oDataDir'
256+ , optExtractMedia = oExtractMedia'
257+ , optCss = oCss'
258+ , optIncludeBeforeBody = oIncludeBeforeBody'
259+ , optIncludeAfterBody = oIncludeAfterBody'
260+ , optIncludeInHeader = oIncludeInHeader'
261+ , optResourcePath = oResourcePath'
262+ , optCSL = oCSL'
263+ , optBibliography = oBibliography'
264+ , optCitationAbbreviations = oCitationAbbreviations'
265+ }
266+
267+ where
268+ resolveVars :: FilePath -> StateT DefaultsState m FilePath
269+ resolveVars [] = return []
270+ resolveVars ('$':'{':xs) =
271+ let (ys, zs) = break (=='}') xs
272+ in if null zs
273+ then return $ '$':'{':xs
274+ else do
275+ val <- lookupEnv' ys
276+ (val ++) <$> resolveVars (drop 1 zs)
277+ resolveVars (c:cs) = (c:) <$> resolveVars cs
278+ lookupEnv' :: String -> StateT DefaultsState m String
279+ lookupEnv' "." = do
280+ mbCurDefaults <- gets curDefaults
281+ maybe (return "")
282+ (fmap takeDirectory . liftIO . canonicalizePath)
283+ mbCurDefaults
284+ lookupEnv' "USERDATA" = do
285+ mbodatadir <- mapM resolveVars oDataDir
286+ mbdatadir <- getUserDataDir
287+ defdatadir <- liftIO defaultUserDataDir
288+ return $ fromMaybe defdatadir (mbodatadir <|> mbdatadir)
289+ lookupEnv' v = do
290+ mbval <- fmap T.unpack <$> lookupEnv (T.pack v)
291+ case mbval of
292+ Nothing -> do
293+ report $ EnvironmentVariableUndefined (T.pack v)
294+ return mempty
295+ Just x -> return x
296+ resolveVarsInFilter (JSONFilter fp) =
297+ JSONFilter <$> resolveVars fp
298+ resolveVarsInFilter (LuaFilter fp) =
299+ LuaFilter <$> resolveVars fp
300+ resolveVarsInFilter CiteprocFilter = return CiteprocFilter
301+
302+
303+
189parseDefaults :: (PandocMonad m, MonadIO m)
304parseDefaults :: (PandocMonad m, MonadIO m)
190 => Node Pos
305 => Node Pos
191 -> Maybe FilePath
306 -> Maybe FilePath
@@ -428,26 +543,18 @@ doOpt (k',v) = do
428 (parseYAML v >>= \x -> return (\o -> o{ optCss = optCss o <>
543 (parseYAML v >>= \x -> return (\o -> o{ optCss = optCss o <>
429 [unpack x] }))
544 [unpack x] }))
430 "bibliography" ->
545 "bibliography" ->
431− do let addItem x o = o{ optMetadata =
546+ (parseYAML v >>= \x -> return (\o ->
432− addMeta "bibliography" (T.unpack x)
547+ o{ optBibliography = optBibliography o <>
433− (optMetadata o) }
548+ map unpack x }))
549+ <|>
434− (parseYAML v >>= \(xs :: [Text]) -> return $ \o ->
550+ (parseYAML v >>= \x -> return (\o ->
435− foldr addItem o xs)
551+ o{ optBibliography = optBibliography o <>
436− <|>
552+ [unpack x] }))
437− (parseYAML v >>= \(x :: Text) -> return $ \o -> addItem x o)
438 "csl" ->
553 "csl" ->
439− do let addItem x o = o{ optMetadata =
554+ parseYAML v >>= \x -> return (\o -> o{ optCSL = unpack <$> x })
440− addMeta "csl" (T.unpack x)
441− (optMetadata o) }
442− (parseYAML v >>= \(xs :: [Text]) -> return $ \o ->
443− foldr addItem o xs)
444− <|>
445− (parseYAML v >>= \(x :: Text) -> return $ \o -> addItem x o)
446 "citation-abbreviations" ->
555 "citation-abbreviations" ->
447− parseYAML v >>= \x ->
556+ parseYAML v >>= \x -> return (\o -> o{ optCitationAbbreviations =
448− return (\o -> o{ optMetadata =
557+ unpack <$> x })
449− addMeta "citation-abbreviations" (T.unpack x)
450− (optMetadata o) })
451 "ipynb-output" ->
558 "ipynb-output" ->
452 parseYAML v >>= \x -> return (\o -> o{ optIpynbOutput = x })
559 parseYAML v >>= \x -> return (\o -> o{ optIpynbOutput = x })
453 "include-before-body" ->
560 "include-before-body" ->
@@ -476,7 +583,8 @@ doOpt (k',v) = do
476 optIncludeInHeader o <> [unpack x] }))
583 optIncludeInHeader o <> [unpack x] }))
477 "resource-path" ->
584 "resource-path" ->
478 parseYAML v >>= \x ->
585 parseYAML v >>= \x ->
479− return (\o -> o{ optResourcePath = map unpack x })
586+ return (\o -> o{ optResourcePath = map unpack x <>
587+ optResourcePath o })
480 "request-headers" ->
588 "request-headers" ->
481 parseYAML v >>= \x ->
589 parseYAML v >>= \x ->
482 return (\o -> o{ optRequestHeaders = x })
590 return (\o -> o{ optRequestHeaders = x })
@@ -562,6 +670,9 @@ defaultOpts = Opt
562 , optNoCheckCertificate = False
670 , optNoCheckCertificate = False
563 , optEol = Native
671 , optEol = Native
564 , optStripComments = False
672 , optStripComments = False
673+ , optCSL = Nothing
674+ , optBibliography = []
675+ , optCitationAbbreviations = Nothing
565 }
676 }
566
677
567parseStringKey :: Node Pos -> Parser Text
678parseStringKey :: Node Pos -> Parser Text
@@ -575,31 +686,11 @@ yamlToMeta (Mapping _ _ m) =
575 either (fail . show) return $ runEverything (yamlMap pMetaString m)
686 either (fail . show) return $ runEverything (yamlMap pMetaString m)
576 where
687 where
577 pMetaString = pure . MetaString <$> P.manyChar P.anyChar
688 pMetaString = pure . MetaString <$> P.manyChar P.anyChar
689+ runEverything p =
578− runEverything p = runPure (P.readWithM p def "")
690+ runPure (P.readWithM p (def :: P.ParserState) ("" :: Text))
579 >>= fmap (Meta . flip P.runF def)
691 >>= fmap (Meta . flip P.runF def)
580yamlToMeta _ = return mempty
692yamlToMeta _ = return mempty
581
693
582−addMeta :: String -> String -> Meta -> Meta
583−addMeta k v meta =
584− case lookupMeta k' meta of
585− Nothing -> setMeta k' v' meta
586− Just (MetaList xs) ->
587− setMeta k' (MetaList (xs ++ [v'])) meta
588− Just x -> setMeta k' (MetaList [x, v']) meta
589− where
590− v' = readMetaValue v
591− k' = T.pack k
592−
593−readMetaValue :: String -> MetaValue
594−readMetaValue s
595− | s == "true" = MetaBool True
596− | s == "True" = MetaBool True
597− | s == "TRUE" = MetaBool True
598− | s == "false" = MetaBool False
599− | s == "False" = MetaBool False
600− | s == "FALSE" = MetaBool False
601− | otherwise = MetaString $ T.pack s
602−
603-- | Apply defaults from --defaults file.
694-- | Apply defaults from --defaults file.
604applyDefaults :: (PandocMonad m, MonadIO m)
695applyDefaults :: (PandocMonad m, MonadIO m)
605 => Opt
696 => Opt
@@ -625,12 +716,9 @@ fullDefaultsPath dataDir file = do
625 let fp = if null (takeExtension file)
716 let fp = if null (takeExtension file)
626 then addExtension file "yaml"
717 then addExtension file "yaml"
627 else file
718 else file
628− dataDirs <- liftIO defaultUserDataDirs
719+ defaultDataDir <- liftIO defaultUserDataDir
629− let fps = fp : case dataDir of
720+ let defaultFp = fromMaybe defaultDataDir dataDir </> "defaults" </> fp
630− Nothing -> map (</> ("defaults" </> fp))
631− dataDirs
632− Just dd -> [dd </> "defaults" </> fp]
633− fromMaybe fp <$> findM fileExists fps
721+ fromMaybe fp <$> findM fileExists [fp, defaultFp]
634
722
635-- | In a list of lists, append another list in front of every list which
723-- | In a list of lists, append another list in front of every list which
636-- starts with specific element.
724-- starts with specific element.
src/Text/Pandoc/App/OutputSettings.hs+2−2
@@ -59,8 +59,8 @@ optToOutputSettings opts = do
59 let outputFile = fromMaybe "-" (optOutputFile opts)
59 let outputFile = fromMaybe "-" (optOutputFile opts)
60
60
61 when (optDumpArgs opts) . liftIO $ do
61 when (optDumpArgs opts) . liftIO $ do
62− UTF8.hPutStrLn stdout outputFile
62+ UTF8.hPutStrLn stdout (T.pack outputFile)
63− mapM_ (UTF8.hPutStrLn stdout) (fromMaybe [] $ optInputFiles opts)
63+ mapM_ (UTF8.hPutStrLn stdout . T.pack) (fromMaybe [] $ optInputFiles opts)
64 exitSuccess
64 exitSuccess
65
65
66 epubMetadata <- traverse readUtf8File $ optEpubMetadata opts
66 epubMetadata <- traverse readUtf8File $ optEpubMetadata opts
src/Text/Pandoc/Asciify.hs+13−390
@@ -10,396 +10,19 @@
10Function to convert accented latin letters to their unaccented
10Function to convert accented latin letters to their unaccented
11ascii equivalents (used in constructing HTML identifiers).
11ascii equivalents (used in constructing HTML identifiers).
12-}
12-}
13−module Text.Pandoc.Asciify (toAsciiChar)
13+module Text.Pandoc.Asciify (toAsciiChar, toAsciiText)
14where
14where
15−import Data.Char (isAscii)
15+import Data.Char (isAscii, isMark)
16−import qualified Data.Map as M
16+import qualified Data.Text.Normalize as TN
17+import Data.Text (Text)
18+import qualified Data.Text as T
17
19
18−toAsciiChar :: Char -> Maybe Char
20+toAsciiText :: Text -> Text
19−toAsciiChar c | isAscii c = Just c
21+toAsciiText = T.filter isAscii . TN.normalize (TN.NFD)
20− | otherwise = M.lookup c asciiMap
21
22
22−asciiMap :: M.Map Char Char
23+toAsciiChar :: Char -> Maybe Char
23−asciiMap = M.fromList
24+toAsciiChar c = case T.unpack (TN.normalize TN.NFD (T.singleton c)) of
24− [('\192','A')
25+ (x:xs) | isAscii x
25− ,('\193','A')
26+ , all isMark xs
26− ,('\194','A')
27+ -> Just x
27− ,('\195','A')
28+ _ -> Nothing
28− ,('\196','A')
29− ,('\197','A')
30− ,('\199','C')
31− ,('\200','E')
32− ,('\201','E')
33− ,('\202','E')
34− ,('\203','E')
35− ,('\204','I')
36− ,('\205','I')
37− ,('\206','I')
38− ,('\207','I')
39− ,('\209','N')
40− ,('\210','O')
41− ,('\211','O')
42− ,('\212','O')
43− ,('\213','O')
44− ,('\214','O')
45− ,('\217','U')
46− ,('\218','U')
47− ,('\219','U')
48− ,('\220','U')
49− ,('\221','Y')
50− ,('\224','a')
51− ,('\225','a')
52− ,('\226','a')
53− ,('\227','a')
54− ,('\228','a')
55− ,('\229','a')
56− ,('\231','c')
57− ,('\232','e')
58− ,('\233','e')
59− ,('\234','e')
60− ,('\235','e')
61− ,('\236','i')
62− ,('\237','i')
63− ,('\238','i')
64− ,('\239','i')
65− ,('\241','n')
66− ,('\242','o')
67− ,('\243','o')
68− ,('\244','o')
69− ,('\245','o')
70− ,('\246','o')
71− ,('\249','u')
72− ,('\250','u')
73− ,('\251','u')
74− ,('\252','u')
75− ,('\253','y')
76− ,('\255','y')
77− ,('\256','A')
78− ,('\257','a')
79− ,('\258','A')
80− ,('\259','a')
81− ,('\260','A')
82− ,('\261','a')
83− ,('\262','C')
84− ,('\263','c')
85− ,('\264','C')
86− ,('\265','c')
87− ,('\266','C')
88− ,('\267','c')
89− ,('\268','C')
90− ,('\269','c')
91− ,('\270','D')
92− ,('\271','d')
93− ,('\274','E')
94− ,('\275','e')
95− ,('\276','E')
96− ,('\277','e')
97− ,('\278','E')
98− ,('\279','e')
99− ,('\280','E')
100− ,('\281','e')
101− ,('\282','E')
102− ,('\283','e')
103− ,('\284','G')
104− ,('\285','g')
105− ,('\286','G')
106− ,('\287','g')
107− ,('\288','G')
108− ,('\289','g')
109− ,('\290','G')
110− ,('\291','g')
111− ,('\292','H')
112− ,('\293','h')
113− ,('\296','I')
114− ,('\297','i')
115− ,('\298','I')
116− ,('\299','i')
117− ,('\300','I')
118− ,('\301','i')
119− ,('\302','I')
120− ,('\303','i')
121− ,('\304','I')
122− ,('\305','i')
123− ,('\308','J')
124− ,('\309','j')
125− ,('\310','K')
126− ,('\311','k')
127− ,('\313','L')
128− ,('\314','l')
129− ,('\315','L')
130− ,('\316','l')
131− ,('\317','L')
132− ,('\318','l')
133− ,('\323','N')
134− ,('\324','n')
135− ,('\325','N')
136− ,('\326','n')
137− ,('\327','N')
138− ,('\328','n')
139− ,('\332','O')
140− ,('\333','o')
141− ,('\334','O')
142− ,('\335','o')
143− ,('\336','O')
144− ,('\337','o')
145− ,('\340','R')
146− ,('\341','r')
147− ,('\342','R')
148− ,('\343','r')
149− ,('\344','R')
150− ,('\345','r')
151− ,('\346','S')
152− ,('\347','s')
153− ,('\348','S')
154− ,('\349','s')
155− ,('\350','S')
156− ,('\351','s')
157− ,('\352','S')
158− ,('\353','s')
159− ,('\354','T')
160− ,('\355','t')
161− ,('\356','T')
162− ,('\357','t')
163− ,('\360','U')
164− ,('\361','u')
165− ,('\362','U')
166− ,('\363','u')
167− ,('\364','U')
168− ,('\365','u')
169− ,('\366','U')
170− ,('\367','u')
171− ,('\368','U')
172− ,('\369','u')
173− ,('\370','U')
174− ,('\371','u')
175− ,('\372','W')
176− ,('\373','w')
177− ,('\374','Y')
178− ,('\375','y')
179− ,('\376','Y')
180− ,('\377','Z')
181− ,('\378','z')
182− ,('\379','Z')
183− ,('\380','z')
184− ,('\381','Z')
185− ,('\382','z')
186− ,('\416','O')
187− ,('\417','o')
188− ,('\431','U')
189− ,('\432','u')
190− ,('\461','A')
191− ,('\462','a')
192− ,('\463','I')
193− ,('\464','i')
194− ,('\465','O')
195− ,('\466','o')
196− ,('\467','U')
197− ,('\468','u')
198− ,('\486','G')
199− ,('\487','g')
200− ,('\488','K')
201− ,('\489','k')
202− ,('\490','O')
203− ,('\491','o')
204− ,('\496','j')
205− ,('\500','G')
206− ,('\501','g')
207− ,('\504','N')
208− ,('\505','n')
209− ,('\512','A')
210− ,('\513','a')
211− ,('\514','A')
212− ,('\515','a')
213− ,('\516','E')
214− ,('\517','e')
215− ,('\518','E')
216− ,('\519','e')
217− ,('\520','I')
218− ,('\521','i')
219− ,('\522','I')
220− ,('\523','i')
221− ,('\524','O')
222− ,('\525','o')
223− ,('\526','O')
224− ,('\527','o')
225− ,('\528','R')
226− ,('\529','r')
227− ,('\530','R')
228− ,('\531','r')
229− ,('\532','U')
230− ,('\533','u')
231− ,('\534','U')
232− ,('\535','u')
233− ,('\536','S')
234− ,('\537','s')
235− ,('\538','T')
236− ,('\539','t')
237− ,('\542','H')
238− ,('\543','h')
239− ,('\550','A')
240− ,('\551','a')
241− ,('\552','E')
242− ,('\553','e')
243− ,('\558','O')
244− ,('\559','o')
245− ,('\562','Y')
246− ,('\563','y')
247− ,('\894',';')
248− ,('\7680','A')
249− ,('\7681','a')
250− ,('\7682','B')
251− ,('\7683','b')
252− ,('\7684','B')
253− ,('\7685','b')
254− ,('\7686','B')
255− ,('\7687','b')
256− ,('\7690','D')
257− ,('\7691','d')
258− ,('\7692','D')
259− ,('\7693','d')
260− ,('\7694','D')
261− ,('\7695','d')
262− ,('\7696','D')
263− ,('\7697','d')
264− ,('\7698','D')
265− ,('\7699','d')
266− ,('\7704','E')
267− ,('\7705','e')
268− ,('\7706','E')
269− ,('\7707','e')
270− ,('\7710','F')
271− ,('\7711','f')
272− ,('\7712','G')
273− ,('\7713','g')
274− ,('\7714','H')
275− ,('\7715','h')
276− ,('\7716','H')
277− ,('\7717','h')
278− ,('\7718','H')
279− ,('\7719','h')
280− ,('\7720','H')
281− ,('\7721','h')
282− ,('\7722','H')
283− ,('\7723','h')
284− ,('\7724','I')
285− ,('\7725','i')
286− ,('\7728','K')
287− ,('\7729','k')
288− ,('\7730','K')
289− ,('\7731','k')
290− ,('\7732','K')
291− ,('\7733','k')
292− ,('\7734','L')
293− ,('\7735','l')
294− ,('\7738','L')
295− ,('\7739','l')
296− ,('\7740','L')
297− ,('\7741','l')
298− ,('\7742','M')
299− ,('\7743','m')
300− ,('\7744','M')
301− ,('\7745','m')
302− ,('\7746','M')
303− ,('\7747','m')
304− ,('\7748','N')
305− ,('\7749','n')
306− ,('\7750','N')
307− ,('\7751','n')
308− ,('\7752','N')
309− ,('\7753','n')
310− ,('\7754','N')
311− ,('\7755','n')
312− ,('\7764','P')
313− ,('\7765','p')
314− ,('\7766','P')
315− ,('\7767','p')
316− ,('\7768','R')
317− ,('\7769','r')
318− ,('\7770','R')
319− ,('\7771','r')
320− ,('\7774','R')
321− ,('\7775','r')
322− ,('\7776','S')
323− ,('\7777','s')
324− ,('\7778','S')
325− ,('\7779','s')
326− ,('\7786','T')
327− ,('\7787','t')
328− ,('\7788','T')
329− ,('\7789','t')
330− ,('\7790','T')
331− ,('\7791','t')
332− ,('\7792','T')
333− ,('\7793','t')
334− ,('\7794','U')
335− ,('\7795','u')
336− ,('\7796','U')
337− ,('\7797','u')
338− ,('\7798','U')
339− ,('\7799','u')
340− ,('\7804','V')
341− ,('\7805','v')
342− ,('\7806','V')
343− ,('\7807','v')
344− ,('\7808','W')
345− ,('\7809','w')
346− ,('\7810','W')
347− ,('\7811','w')
348− ,('\7812','W')
349− ,('\7813','w')
350− ,('\7814','W')
351− ,('\7815','w')
352− ,('\7816','W')
353− ,('\7817','w')
354− ,('\7818','X')
355− ,('\7819','x')
356− ,('\7820','X')
357− ,('\7821','x')
358− ,('\7822','Y')
359− ,('\7823','y')
360− ,('\7824','Z')
361− ,('\7825','z')
362− ,('\7826','Z')
363− ,('\7827','z')
364− ,('\7828','Z')
365− ,('\7829','z')
366− ,('\7830','h')
367− ,('\7831','t')
368− ,('\7832','w')
369− ,('\7833','y')
370− ,('\7840','A')
371− ,('\7841','a')
372− ,('\7842','A')
373− ,('\7843','a')
374− ,('\7864','E')
375− ,('\7865','e')
376− ,('\7866','E')
377− ,('\7867','e')
378− ,('\7868','E')
379− ,('\7869','e')
380− ,('\7880','I')
381− ,('\7881','i')
382− ,('\7882','I')
383− ,('\7883','i')
384− ,('\7884','O')
385− ,('\7885','o')
386− ,('\7886','O')
387− ,('\7887','o')
388− ,('\7908','U')
389− ,('\7909','u')
390− ,('\7910','U')
391− ,('\7911','u')
392− ,('\7922','Y')
393− ,('\7923','y')
394− ,('\7924','Y')
395− ,('\7925','y')
396− ,('\7926','Y')
397− ,('\7927','y')
398− ,('\7928','Y')
399− ,('\7929','y')
400− ,('\8175','`')
401− ,('\8490','K')
402− ,('\8800','=')
403− ,('\8814','<')
404− ,('\8815','>')
405− ]
src/Text/Pandoc/BCP47.hsdeleted−112
@@ -1,112 +0,0 @@
1−{-# LANGUAGE OverloadedStrings #-}
2−{- |
3− Module : Text.Pandoc.BCP47
4− Copyright : Copyright (C) 2017-2021 John MacFarlane
5− License : GNU GPL, version 2 or above
6−
7− Maintainer : John MacFarlane <jgm@berkeley.edu>
8− Stability : alpha
9− Portability : portable
10−
11−Functions for parsing and rendering BCP47 language identifiers.
12−-}
13−module Text.Pandoc.BCP47 (
14− getLang
15− , parseBCP47
16− , Lang(..)
17− , renderLang
18− )
19−where
20−import Control.Monad (guard)
21−import Data.Char (isAlphaNum, isAscii, isLetter, isLower, isUpper)
22−import Text.Pandoc.Definition
23−import Text.Pandoc.Options
24−import Text.DocTemplates (FromContext(..))
25−import qualified Data.Text as T
26−import qualified Text.Parsec as P
27−
28−-- | Represents BCP 47 language/country code.
29−data Lang = Lang{ langLanguage :: T.Text
30− , langScript :: T.Text
31− , langRegion :: T.Text
32− , langVariants :: [T.Text] }
33− deriving (Eq, Ord, Show)
34−
35−-- | Render a Lang as BCP 47.
36−renderLang :: Lang -> T.Text
37−renderLang lang = T.intercalate "-" (langLanguage lang : filter (not . T.null)
38− ([langScript lang, langRegion lang] ++ langVariants lang))
39−
40−-- | Get the contents of the `lang` metadata field or variable.
41−getLang :: WriterOptions -> Meta -> Maybe T.Text
42−getLang opts meta =
43− case lookupContext "lang" (writerVariables opts) of
44− Just s -> Just s
45− _ ->
46− case lookupMeta "lang" meta of
47− Just (MetaBlocks [Para [Str s]]) -> Just s
48− Just (MetaBlocks [Plain [Str s]]) -> Just s
49− Just (MetaInlines [Str s]) -> Just s
50− Just (MetaString s) -> Just s
51− _ -> Nothing
52−
53−-- | Parse a BCP 47 string as a Lang. Currently we parse
54−-- extensions and private-use fields as "variants," even
55−-- though officially they aren't.
56−parseBCP47 :: T.Text -> Either T.Text Lang
57−parseBCP47 lang =
58− case P.parse bcp47 "lang" lang of
59− Right r -> Right r
60− Left e -> Left $ T.pack $ show e
61− where bcp47 = do
62− language <- pLanguage
63− script <- P.option "" pScript
64− region <- P.option "" pRegion
65− variants <- P.many (pVariant P.<|> pExtension P.<|> pPrivateUse)
66− P.eof
67− return Lang{ langLanguage = language
68− , langScript = script
69− , langRegion = region
70− , langVariants = variants }
71− asciiLetter = P.satisfy (\c -> isAscii c && isLetter c)
72− pLanguage = do
73− cs <- P.many1 asciiLetter
74− let lcs = length cs
75− guard $ lcs == 2 || lcs == 3
76− return $ T.toLower $ T.pack cs
77− pScript = P.try $ do
78− P.char '-'
79− x <- P.satisfy (\c -> isAscii c && isLetter c && isUpper c)
80− xs <- P.count 3
81− (P.satisfy (\c -> isAscii c && isLetter c && isLower c))
82− return $ T.toLower $ T.pack (x:xs)
83− pRegion = P.try $ do
84− P.char '-'
85− cs <- P.many1 asciiLetter
86− let lcs = length cs
87− guard $ lcs == 2 || lcs == 3
88− return $ T.toUpper $ T.pack cs
89− pVariant = P.try $ do
90− P.char '-'
91− ds <- P.option "" (P.count 1 P.digit)
92− cs <- P.many1 asciiLetter
93− let var = ds ++ cs
94− lv = length var
95− guard $ if null ds
96− then lv >= 5 && lv <= 8
97− else lv == 4
98− return $ T.toLower $ T.pack var
99− pExtension = P.try $ do
100− P.char '-'
101− cs <- P.many1 $ P.satisfy (\c -> isAscii c && isAlphaNum c)
102− let lcs = length cs
103− guard $ lcs >= 2 && lcs <= 8
104− return $ T.toLower $ T.pack cs
105− pPrivateUse = P.try $ do
106− P.char '-'
107− P.char 'x'
108− P.char '-'
109− cs <- P.many1 $ P.satisfy (\c -> isAscii c && isAlphaNum c)
110− guard $ not (null cs) && length cs <= 8
111− let var = "x-" ++ cs
112− return $ T.toLower $ T.pack var
src/Text/Pandoc/CSV.hs+1−2
@@ -68,8 +68,7 @@ escaped opts = try $
68
68
69pCSVUnquotedCell :: CSVOptions -> Parser Text
69pCSVUnquotedCell :: CSVOptions -> Parser Text
70pCSVUnquotedCell opts = T.pack <$>
70pCSVUnquotedCell opts = T.pack <$>
71− many (satisfy (\c -> c /= csvDelim opts && c /= '\r' && c /= '\n'
71+ many (satisfy (\c -> c /= csvDelim opts && c /= '\r' && c /= '\n'))
72− && c /= csvQuote opts))
73
72
74pCSVDelim :: CSVOptions -> Parser ()
73pCSVDelim :: CSVOptions -> Parser ()
75pCSVDelim opts = do
74pCSVDelim opts = do
src/Text/Pandoc/Citeproc.hs+76−57
@@ -18,7 +18,6 @@ import Text.Pandoc.Citeproc.CslJson (cslJsonToReferences)
18import Text.Pandoc.Citeproc.BibTeX (readBibtexString, Variant(..))
18import Text.Pandoc.Citeproc.BibTeX (readBibtexString, Variant(..))
19import Text.Pandoc.Citeproc.MetaValue (metaValueToReference, metaValueToText)
19import Text.Pandoc.Citeproc.MetaValue (metaValueToReference, metaValueToText)
20import Text.Pandoc.Readers.Markdown (yamlToRefs)
20import Text.Pandoc.Readers.Markdown (yamlToRefs)
21−import qualified Text.Pandoc.BCP47 as BCP47
22import Text.Pandoc.Builder (Inlines, Many(..), deleteMeta, setMeta)
21import Text.Pandoc.Builder (Inlines, Many(..), deleteMeta, setMeta)
23import qualified Text.Pandoc.Builder as B
22import qualified Text.Pandoc.Builder as B
24import Text.Pandoc.Definition as Pandoc
23import Text.Pandoc.Definition as Pandoc
@@ -28,7 +27,7 @@ import Text.Pandoc.Error (PandocError(..))
28import Text.Pandoc.Extensions (pandocExtensions)
27import Text.Pandoc.Extensions (pandocExtensions)
29import Text.Pandoc.Logging (LogMessage(..))
28import Text.Pandoc.Logging (LogMessage(..))
30import Text.Pandoc.Options (ReaderOptions(..))
29import Text.Pandoc.Options (ReaderOptions(..))
31−import Text.Pandoc.Shared (stringify, ordNub, blocksToInlines, tshow)
30+import Text.Pandoc.Shared (stringify, ordNub, tshow)
32import qualified Text.Pandoc.UTF8 as UTF8
31import qualified Text.Pandoc.UTF8 as UTF8
33import Text.Pandoc.Walk (query, walk, walkM)
32import Text.Pandoc.Walk (query, walk, walkM)
34import Control.Applicative ((<|>))
33import Control.Applicative ((<|>))
@@ -49,7 +48,6 @@ import Data.Text (Text)
49import qualified Data.Text as T
48import qualified Data.Text as T
50import System.FilePath (takeExtension)
49import System.FilePath (takeExtension)
51import Safe (lastMay, initSafe)
50import Safe (lastMay, initSafe)
52−-- import Debug.Trace as Trace (trace, traceShowId)
53
51
54
52
55processCitations :: PandocMonad m => Pandoc -> m Pandoc
53processCitations :: PandocMonad m => Pandoc -> m Pandoc
@@ -76,8 +74,7 @@ processCitations (Pandoc meta bs) = do
76
74
77 let linkCites = maybe False truish $ lookupMeta "link-citations" meta
75 let linkCites = maybe False truish $ lookupMeta "link-citations" meta
78 let opts = defaultCiteprocOptions{ linkCitations = linkCites }
76 let opts = defaultCiteprocOptions{ linkCitations = linkCites }
79− let result = Citeproc.citeproc opts style (localeLanguage locale)
77+ let result = Citeproc.citeproc opts style mblang refs citations
80− refs citations
81 mapM_ (report . CiteprocWarning) (resultWarnings result)
78 mapM_ (report . CiteprocWarning) (resultWarnings result)
82 let sopts = styleOptions style
79 let sopts = styleOptions style
83 let classes = "references" : -- TODO remove this or keep for compatibility?
80 let classes = "references" : -- TODO remove this or keep for compatibility?
@@ -91,24 +88,21 @@ processCitations (Pandoc meta bs) = do
91 _ -> id) $ []
88 _ -> id) $ []
92 let bibs = mconcat $ map (\(ident, out) ->
89 let bibs = mconcat $ map (\(ident, out) ->
93 B.divWith ("ref-" <> ident,["csl-entry"],[]) . B.para .
90 B.divWith ("ref-" <> ident,["csl-entry"],[]) . B.para .
94− walk (convertQuotes locale) . insertSpace $ out)
91+ walk (convertQuotes locale) .
92+ insertSpace $ out)
95 (resultBibliography result)
93 (resultBibliography result)
96− let moveNotes = maybe True truish $
94+ let moveNotes = styleIsNoteStyle sopts &&
97− lookupMeta "notes-after-punctuation" meta
95+ maybe True truish (lookupMeta "notes-after-punctuation" meta)
98− let cits = map (walk fixLinks . walk (convertQuotes locale)) $
96+ let cits = map (walk (convertQuotes locale)) $
99 resultCitations result
97 resultCitations result
100
98
101− let fixQuotes = case localePunctuationInQuote locale of
102− Just True ->
103− B.toList . movePunctuationInsideQuotes . B.fromList
104− _ -> id
105−
106 let metanocites = lookupMeta "nocite" meta
99 let metanocites = lookupMeta "nocite" meta
107 let Pandoc meta'' bs' =
100 let Pandoc meta'' bs' =
108 maybe id (setMeta "nocite") metanocites .
101 maybe id (setMeta "nocite") metanocites .
109− walk (map capitalizeNoteCitation .
110− fixQuotes . mvPunct moveNotes locale) .
102+ walk (mvPunct moveNotes locale) .
103+ (if styleIsNoteStyle sopts
111− walk deNote .
104+ then walk addNote . walk deNote
105+ else id) .
112 evalState (walkM insertResolvedCitations $ Pandoc meta' bs)
106 evalState (walkM insertResolvedCitations $ Pandoc meta' bs)
113 $ cits
107 $ cits
114 return $ Pandoc meta''
108 return $ Pandoc meta''
@@ -197,7 +191,9 @@ getReferences mblocale (Pandoc meta bs) = do
197 then const True
191 then const True
198 else (`Set.member` citeIds)
192 else (`Set.member` citeIds)
199 let inlineRefs = case lookupMeta "references" meta of
193 let inlineRefs = case lookupMeta "references" meta of
194+ Just (MetaList rs) ->
195+ filter (idpred . unItemId . referenceId)
200− Just (MetaList rs) -> mapMaybe metaValueToReference rs
196+ $ mapMaybe metaValueToReference rs
201 _ -> []
197 _ -> []
202 externalRefs <- case lookupMeta "bibliography" meta of
198 externalRefs <- case lookupMeta "bibliography" meta of
203 Just (MetaList xs) ->
199 Just (MetaList xs) ->
@@ -294,7 +290,7 @@ insertResolvedCitations (Cite cs ils) = do
294 [] -> return (Cite cs ils)
290 [] -> return (Cite cs ils)
295 (x:xs) -> do
291 (x:xs) -> do
296 put xs
292 put xs
297− return $ Cite cs (B.toList x)
293+ return $ Cite cs (walk fixLinks $ B.toList x)
298insertResolvedCitations x = return x
294insertResolvedCitations x = return x
299
295
300getCitations :: Locale
296getCitations :: Locale
@@ -376,7 +372,6 @@ formatFromExtension fp = case dropWhile (== '.') $ takeExtension fp of
376
372
377
373
378isNote :: Inline -> Bool
374isNote :: Inline -> Bool
379−isNote (Note _) = True
380isNote (Cite _ [Note _]) = True
375isNote (Cite _ [Note _]) = True
381 -- the following allows citation styles that are "in-text" but use superscript
376 -- the following allows citation styles that are "in-text" but use superscript
382 -- references to be treated as if they are "notes" for the purposes of moving
377 -- references to be treated as if they are "notes" for the purposes of moving
@@ -389,6 +384,12 @@ isSpacy Space = True
389isSpacy SoftBreak = True
384isSpacy SoftBreak = True
390isSpacy _ = False
385isSpacy _ = False
391
386
387+movePunctInsideQuotes :: Locale -> [Inline] -> [Inline]
388+movePunctInsideQuotes locale
389+ | localePunctuationInQuote locale == Just True
390+ = B.toList . movePunctuationInsideQuotes . B.fromList
391+ | otherwise
392+ = id
392
393
393mvPunct :: Bool -> Locale -> [Inline] -> [Inline]
394mvPunct :: Bool -> Locale -> [Inline] -> [Inline]
394mvPunct moveNotes locale (x : xs)
395mvPunct moveNotes locale (x : xs)
@@ -401,7 +402,8 @@ mvPunct moveNotes locale (q : s : x : ys)
401 in if moveNotes
402 in if moveNotes
402 then if T.null spunct
403 then if T.null spunct
403 then q : x : mvPunct moveNotes locale ys
404 then q : x : mvPunct moveNotes locale ys
405+ else movePunctInsideQuotes locale
404− else q : Str spunct : x : mvPunct moveNotes locale
406+ [q , Str spunct , x] ++ mvPunct moveNotes locale
405 (B.toList
407 (B.toList
406 (dropTextWhile isPunctuation (B.fromList ys)))
408 (dropTextWhile isPunctuation (B.fromList ys)))
407 else q : x : mvPunct moveNotes locale ys
409 else q : x : mvPunct moveNotes locale ys
@@ -413,9 +415,10 @@ mvPunct moveNotes locale (Cite cs ils : ys)
413 , moveNotes
415 , moveNotes
414 = let s = stringify ys
416 = let s = stringify ys
415 spunct = T.takeWhile isPunctuation s
417 spunct = T.takeWhile isPunctuation s
418+ in Cite cs (movePunctInsideQuotes locale $
416− in Cite cs (init ils
419+ init ils
417− ++ [Str spunct | not (endWithPunct False (init ils))]
420+ ++ [Str spunct | not (endWithPunct False (init ils))]
418− ++ [last ils]) :
421+ ++ [last ils]) :
419 mvPunct moveNotes locale
422 mvPunct moveNotes locale
420 (B.toList (dropTextWhile isPunctuation (B.fromList ys)))
423 (B.toList (dropTextWhile isPunctuation (B.fromList ys)))
421mvPunct moveNotes locale (s : x : ys) | isSpacy s, isNote x =
424mvPunct moveNotes locale (s : x : ys) | isSpacy s, isNote x =
@@ -431,7 +434,7 @@ mvPunct _ _ [] = []
431-- move https://doi.org etc. prefix inside link text (#6723):
434-- move https://doi.org etc. prefix inside link text (#6723):
432fixLinks :: [Inline] -> [Inline]
435fixLinks :: [Inline] -> [Inline]
433fixLinks (Str t : Link attr [Str u1] (u2,tit) : xs)
436fixLinks (Str t : Link attr [Str u1] (u2,tit) : xs)
434− | t <> u1 == u2
437+ | u2 == t <> u1
435 = Link attr [Str (t <> u1)] (u2,tit) : fixLinks xs
438 = Link attr [Str (t <> u1)] (u2,tit) : fixLinks xs
436fixLinks (x:xs) = x : fixLinks xs
439fixLinks (x:xs) = x : fixLinks xs
437fixLinks [] = []
440fixLinks [] = []
@@ -501,7 +504,8 @@ insertRefs refkvs refclasses meta refs bs =
501 put True
504 put True
502 -- refHeader isn't used if you have an explicit references div
505 -- refHeader isn't used if you have an explicit references div
503 let cs' = ordNub $ cs ++ refclasses
506 let cs' = ordNub $ cs ++ refclasses
507+ let kvs' = ordNub $ kvs ++ refkvs
504− return $ Div ("refs",cs',kvs) (xs ++ refs)
508+ return $ Div ("refs",cs',kvs') (xs ++ refs)
505 go x = return x
509 go x = return x
506
510
507refTitle :: Meta -> Maybe [Inline]
511refTitle :: Meta -> Maybe [Inline]
@@ -544,7 +548,7 @@ linkifyVariables ref =
544 fixShortDOI x = let x' = extractText x
548 fixShortDOI x = let x' = extractText x
545 in if "10/" `T.isPrefixOf` x'
549 in if "10/" `T.isPrefixOf` x'
546 then TextVal $ T.drop 3 x'
550 then TextVal $ T.drop 3 x'
547− -- see http://shortdoi.org
551+ -- see https://shortdoi.org
548 else TextVal x'
552 else TextVal x'
549 tolink pref x = let x' = extractText x
553 tolink pref x = let x' = extractText x
550 x'' = if "://" `T.isInfixOf` x'
554 x'' = if "://" `T.isInfixOf` x'
@@ -560,42 +564,62 @@ extractText (FancyVal x) = toText x
560extractText (NumVal n) = T.pack (show n)
564extractText (NumVal n) = T.pack (show n)
561extractText _ = mempty
565extractText _ = mempty
562
566
567+-- Here we take the Spans with class csl-note that are left
568+-- after deNote has removed nested ones, and convert them
569+-- into real notes.
563−capitalizeNoteCitation :: Inline -> Inline
570+addNote :: Inline -> Inline
564−capitalizeNoteCitation (Cite cs [Note [Para ils]]) =
571+addNote (Span ("",["csl-note"],[]) ils) =
565− Cite cs
572+ Note [Para $
566− [Note [Para $ B.toList $ addTextCase Nothing CapitalizeFirst
573+ B.toList . addTextCase Nothing CapitalizeFirst . B.fromList $ ils]
567− $ B.fromList ils]]
574+addNote x = x
568−capitalizeNoteCitation x = x
575+
569−
576+-- Here we handle citation notes that occur inside footnotes
577+-- or other citation notes, in a note style. We don't want
578+-- notes inside notes, so we convert these to parenthesized
579+-- or comma-separated citations.
570−deNote :: [Inline] -> [Inline]
580+deNote :: Inline -> Inline
571−deNote [] = []
581+deNote (Note bs) =
572−deNote (Note bs:rest) =
582+ case bs of
573− Note (walk go bs) : deNote rest
583+ [Para (cit@(Cite (c:_) _) : ils)]
584+ | citationMode c /= AuthorInText ->
585+ -- if citation is first in note, no need to parenthesize.
586+ Note [Para (walk removeNotes $ cit : walk addParens ils)]
587+ _ -> Note (walk removeNotes . walk addParens $ bs)
588+
574 where
589 where
575− go [] = []
590+ addParens [] = []
576− go (Cite (c:cs) ils : zs)
591+ addParens (Cite (c:cs) ils : zs)
577 | citationMode c == AuthorInText
592 | citationMode c == AuthorInText
578− = Cite (c:cs) (concatMap (noteAfterComma (needsPeriod zs)) ils) : go zs
593+ = Cite (c:cs) (concatMap (noteAfterComma (needsPeriod zs)) ils) :
594+ addParens zs
579 | otherwise
595 | otherwise
580− = Cite (c:cs) (concatMap noteInParens ils) : go zs
596+ = Cite (c:cs) (concatMap noteInParens ils) : addParens zs
581− go (x:xs) = x : go xs
597+ addParens (x:xs) = x : addParens xs
598+
599+ removeNotes (Span ("",["csl-note"],[]) ils) = Span ("",[],[]) ils
600+ removeNotes x = x
601+
582 needsPeriod [] = True
602 needsPeriod [] = True
583 needsPeriod (Str t:_) = case T.uncons t of
603 needsPeriod (Str t:_) = case T.uncons t of
584 Nothing -> False
604 Nothing -> False
585 Just (c,_) -> isUpper c
605 Just (c,_) -> isUpper c
586 needsPeriod (Space:zs) = needsPeriod zs
606 needsPeriod (Space:zs) = needsPeriod zs
587 needsPeriod _ = False
607 needsPeriod _ = False
608+
588− noteInParens (Note bs')
609+ noteInParens (Span ("",["csl-note"],[]) ils)
589 = Space : Str "(" :
610 = Space : Str "(" :
590− removeFinalPeriod (blocksToInlines bs') ++ [Str ")"]
611+ removeFinalPeriod ils ++ [Str ")"]
591 noteInParens x = [x]
612 noteInParens x = [x]
613+
592− noteAfterComma needsPer (Note bs')
614+ noteAfterComma needsPer (Span ("",["csl-note"],[]) ils)
615+ | not (null ils)
593 = Str "," : Space :
616 = Str "," : Space :
594− (if needsPer
617+ if needsPer
595− then id
618+ then ils
596− else removeFinalPeriod) (blocksToInlines bs')
619+ else removeFinalPeriod ils
597 noteAfterComma _ x = [x]
620 noteAfterComma _ x = [x]
621+
598−deNote (x:xs) = x : deNote xs
622+deNote x = x
599
623
600-- Note: we can't use dropTextWhileEnd indiscriminately,
624-- Note: we can't use dropTextWhileEnd indiscriminately,
601-- because this would remove the final period on abbreviations like Ibid.
625-- because this would remove the final period on abbreviations like Ibid.
@@ -630,13 +654,8 @@ removeFinalPeriod ils =
630
654
631bcp47LangToIETF :: PandocMonad m => Text -> m (Maybe Lang)
655bcp47LangToIETF :: PandocMonad m => Text -> m (Maybe Lang)
632bcp47LangToIETF bcplang =
656bcp47LangToIETF bcplang =
633− case BCP47.parseBCP47 bcplang of
657+ case parseLang bcplang of
634 Left _ -> do
658 Left _ -> do
635 report $ InvalidLang bcplang
659 report $ InvalidLang bcplang
636 return Nothing
660 return Nothing
637− Right lang ->
638− return $ Just
661+ Right lang -> return $ Just lang
639− $ Lang (BCP47.langLanguage lang)
640− (if T.null (BCP47.langRegion lang)
641− then Nothing
642− else Just (BCP47.langRegion lang))
src/Text/Pandoc/Citeproc/BibTeX.hs+67−74
@@ -52,15 +52,18 @@ import Data.Char (isAlphaNum, isDigit, isLetter,
52import Data.List (foldl', intercalate, intersperse)
52import Data.List (foldl', intercalate, intersperse)
53import Safe (readMay)
53import Safe (readMay)
54import Text.Printf (printf)
54import Text.Printf (printf)
55+import Text.DocLayout (literal, hsep, nest, hang, Doc(..),
56+ braces, ($$), cr)
55
57
56data Variant = Bibtex | Biblatex
58data Variant = Bibtex | Biblatex
57 deriving (Show, Eq, Ord)
59 deriving (Show, Eq, Ord)
58
60
59-- | Parse BibTeX or BibLaTeX into a list of 'Reference's.
61-- | Parse BibTeX or BibLaTeX into a list of 'Reference's.
62+readBibtexString :: ToSources a
60−readBibtexString :: Variant -- ^ bibtex or biblatex
63+ => Variant -- ^ bibtex or biblatex
61 -> Locale -- ^ Locale
64 -> Locale -- ^ Locale
62 -> (Text -> Bool) -- ^ Filter on citation ids
65 -> (Text -> Bool) -- ^ Filter on citation ids
63− -> Text -- ^ bibtex/biblatex text
66+ -> a -- ^ bibtex/biblatex text
64 -> Either ParseError [Reference Inlines]
67 -> Either ParseError [Reference Inlines]
65readBibtexString variant locale idpred contents = do
68readBibtexString variant locale idpred contents = do
66 case runParser (((resolveCrossRefs variant <$> bibEntries) <* eof) >>=
69 case runParser (((resolveCrossRefs variant <$> bibEntries) <* eof) >>=
@@ -68,19 +71,22 @@ readBibtexString variant locale idpred contents = do
68 filter (\item -> idpred (identifier item) &&
71 filter (\item -> idpred (identifier item) &&
69 entryType item /= "xdata"))
72 entryType item /= "xdata"))
70 (fromMaybe defaultLang $ localeLanguage locale, Map.empty)
73 (fromMaybe defaultLang $ localeLanguage locale, Map.empty)
71− "" contents of
74+ (initialSourceName sources) sources of
72 Left err -> Left err
75 Left err -> Left err
73 Right xs -> return xs
76 Right xs -> return xs
77+ where
78+ sources = toSources contents
74
79
75-- | Write BibTeX or BibLaTeX given given a 'Reference'.
80-- | Write BibTeX or BibLaTeX given given a 'Reference'.
76writeBibtexString :: WriterOptions -- ^ options (for writing LaTex)
81writeBibtexString :: WriterOptions -- ^ options (for writing LaTex)
77 -> Variant -- ^ bibtex or biblatex
82 -> Variant -- ^ bibtex or biblatex
78 -> Maybe Lang -- ^ Language
83 -> Maybe Lang -- ^ Language
79 -> Reference Inlines -- ^ Reference to write
84 -> Reference Inlines -- ^ Reference to write
80− -> Text
85+ -> Doc Text
81writeBibtexString opts variant mblang ref =
86writeBibtexString opts variant mblang ref =
82− "@" <> bibtexType <> "{" <> unItemId (referenceId ref) <> ",\n " <>
87+ "@" <> bibtexType <> "{" <> literal (unItemId (referenceId ref)) <> ","
83− renderFields fs <> "\n}\n"
88+ $$ nest 2 (renderFields fs)
89+ $$ "}" <> cr
84
90
85 where
91 where
86 bibtexType =
92 bibtexType =
@@ -115,7 +121,7 @@ writeBibtexString opts variant mblang ref =
115 "motion_picture" | variant == Biblatex -> "movie"
121 "motion_picture" | variant == Biblatex -> "movie"
116 "review" | variant == Biblatex -> "review"
122 "review" | variant == Biblatex -> "review"
117 _ -> "misc"
123 _ -> "misc"
118−
124+
119 mbSubtype =
125 mbSubtype =
120 case referenceType ref of
126 case referenceType ref of
121 "article-magazine" -> Just "magazine"
127 "article-magazine" -> Just "magazine"
@@ -149,9 +155,10 @@ writeBibtexString opts variant mblang ref =
149 , "type"
155 , "type"
150 , "entrysubtype"
156 , "entrysubtype"
151 , "note"
157 , "note"
152− , "language"
158+ , "langid"
153 , "abstract"
159 , "abstract"
154 , "keywords"
160 , "keywords"
161+ , "annote"
155 ]
162 ]
156 Bibtex ->
163 Bibtex ->
157 [ "author"
164 [ "author"
@@ -171,6 +178,7 @@ writeBibtexString opts variant mblang ref =
171 , "address"
178 , "address"
172 , "type"
179 , "type"
173 , "note"
180 , "note"
181+ , "annote"
174 ]
182 ]
175
183
176 valToInlines (TextVal t) = B.text t
184 valToInlines (TextVal t) = B.text t
@@ -202,12 +210,22 @@ writeBibtexString opts variant mblang ref =
202 [ (", " <>) <$> nameGiven name,
210 [ (", " <>) <$> nameGiven name,
203 nameDroppingParticle name ]
211 nameDroppingParticle name ]
204
212
205− titlecase = case mblang of
213+ mblang' = case getVariableAsText "language" of
206− Just (Lang "en" _) -> titlecase'
214+ Just l -> either (const Nothing) Just $ parseLang l
207− Nothing -> titlecase'
215+ Nothing -> mblang
208− _ -> id
209
216
217+ titlecase = case mblang' of
218+ Just lang | langLanguage lang == "en"
219+ -> titlecase'
220+ Nothing -> titlecase'
221+ _ ->
222+ case variant of
223+ Bibtex -> B.spanWith nullAttr
224+ -- BibTex lacks a language field, so we wrap non-English
225+ -- titles in {} to protect case.
226+ Biblatex -> id
227+
210− titlecase' = addTextCase mblang TitleCase .
228+ titlecase' = addTextCase mblang' TitleCase .
211 (\ils -> B.fromList
229 (\ils -> B.fromList
212 (case B.toList ils of
230 (case B.toList ils of
213 Str t : xs -> Str t : Walk.walk spanAroundCapitalizedWords xs
231 Str t : xs -> Str t : Walk.walk spanAroundCapitalizedWords xs
@@ -224,10 +242,12 @@ writeBibtexString opts variant mblang ref =
224 toLaTeX x =
242 toLaTeX x =
225 case runPure (writeLaTeX opts $ doc (B.plain x)) of
243 case runPure (writeLaTeX opts $ doc (B.plain x)) of
226 Left _ -> Nothing
244 Left _ -> Nothing
227− Right t -> Just t
245+ Right t -> Just $ hsep . map literal $ T.words t
228
246
229− renderField name = (\contents -> name <> " = {" <> contents <> "}")
247+ renderField :: Text -> Maybe (Doc Text)
248+ renderField name =
249+ (((literal name) <>) . hang 2 " = " . braces)
230− <$> getContentsFor name
250+ <$> getContentsFor name
231
251
232 getVariable v = lookupVariable (toVariable v) ref
252 getVariable v = lookupVariable (toVariable v) ref
233
253
@@ -241,10 +261,10 @@ writeBibtexString opts variant mblang ref =
241 Nothing ->
261 Nothing ->
242 case dateParts date of
262 case dateParts date of
243 [DateParts (y1:_), DateParts (y2:_)] ->
263 [DateParts (y1:_), DateParts (y2:_)] ->
244− Just (T.pack (printf "%04d" y1) <> "--" <>
264+ Just $ literal (T.pack (printf "%04d" y1) <> "--" <>
245 T.pack (printf "%04d" y2))
265 T.pack (printf "%04d" y2))
246 [DateParts (y1:_)] ->
266 [DateParts (y1:_)] ->
247− Just (T.pack (printf "%04d" y1))
267+ Just $ literal (T.pack (printf "%04d" y1))
248 _ -> Nothing
268 _ -> Nothing
249 _ -> Nothing
269 _ -> Nothing
250
270
@@ -267,19 +287,19 @@ writeBibtexString opts variant mblang ref =
267 DateVal date ->
287 DateVal date ->
268 case dateParts date of
288 case dateParts date of
269 [DateParts (_:m1:_), DateParts (_:m2:_)] ->
289 [DateParts (_:m1:_), DateParts (_:m2:_)] ->
270− Just (toMonth m1 <> "--" <> toMonth m2)
290+ Just $ literal (toMonth m1 <> "--" <> toMonth m2)
271− [DateParts (_:m1:_)] -> Just (toMonth m1)
291+ [DateParts (_:m1:_)] -> Just $ literal (toMonth m1)
272 _ -> Nothing
292 _ -> Nothing
273 _ -> Nothing
293 _ -> Nothing
274
294
275− getContentsFor :: Text -> Maybe Text
295+ getContentsFor :: Text -> Maybe (Doc Text)
276 getContentsFor "type" =
296 getContentsFor "type" =
277 getVariableAsText "genre" >>=
297 getVariableAsText "genre" >>=
278 \case
298 \case
279 "mathesis" -> Just "mastersthesis"
299 "mathesis" -> Just "mastersthesis"
280 "phdthesis" -> Just "phdthesis"
300 "phdthesis" -> Just "phdthesis"
281 _ -> Nothing
301 _ -> Nothing
282− getContentsFor "entrysubtype" = mbSubtype
302+ getContentsFor "entrysubtype" = literal <$> mbSubtype
283 getContentsFor "journal"
303 getContentsFor "journal"
284 | bibtexType `elem` ["article", "periodical", "suppperiodical", "review"]
304 | bibtexType `elem` ["article", "periodical", "suppperiodical", "review"]
285 = getVariable "container-title" >>= toLaTeX . valToInlines
305 = getVariable "container-title" >>= toLaTeX . valToInlines
@@ -299,13 +319,15 @@ writeBibtexString opts variant mblang ref =
299 getContentsFor "urldate" = getVariable "accessed" >>= toLaTeX . valToInlines
319 getContentsFor "urldate" = getVariable "accessed" >>= toLaTeX . valToInlines
300 getContentsFor "year" = getVariable "issued" >>= getYear
320 getContentsFor "year" = getVariable "issued" >>= getYear
301 getContentsFor "month" = getVariable "issued" >>= getMonth
321 getContentsFor "month" = getVariable "issued" >>= getMonth
322+ getContentsFor "pages" = getVariable "page" >>= toLaTeX . valToInlines
323+ getContentsFor "langid" = getVariable "language" >>= toLaTeX . valToInlines
302 getContentsFor "number" = (getVariable "number"
324 getContentsFor "number" = (getVariable "number"
303 <|> getVariable "collection-number"
325 <|> getVariable "collection-number"
304 <|> getVariable "issue") >>= toLaTeX . valToInlines
326 <|> getVariable "issue") >>= toLaTeX . valToInlines
305
327
306 getContentsFor x = getVariable x >>=
328 getContentsFor x = getVariable x >>=
307 if isURL x
329 if isURL x
308− then Just . stringify . valToInlines
330+ then Just . literal . stringify . valToInlines
309 else toLaTeX .
331 else toLaTeX .
310 (if x == "title"
332 (if x == "title"
311 then titlecase
333 then titlecase
@@ -314,15 +336,15 @@ writeBibtexString opts variant mblang ref =
314
336
315 isURL x = x `elem` ["url","doi","issn","isbn"]
337 isURL x = x `elem` ["url","doi","issn","isbn"]
316
338
317− renderFields = T.intercalate ",\n " . mapMaybe renderField
339+ renderFields = mconcat . intersperse ("," <> cr) . mapMaybe renderField
318
340
319defaultLang :: Lang
341defaultLang :: Lang
320−defaultLang = Lang "en" (Just "US")
342+defaultLang = Lang "en" Nothing (Just "US") [] [] []
321
343
322-- a map of bibtex "string" macros
344-- a map of bibtex "string" macros
323type StringMap = Map.Map Text Text
345type StringMap = Map.Map Text Text
324
346
325−type BibParser = Parser Text (Lang, StringMap)
347+type BibParser = Parser Sources (Lang, StringMap)
326
348
327data Item = Item{ identifier :: Text
349data Item = Item{ identifier :: Text
328 , sourcePos :: SourcePos
350 , sourcePos :: SourcePos
@@ -337,9 +359,7 @@ itemToReference locale variant item = do
337 bib item $ do
359 bib item $ do
338 let lang = fromMaybe defaultLang $ localeLanguage locale
360 let lang = fromMaybe defaultLang $ localeLanguage locale
339 modify $ \st -> st{ localeLang = lang,
361 modify $ \st -> st{ localeLang = lang,
340− untitlecase = case lang of
362+ untitlecase = langLanguage lang == "en" }
341− (Lang "en" _) -> True
342− _ -> False }
343
363
344 id' <- asks identifier
364 id' <- asks identifier
345 otherIds <- (Just <$> getRawField "ids")
365 otherIds <- (Just <$> getRawField "ids")
@@ -563,10 +583,10 @@ itemToReference locale variant item = do
563 eprint <- getRawField "eprint"
583 eprint <- getRawField "eprint"
564 let baseUrl =
584 let baseUrl =
565 case T.toLower etype of
585 case T.toLower etype of
566− "arxiv" -> "http://arxiv.org/abs/"
586+ "arxiv" -> "https://arxiv.org/abs/"
567− "jstor" -> "http://www.jstor.org/stable/"
587+ "jstor" -> "https://www.jstor.org/stable/"
568− "pubmed" -> "http://www.ncbi.nlm.nih.gov/pubmed/"
588+ "pubmed" -> "https://www.ncbi.nlm.nih.gov/pubmed/"
569− "googlebooks" -> "http://books.google.com?id="
589+ "googlebooks" -> "https://books.google.com?id="
570 _ -> ""
590 _ -> ""
571 if T.null baseUrl
591 if T.null baseUrl
572 then mzero
592 then mzero
@@ -697,7 +717,7 @@ itemToReference locale variant item = do
697
717
698
718
699bib :: Item -> Bib a -> BibParser a
719bib :: Item -> Bib a -> BibParser a
700−bib entry m = fst <$> evalRWST m entry (BibState True (Lang "en" (Just "US")))
720+bib entry m = fst <$> evalRWST m entry (BibState True defaultLang)
701
721
702resolveCrossRefs :: Variant -> [Item] -> [Item]
722resolveCrossRefs :: Variant -> [Item] -> [Item]
703resolveCrossRefs variant entries =
723resolveCrossRefs variant entries =
@@ -750,41 +770,10 @@ blocksToInlines bs =
750 _ -> B.fromList $ Walk.query (:[]) bs
770 _ -> B.fromList $ Walk.query (:[]) bs
751
771
752adjustSpans :: Lang -> Inline -> Inline
772adjustSpans :: Lang -> Inline -> Inline
753−adjustSpans lang (RawInline (Format "latex") s)
773+adjustSpans lang (Span ("",[],[("bibstring",s)]) _) = Str $ resolveKey' lang s
754− | s == "\\hyphen" || s == "\\hyphen " = Str "-"
755− | otherwise = parseRawLaTeX lang s
756adjustSpans _ SoftBreak = Space
774adjustSpans _ SoftBreak = Space
757adjustSpans _ x = x
775adjustSpans _ x = x
758
776
759−parseRawLaTeX :: Lang -> Text -> Inline
760−parseRawLaTeX lang t@(T.stripPrefix "\\" -> Just xs) =
761− case parseLaTeX lang contents of
762− Right [Para ys] -> f command ys
763− Right [Plain ys] -> f command ys
764− Right [] -> f command []
765− _ -> RawInline (Format "latex") t
766− where (command', contents') = T.break (\c -> c =='{' || c =='\\') xs
767− command = T.strip command'
768− contents = T.drop 1 $ T.dropEnd 1 contents'
769− f "mkbibquote" ils = Span nullAttr [Quoted DoubleQuote ils]
770− f "mkbibemph" ils = Span nullAttr [Emph ils]
771− f "mkbibitalic" ils = Span nullAttr [Emph ils]
772− f "mkbibbold" ils = Span nullAttr [Strong ils]
773− f "mkbibparens" ils = Span nullAttr $
774− [Str "("] ++ ils ++ [Str ")"]
775− f "mkbibbrackets" ils = Span nullAttr $
776− [Str "["] ++ ils ++ [Str "]"]
777− -- ... both should be nestable & should work in year fields
778− f "autocap" ils = Span nullAttr ils
779− -- TODO: should work in year fields
780− f "textnormal" ils = Span ("",["nodecor"],[]) ils
781− f "bibstring" [Str s] = Str $ resolveKey' lang s
782− f "adddot" [] = Str "."
783− f "adddotspace" [] = Span nullAttr [Str ".", Space]
784− f "addabbrvspace" [] = Space
785− f _ ils = Span nullAttr ils
786−parseRawLaTeX _ t = RawInline (Format "latex") t
787−
788latex' :: Text -> Bib [Block]
777latex' :: Text -> Bib [Block]
789latex' t = do
778latex' t = do
790 lang <- gets localeLang
779 lang <- gets localeLang
@@ -820,7 +809,7 @@ bibEntries = do
820 (bibComment <|> bibPreamble <|> bibString))
809 (bibComment <|> bibPreamble <|> bibString))
821
810
822bibSkip :: BibParser ()
811bibSkip :: BibParser ()
823−bibSkip = () <$ take1WhileP (/='@')
812+bibSkip = skipMany1 (satisfy (/='@'))
824
813
825bibComment :: BibParser ()
814bibComment :: BibParser ()
826bibComment = do
815bibComment = do
@@ -845,6 +834,9 @@ bibString = do
845 updateState (\(l,m) -> (l, Map.insert k v m))
834 updateState (\(l,m) -> (l, Map.insert k v m))
846 return ()
835 return ()
847
836
837+take1WhileP :: Monad m => (Char -> Bool) -> ParserT Sources u m Text
838+take1WhileP f = T.pack <$> many1 (satisfy f)
839+
848inBraces :: BibParser Text
840inBraces :: BibParser Text
849inBraces = do
841inBraces = do
850 char '{'
842 char '{'
@@ -876,7 +868,7 @@ fieldName = resolveAlias . T.toLower
876
868
877isBibtexKeyChar :: Char -> Bool
869isBibtexKeyChar :: Char -> Bool
878isBibtexKeyChar c =
870isBibtexKeyChar c =
879− isAlphaNum c || c `elem` (".:;?!`'()/*@_+=-[]*&" :: [Char])
871+ isAlphaNum c || c `elem` (".:;?!`'()$/*@_+=-[]*&" :: [Char])
880
872
881bibItem :: BibParser Item
873bibItem :: BibParser Item
882bibItem = do
874bibItem = do
@@ -1060,14 +1052,14 @@ getOldDate prefix = do
1060 let dateparts = filter (\x -> x /= DateParts [])
1052 let dateparts = filter (\x -> x /= DateParts [])
1061 $ map toDateParts [(year',month',day'),
1053 $ map toDateParts [(year',month',day'),
1062 (endyear',endmonth',endday')]
1054 (endyear',endmonth',endday')]
1063− literal <- if null dateparts
1055+ literal' <- if null dateparts
1064− then Just <$> getRawField (prefix <> "year")
1056+ then Just <$> getRawField (prefix <> "year")
1065− else return Nothing
1057+ else return Nothing
1066 return $
1058 return $
1067 Date { dateParts = dateparts
1059 Date { dateParts = dateparts
1068 , dateCirca = False
1060 , dateCirca = False
1069 , dateSeason = Nothing
1061 , dateSeason = Nothing
1070− , dateLiteral = literal }
1062+ , dateLiteral = literal' }
1071
1063
1072getRawField :: Text -> Bib Text
1064getRawField :: Text -> Bib Text
1073getRawField f = do
1065getRawField f = do
@@ -1473,8 +1465,9 @@ resolveKey lang ils = Walk.walk go ils
1473 go x = x
1465 go x = x
1474
1466
1475resolveKey' :: Lang -> Text -> Text
1467resolveKey' :: Lang -> Text -> Text
1476−resolveKey' lang@(Lang l _) k =
1468+resolveKey' lang k =
1477− case Map.lookup l biblatexStringMap >>= Map.lookup (T.toLower k) of
1469+ case Map.lookup (langLanguage lang) biblatexStringMap >>=
1470+ Map.lookup (T.toLower k) of
1478 Nothing -> k
1471 Nothing -> k
1479 Just (x, _) -> either (const k) stringify $ parseLaTeX lang x
1472 Just (x, _) -> either (const k) stringify $ parseLaTeX lang x
1480
1473
src/Text/Pandoc/Citeproc/Data.hs+7−6
@@ -10,7 +10,7 @@ import qualified Data.Text.Encoding as TE
10import qualified Data.Text as T
10import qualified Data.Text as T
11import Data.Text (Text)
11import Data.Text (Text)
12import Text.Pandoc.Citeproc.Util (toIETF)
12import Text.Pandoc.Citeproc.Util (toIETF)
13−import Citeproc (Lang(..), parseLang)
13+import Text.Collate.Lang (Lang(..), parseLang)
14
14
15biblatexLocalizations :: [(FilePath, ByteString)]
15biblatexLocalizations :: [(FilePath, ByteString)]
16biblatexLocalizations = $(embedDir "citeproc/biblatex-localization")
16biblatexLocalizations = $(embedDir "citeproc/biblatex-localization")
@@ -21,11 +21,12 @@ biblatexStringMap :: M.Map Text (M.Map Text (Text, Text))
21biblatexStringMap = foldr go mempty biblatexLocalizations
21biblatexStringMap = foldr go mempty biblatexLocalizations
22 where
22 where
23 go (fp, bs) =
23 go (fp, bs) =
24− let Lang lang _ = parseLang (toIETF $ T.takeWhile (/= '.') $ T.pack fp)
25− ls = T.lines $ TE.decodeUtf8 bs
24+ let ls = T.lines $ TE.decodeUtf8 bs
25+ in case parseLang (toIETF $ T.takeWhile (/= '.') $ T.pack fp) of
26− in if length ls > 4
26+ Right lang | length ls > 4
27+ -> M.insert (langLanguage lang)
27− then M.insert lang (toStringMap $ map (T.splitOn "|") ls)
28+ (toStringMap $ map (T.splitOn "|") ls)
28− else id
29+ _ -> id
29 toStringMap = foldr go' mempty
30 toStringMap = foldr go' mempty
30 go' [term, x, y] = M.insert term (x, y)
31 go' [term, x, y] = M.insert term (x, y)
31 go' _ = id
32 go' _ = id
src/Text/Pandoc/Citeproc/Locator.hs+20−4
@@ -7,6 +7,7 @@ where
7import Citeproc.Types
7import Citeproc.Types
8import Data.Text (Text)
8import Data.Text (Text)
9import qualified Data.Text as T
9import qualified Data.Text as T
10+import Data.List (foldl')
10import Text.Parsec
11import Text.Parsec
11import Text.Pandoc.Definition
12import Text.Pandoc.Definition
12import Text.Pandoc.Parsing (romanNumeral)
13import Text.Pandoc.Parsing (romanNumeral)
@@ -19,7 +20,7 @@ parseLocator :: Locale -> [Inline] -> (Maybe (Text, Text), [Inline])
19parseLocator locale inp =
20parseLocator locale inp =
20 case parse (pLocatorWords (toLocatorMap locale)) "suffix" $ splitInp inp of
21 case parse (pLocatorWords (toLocatorMap locale)) "suffix" $ splitInp inp of
21 Right r -> r
22 Right r -> r
22− Left _ -> (Nothing, inp)
23+ Left _ -> (Nothing, maybeAddComma inp)
23
24
24splitInp :: [Inline] -> [Inline]
25splitInp :: [Inline] -> [Inline]
25splitInp = splitStrWhen (\c -> isSpace c || (isPunctuation c && c /= ':'))
26splitInp = splitStrWhen (\c -> isSpace c || (isPunctuation c && c /= ':'))
@@ -41,9 +42,17 @@ pLocatorWords locMap = do
41 -- i.e. the first one will be " 9"
42 -- i.e. the first one will be " 9"
42 return $
43 return $
43 if T.null la && T.null lo
44 if T.null la && T.null lo
44− then (Nothing, s)
45+ then (Nothing, maybeAddComma s)
45 else (Just (la, T.strip lo), s)
46 else (Just (la, T.strip lo), s)
46
47
48+maybeAddComma :: [Inline] -> [Inline]
49+maybeAddComma [] = []
50+maybeAddComma ils@(Space : _) = ils
51+maybeAddComma ils@(Str t : _)
52+ | Just (c, _) <- T.uncons t
53+ , isPunctuation c = ils
54+maybeAddComma ils = Str "," : Space : ils
55+
47pLocatorDelimited :: LocatorMap -> LocatorParser (Text, Text)
56pLocatorDelimited :: LocatorMap -> LocatorParser (Text, Text)
48pLocatorDelimited locMap = try $ do
57pLocatorDelimited locMap = try $ do
49 _ <- pMatchChar "{" (== '{')
58 _ <- pMatchChar "{" (== '{')
@@ -96,7 +105,7 @@ pLocatorLabel' locMap lim = go ""
96 t <- anyToken
105 t <- anyToken
97 ts <- manyTill anyToken (try $ lookAhead lim)
106 ts <- manyTill anyToken (try $ lookAhead lim)
98 let s = acc <> stringify (t:ts)
107 let s = acc <> stringify (t:ts)
99− case M.lookup (T.strip s) locMap of
108+ case M.lookup (T.toCaseFold $ T.strip s) locMap of
100 -- try to find a longer one, or return this one
109 -- try to find a longer one, or return this one
101 Just l -> go s <|> return (l, False)
110 Just l -> go s <|> return (l, False)
102 Nothing -> go s
111 Nothing -> go s
@@ -139,7 +148,7 @@ pBalancedBraces braces p = try $ do
139 where
148 where
140 except = notFollowedBy pBraces >> p
149 except = notFollowedBy pBraces >> p
141 -- outer and inner
150 -- outer and inner
142− surround = foldl (\a (open, close) -> sur open close except <|> a)
151+ surround = foldl' (\a (open, close) -> sur open close except <|> a)
143 except
152 except
144 braces
153 braces
145
154
@@ -180,6 +189,7 @@ pPageUnit = roman <|> plainUnit
180 plainUnit = do
189 plainUnit = do
181 ts <- many1 (notFollowedBy pSpace >>
190 ts <- many1 (notFollowedBy pSpace >>
182 notFollowedBy pLocatorPunct >>
191 notFollowedBy pLocatorPunct >>
192+ notFollowedBy pMath >>
183 anyToken)
193 anyToken)
184 let s = stringify ts
194 let s = stringify ts
185 -- otherwise look for actual digits or -s
195 -- otherwise look for actual digits or -s
@@ -210,6 +220,12 @@ pMatchChar msg f = satisfyTok f' <?> msg
210pSpace :: LocatorParser Inline
220pSpace :: LocatorParser Inline
211pSpace = satisfyTok (\t -> isSpacey t || t == Str "\160") <?> "space"
221pSpace = satisfyTok (\t -> isSpacey t || t == Str "\160") <?> "space"
212
222
223+pMath :: LocatorParser Inline
224+pMath = satisfyTok isMath
225+ where
226+ isMath (Math{}) = True
227+ isMath _ = False
228+
213satisfyTok :: (Inline -> Bool) -> LocatorParser Inline
229satisfyTok :: (Inline -> Bool) -> LocatorParser Inline
214satisfyTok f = tokenPrim show (\sp _ _ -> sp) (\tok -> if f tok
230satisfyTok f = tokenPrim show (\sp _ _ -> sp) (\tok -> if f tok
215 then Just tok
231 then Just tok
src/Text/Pandoc/Citeproc/MetaValue.hs+5−3
@@ -135,12 +135,13 @@ metaValueToVal k v
135 MetaMap _ -> TextVal mempty
135 MetaMap _ -> TextVal mempty
136
136
137metaValueToDate :: MetaValue -> Date
137metaValueToDate :: MetaValue -> Date
138−metaValueToDate (MetaMap m) =
138+metaValueToDate (MetaMap m) = fromMaybe
139− Date
139+ (Date
140 { dateParts = dateparts
140 { dateParts = dateparts
141 , dateCirca = circa
141 , dateCirca = circa
142 , dateSeason = season
142 , dateSeason = season
143− , dateLiteral = literal }
143+ , dateLiteral = literal })
144+ rawdate
144 where
145 where
145 dateparts = case M.lookup "date-parts" m of
146 dateparts = case M.lookup "date-parts" m of
146 Just (MetaList xs) ->
147 Just (MetaList xs) ->
@@ -152,6 +153,7 @@ metaValueToDate (MetaMap m) =
152 M.lookup "circa" m >>= metaValueToBool
153 M.lookup "circa" m >>= metaValueToBool
153 season = M.lookup "season" m >>= metaValueToInt
154 season = M.lookup "season" m >>= metaValueToInt
154 literal = M.lookup "literal" m >>= metaValueToText
155 literal = M.lookup "literal" m >>= metaValueToText
156+ rawdate = M.lookup "raw" m >>= metaValueToText >>= rawDateEDTF
155metaValueToDate (MetaList xs) =
157metaValueToDate (MetaList xs) =
156 Date{ dateParts = mapMaybe metaValueToDateParts xs
158 Date{ dateParts = mapMaybe metaValueToDateParts xs
157 , dateCirca = False
159 , dateCirca = False
src/Text/Pandoc/Class/CommonState.hs+1−1
@@ -19,7 +19,7 @@ where
19
19
20import Data.Default (Default (def))
20import Data.Default (Default (def))
21import Data.Text (Text)
21import Data.Text (Text)
22−import Text.Pandoc.BCP47 (Lang)
22+import Text.Collate.Lang (Lang)
23import Text.Pandoc.MediaBag (MediaBag)
23import Text.Pandoc.MediaBag (MediaBag)
24import Text.Pandoc.Logging (LogMessage, Verbosity (WARNING))
24import Text.Pandoc.Logging (LogMessage, Verbosity (WARNING))
25import Text.Pandoc.Translations (Translations)
25import Text.Pandoc.Translations (Translations)
src/Text/Pandoc/Class/IO.hs+24−25
@@ -62,7 +62,7 @@ import Text.Pandoc.Definition (Pandoc, Inline (Image))
62import Text.Pandoc.Error (PandocError (..))
62import Text.Pandoc.Error (PandocError (..))
63import Text.Pandoc.Logging (LogMessage (..), messageVerbosity, showLogMessage)
63import Text.Pandoc.Logging (LogMessage (..), messageVerbosity, showLogMessage)
64import Text.Pandoc.MIME (MimeType)
64import Text.Pandoc.MIME (MimeType)
65−import Text.Pandoc.MediaBag (MediaBag, lookupMedia, mediaDirectory)
65+import Text.Pandoc.MediaBag (MediaBag, MediaItem(..), lookupMedia, mediaItems)
66import Text.Pandoc.Walk (walk)
66import Text.Pandoc.Walk (walk)
67import qualified Control.Exception as E
67import qualified Control.Exception as E
68import qualified Data.ByteString as B
68import qualified Data.ByteString as B
@@ -183,7 +183,7 @@ getModificationTime = liftIOError System.Directory.getModificationTime
183logOutput :: (PandocMonad m, MonadIO m) => LogMessage -> m ()
183logOutput :: (PandocMonad m, MonadIO m) => LogMessage -> m ()
184logOutput msg = liftIO $ do
184logOutput msg = liftIO $ do
185 UTF8.hPutStr stderr $
185 UTF8.hPutStr stderr $
186− "[" ++ show (messageVerbosity msg) ++ "] "
186+ "[" <> T.pack (show (messageVerbosity msg)) <> "] "
187 alertIndent $ T.lines $ showLogMessage msg
187 alertIndent $ T.lines $ showLogMessage msg
188
188
189-- | Prints the list of lines to @stderr@, indenting every but the first
189-- | Prints the list of lines to @stderr@, indenting every but the first
@@ -191,42 +191,41 @@ logOutput msg = liftIO $ do
191alertIndent :: [Text] -> IO ()
191alertIndent :: [Text] -> IO ()
192alertIndent [] = return ()
192alertIndent [] = return ()
193alertIndent (l:ls) = do
193alertIndent (l:ls) = do
194− UTF8.hPutStrLn stderr $ unpack l
194+ UTF8.hPutStrLn stderr l
195 mapM_ go ls
195 mapM_ go ls
196 where go l' = do UTF8.hPutStr stderr " "
196 where go l' = do UTF8.hPutStr stderr " "
197− UTF8.hPutStrLn stderr $ unpack l'
197+ UTF8.hPutStrLn stderr l'
198
198
199-- | Extract media from the mediabag into a directory.
199-- | Extract media from the mediabag into a directory.
200extractMedia :: (PandocMonad m, MonadIO m) => FilePath -> Pandoc -> m Pandoc
200extractMedia :: (PandocMonad m, MonadIO m) => FilePath -> Pandoc -> m Pandoc
201extractMedia dir d = do
201extractMedia dir d = do
202 media <- getMediaBag
202 media <- getMediaBag
203− case [fp | (fp, _, _) <- mediaDirectory media] of
203+ let items = mediaItems media
204+ if null items
204− [] -> return d
205+ then return d
205− fps -> do
206+ else do
206− mapM_ (writeMedia dir media) fps
207+ mapM_ (writeMedia dir) items
207− return $ walk (adjustImagePath dir fps) d
208+ return $ walk (adjustImagePath dir media) d
208
209
209-- | Write the contents of a media bag to a path.
210-- | Write the contents of a media bag to a path.
210writeMedia :: (PandocMonad m, MonadIO m)
211writeMedia :: (PandocMonad m, MonadIO m)
211− => FilePath -> MediaBag -> FilePath
212+ => FilePath
213+ -> (FilePath, MimeType, BL.ByteString)
212 -> m ()
214 -> m ()
213−writeMedia dir mediabag subpath = do
215+writeMedia dir (fp, _mt, bs) = do
214− -- we join and split to convert a/b/c to a\b\c on Windows;
216+ -- we normalize to get proper path separators for the platform
215− -- in zip containers all paths use /
216− let fullpath = dir </> unEscapeString (normalise subpath)
217+ let fullpath = normalise $ dir </> fp
217− let mbcontents = lookupMedia subpath mediabag
218− case mbcontents of
219− Nothing -> throwError $ PandocResourceNotFound $ pack subpath
220− Just (_, bs) -> do
221− report $ Extracting $ pack fullpath
222− liftIOError (createDirectoryIfMissing True) (takeDirectory fullpath)
218+ liftIOError (createDirectoryIfMissing True) (takeDirectory fullpath)
223− logIOError $ BL.writeFile fullpath bs
219+ logIOError $ BL.writeFile fullpath bs
224
220
225-- | If the given Inline element is an image with a @src@ path equal to
221-- | If the given Inline element is an image with a @src@ path equal to
226-- one in the list of @paths@, then prepends @dir@ to the image source;
222-- one in the list of @paths@, then prepends @dir@ to the image source;
227-- returns the element unchanged otherwise.
223-- returns the element unchanged otherwise.
228−adjustImagePath :: FilePath -> [FilePath] -> Inline -> Inline
224+adjustImagePath :: FilePath -> MediaBag -> Inline -> Inline
229−adjustImagePath dir paths (Image attr lab (src, tit))
225+adjustImagePath dir mediabag (Image attr lab (src, tit)) =
230− | unpack src `elem` paths
226+ case lookupMedia (T.unpack src) mediabag of
227+ Nothing -> Image attr lab (src, tit)
228+ Just item ->
229+ let fullpath = dir </> mediaPath item
231− = Image attr lab (pack (normalise $ dir </> unpack src), tit)
230+ in Image attr lab (T.pack fullpath, tit)
232adjustImagePath _ _ x = x
231adjustImagePath _ _ x = x
src/Text/Pandoc/Class/PandocMonad.hs+43−36
@@ -1,4 +1,5 @@
1{-# LANGUAGE CPP #-}
1{-# LANGUAGE CPP #-}
2+{-# LANGUAGE TupleSections #-}
2{-# LANGUAGE FlexibleContexts #-}
3{-# LANGUAGE FlexibleContexts #-}
3{-# LANGUAGE FlexibleInstances #-}
4{-# LANGUAGE FlexibleInstances #-}
4{-# LANGUAGE MultiParamTypeClasses #-}
5{-# LANGUAGE MultiParamTypeClasses #-}
@@ -37,7 +38,6 @@ module Text.Pandoc.Class.PandocMonad
37 , setUserDataDir
38 , setUserDataDir
38 , getUserDataDir
39 , getUserDataDir
39 , fetchItem
40 , fetchItem
40− , fetchMediaResource
41 , getInputFiles
41 , getInputFiles
42 , setInputFiles
42 , setInputFiles
43 , getOutputFile
43 , getOutputFile
@@ -51,30 +51,31 @@ module Text.Pandoc.Class.PandocMonad
51 , setTranslations
51 , setTranslations
52 , translateTerm
52 , translateTerm
53 , makeCanonical
53 , makeCanonical
54+ , getTimestamp
54 ) where
55 ) where
55
56
56import Codec.Archive.Zip
57import Codec.Archive.Zip
57import Control.Monad.Except (MonadError (catchError, throwError),
58import Control.Monad.Except (MonadError (catchError, throwError),
58 MonadTrans, lift, when)
59 MonadTrans, lift, when)
59−import Data.Digest.Pure.SHA (sha1, showDigest)
60+import Data.List (foldl')
60−import Data.Maybe (fromMaybe)
61import Data.Time (UTCTime)
61import Data.Time (UTCTime)
62−import Data.Time.Clock.POSIX (POSIXTime, utcTimeToPOSIXSeconds)
62+import Data.Time.Clock.POSIX (POSIXTime, utcTimeToPOSIXSeconds,
63+ posixSecondsToUTCTime)
63import Data.Time.LocalTime (TimeZone, ZonedTime, utcToZonedTime)
64import Data.Time.LocalTime (TimeZone, ZonedTime, utcToZonedTime)
64import Network.URI ( escapeURIString, nonStrictRelativeTo,
65import Network.URI ( escapeURIString, nonStrictRelativeTo,
65 unEscapeString, parseURIReference, isAllowedInURI,
66 unEscapeString, parseURIReference, isAllowedInURI,
66 parseURI, URI(..) )
67 parseURI, URI(..) )
67−import System.FilePath ((</>), (<.>), takeExtension, dropExtension,
68+import System.FilePath ((</>), takeExtension, dropExtension,
68− isRelative, splitDirectories)
69+ isRelative, splitDirectories, makeRelative)
69import System.Random (StdGen)
70import System.Random (StdGen)
70−import Text.Pandoc.BCP47 (Lang(..), parseBCP47, renderLang)
71+import Text.Collate.Lang (Lang(..), parseLang, renderLang)
71import Text.Pandoc.Class.CommonState (CommonState (..))
72import Text.Pandoc.Class.CommonState (CommonState (..))
72import Text.Pandoc.Definition
73import Text.Pandoc.Definition
73import Text.Pandoc.Error
74import Text.Pandoc.Error
74import Text.Pandoc.Logging
75import Text.Pandoc.Logging
75−import Text.Pandoc.MIME (MimeType, getMimeType, extensionFromMimeType)
76+import Text.Pandoc.MIME (MimeType, getMimeType)
76−import Text.Pandoc.MediaBag (MediaBag, lookupMedia)
77+import Text.Pandoc.MediaBag (MediaBag, lookupMedia, MediaItem(..))
77−import Text.Pandoc.Shared (uriPathToPath)
78+import Text.Pandoc.Shared (uriPathToPath, safeRead)
78import Text.Pandoc.Translations (Term(..), Translations, lookupTerm,
79import Text.Pandoc.Translations (Term(..), Translations, lookupTerm,
79 readTranslations)
80 readTranslations)
80import Text.Pandoc.Walk (walkM)
81import Text.Pandoc.Walk (walkM)
@@ -175,6 +176,21 @@ report msg = do
175 when (level <= verbosity) $ logOutput msg
176 when (level <= verbosity) $ logOutput msg
176 modifyCommonState $ \st -> st{ stLog = msg : stLog st }
177 modifyCommonState $ \st -> st{ stLog = msg : stLog st }
177
178
179+-- | Get the time from the @SOURCE_DATE_EPOCH@
180+-- environment variable. The variable should contain a
181+-- unix time stamp, the number of seconds since midnight Jan 01
182+-- 1970 UTC. If the variable is not set or cannot be
183+-- parsed as a unix time stamp, the current time is returned.
184+-- This function is designed to make possible reproducible
185+-- builds in formats that include a creation timestamp.
186+getTimestamp :: PandocMonad m => m UTCTime
187+getTimestamp = do
188+ mbSourceDateEpoch <- lookupEnv "SOURCE_DATE_EPOCH"
189+ case mbSourceDateEpoch >>= safeRead of
190+ Just (epoch :: Integer) ->
191+ return $ posixSecondsToUTCTime $ fromIntegral epoch
192+ Nothing -> getCurrentTime
193+
178-- | Determine whether tracing is enabled. This affects
194-- | Determine whether tracing is enabled. This affects
179-- the behavior of 'trace'. If tracing is not enabled,
195-- the behavior of 'trace'. If tracing is not enabled,
180-- 'trace' does nothing.
196-- 'trace' does nothing.
@@ -267,7 +283,7 @@ readFileFromDirs (d:ds) f = catchError
267toLang :: PandocMonad m => Maybe T.Text -> m (Maybe Lang)
283toLang :: PandocMonad m => Maybe T.Text -> m (Maybe Lang)
268toLang Nothing = return Nothing
284toLang Nothing = return Nothing
269toLang (Just s) =
285toLang (Just s) =
270− case parseBCP47 s of
286+ case parseLang s of
271 Left _ -> do
287 Left _ -> do
272 report $ InvalidLang s
288 report $ InvalidLang s
273 return Nothing
289 return Nothing
@@ -358,7 +374,8 @@ fetchItem :: PandocMonad m
358fetchItem s = do
374fetchItem s = do
359 mediabag <- getMediaBag
375 mediabag <- getMediaBag
360 case lookupMedia (T.unpack s) mediabag of
376 case lookupMedia (T.unpack s) mediabag of
361− Just (mime, bs) -> return (BL.toStrict bs, Just mime)
377+ Just item -> return (BL.toStrict (mediaContents item),
378+ Just (mediaMimeType item))
362 Nothing -> downloadOrRead s
379 Nothing -> downloadOrRead s
363
380
364-- | Returns the content and, if available, the MIME type of a resource.
381-- | Returns the content and, if available, the MIME type of a resource.
@@ -393,9 +410,10 @@ downloadOrRead s = do
393 _ -> readLocalFile fp -- get from local file system
410 _ -> readLocalFile fp -- get from local file system
394 where readLocalFile f = do
411 where readLocalFile f = do
395 resourcePath <- getResourcePath
412 resourcePath <- getResourcePath
396− cont <- if isRelative f
413+ (fp', cont) <- if isRelative f
397− then withPaths resourcePath readFileStrict f
414+ then withPaths resourcePath readFileStrict f
398− else readFileStrict f
415+ else (f,) <$> readFileStrict f
416+ report $ LoadedResource f (makeRelative "." fp')
399 return (cont, mime)
417 return (cont, mime)
400 httpcolon = URI{ uriScheme = "http:",
418 httpcolon = URI{ uriScheme = "http:",
401 uriAuthority = Nothing,
419 uriAuthority = Nothing,
@@ -595,7 +613,7 @@ checkExistence fn = do
595-- | Canonicalizes a file path by removing redundant @.@ and @..@.
613-- | Canonicalizes a file path by removing redundant @.@ and @..@.
596makeCanonical :: FilePath -> FilePath
614makeCanonical :: FilePath -> FilePath
597makeCanonical = Posix.joinPath . transformPathParts . splitDirectories
615makeCanonical = Posix.joinPath . transformPathParts . splitDirectories
598− where transformPathParts = reverse . foldl go []
616+ where transformPathParts = reverse . foldl' go []
599 go as "." = as
617 go as "." = as
600 go (_:as) ".." = as
618 go (_:as) ".." = as
601 go as x = x : as
619 go as x = x : as
@@ -605,25 +623,13 @@ makeCanonical = Posix.joinPath . transformPathParts . splitDirectories
605-- that filepath. Returns the result of the first successful execution
623-- that filepath. Returns the result of the first successful execution
606-- of the action, or throws a @PandocResourceNotFound@ exception if the
624-- of the action, or throws a @PandocResourceNotFound@ exception if the
607-- action errors for all filepaths.
625-- action errors for all filepaths.
626+withPaths :: PandocMonad m
608−withPaths :: PandocMonad m => [FilePath] -> (FilePath -> m a) -> FilePath -> m a
627+ => [FilePath] -> (FilePath -> m a) -> FilePath -> m (FilePath, a)
609withPaths [] _ fp = throwError $ PandocResourceNotFound $ T.pack fp
628withPaths [] _ fp = throwError $ PandocResourceNotFound $ T.pack fp
610withPaths (p:ps) action fp =
629withPaths (p:ps) action fp =
611− catchError (action (p </> fp))
630+ catchError ((p </> fp,) <$> action (p </> fp))
612 (\_ -> withPaths ps action fp)
631 (\_ -> withPaths ps action fp)
613
632
614−-- | Fetch local or remote resource (like an image) and provide data suitable
615−-- for adding it to the MediaBag.
616−fetchMediaResource :: PandocMonad m
617− => T.Text -> m (FilePath, Maybe MimeType, BL.ByteString)
618−fetchMediaResource src = do
619− (bs, mt) <- downloadOrRead src
620− let ext = fromMaybe (T.pack $ takeExtension $ T.unpack src)
621− (mt >>= extensionFromMimeType)
622− let bs' = BL.fromChunks [bs]
623− let basename = showDigest $ sha1 bs'
624− let fname = basename <.> T.unpack ext
625− return (fname, mt, bs')
626−
627-- | Traverse tree, filling media bag for any images that
633-- | Traverse tree, filling media bag for any images that
628-- aren't already in the media bag.
634-- aren't already in the media bag.
629fillMediaBag :: PandocMonad m => Pandoc -> m Pandoc
635fillMediaBag :: PandocMonad m => Pandoc -> m Pandoc
@@ -631,12 +637,13 @@ fillMediaBag d = walkM handleImage d
631 where handleImage :: PandocMonad m => Inline -> m Inline
637 where handleImage :: PandocMonad m => Inline -> m Inline
632 handleImage (Image attr lab (src, tit)) = catchError
638 handleImage (Image attr lab (src, tit)) = catchError
633 (do mediabag <- getMediaBag
639 (do mediabag <- getMediaBag
634− case lookupMedia (T.unpack src) mediabag of
640+ let fp = T.unpack src
641+ case lookupMedia fp mediabag of
635− Just (_, _) -> return $ Image attr lab (src, tit)
642+ Just _ -> return ()
636 Nothing -> do
643 Nothing -> do
637− (fname, mt, bs) <- fetchMediaResource src
644+ (bs, mt) <- fetchItem src
638− insertMedia fname mt bs
645+ insertMedia fp mt (BL.fromStrict bs)
639− return $ Image attr lab (T.pack fname, tit))
646+ return $ Image attr lab (src, tit))
640 (\e ->
647 (\e ->
641 case e of
648 case e of
642 PandocResourceNotFound _ -> do
649 PandocResourceNotFound _ -> do
src/Text/Pandoc/Error.hs+101−48
@@ -16,38 +16,41 @@ encountered during parsing.
16-}
16-}
17module Text.Pandoc.Error (
17module Text.Pandoc.Error (
18 PandocError(..),
18 PandocError(..),
19+ renderError,
19 handleError) where
20 handleError) where
20
21
21−import Control.Exception (Exception)
22+import Control.Exception (Exception, displayException)
22import Data.Typeable (Typeable)
23import Data.Typeable (Typeable)
23import Data.Word (Word8)
24import Data.Word (Word8)
24import Data.Text (Text)
25import Data.Text (Text)
26+import Data.List (sortOn)
25import qualified Data.Text as T
27import qualified Data.Text as T
28+import Data.Ord (Down(..))
26import GHC.Generics (Generic)
29import GHC.Generics (Generic)
27import Network.HTTP.Client (HttpException)
30import Network.HTTP.Client (HttpException)
28import System.Exit (ExitCode (..), exitWith)
31import System.Exit (ExitCode (..), exitWith)
29import System.IO (stderr)
32import System.IO (stderr)
30import qualified Text.Pandoc.UTF8 as UTF8
33import qualified Text.Pandoc.UTF8 as UTF8
34+import Text.Pandoc.Sources (Sources(..))
31import Text.Printf (printf)
35import Text.Printf (printf)
32import Text.Parsec.Error
36import Text.Parsec.Error
33import Text.Parsec.Pos hiding (Line)
37import Text.Parsec.Pos hiding (Line)
34import Text.Pandoc.Shared (tshow)
38import Text.Pandoc.Shared (tshow)
35import Citeproc (CiteprocError, prettyCiteprocError)
39import Citeproc (CiteprocError, prettyCiteprocError)
36
40
37−type Input = Text
38−
39data PandocError = PandocIOError Text IOError
41data PandocError = PandocIOError Text IOError
40 | PandocHttpError Text HttpException
42 | PandocHttpError Text HttpException
41 | PandocShouldNeverHappenError Text
43 | PandocShouldNeverHappenError Text
42 | PandocSomeError Text
44 | PandocSomeError Text
43 | PandocParseError Text
45 | PandocParseError Text
44− | PandocParsecError Input ParseError
46+ | PandocParsecError Sources ParseError
45 | PandocMakePDFError Text
47 | PandocMakePDFError Text
46 | PandocOptionError Text
48 | PandocOptionError Text
47 | PandocSyntaxMapError Text
49 | PandocSyntaxMapError Text
48 | PandocFailOnWarningError
50 | PandocFailOnWarningError
49 | PandocPDFProgramNotFoundError Text
51 | PandocPDFProgramNotFoundError Text
50 | PandocPDFError Text
52 | PandocPDFError Text
53+ | PandocXMLError Text Text
51 | PandocFilterError Text Text
54 | PandocFilterError Text Text
52 | PandocLuaError Text
55 | PandocLuaError Text
53 | PandocCouldNotFindDataFileError Text
56 | PandocCouldNotFindDataFileError Text
@@ -58,6 +61,7 @@ data PandocError = PandocIOError Text IOError
58 | PandocMacroLoop Text
61 | PandocMacroLoop Text
59 | PandocUTF8DecodingError Text Int Word8
62 | PandocUTF8DecodingError Text Int Word8
60 | PandocIpynbDecodingError Text
63 | PandocIpynbDecodingError Text
64+ | PandocUnsupportedCharsetError Text
61 | PandocUnknownReaderError Text
65 | PandocUnknownReaderError Text
62 | PandocUnknownWriterError Text
66 | PandocUnknownWriterError Text
63 | PandocUnsupportedExtensionError Text Text
67 | PandocUnsupportedExtensionError Text Text
@@ -67,62 +71,70 @@ data PandocError = PandocIOError Text IOError
67
71
68instance Exception PandocError
72instance Exception PandocError
69
73
70−-- | Handle PandocError by exiting with an error message.
74+renderError :: PandocError -> Text
71−handleError :: Either PandocError a -> IO a
75+renderError e =
72−handleError (Right r) = return r
73−handleError (Left e) =
74 case e of
76 case e of
75− PandocIOError _ err' -> ioError err'
77+ PandocIOError _ err' -> T.pack $ displayException err'
76− PandocHttpError u err' -> err 61 $
78+ PandocHttpError u err' ->
77 "Could not fetch " <> u <> "\n" <> tshow err'
79 "Could not fetch " <> u <> "\n" <> tshow err'
78− PandocShouldNeverHappenError s -> err 62 $
80+ PandocShouldNeverHappenError s ->
79 "Something we thought was impossible happened!\n" <>
81 "Something we thought was impossible happened!\n" <>
80 "Please report this to pandoc's developers: " <> s
82 "Please report this to pandoc's developers: " <> s
81− PandocSomeError s -> err 63 s
83+ PandocSomeError s -> s
82− PandocParseError s -> err 64 s
84+ PandocParseError s -> s
83− PandocParsecError input err' ->
85+ PandocParsecError (Sources inputs) err' ->
84 let errPos = errorPos err'
86 let errPos = errorPos err'
85 errLine = sourceLine errPos
87 errLine = sourceLine errPos
86 errColumn = sourceColumn errPos
88 errColumn = sourceColumn errPos
89+ errFile = sourceName errPos
90+ errorInFile =
91+ case sortOn (Down . sourceLine . fst)
92+ [ (pos,t)
93+ | (pos,t) <- inputs
94+ , sourceName pos == errFile
95+ , sourceLine pos <= errLine
96+ ] of
97+ [] -> ""
98+ ((pos,txt):_) ->
87− ls = T.lines input <> [""]
99+ let ls = T.lines txt <> [""]
100+ ln = (errLine - sourceLine pos) + 1
88− errorInFile = if length ls > errLine - 1
101+ in if length ls > ln && ln >= 1
89− then T.concat ["\n", ls !! (errLine - 1)
102+ then T.concat ["\n", ls !! (ln - 1)
90− ,"\n", T.replicate (errColumn - 1) " "
103+ ,"\n", T.replicate (errColumn - 1) " "
91− ,"^"]
104+ ,"^"]
92− else ""
105+ else ""
93− in err 65 $ "\nError at " <> tshow err' <>
94− -- if error comes from a chunk or included file,
95− -- then we won't get the right text this way:
96− if sourceName errPos == "source"
97− then errorInFile
106+ in "Error at " <> tshow err' <> errorInFile
98− else ""
99− PandocMakePDFError s -> err 66 s
107+ PandocMakePDFError s -> s
100− PandocOptionError s -> err 6 s
108+ PandocOptionError s -> s
101− PandocSyntaxMapError s -> err 67 s
109+ PandocSyntaxMapError s -> s
102− PandocFailOnWarningError -> err 3 "Failing because there were warnings."
110+ PandocFailOnWarningError -> "Failing because there were warnings."
103− PandocPDFProgramNotFoundError pdfprog -> err 47 $
111+ PandocPDFProgramNotFoundError pdfprog ->
104 pdfprog <> " not found. Please select a different --pdf-engine or install " <> pdfprog
112 pdfprog <> " not found. Please select a different --pdf-engine or install " <> pdfprog
105− PandocPDFError logmsg -> err 43 $ "Error producing PDF.\n" <> logmsg
113+ PandocPDFError logmsg -> "Error producing PDF.\n" <> logmsg
114+ PandocXMLError fp logmsg -> "Invalid XML" <>
115+ (if T.null fp then "" else " in " <> fp) <> ":\n" <> logmsg
106− PandocFilterError filtername msg -> err 83 $ "Error running filter " <>
116+ PandocFilterError filtername msg -> "Error running filter " <>
107 filtername <> ":\n" <> msg
117 filtername <> ":\n" <> msg
108− PandocLuaError msg -> err 84 $ "Error running Lua:\n" <> msg
118+ PandocLuaError msg -> "Error running Lua:\n" <> msg
109− PandocCouldNotFindDataFileError fn -> err 97 $
119+ PandocCouldNotFindDataFileError fn ->
110 "Could not find data file " <> fn
120 "Could not find data file " <> fn
111− PandocResourceNotFound fn -> err 99 $
121+ PandocResourceNotFound fn ->
112 "File " <> fn <> " not found in resource path"
122 "File " <> fn <> " not found in resource path"
113− PandocTemplateError s -> err 5 $ "Error compiling template " <> s
123+ PandocTemplateError s -> "Error compiling template " <> s
114− PandocAppError s -> err 4 s
124+ PandocAppError s -> s
115− PandocEpubSubdirectoryError s -> err 31 $
125+ PandocEpubSubdirectoryError s ->
116 "EPUB subdirectory name '" <> s <> "' contains illegal characters"
126 "EPUB subdirectory name '" <> s <> "' contains illegal characters"
117− PandocMacroLoop s -> err 91 $
127+ PandocMacroLoop s ->
118 "Loop encountered in expanding macro " <> s
128 "Loop encountered in expanding macro " <> s
119− PandocUTF8DecodingError f offset w -> err 92 $
129+ PandocUTF8DecodingError f offset w ->
120 "UTF-8 decoding error in " <> f <> " at byte offset " <> tshow offset <>
130 "UTF-8 decoding error in " <> f <> " at byte offset " <> tshow offset <>
121 " (" <> T.pack (printf "%2x" w) <> ").\n" <>
131 " (" <> T.pack (printf "%2x" w) <> ").\n" <>
122 "The input must be a UTF-8 encoded text."
132 "The input must be a UTF-8 encoded text."
123− PandocIpynbDecodingError w -> err 93 $
133+ PandocIpynbDecodingError w ->
124 "ipynb decoding error: " <> w
134 "ipynb decoding error: " <> w
135+ PandocUnsupportedCharsetError charset ->
136+ "Unsupported charset " <> charset
125− PandocUnknownReaderError r -> err 21 $
137+ PandocUnknownReaderError r ->
126 "Unknown input format " <> r <>
138 "Unknown input format " <> r <>
127 case r of
139 case r of
128 "doc" -> "\nPandoc can convert from DOCX, but not from DOC." <>
140 "doc" -> "\nPandoc can convert from DOCX, but not from DOC." <>
@@ -130,7 +142,7 @@ handleError (Left e) =
130 " and convert that with pandoc."
142 " and convert that with pandoc."
131 "pdf" -> "\nPandoc can convert to PDF, but not from PDF."
143 "pdf" -> "\nPandoc can convert to PDF, but not from PDF."
132 _ -> ""
144 _ -> ""
133− PandocUnknownWriterError w -> err 22 $
145+ PandocUnknownWriterError w ->
134 "Unknown output format " <> w <>
146 "Unknown output format " <> w <>
135 case w of
147 case w of
136 "pdf" -> "To create a pdf using pandoc, use" <>
148 "pdf" -> "To create a pdf using pandoc, use" <>
@@ -139,16 +151,57 @@ handleError (Left e) =
139 ".pdf extension (-o filename.pdf)."
151 ".pdf extension (-o filename.pdf)."
140 "doc" -> "\nPandoc can convert to DOCX, but not to DOC."
152 "doc" -> "\nPandoc can convert to DOCX, but not to DOC."
141 _ -> ""
153 _ -> ""
142− PandocUnsupportedExtensionError ext f -> err 23 $
154+ PandocUnsupportedExtensionError ext f ->
143 "The extension " <> ext <> " is not supported " <>
155 "The extension " <> ext <> " is not supported " <>
144 "for " <> f
156 "for " <> f
145− PandocCiteprocError e' -> err 24 $
157+ PandocCiteprocError e' ->
146 prettyCiteprocError e'
158 prettyCiteprocError e'
147− PandocBibliographyError fp msg -> err 25 $
159+ PandocBibliographyError fp msg ->
148 "Error reading bibliography file " <> fp <> ":\n" <> msg
160 "Error reading bibliography file " <> fp <> ":\n" <> msg
149
161
162+
163+-- | Handle PandocError by exiting with an error message.
164+handleError :: Either PandocError a -> IO a
165+handleError (Right r) = return r
166+handleError (Left e) =
167+ case e of
168+ PandocIOError _ err' -> ioError err'
169+ _ -> err exitCode (renderError e)
170+ where
171+ exitCode =
172+ case e of
173+ PandocIOError{} -> 1
174+ PandocHttpError{} -> 61
175+ PandocShouldNeverHappenError{} -> 62
176+ PandocSomeError{} -> 63
177+ PandocParseError{} -> 64
178+ PandocParsecError{} -> 65
179+ PandocMakePDFError{} -> 66
180+ PandocOptionError{} -> 6
181+ PandocSyntaxMapError{} -> 67
182+ PandocFailOnWarningError{} -> 3
183+ PandocPDFProgramNotFoundError{} -> 47
184+ PandocPDFError{} -> 43
185+ PandocXMLError{} -> 44
186+ PandocFilterError{} -> 83
187+ PandocLuaError{} -> 84
188+ PandocCouldNotFindDataFileError{} -> 97
189+ PandocResourceNotFound{} -> 99
190+ PandocTemplateError{} -> 5
191+ PandocAppError{} -> 4
192+ PandocEpubSubdirectoryError{} -> 31
193+ PandocMacroLoop{} -> 91
194+ PandocUTF8DecodingError{} -> 92
195+ PandocIpynbDecodingError{} -> 93
196+ PandocUnsupportedCharsetError{} -> 94
197+ PandocUnknownReaderError{} -> 21
198+ PandocUnknownWriterError{} -> 22
199+ PandocUnsupportedExtensionError{} -> 23
200+ PandocCiteprocError{} -> 24
201+ PandocBibliographyError{} -> 25
202+
150err :: Int -> Text -> IO a
203err :: Int -> Text -> IO a
151err exitCode msg = do
204err exitCode msg = do
152− UTF8.hPutStrLn stderr (T.unpack msg)
205+ UTF8.hPutStrLn stderr msg
153 exitWith $ ExitFailure exitCode
206 exitWith $ ExitFailure exitCode
154 return undefined
207 return undefined
src/Text/Pandoc/Extensions.hs+11−3
@@ -34,6 +34,7 @@ module Text.Pandoc.Extensions ( Extension(..)
34where
34where
35import Data.Bits (clearBit, setBit, testBit, (.|.))
35import Data.Bits (clearBit, setBit, testBit, (.|.))
36import Data.Data (Data)
36import Data.Data (Data)
37+import Data.List (foldl')
37import qualified Data.Text as T
38import qualified Data.Text as T
38import Data.Typeable (Typeable)
39import Data.Typeable (Typeable)
39import GHC.Generics (Generic)
40import GHC.Generics (Generic)
@@ -135,6 +136,8 @@ data Extension =
135 | Ext_raw_html -- ^ Allow raw HTML
136 | Ext_raw_html -- ^ Allow raw HTML
136 | Ext_raw_tex -- ^ Allow raw TeX (other than math)
137 | Ext_raw_tex -- ^ Allow raw TeX (other than math)
137 | Ext_raw_markdown -- ^ Parse markdown in ipynb as raw markdown
138 | Ext_raw_markdown -- ^ Parse markdown in ipynb as raw markdown
139+ | Ext_rebase_relative_paths -- ^ Rebase relative image and link paths,
140+ -- relative to directory of containing file
138 | Ext_shortcut_reference_links -- ^ Shortcut reference links
141 | Ext_shortcut_reference_links -- ^ Shortcut reference links
139 | Ext_simple_tables -- ^ Pandoc-style simple tables
142 | Ext_simple_tables -- ^ Pandoc-style simple tables
140 | Ext_smart -- ^ "Smart" quotes, apostrophes, ellipses, dashes
143 | Ext_smart -- ^ "Smart" quotes, apostrophes, ellipses, dashes
@@ -353,6 +356,7 @@ getDefaultExtensions "gfm" = extensionsFromList
353 , Ext_strikeout
356 , Ext_strikeout
354 , Ext_task_lists
357 , Ext_task_lists
355 , Ext_emoji
358 , Ext_emoji
359+ , Ext_yaml_metadata_block
356 ]
360 ]
357getDefaultExtensions "commonmark" = extensionsFromList
361getDefaultExtensions "commonmark" = extensionsFromList
358 [Ext_raw_html]
362 [Ext_raw_html]
@@ -378,10 +382,11 @@ getDefaultExtensions "commonmark_x" = extensionsFromList
378 , Ext_raw_attribute
382 , Ext_raw_attribute
379 , Ext_implicit_header_references
383 , Ext_implicit_header_references
380 , Ext_attributes
384 , Ext_attributes
381− , Ext_fenced_code_attributes
385+ , Ext_yaml_metadata_block
382 ]
386 ]
383getDefaultExtensions "org" = extensionsFromList
387getDefaultExtensions "org" = extensionsFromList
384 [Ext_citations,
388 [Ext_citations,
389+ Ext_task_lists,
385 Ext_auto_identifiers]
390 Ext_auto_identifiers]
386getDefaultExtensions "html" = extensionsFromList
391getDefaultExtensions "html" = extensionsFromList
387 [Ext_auto_identifiers,
392 [Ext_auto_identifiers,
@@ -459,6 +464,7 @@ getAllExtensions f = universalExtensions <> getAll f
459 , Ext_gutenberg
464 , Ext_gutenberg
460 , Ext_smart
465 , Ext_smart
461 , Ext_literate_haskell
466 , Ext_literate_haskell
467+ , Ext_rebase_relative_paths
462 ]
468 ]
463 getAll "markdown_strict" = allMarkdownExtensions
469 getAll "markdown_strict" = allMarkdownExtensions
464 getAll "markdown_phpextra" = allMarkdownExtensions
470 getAll "markdown_phpextra" = allMarkdownExtensions
@@ -509,14 +515,16 @@ getAllExtensions f = universalExtensions <> getAll f
509 , Ext_raw_attribute
515 , Ext_raw_attribute
510 , Ext_implicit_header_references
516 , Ext_implicit_header_references
511 , Ext_attributes
517 , Ext_attributes
512− , Ext_fenced_code_attributes
513 , Ext_sourcepos
518 , Ext_sourcepos
519+ , Ext_yaml_metadata_block
520+ , Ext_rebase_relative_paths
514 ]
521 ]
515 getAll "commonmark_x" = getAll "commonmark"
522 getAll "commonmark_x" = getAll "commonmark"
516 getAll "org" = autoIdExtensions <>
523 getAll "org" = autoIdExtensions <>
517 extensionsFromList
524 extensionsFromList
518 [ Ext_citations
525 [ Ext_citations
519 , Ext_smart
526 , Ext_smart
527+ , Ext_task_lists
520 ]
528 ]
521 getAll "html" = autoIdExtensions <>
529 getAll "html" = autoIdExtensions <>
522 extensionsFromList
530 extensionsFromList
@@ -593,7 +601,7 @@ parseFormatSpec :: T.Text
593parseFormatSpec = parse formatSpec ""
601parseFormatSpec = parse formatSpec ""
594 where formatSpec = do
602 where formatSpec = do
595 name <- formatName
603 name <- formatName
596− (extsToEnable, extsToDisable) <- foldl (flip ($)) ([],[]) <$>
604+ (extsToEnable, extsToDisable) <- foldl' (flip ($)) ([],[]) <$>
597 many extMod
605 many extMod
598 return (T.pack name, reverse extsToEnable, reverse extsToDisable)
606 return (T.pack name, reverse extsToEnable, reverse extsToDisable)
599 formatName = many1 $ noneOf "-+"
607 formatName = many1 $ noneOf "-+"
src/Text/Pandoc/Highlighting.hs+5−5
@@ -52,12 +52,12 @@ highlightingStyles =
52 ("breezedark", breezeDark),
52 ("breezedark", breezeDark),
53 ("haddock", haddock)]
53 ("haddock", haddock)]
54
54
55−languages :: [T.Text]
55+languages :: SyntaxMap -> [T.Text]
56−languages = [T.toLower (sName s) | s <- M.elems defaultSyntaxMap]
56+languages syntaxmap = [T.toLower (sName s) | s <- M.elems syntaxmap]
57
57
58−languagesByExtension :: T.Text -> [T.Text]
58+languagesByExtension :: SyntaxMap -> T.Text -> [T.Text]
59−languagesByExtension ext =
59+languagesByExtension syntaxmap ext =
60− [T.toLower (sName s) | s <- syntaxesByExtension defaultSyntaxMap (T.unpack ext)]
60+ [T.toLower (sName s) | s <- syntaxesByExtension syntaxmap (T.unpack ext)]
61
61
62highlight :: SyntaxMap
62highlight :: SyntaxMap
63 -> (FormatOptions -> [SourceLine] -> a) -- ^ Formatter
63 -> (FormatOptions -> [SourceLine] -> a) -- ^ Formatter
src/Text/Pandoc/ImageSize.hs+12−6
@@ -44,8 +44,9 @@ import Numeric (showFFloat)
44import Text.Pandoc.Definition
44import Text.Pandoc.Definition
45import Text.Pandoc.Options
45import Text.Pandoc.Options
46import qualified Text.Pandoc.UTF8 as UTF8
46import qualified Text.Pandoc.UTF8 as UTF8
47−import qualified Text.XML.Light as Xml
47+import Text.Pandoc.XML.Light hiding (Attr)
48import qualified Data.Text as T
48import qualified Data.Text as T
49+import qualified Data.Text.Lazy as TL
49import qualified Data.Text.Encoding as TE
50import qualified Data.Text.Encoding as TE
50import Control.Applicative
51import Control.Applicative
51import qualified Data.Attoparsec.ByteString.Char8 as A
52import qualified Data.Attoparsec.ByteString.Char8 as A
@@ -56,7 +57,8 @@ import Codec.Picture (decodeImageWithMetadata)
56-- quick and dirty functions to get image sizes
57-- quick and dirty functions to get image sizes
57-- algorithms borrowed from wwwis.pl
58-- algorithms borrowed from wwwis.pl
58
59
59−data ImageType = Png | Gif | Jpeg | Svg | Pdf | Eps | Emf deriving Show
60+data ImageType = Png | Gif | Jpeg | Svg | Pdf | Eps | Emf | Tiff
61+ deriving Show
60data Direction = Width | Height
62data Direction = Width | Height
61instance Show Direction where
63instance Show Direction where
62 show Width = "width"
64 show Width = "width"
@@ -99,6 +101,8 @@ imageType :: ByteString -> Maybe ImageType
99imageType img = case B.take 4 img of
101imageType img = case B.take 4 img of
100 "\x89\x50\x4e\x47" -> return Png
102 "\x89\x50\x4e\x47" -> return Png
101 "\x47\x49\x46\x38" -> return Gif
103 "\x47\x49\x46\x38" -> return Gif
104+ "\x49\x49\x2a\x00" -> return Tiff
105+ "\x4D\x4D\x00\x2a" -> return Tiff
102 "\xff\xd8\xff\xe0" -> return Jpeg -- JFIF
106 "\xff\xd8\xff\xe0" -> return Jpeg -- JFIF
103 "\xff\xd8\xff\xe1" -> return Jpeg -- Exif
107 "\xff\xd8\xff\xe1" -> return Jpeg -- Exif
104 "%PDF" -> return Pdf
108 "%PDF" -> return Pdf
@@ -123,6 +127,7 @@ imageSize opts img = checkDpi <$>
123 Just Png -> getSize img
127 Just Png -> getSize img
124 Just Gif -> getSize img
128 Just Gif -> getSize img
125 Just Jpeg -> getSize img
129 Just Jpeg -> getSize img
130+ Just Tiff -> getSize img
126 Just Svg -> mbToEither "could not determine SVG size" $ svgSize opts img
131 Just Svg -> mbToEither "could not determine SVG size" $ svgSize opts img
127 Just Eps -> mbToEither "could not determine EPS size" $ epsSize img
132 Just Eps -> mbToEither "could not determine EPS size" $ epsSize img
128 Just Pdf -> mbToEither "could not determine PDF size" $ pdfSize img
133 Just Pdf -> mbToEither "could not determine PDF size" $ pdfSize img
@@ -327,14 +332,15 @@ getSize img =
327
332
328svgSize :: WriterOptions -> ByteString -> Maybe ImageSize
333svgSize :: WriterOptions -> ByteString -> Maybe ImageSize
329svgSize opts img = do
334svgSize opts img = do
330− doc <- Xml.parseXMLDoc $ UTF8.toString img
335+ doc <- either (const mzero) return $ parseXMLElement
336+ $ TL.fromStrict $ UTF8.toText img
331 let viewboxSize = do
337 let viewboxSize = do
332− vb <- Xml.findAttrBy (== Xml.QName "viewBox" Nothing Nothing) doc
338+ vb <- findAttrBy (== QName "viewBox" Nothing Nothing) doc
333− [_,_,w,h] <- mapM safeRead (T.words (T.pack vb))
339+ [_,_,w,h] <- mapM safeRead (T.words vb)
334 return (w,h)
340 return (w,h)
335 let dpi = fromIntegral $ writerDpi opts
341 let dpi = fromIntegral $ writerDpi opts
336 let dirToInt dir = do
342 let dirToInt dir = do
337− dim <- Xml.findAttrBy (== Xml.QName dir Nothing Nothing) doc >>= lengthToDim . T.pack
343+ dim <- findAttrBy (== QName dir Nothing Nothing) doc >>= lengthToDim
338 return $ inPixel opts dim
344 return $ inPixel opts dim
339 w <- dirToInt "width" <|> (fst <$> viewboxSize)
345 w <- dirToInt "width" <|> (fst <$> viewboxSize)
340 h <- dirToInt "height" <|> (snd <$> viewboxSize)
346 h <- dirToInt "height" <|> (snd <$> viewboxSize)
src/Text/Pandoc/Logging.hs+32−9
@@ -36,6 +36,7 @@ import Data.Typeable (Typeable)
36import GHC.Generics (Generic)
36import GHC.Generics (Generic)
37import Text.Pandoc.Definition
37import Text.Pandoc.Definition
38import Text.Parsec.Pos
38import Text.Parsec.Pos
39+import Text.Pandoc.Shared (tshow)
39
40
40-- | Verbosity level.
41-- | Verbosity level.
41data Verbosity = ERROR | WARNING | INFO
42data Verbosity = ERROR | WARNING | INFO
@@ -84,6 +85,7 @@ data LogMessage =
84 | CouldNotParseCSS Text
85 | CouldNotParseCSS Text
85 | Fetching Text
86 | Fetching Text
86 | Extracting Text
87 | Extracting Text
88+ | LoadedResource FilePath FilePath
87 | NoTitleElement Text
89 | NoTitleElement Text
88 | NoLangSpecified
90 | NoLangSpecified
89 | InvalidLang Text
91 | InvalidLang Text
@@ -100,6 +102,8 @@ data LogMessage =
100 | FilterCompleted FilePath Integer
102 | FilterCompleted FilePath Integer
101 | CiteprocWarning Text
103 | CiteprocWarning Text
102 | ATXHeadingInLHS Int Text
104 | ATXHeadingInLHS Int Text
105+ | EnvironmentVariableUndefined Text
106+ | DuplicateAttribute Text Text
103 deriving (Show, Eq, Data, Ord, Typeable, Generic)
107 deriving (Show, Eq, Data, Ord, Typeable, Generic)
104
108
105instance ToJSON LogMessage where
109instance ToJSON LogMessage where
@@ -192,6 +196,9 @@ instance ToJSON LogMessage where
192 ["path" .= fp]
196 ["path" .= fp]
193 Extracting fp ->
197 Extracting fp ->
194 ["path" .= fp]
198 ["path" .= fp]
199+ LoadedResource orig found ->
200+ ["for" .= orig
201+ ,"from" .= found]
195 NoTitleElement fallback ->
202 NoTitleElement fallback ->
196 ["fallback" .= fallback]
203 ["fallback" .= fallback]
197 NoLangSpecified -> []
204 NoLangSpecified -> []
@@ -229,13 +236,20 @@ instance ToJSON LogMessage where
229 ATXHeadingInLHS lvl contents ->
236 ATXHeadingInLHS lvl contents ->
230 ["level" .= lvl
237 ["level" .= lvl
231 ,"contents" .= contents]
238 ,"contents" .= contents]
239+ EnvironmentVariableUndefined var ->
240+ ["variable" .= var ]
241+ DuplicateAttribute attr val ->
242+ ["attribute" .= attr
243+ ,"value" .= val]
232
244
233showPos :: SourcePos -> Text
245showPos :: SourcePos -> Text
234showPos pos = Text.pack $ sn ++ "line " ++
246showPos pos = Text.pack $ sn ++ "line " ++
235 show (sourceLine pos) ++ " column " ++ show (sourceColumn pos)
247 show (sourceLine pos) ++ " column " ++ show (sourceColumn pos)
236− where sn = if sourceName pos == "source" || sourceName pos == ""
248+ where
249+ sn' = sourceName pos
250+ sn = if sn' == "source" || sn' == "" || sn' == "-"
237− then ""
251+ then ""
238− else sourceName pos ++ " "
252+ else sn' ++ " "
239
253
240encodeLogMessages :: [LogMessage] -> BL.ByteString
254encodeLogMessages :: [LogMessage] -> BL.ByteString
241encodeLogMessages ms =
255encodeLogMessages ms =
@@ -268,7 +282,7 @@ showLogMessage msg =
268 ParsingUnescaped s pos ->
282 ParsingUnescaped s pos ->
269 "Parsing unescaped '" <> s <> "' at " <> showPos pos
283 "Parsing unescaped '" <> s <> "' at " <> showPos pos
270 CouldNotLoadIncludeFile fp pos ->
284 CouldNotLoadIncludeFile fp pos ->
271− "Could not load include file '" <> fp <> "' at " <> showPos pos
285+ "Could not load include file " <> fp <> " at " <> showPos pos
272 MacroAlreadyDefined name pos ->
286 MacroAlreadyDefined name pos ->
273 "Macro '" <> name <> "' already defined, ignoring at " <> showPos pos
287 "Macro '" <> name <> "' already defined, ignoring at " <> showPos pos
274 InlineNotRendered il ->
288 InlineNotRendered il ->
@@ -280,18 +294,18 @@ showLogMessage msg =
280 IgnoredIOError s ->
294 IgnoredIOError s ->
281 "IO Error (ignored): " <> s
295 "IO Error (ignored): " <> s
282 CouldNotFetchResource fp s ->
296 CouldNotFetchResource fp s ->
283− "Could not fetch resource '" <> fp <> "'" <>
297+ "Could not fetch resource " <> fp <>
284 if Text.null s then "" else ": " <> s
298 if Text.null s then "" else ": " <> s
285 CouldNotDetermineImageSize fp s ->
299 CouldNotDetermineImageSize fp s ->
286− "Could not determine image size for '" <> fp <> "'" <>
300+ "Could not determine image size for " <> fp <>
287 if Text.null s then "" else ": " <> s
301 if Text.null s then "" else ": " <> s
288 CouldNotConvertImage fp s ->
302 CouldNotConvertImage fp s ->
289− "Could not convert image '" <> fp <> "'" <>
303+ "Could not convert image " <> fp <>
290 if Text.null s then "" else ": " <> s
304 if Text.null s then "" else ": " <> s
291 CouldNotDetermineMimeType fp ->
305 CouldNotDetermineMimeType fp ->
292− "Could not determine mime type for '" <> fp <> "'"
306+ "Could not determine mime type for " <> fp
293 CouldNotConvertTeXMath s m ->
307 CouldNotConvertTeXMath s m ->
294− "Could not convert TeX math '" <> s <> "', rendering as TeX" <>
308+ "Could not convert TeX math " <> s <> ", rendering as TeX" <>
295 if Text.null m then "" else ":\n" <> m
309 if Text.null m then "" else ":\n" <> m
296 CouldNotParseCSS m ->
310 CouldNotParseCSS m ->
297 "Could not parse CSS" <> if Text.null m then "" else ":\n" <> m
311 "Could not parse CSS" <> if Text.null m then "" else ":\n" <> m
@@ -299,6 +313,8 @@ showLogMessage msg =
299 "Fetching " <> fp <> "..."
313 "Fetching " <> fp <> "..."
300 Extracting fp ->
314 Extracting fp ->
301 "Extracting " <> fp <> "..."
315 "Extracting " <> fp <> "..."
316+ LoadedResource orig found ->
317+ "Loaded " <> Text.pack orig <> " from " <> Text.pack found
302 NoTitleElement fallback ->
318 NoTitleElement fallback ->
303 "This document format requires a nonempty <title> element.\n" <>
319 "This document format requires a nonempty <title> element.\n" <>
304 "Defaulting to '" <> fallback <> "' as the title.\n" <>
320 "Defaulting to '" <> fallback <> "' as the title.\n" <>
@@ -345,6 +361,10 @@ showLogMessage msg =
345 if lvl < 3
361 if lvl < 3
346 then " Consider using --markdown-headings=setext."
362 then " Consider using --markdown-headings=setext."
347 else ""
363 else ""
364+ EnvironmentVariableUndefined var ->
365+ "Undefined environment variable " <> var <> " in defaults file."
366+ DuplicateAttribute attr val ->
367+ "Ignoring duplicate attribute " <> attr <> "=" <> tshow val <> "."
348
368
349messageVerbosity :: LogMessage -> Verbosity
369messageVerbosity :: LogMessage -> Verbosity
350messageVerbosity msg =
370messageVerbosity msg =
@@ -375,6 +395,7 @@ messageVerbosity msg =
375 CouldNotParseCSS{} -> WARNING
395 CouldNotParseCSS{} -> WARNING
376 Fetching{} -> INFO
396 Fetching{} -> INFO
377 Extracting{} -> INFO
397 Extracting{} -> INFO
398+ LoadedResource{} -> INFO
378 NoTitleElement{} -> WARNING
399 NoTitleElement{} -> WARNING
379 NoLangSpecified -> INFO
400 NoLangSpecified -> INFO
380 InvalidLang{} -> WARNING
401 InvalidLang{} -> WARNING
@@ -391,3 +412,5 @@ messageVerbosity msg =
391 FilterCompleted{} -> INFO
412 FilterCompleted{} -> INFO
392 CiteprocWarning{} -> WARNING
413 CiteprocWarning{} -> WARNING
393 ATXHeadingInLHS{} -> WARNING
414 ATXHeadingInLHS{} -> WARNING
415+ EnvironmentVariableUndefined{}-> WARNING
416+ DuplicateAttribute{} -> WARNING
src/Text/Pandoc/Lua/Filter.hs+4−1
@@ -13,7 +13,9 @@ module Text.Pandoc.Lua.Filter ( LuaFilterFunction
13 , LuaFilter
13 , LuaFilter
14 , runFilterFile
14 , runFilterFile
15 , walkInlines
15 , walkInlines
16+ , walkInlineLists
16 , walkBlocks
17 , walkBlocks
18+ , walkBlockLists
17 , module Text.Pandoc.Lua.Walk
19 , module Text.Pandoc.Lua.Walk
18 ) where
20 ) where
19import Control.Applicative ((<|>))
21import Control.Applicative ((<|>))
@@ -22,6 +24,7 @@ import Control.Monad.Catch (finally, try)
22import Data.Data (Data, DataType, dataTypeConstrs, dataTypeName, dataTypeOf,
24import Data.Data (Data, DataType, dataTypeConstrs, dataTypeName, dataTypeOf,
23 showConstr, toConstr, tyconUQname)
25 showConstr, toConstr, tyconUQname)
24import Data.Foldable (foldrM)
26import Data.Foldable (foldrM)
27+import Data.List (foldl')
25import Data.Map (Map)
28import Data.Map (Map)
26import Data.Maybe (fromMaybe)
29import Data.Maybe (fromMaybe)
27import Foreign.Lua (Lua, Peekable, Pushable, StackIndex)
30import Foreign.Lua (Lua, Peekable, Pushable, StackIndex)
@@ -204,7 +207,7 @@ walkMeta lf (Pandoc m bs) = do
204
207
205walkPandoc :: LuaFilter -> Pandoc -> Lua Pandoc
208walkPandoc :: LuaFilter -> Pandoc -> Lua Pandoc
206walkPandoc (LuaFilter fnMap) =
209walkPandoc (LuaFilter fnMap) =
207− case foldl mplus Nothing (map (`Map.lookup` fnMap) pandocFilterNames) of
210+ case foldl' mplus Nothing (map (`Map.lookup` fnMap) pandocFilterNames) of
208 Just fn -> \x -> runFilterFunction fn x *> singleElement x
211 Just fn -> \x -> runFilterFunction fn x *> singleElement x
209 Nothing -> return
212 Nothing -> return
210
213
src/Text/Pandoc/Lua/Marshaling/AST.hs+77−79
@@ -1,4 +1,5 @@
1{-# OPTIONS_GHC -fno-warn-orphans #-}
1{-# OPTIONS_GHC -fno-warn-orphans #-}
2+{-# LANGUAGE BangPatterns #-}
2{-# LANGUAGE LambdaCase #-}
3{-# LANGUAGE LambdaCase #-}
3{- |
4{- |
4 Module : Text.Pandoc.Lua.Marshaling.AST
5 Module : Text.Pandoc.Lua.Marshaling.AST
@@ -17,6 +18,7 @@ module Text.Pandoc.Lua.Marshaling.AST
17 ) where
18 ) where
18
19
19import Control.Applicative ((<|>))
20import Control.Applicative ((<|>))
21+import Control.Monad ((<$!>))
20import Foreign.Lua (Lua, Peekable, Pushable, StackIndex)
22import Foreign.Lua (Lua, Peekable, Pushable, StackIndex)
21import Text.Pandoc.Definition
23import Text.Pandoc.Definition
22import Text.Pandoc.Error (PandocError)
24import Text.Pandoc.Error (PandocError)
@@ -32,17 +34,16 @@ instance Pushable Pandoc where
32 pushViaConstructor "Pandoc" blocks meta
34 pushViaConstructor "Pandoc" blocks meta
33
35
34instance Peekable Pandoc where
36instance Peekable Pandoc where
35− peek idx = defineHowTo "get Pandoc value" $ do
37+ peek idx = defineHowTo "get Pandoc value" $! Pandoc
36− blocks <- LuaUtil.rawField idx "blocks"
38+ <$!> LuaUtil.rawField idx "meta"
37− meta <- LuaUtil.rawField idx "meta"
39+ <*> LuaUtil.rawField idx "blocks"
38− return $ Pandoc meta blocks
39
40
40instance Pushable Meta where
41instance Pushable Meta where
41 push (Meta mmap) =
42 push (Meta mmap) =
42 pushViaConstructor "Meta" mmap
43 pushViaConstructor "Meta" mmap
43instance Peekable Meta where
44instance Peekable Meta where
44− peek idx = defineHowTo "get Meta value" $
45+ peek idx = defineHowTo "get Meta value" $!
45− Meta <$> Lua.peek idx
46+ Meta <$!> Lua.peek idx
46
47
47instance Pushable MetaValue where
48instance Pushable MetaValue where
48 push = pushMetaValue
49 push = pushMetaValue
@@ -68,14 +69,13 @@ instance Pushable Citation where
68 pushViaConstructor "Citation" cid mode prefix suffix noteNum hash
69 pushViaConstructor "Citation" cid mode prefix suffix noteNum hash
69
70
70instance Peekable Citation where
71instance Peekable Citation where
71− peek idx = do
72+ peek idx = Citation
72− id' <- LuaUtil.rawField idx "id"
73+ <$!> LuaUtil.rawField idx "id"
73− prefix <- LuaUtil.rawField idx "prefix"
74+ <*> LuaUtil.rawField idx "prefix"
74− suffix <- LuaUtil.rawField idx "suffix"
75+ <*> LuaUtil.rawField idx "suffix"
75− mode <- LuaUtil.rawField idx "mode"
76+ <*> LuaUtil.rawField idx "mode"
76− num <- LuaUtil.rawField idx "note_num"
77+ <*> LuaUtil.rawField idx "note_num"
77− hash <- LuaUtil.rawField idx "hash"
78+ <*> LuaUtil.rawField idx "hash"
78− return $ Citation id' prefix suffix mode num hash
79
79
80instance Pushable Alignment where
80instance Pushable Alignment where
81 push = Lua.push . show
81 push = Lua.push . show
@@ -90,7 +90,7 @@ instance Peekable CitationMode where
90instance Pushable Format where
90instance Pushable Format where
91 push (Format f) = Lua.push f
91 push (Format f) = Lua.push f
92instance Peekable Format where
92instance Peekable Format where
93− peek idx = Format <$> Lua.peek idx
93+ peek idx = Format <$!> Lua.peek idx
94
94
95instance Pushable ListNumberDelim where
95instance Pushable ListNumberDelim where
96 push = Lua.push . show
96 push = Lua.push . show
@@ -130,26 +130,26 @@ peekMetaValue idx = defineHowTo "get MetaValue" $ do
130 elementContent = Lua.peek idx
130 elementContent = Lua.peek idx
131 luatype <- Lua.ltype idx
131 luatype <- Lua.ltype idx
132 case luatype of
132 case luatype of
133− Lua.TypeBoolean -> MetaBool <$> Lua.peek idx
133+ Lua.TypeBoolean -> MetaBool <$!> Lua.peek idx
134− Lua.TypeString -> MetaString <$> Lua.peek idx
134+ Lua.TypeString -> MetaString <$!> Lua.peek idx
135 Lua.TypeTable -> do
135 Lua.TypeTable -> do
136 tag <- try $ LuaUtil.getTag idx
136 tag <- try $ LuaUtil.getTag idx
137 case tag of
137 case tag of
138− Right "MetaBlocks" -> MetaBlocks <$> elementContent
138+ Right "MetaBlocks" -> MetaBlocks <$!> elementContent
139− Right "MetaBool" -> MetaBool <$> elementContent
139+ Right "MetaBool" -> MetaBool <$!> elementContent
140− Right "MetaMap" -> MetaMap <$> elementContent
140+ Right "MetaMap" -> MetaMap <$!> elementContent
141− Right "MetaInlines" -> MetaInlines <$> elementContent
141+ Right "MetaInlines" -> MetaInlines <$!> elementContent
142− Right "MetaList" -> MetaList <$> elementContent
142+ Right "MetaList" -> MetaList <$!> elementContent
143− Right "MetaString" -> MetaString <$> elementContent
143+ Right "MetaString" -> MetaString <$!> elementContent
144 Right t -> Lua.throwMessage ("Unknown meta tag: " <> t)
144 Right t -> Lua.throwMessage ("Unknown meta tag: " <> t)
145 Left _ -> do
145 Left _ -> do
146 -- no meta value tag given, try to guess.
146 -- no meta value tag given, try to guess.
147 len <- Lua.rawlen idx
147 len <- Lua.rawlen idx
148 if len <= 0
148 if len <= 0
149− then MetaMap <$> Lua.peek idx
149+ then MetaMap <$!> Lua.peek idx
150− else (MetaInlines <$> Lua.peek idx)
150+ else (MetaInlines <$!> Lua.peek idx)
151− <|> (MetaBlocks <$> Lua.peek idx)
151+ <|> (MetaBlocks <$!> Lua.peek idx)
152− <|> (MetaList <$> Lua.peek idx)
152+ <|> (MetaList <$!> Lua.peek idx)
153 _ -> Lua.throwMessage "could not get meta value"
153 _ -> Lua.throwMessage "could not get meta value"
154
154
155-- | Push a block element to the top of the Lua stack.
155-- | Push a block element to the top of the Lua stack.
@@ -174,25 +174,25 @@ pushBlock = \case
174
174
175-- | Return the value at the given index as block if possible.
175-- | Return the value at the given index as block if possible.
176peekBlock :: StackIndex -> Lua Block
176peekBlock :: StackIndex -> Lua Block
177−peekBlock idx = defineHowTo "get Block value" $ do
177+peekBlock idx = defineHowTo "get Block value" $! do
178 tag <- LuaUtil.getTag idx
178 tag <- LuaUtil.getTag idx
179 case tag of
179 case tag of
180− "BlockQuote" -> BlockQuote <$> elementContent
180+ "BlockQuote" -> BlockQuote <$!> elementContent
181− "BulletList" -> BulletList <$> elementContent
181+ "BulletList" -> BulletList <$!> elementContent
182− "CodeBlock" -> withAttr CodeBlock <$> elementContent
182+ "CodeBlock" -> withAttr CodeBlock <$!> elementContent
183− "DefinitionList" -> DefinitionList <$> elementContent
183+ "DefinitionList" -> DefinitionList <$!> elementContent
184− "Div" -> withAttr Div <$> elementContent
184+ "Div" -> withAttr Div <$!> elementContent
185 "Header" -> (\(lvl, LuaAttr attr, lst) -> Header lvl attr lst)
185 "Header" -> (\(lvl, LuaAttr attr, lst) -> Header lvl attr lst)
186− <$> elementContent
186+ <$!> elementContent
187 "HorizontalRule" -> return HorizontalRule
187 "HorizontalRule" -> return HorizontalRule
188− "LineBlock" -> LineBlock <$> elementContent
188+ "LineBlock" -> LineBlock <$!> elementContent
189 "OrderedList" -> (\(LuaListAttributes lstAttr, lst) ->
189 "OrderedList" -> (\(LuaListAttributes lstAttr, lst) ->
190 OrderedList lstAttr lst)
190 OrderedList lstAttr lst)
191− <$> elementContent
191+ <$!> elementContent
192 "Null" -> return Null
192 "Null" -> return Null
193− "Para" -> Para <$> elementContent
193+ "Para" -> Para <$!> elementContent
194− "Plain" -> Plain <$> elementContent
194+ "Plain" -> Plain <$!> elementContent
195− "RawBlock" -> uncurry RawBlock <$> elementContent
195+ "RawBlock" -> uncurry RawBlock <$!> elementContent
196 "Table" -> (\(attr, capt, colSpecs, thead, tbodies, tfoot) ->
196 "Table" -> (\(attr, capt, colSpecs, thead, tbodies, tfoot) ->
197 Table (fromLuaAttr attr)
197 Table (fromLuaAttr attr)
198 capt
198 capt
@@ -200,7 +200,7 @@ peekBlock idx = defineHowTo "get Block value" $ do
200 thead
200 thead
201 tbodies
201 tbodies
202 tfoot)
202 tfoot)
203− <$> elementContent
203+ <$!> elementContent
204 _ -> Lua.throwMessage ("Unknown block type: " <> tag)
204 _ -> Lua.throwMessage ("Unknown block type: " <> tag)
205 where
205 where
206 -- Get the contents of an AST element.
206 -- Get the contents of an AST element.
@@ -222,15 +222,14 @@ pushCaption (Caption shortCaption longCaption) = do
222
222
223-- | Peek Caption element
223-- | Peek Caption element
224peekCaption :: StackIndex -> Lua Caption
224peekCaption :: StackIndex -> Lua Caption
225−peekCaption idx = do
225+peekCaption idx = Caption
226− short <- Lua.fromOptional <$> LuaUtil.rawField idx "short"
226+ <$!> (Lua.fromOptional <$!> LuaUtil.rawField idx "short")
227− long <- LuaUtil.rawField idx "long"
227+ <*> LuaUtil.rawField idx "long"
228− return $ Caption short long
229
228
230instance Peekable ColWidth where
229instance Peekable ColWidth where
231 peek idx = do
230 peek idx = do
232− width <- Lua.fromOptional <$> Lua.peek idx
231+ width <- Lua.fromOptional <$!> Lua.peek idx
233− return $ maybe ColWidthDefault ColWidth width
232+ return $! maybe ColWidthDefault ColWidth width
234
233
235instance Pushable ColWidth where
234instance Pushable ColWidth where
236 push = \case
235 push = \case
@@ -252,12 +251,11 @@ instance Pushable TableBody where
252 LuaUtil.addField "body" body
251 LuaUtil.addField "body" body
253
252
254instance Peekable TableBody where
253instance Peekable TableBody where
255− peek idx = do
254+ peek idx = TableBody
256− attr <- LuaUtil.rawField idx "attr"
255+ <$!> LuaUtil.rawField idx "attr"
257− rowHeadColumns <- LuaUtil.rawField idx "row_head_columns"
256+ <*> (RowHeadColumns <$!> LuaUtil.rawField idx "row_head_columns")
258− head' <- LuaUtil.rawField idx "head"
257+ <*> LuaUtil.rawField idx "head"
259− body <- LuaUtil.rawField idx "body"
258+ <*> LuaUtil.rawField idx "body"
260− return $ TableBody attr (RowHeadColumns rowHeadColumns) head' body
261
259
262instance Pushable TableHead where
260instance Pushable TableHead where
263 push (TableHead attr rows) = Lua.push (attr, rows)
261 push (TableHead attr rows) = Lua.push (attr, rows)
@@ -287,13 +285,12 @@ pushCell (Cell attr align (RowSpan rowSpan) (ColSpan colSpan) contents) = do
287 LuaUtil.addField "contents" contents
285 LuaUtil.addField "contents" contents
288
286
289peekCell :: StackIndex -> Lua Cell
287peekCell :: StackIndex -> Lua Cell
290−peekCell idx = do
288+peekCell idx = Cell
291− attr <- fromLuaAttr <$> LuaUtil.rawField idx "attr"
289+ <$!> (fromLuaAttr <$!> LuaUtil.rawField idx "attr")
292− align <- LuaUtil.rawField idx "alignment"
290+ <*> LuaUtil.rawField idx "alignment"
293− rowSpan <- LuaUtil.rawField idx "row_span"
291+ <*> (RowSpan <$!> LuaUtil.rawField idx "row_span")
294− colSpan <- LuaUtil.rawField idx "col_span"
292+ <*> (ColSpan <$!> LuaUtil.rawField idx "col_span")
295− contents <- LuaUtil.rawField idx "contents"
293+ <*> LuaUtil.rawField idx "contents"
296− return $ Cell attr align (RowSpan rowSpan) (ColSpan colSpan) contents
297
294
298-- | Push an inline element to the top of the lua stack.
295-- | Push an inline element to the top of the lua stack.
299pushInline :: Inline -> Lua ()
296pushInline :: Inline -> Lua ()
@@ -324,28 +321,29 @@ peekInline :: StackIndex -> Lua Inline
324peekInline idx = defineHowTo "get Inline value" $ do
321peekInline idx = defineHowTo "get Inline value" $ do
325 tag <- LuaUtil.getTag idx
322 tag <- LuaUtil.getTag idx
326 case tag of
323 case tag of
327− "Cite" -> uncurry Cite <$> elementContent
324+ "Cite" -> uncurry Cite <$!> elementContent
328− "Code" -> withAttr Code <$> elementContent
325+ "Code" -> withAttr Code <$!> elementContent
329− "Emph" -> Emph <$> elementContent
326+ "Emph" -> Emph <$!> elementContent
330− "Underline" -> Underline <$> elementContent
327+ "Underline" -> Underline <$!> elementContent
331− "Image" -> (\(LuaAttr attr, lst, tgt) -> Image attr lst tgt)
328+ "Image" -> (\(LuaAttr !attr, !lst, !tgt) -> Image attr lst tgt)
332− <$> elementContent
329+ <$!> elementContent
333− "Link" -> (\(LuaAttr attr, lst, tgt) -> Link attr lst tgt)
330+ "Link" -> (\(LuaAttr !attr, !lst, !tgt) -> Link attr lst tgt)
334− <$> elementContent
331+ <$!> elementContent
335 "LineBreak" -> return LineBreak
332 "LineBreak" -> return LineBreak
336− "Note" -> Note <$> elementContent
333+ "Note" -> Note <$!> elementContent
337− "Math" -> uncurry Math <$> elementContent
334+ "Math" -> uncurry Math <$!> elementContent
338− "Quoted" -> uncurry Quoted <$> elementContent
335+ "Quoted" -> uncurry Quoted <$!> elementContent
339− "RawInline" -> uncurry RawInline <$> elementContent
336+ "RawInline" -> uncurry RawInline <$!> elementContent
340− "SmallCaps" -> SmallCaps <$> elementContent
337+ "SmallCaps" -> SmallCaps <$!> elementContent
341 "SoftBreak" -> return SoftBreak
338 "SoftBreak" -> return SoftBreak
342 "Space" -> return Space
339 "Space" -> return Space
343− "Span" -> withAttr Span <$> elementContent
340+ "Span" -> withAttr Span <$!> elementContent
341+ -- strict to Lua string is copied before gc
344− "Str" -> Str <$> elementContent
342+ "Str" -> Str <$!> elementContent
345− "Strikeout" -> Strikeout <$> elementContent
343+ "Strikeout" -> Strikeout <$!> elementContent
346− "Strong" -> Strong <$> elementContent
344+ "Strong" -> Strong <$!> elementContent
347− "Subscript" -> Subscript <$> elementContent
345+ "Subscript" -> Subscript <$!> elementContent
348− "Superscript"-> Superscript <$> elementContent
346+ "Superscript"-> Superscript <$!> elementContent
349 _ -> Lua.throwMessage ("Unknown inline type: " <> tag)
347 _ -> Lua.throwMessage ("Unknown inline type: " <> tag)
350 where
348 where
351 -- Get the contents of an AST element.
349 -- Get the contents of an AST element.
@@ -366,7 +364,7 @@ instance Pushable LuaAttr where
366 pushViaConstructor "Attr" id' classes kv
364 pushViaConstructor "Attr" id' classes kv
367
365
368instance Peekable LuaAttr where
366instance Peekable LuaAttr where
369− peek idx = defineHowTo "get Attr value" (LuaAttr <$> Lua.peek idx)
367+ peek idx = defineHowTo "get Attr value" $! (LuaAttr <$!> Lua.peek idx)
370
368
371-- | Wrapper for ListAttributes
369-- | Wrapper for ListAttributes
372newtype LuaListAttributes = LuaListAttributes ListAttributes
370newtype LuaListAttributes = LuaListAttributes ListAttributes
src/Text/Pandoc/Lua/Module/MediaBag.hs+17−16
@@ -13,6 +13,7 @@ module Text.Pandoc.Lua.Module.MediaBag
13 ( pushModule
13 ( pushModule
14 ) where
14 ) where
15
15
16+import Prelude hiding (lookup)
16import Control.Monad (zipWithM_)
17import Control.Monad (zipWithM_)
17import Foreign.Lua (Lua, NumResults, Optional)
18import Foreign.Lua (Lua, NumResults, Optional)
18import Text.Pandoc.Class.CommonState (CommonState (..))
19import Text.Pandoc.Class.CommonState (CommonState (..))
@@ -36,10 +37,10 @@ pushModule = do
36 liftPandocLua Lua.newtable
37 liftPandocLua Lua.newtable
37 addFunction "delete" delete
38 addFunction "delete" delete
38 addFunction "empty" empty
39 addFunction "empty" empty
39− addFunction "insert" insertMediaFn
40+ addFunction "insert" insert
40 addFunction "items" items
41 addFunction "items" items
41− addFunction "lookup" lookupMediaFn
42+ addFunction "lookup" lookup
42− addFunction "list" mediaDirectoryFn
43+ addFunction "list" list
43 addFunction "fetch" fetch
44 addFunction "fetch" fetch
44 return 1
45 return 1
45
46
@@ -53,11 +54,11 @@ empty :: PandocLua NumResults
53empty = 0 <$ modifyCommonState (\st -> st { stMediaBag = mempty })
54empty = 0 <$ modifyCommonState (\st -> st { stMediaBag = mempty })
54
55
55-- | Insert a new item into the media bag.
56-- | Insert a new item into the media bag.
56−insertMediaFn :: FilePath
57+insert :: FilePath
57− -> Optional MimeType
58+ -> Optional MimeType
58− -> BL.ByteString
59+ -> BL.ByteString
59− -> PandocLua NumResults
60+ -> PandocLua NumResults
60−insertMediaFn fp optionalMime contents = do
61+insert fp optionalMime contents = do
61 mb <- getMediaBag
62 mb <- getMediaBag
62 setMediaBag $ MB.insertMedia fp (Lua.fromOptional optionalMime) contents mb
63 setMediaBag $ MB.insertMedia fp (Lua.fromOptional optionalMime) contents mb
63 return (Lua.NumResults 0)
64 return (Lua.NumResults 0)
@@ -66,19 +67,19 @@ insertMediaFn fp optionalMime contents = do
66items :: PandocLua NumResults
67items :: PandocLua NumResults
67items = getMediaBag >>= liftPandocLua . pushIterator
68items = getMediaBag >>= liftPandocLua . pushIterator
68
69
69−lookupMediaFn :: FilePath
70+lookup :: FilePath
70− -> PandocLua NumResults
71+ -> PandocLua NumResults
71−lookupMediaFn fp = do
72+lookup fp = do
72 res <- MB.lookupMedia fp <$> getMediaBag
73 res <- MB.lookupMedia fp <$> getMediaBag
73 liftPandocLua $ case res of
74 liftPandocLua $ case res of
74 Nothing -> 1 <$ Lua.pushnil
75 Nothing -> 1 <$ Lua.pushnil
75− Just (mimeType, contents) -> do
76+ Just item -> do
76− Lua.push mimeType
77+ Lua.push $ MB.mediaMimeType item
77− Lua.push contents
78+ Lua.push $ MB.mediaContents item
78 return 2
79 return 2
79
80
80−mediaDirectoryFn :: PandocLua NumResults
81+list :: PandocLua NumResults
81−mediaDirectoryFn = do
82+list = do
82 dirContents <- MB.mediaDirectory <$> getMediaBag
83 dirContents <- MB.mediaDirectory <$> getMediaBag
83 liftPandocLua $ do
84 liftPandocLua $ do
84 Lua.newtable
85 Lua.newtable
src/Text/Pandoc/Lua/Module/Pandoc.hs+24−18
@@ -14,6 +14,7 @@ module Text.Pandoc.Lua.Module.Pandoc
14 ( pushModule
14 ( pushModule
15 ) where
15 ) where
16
16
17+import Prelude hiding (read)
17import Control.Monad (when)
18import Control.Monad (when)
18import Control.Monad.Except (throwError)
19import Control.Monad.Except (throwError)
19import Data.Default (Default (..))
20import Data.Default (Default (..))
@@ -22,8 +23,10 @@ import Foreign.Lua (Lua, NumResults, Optional, Peekable, Pushable)
22import System.Exit (ExitCode (..))
23import System.Exit (ExitCode (..))
23import Text.Pandoc.Class.PandocIO (runIO)
24import Text.Pandoc.Class.PandocIO (runIO)
24import Text.Pandoc.Definition (Block, Inline)
25import Text.Pandoc.Definition (Block, Inline)
25−import Text.Pandoc.Lua.Filter (walkInlines, walkBlocks, LuaFilter, SingletonsList (..))
26+import Text.Pandoc.Lua.Filter (LuaFilter, SingletonsList (..), walkInlines,
27+ walkInlineLists, walkBlocks, walkBlockLists)
26import Text.Pandoc.Lua.Marshaling ()
28import Text.Pandoc.Lua.Marshaling ()
29+import Text.Pandoc.Lua.Marshaling.List (List (..))
27import Text.Pandoc.Lua.PandocLua (PandocLua, addFunction, liftPandocLua,
30import Text.Pandoc.Lua.PandocLua (PandocLua, addFunction, liftPandocLua,
28 loadDefaultModule)
31 loadDefaultModule)
29import Text.Pandoc.Walk (Walkable)
32import Text.Pandoc.Walk (Walkable)
@@ -43,25 +46,28 @@ import Text.Pandoc.Error
43pushModule :: PandocLua NumResults
46pushModule :: PandocLua NumResults
44pushModule = do
47pushModule = do
45 loadDefaultModule "pandoc"
48 loadDefaultModule "pandoc"
46− addFunction "read" readDoc
49+ addFunction "read" read
47− addFunction "pipe" pipeFn
50+ addFunction "pipe" pipe
48− addFunction "walk_block" walkBlock
51+ addFunction "walk_block" walk_block
49− addFunction "walk_inline" walkInline
52+ addFunction "walk_inline" walk_inline
50 return 1
53 return 1
51
54
52walkElement :: (Walkable (SingletonsList Inline) a,
55walkElement :: (Walkable (SingletonsList Inline) a,
53− Walkable (SingletonsList Block) a)
56+ Walkable (SingletonsList Block) a,
57+ Walkable (List Inline) a,
58+ Walkable (List Block) a)
54 => a -> LuaFilter -> PandocLua a
59 => a -> LuaFilter -> PandocLua a
55−walkElement x f = liftPandocLua $ walkInlines f x >>= walkBlocks f
60+walkElement x f = liftPandocLua $
61+ walkInlines f x >>= walkInlineLists f >>= walkBlocks f >>= walkBlockLists f
56
62
57−walkInline :: Inline -> LuaFilter -> PandocLua Inline
63+walk_inline :: Inline -> LuaFilter -> PandocLua Inline
58−walkInline = walkElement
64+walk_inline = walkElement
59
65
60−walkBlock :: Block -> LuaFilter -> PandocLua Block
66+walk_block :: Block -> LuaFilter -> PandocLua Block
61−walkBlock = walkElement
67+walk_block = walkElement
62
68
63−readDoc :: T.Text -> Optional T.Text -> PandocLua NumResults
69+read :: T.Text -> Optional T.Text -> PandocLua NumResults
64−readDoc content formatSpecOrNil = liftPandocLua $ do
70+read content formatSpecOrNil = liftPandocLua $ do
65 let formatSpec = fromMaybe "markdown" (Lua.fromOptional formatSpecOrNil)
71 let formatSpec = fromMaybe "markdown" (Lua.fromOptional formatSpecOrNil)
66 res <- Lua.liftIO . runIO $
72 res <- Lua.liftIO . runIO $
67 getReader formatSpec >>= \(rdr,es) ->
73 getReader formatSpec >>= \(rdr,es) ->
@@ -79,11 +85,11 @@ readDoc content formatSpecOrNil = liftPandocLua $ do
79 Left e -> Lua.raiseError $ show e
85 Left e -> Lua.raiseError $ show e
80
86
81-- | Pipes input through a command.
87-- | Pipes input through a command.
82−pipeFn :: String
88+pipe :: String -- ^ path to executable
83− -> [String]
89+ -> [String] -- ^ list of arguments
84− -> BL.ByteString
90+ -> BL.ByteString -- ^ input passed to process via stdin
85− -> PandocLua NumResults
91+ -> PandocLua NumResults
86−pipeFn command args input = liftPandocLua $ do
92+pipe command args input = liftPandocLua $ do
87 (ec, output) <- Lua.liftIO $ pipeProcess Nothing command args input
93 (ec, output) <- Lua.liftIO $ pipeProcess Nothing command args input
88 case ec of
94 case ec of
89 ExitSuccess -> 1 <$ Lua.push output
95 ExitSuccess -> 1 <$ Lua.push output
src/Text/Pandoc/Lua/Module/Utils.hs+1−1
@@ -146,7 +146,7 @@ from_simple_table (SimpleTable capt aligns widths head' body) = do
146 nullAttr
146 nullAttr
147 (Caption Nothing [Plain capt])
147 (Caption Nothing [Plain capt])
148 (zipWith (\a w -> (a, toColWidth w)) aligns widths)
148 (zipWith (\a w -> (a, toColWidth w)) aligns widths)
149− (TableHead nullAttr [blockListToRow head'])
149+ (TableHead nullAttr [blockListToRow head' | not (null head') ])
150 [TableBody nullAttr 0 [] $ map blockListToRow body]
150 [TableBody nullAttr 0 [] $ map blockListToRow body]
151 (TableFoot nullAttr [])
151 (TableFoot nullAttr [])
152 return (NumResults 1)
152 return (NumResults 1)
src/Text/Pandoc/Lua/Packages.hs+2
@@ -17,6 +17,7 @@ import Foreign.Lua (NumResults)
17import Text.Pandoc.Lua.PandocLua (PandocLua, liftPandocLua, loadDefaultModule)
17import Text.Pandoc.Lua.PandocLua (PandocLua, liftPandocLua, loadDefaultModule)
18
18
19import qualified Foreign.Lua as Lua
19import qualified Foreign.Lua as Lua
20+import qualified Foreign.Lua.Module.Path as Path
20import qualified Foreign.Lua.Module.Text as Text
21import qualified Foreign.Lua.Module.Text as Text
21import qualified Text.Pandoc.Lua.Module.Pandoc as Pandoc
22import qualified Text.Pandoc.Lua.Module.Pandoc as Pandoc
22import qualified Text.Pandoc.Lua.Module.MediaBag as MediaBag
23import qualified Text.Pandoc.Lua.Module.MediaBag as MediaBag
@@ -43,6 +44,7 @@ pandocPackageSearcher pkgName =
43 case pkgName of
44 case pkgName of
44 "pandoc" -> pushWrappedHsFun Pandoc.pushModule
45 "pandoc" -> pushWrappedHsFun Pandoc.pushModule
45 "pandoc.mediabag" -> pushWrappedHsFun MediaBag.pushModule
46 "pandoc.mediabag" -> pushWrappedHsFun MediaBag.pushModule
47+ "pandoc.path" -> pushWrappedHsFun Path.pushModule
46 "pandoc.system" -> pushWrappedHsFun System.pushModule
48 "pandoc.system" -> pushWrappedHsFun System.pushModule
47 "pandoc.types" -> pushWrappedHsFun Types.pushModule
49 "pandoc.types" -> pushWrappedHsFun Types.pushModule
48 "pandoc.utils" -> pushWrappedHsFun Utils.pushModule
50 "pandoc.utils" -> pushWrappedHsFun Utils.pushModule
src/Text/Pandoc/MIME.hs+25−2
@@ -10,8 +10,13 @@
10
10
11Mime type lookup.
11Mime type lookup.
12-}
12-}
13−module Text.Pandoc.MIME ( MimeType, getMimeType, getMimeTypeDef,
13+module Text.Pandoc.MIME (
14+ MimeType,
15+ getMimeType,
16+ getMimeTypeDef,
17+ getCharset,
14− extensionFromMimeType, mediaCategory ) where
18+ extensionFromMimeType,
19+ mediaCategory ) where
15import Data.List (isPrefixOf, isSuffixOf)
20import Data.List (isPrefixOf, isSuffixOf)
16import qualified Data.Map as M
21import qualified Data.Map as M
17import qualified Data.Text as T
22import qualified Data.Text as T
@@ -38,6 +43,16 @@ getMimeTypeDef :: FilePath -> MimeType
38getMimeTypeDef = fromMaybe "application/octet-stream" . getMimeType
43getMimeTypeDef = fromMaybe "application/octet-stream" . getMimeType
39
44
40extensionFromMimeType :: MimeType -> Maybe T.Text
45extensionFromMimeType :: MimeType -> Maybe T.Text
46+-- few special cases, where there are multiple options:
47+extensionFromMimeType "text/plain" = Just "txt"
48+extensionFromMimeType "video/quicktime" = Just "mov"
49+extensionFromMimeType "video/mpeg" = Just "mpeg"
50+extensionFromMimeType "video/dv" = Just "dv"
51+extensionFromMimeType "image/vnd.djvu" = Just "djvu"
52+extensionFromMimeType "image/tiff" = Just "tiff"
53+extensionFromMimeType "image/jpeg" = Just "jpg"
54+extensionFromMimeType "application/xml" = Just "xml"
55+extensionFromMimeType "application/ogg" = Just "ogg"
41extensionFromMimeType mimetype =
56extensionFromMimeType mimetype =
42 M.lookup (T.takeWhile (/=';') mimetype) reverseMimeTypes
57 M.lookup (T.takeWhile (/=';') mimetype) reverseMimeTypes
43 -- note: we just look up the basic mime type, dropping the content-encoding etc.
58 -- note: we just look up the basic mime type, dropping the content-encoding etc.
@@ -54,6 +69,14 @@ reverseMimeTypes = M.fromList $ map swap mimeTypesList
54mimeTypes :: M.Map T.Text MimeType
69mimeTypes :: M.Map T.Text MimeType
55mimeTypes = M.fromList mimeTypesList
70mimeTypes = M.fromList mimeTypesList
56
71
72+-- | Get the charset from a mime type, if one is present.
73+getCharset :: MimeType -> Maybe T.Text
74+getCharset mt =
75+ let (_,y) = T.breakOn "charset=" mt
76+ in if T.null y
77+ then Nothing
78+ else Just $ T.toUpper $ T.takeWhile (/= ';') $ T.drop 8 y
79+
57-- | Collection of common mime types.
80-- | Collection of common mime types.
58-- Except for first entry, list borrowed from
81-- Except for first entry, list borrowed from
59-- <https://github.com/Happstack/happstack-server/blob/master/src/Happstack/Server/FileServe/BuildingBlocks.hs happstack-server>
82-- <https://github.com/Happstack/happstack-server/blob/master/src/Happstack/Server/FileServe/BuildingBlocks.hs happstack-server>
src/Text/Pandoc/MediaBag.hs+45−14
@@ -1,4 +1,5 @@
1{-# LANGUAGE CPP #-}
1{-# LANGUAGE CPP #-}
2+{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE DeriveDataTypeable #-}
3{-# LANGUAGE DeriveDataTypeable #-}
3{-# LANGUAGE GeneralizedNewtypeDeriving #-}
4{-# LANGUAGE GeneralizedNewtypeDeriving #-}
4{- |
5{- |
@@ -14,6 +15,7 @@ Definition of a MediaBag object to hold binary resources, and an
14interface for interacting with it.
15interface for interacting with it.
15-}
16-}
16module Text.Pandoc.MediaBag (
17module Text.Pandoc.MediaBag (
18+ MediaItem(..),
17 MediaBag,
19 MediaBag,
18 deleteMedia,
20 deleteMedia,
19 lookupMedia,
21 lookupMedia,
@@ -24,29 +26,43 @@ module Text.Pandoc.MediaBag (
24import qualified Data.ByteString.Lazy as BL
26import qualified Data.ByteString.Lazy as BL
25import Data.Data (Data)
27import Data.Data (Data)
26import qualified Data.Map as M
28import qualified Data.Map as M
27−import Data.Maybe (fromMaybe)
29+import Data.Maybe (fromMaybe, isNothing)
28import Data.Typeable (Typeable)
30import Data.Typeable (Typeable)
29import System.FilePath
31import System.FilePath
30−import qualified System.FilePath.Posix as Posix
31−import Text.Pandoc.MIME (MimeType, getMimeTypeDef)
32+import Text.Pandoc.MIME (MimeType, getMimeTypeDef, extensionFromMimeType)
33+import Data.Text (Text)
34+import qualified Data.Text as T
35+import Data.Digest.Pure.SHA (sha1, showDigest)
36+import Network.URI (URI (..), parseURI)
37+
38+data MediaItem =
39+ MediaItem
40+ { mediaMimeType :: MimeType
41+ , mediaPath :: FilePath
42+ , mediaContents :: BL.ByteString
43+ } deriving (Eq, Ord, Show, Data, Typeable)
32
44
33-- | A container for a collection of binary resources, with names and
45-- | A container for a collection of binary resources, with names and
34-- mime types. Note that a 'MediaBag' is a Monoid, so 'mempty'
46-- mime types. Note that a 'MediaBag' is a Monoid, so 'mempty'
35-- can be used for an empty 'MediaBag', and '<>' can be used to append
47-- can be used for an empty 'MediaBag', and '<>' can be used to append
36-- two 'MediaBag's.
48-- two 'MediaBag's.
37−newtype MediaBag = MediaBag (M.Map [FilePath] (MimeType, BL.ByteString))
49+newtype MediaBag = MediaBag (M.Map Text MediaItem)
38 deriving (Semigroup, Monoid, Data, Typeable)
50 deriving (Semigroup, Monoid, Data, Typeable)
39
51
40instance Show MediaBag where
52instance Show MediaBag where
41 show bag = "MediaBag " ++ show (mediaDirectory bag)
53 show bag = "MediaBag " ++ show (mediaDirectory bag)
42
54
55+-- | We represent paths with /, in normalized form.
56+canonicalize :: FilePath -> Text
57+canonicalize = T.replace "\\" "/" . T.pack . normalise
58+
43-- | Delete a media item from a 'MediaBag', or do nothing if no item corresponds
59-- | Delete a media item from a 'MediaBag', or do nothing if no item corresponds
44-- to the given path.
60-- to the given path.
45deleteMedia :: FilePath -- ^ relative path and canonical name of resource
61deleteMedia :: FilePath -- ^ relative path and canonical name of resource
46 -> MediaBag
62 -> MediaBag
47 -> MediaBag
63 -> MediaBag
48deleteMedia fp (MediaBag mediamap) =
64deleteMedia fp (MediaBag mediamap) =
49− MediaBag $ M.delete (splitDirectories fp) mediamap
65+ MediaBag $ M.delete (canonicalize fp) mediamap
50
66
51-- | Insert a media item into a 'MediaBag', replacing any existing
67-- | Insert a media item into a 'MediaBag', replacing any existing
52-- value with the same name.
68-- value with the same name.
@@ -56,26 +72,41 @@ insertMedia :: FilePath -- ^ relative path and canonical name of resource
56 -> MediaBag
72 -> MediaBag
57 -> MediaBag
73 -> MediaBag
58insertMedia fp mbMime contents (MediaBag mediamap) =
74insertMedia fp mbMime contents (MediaBag mediamap) =
59− MediaBag (M.insert (splitDirectories fp) (mime, contents) mediamap)
75+ MediaBag (M.insert fp' mediaItem mediamap)
60− where mime = fromMaybe fallback mbMime
76+ where mediaItem = MediaItem{ mediaPath = newpath
77+ , mediaContents = contents
78+ , mediaMimeType = mt }
79+ fp' = canonicalize fp
80+ uri = parseURI fp
81+ newpath = if isRelative fp
82+ && isNothing uri
83+ && ".." `notElem` splitPath fp
84+ then T.unpack fp'
85+ else showDigest (sha1 contents) <> "." <> ext
61 fallback = case takeExtension fp of
86 fallback = case takeExtension fp of
62 ".gz" -> getMimeTypeDef $ dropExtension fp
87 ".gz" -> getMimeTypeDef $ dropExtension fp
63 _ -> getMimeTypeDef fp
88 _ -> getMimeTypeDef fp
89+ mt = fromMaybe fallback mbMime
90+ path = maybe fp uriPath uri
91+ ext = case takeExtension path of
92+ '.':e -> e
93+ _ -> maybe "" T.unpack $ extensionFromMimeType mt
94+
64
95
65-- | Lookup a media item in a 'MediaBag', returning mime type and contents.
96-- | Lookup a media item in a 'MediaBag', returning mime type and contents.
66lookupMedia :: FilePath
97lookupMedia :: FilePath
67 -> MediaBag
98 -> MediaBag
68− -> Maybe (MimeType, BL.ByteString)
99+ -> Maybe MediaItem
69−lookupMedia fp (MediaBag mediamap) = M.lookup (splitDirectories fp) mediamap
100+lookupMedia fp (MediaBag mediamap) = M.lookup (canonicalize fp) mediamap
70
101
71-- | Get a list of the file paths stored in a 'MediaBag', with
102-- | Get a list of the file paths stored in a 'MediaBag', with
72-- their corresponding mime types and the lengths in bytes of the contents.
103-- their corresponding mime types and the lengths in bytes of the contents.
73mediaDirectory :: MediaBag -> [(FilePath, MimeType, Int)]
104mediaDirectory :: MediaBag -> [(FilePath, MimeType, Int)]
74−mediaDirectory (MediaBag mediamap) =
105+mediaDirectory mediabag =
75− M.foldrWithKey (\fp (mime,contents) ->
76− ((Posix.joinPath fp, mime, fromIntegral $ BL.length contents):)) [] mediamap
106+ map (\(fp, mt, bs) -> (fp, mt, fromIntegral (BL.length bs)))
107+ (mediaItems mediabag)
77
108
78mediaItems :: MediaBag -> [(FilePath, MimeType, BL.ByteString)]
109mediaItems :: MediaBag -> [(FilePath, MimeType, BL.ByteString)]
79mediaItems (MediaBag mediamap) =
110mediaItems (MediaBag mediamap) =
80− M.foldrWithKey (\fp (mime,contents) ->
111+ map (\item -> (mediaPath item, mediaMimeType item, mediaContents item))
81− ((Posix.joinPath fp, mime, contents):)) [] mediamap
112+ (M.elems mediamap)
src/Text/Pandoc/Options.hs+17−17
@@ -315,6 +315,23 @@ defaultMathJaxURL = "https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml-full.j
315defaultKaTeXURL :: Text
315defaultKaTeXURL :: Text
316defaultKaTeXURL = "https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.11.1/"
316defaultKaTeXURL = "https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.11.1/"
317
317
318+-- Update documentation in doc/filters.md if this is changed.
319+$(deriveJSON defaultOptions{ constructorTagModifier =
320+ camelCaseStrToHyphenated
321+ } ''TrackChanges)
322+
323+$(deriveJSON defaultOptions{ constructorTagModifier =
324+ camelCaseStrToHyphenated
325+ } ''WrapOption)
326+
327+$(deriveJSON defaultOptions{ constructorTagModifier =
328+ camelCaseStrToHyphenated . drop 8
329+ } ''TopLevelDivision)
330+
331+$(deriveJSON defaultOptions{ constructorTagModifier =
332+ camelCaseStrToHyphenated
333+ } ''ReferenceLocation)
334+
318-- Update documentation in doc/filters.md if this is changed.
335-- Update documentation in doc/filters.md if this is changed.
319$(deriveJSON defaultOptions ''ReaderOptions)
336$(deriveJSON defaultOptions ''ReaderOptions)
320
337
@@ -338,20 +355,3 @@ $(deriveJSON defaultOptions{ constructorTagModifier =
338 } ''ObfuscationMethod)
355 } ''ObfuscationMethod)
339
356
340$(deriveJSON defaultOptions ''HTMLSlideVariant)
357$(deriveJSON defaultOptions ''HTMLSlideVariant)
341−
342−-- Update documentation in doc/filters.md if this is changed.
343−$(deriveJSON defaultOptions{ constructorTagModifier =
344− camelCaseStrToHyphenated
345− } ''TrackChanges)
346−
347−$(deriveJSON defaultOptions{ constructorTagModifier =
348− camelCaseStrToHyphenated
349− } ''WrapOption)
350−
351−$(deriveJSON defaultOptions{ constructorTagModifier =
352− camelCaseStrToHyphenated . drop 8
353− } ''TopLevelDivision)
354−
355−$(deriveJSON defaultOptions{ constructorTagModifier =
356− camelCaseStrToHyphenated
357− } ''ReferenceLocation)
src/Text/Pandoc/PDF.hs+33−15
@@ -43,6 +43,7 @@ import Text.Pandoc.Definition
43import Text.Pandoc.Error (PandocError (PandocPDFProgramNotFoundError))
43import Text.Pandoc.Error (PandocError (PandocPDFProgramNotFoundError))
44import Text.Pandoc.MIME (getMimeType)
44import Text.Pandoc.MIME (getMimeType)
45import Text.Pandoc.Options (HTMLMathMethod (..), WriterOptions (..))
45import Text.Pandoc.Options (HTMLMathMethod (..), WriterOptions (..))
46+import Text.Pandoc.Extensions (disableExtension, Extension(Ext_smart))
46import Text.Pandoc.Process (pipeProcess)
47import Text.Pandoc.Process (pipeProcess)
47import System.Process (readProcessWithExitCode)
48import System.Process (readProcessWithExitCode)
48import Text.Pandoc.Shared (inDirectory, stringify, tshow)
49import Text.Pandoc.Shared (inDirectory, stringify, tshow)
@@ -114,7 +115,10 @@ makePDF program pdfargs writer opts doc =
114 runIOorExplode $ do
115 runIOorExplode $ do
115 putCommonState commonState
116 putCommonState commonState
116 doc' <- handleImages opts tmpdir doc
117 doc' <- handleImages opts tmpdir doc
117− source <- writer opts doc'
118+ source <- writer opts{ writerExtensions = -- disable use of quote
119+ -- ligatures to avoid bad ligatures like ?`
120+ disableExtension Ext_smart
121+ (writerExtensions opts) } doc'
118 res <- case baseProg of
122 res <- case baseProg of
119 "context" -> context2pdf verbosity program pdfargs tmpdir source
123 "context" -> context2pdf verbosity program pdfargs tmpdir source
120 "tectonic" -> tectonic2pdf verbosity program pdfargs tmpdir source
124 "tectonic" -> tectonic2pdf verbosity program pdfargs tmpdir source
@@ -198,7 +202,7 @@ convertImage opts tmpdir fname = do
198 Just "image/svg+xml" -> E.catch (do
202 Just "image/svg+xml" -> E.catch (do
199 (exit, _) <- pipeProcess Nothing "rsvg-convert"
203 (exit, _) <- pipeProcess Nothing "rsvg-convert"
200 ["-f","pdf","-a","--dpi-x",dpi,"--dpi-y",dpi,
204 ["-f","pdf","-a","--dpi-x",dpi,"--dpi-y",dpi,
201− "-o",pdfOut,fname] BL.empty
205+ "-o",pdfOut,svgIn] BL.empty
202 if exit == ExitSuccess
206 if exit == ExitSuccess
203 then return $ Right pdfOut
207 then return $ Right pdfOut
204 else return $ Left "conversion from SVG failed")
208 else return $ Left "conversion from SVG failed")
@@ -213,6 +217,7 @@ convertImage opts tmpdir fname = do
213 where
217 where
214 pngOut = replaceDirectory (replaceExtension fname ".png") tmpdir
218 pngOut = replaceDirectory (replaceExtension fname ".png") tmpdir
215 pdfOut = replaceDirectory (replaceExtension fname ".pdf") tmpdir
219 pdfOut = replaceDirectory (replaceExtension fname ".pdf") tmpdir
220+ svgIn = tmpdir </> fname
216 mime = getMimeType fname
221 mime = getMimeType fname
217 doNothing = return (Right fname)
222 doNothing = return (Right fname)
218
223
@@ -266,7 +271,7 @@ missingCharacterWarnings verbosity log' = do
266 | isAscii c = T.singleton c
271 | isAscii c = T.singleton c
267 | otherwise = T.pack $ c : " (U+" ++ printf "%04X" (ord c) ++ ")"
272 | otherwise = T.pack $ c : " (U+" ++ printf "%04X" (ord c) ++ ")"
268 let addCodePoint = T.concatMap toCodePoint
273 let addCodePoint = T.concatMap toCodePoint
269− let warnings = [ addCodePoint (T.pack $ utf8ToString (BC.drop 19 l))
274+ let warnings = [ addCodePoint (utf8ToText (BC.drop 19 l))
270 | l <- ls
275 | l <- ls
271 , isMissingCharacterWarning l
276 , isMissingCharacterWarning l
272 ]
277 ]
@@ -310,7 +315,7 @@ runTectonic verbosity program args' tmpDir' source = do
310 env <- liftIO getEnvironment
315 env <- liftIO getEnvironment
311 when (verbosity >= INFO) $ liftIO $
316 when (verbosity >= INFO) $ liftIO $
312 showVerboseInfo (Just tmpDir) program programArgs env
317 showVerboseInfo (Just tmpDir) program programArgs env
313− (utf8ToString sourceBL)
318+ (utf8ToText sourceBL)
314 (exit, out) <- liftIO $ E.catch
319 (exit, out) <- liftIO $ E.catch
315 (pipeProcess (Just env) program programArgs sourceBL)
320 (pipeProcess (Just env) program programArgs sourceBL)
316 (handlePDFProgramNotFound program)
321 (handlePDFProgramNotFound program)
@@ -381,7 +386,7 @@ runTeXProgram verbosity program args numRuns tmpDir' source = do
381 (pipeProcess (Just env'') program programArgs BL.empty)
386 (pipeProcess (Just env'') program programArgs BL.empty)
382 (handlePDFProgramNotFound program)
387 (handlePDFProgramNotFound program)
383 when (verbosity >= INFO) $ liftIO $ do
388 when (verbosity >= INFO) $ liftIO $ do
384− UTF8.hPutStrLn stderr $ "[makePDF] Run #" ++ show runNumber
389+ UTF8.hPutStrLn stderr $ "[makePDF] Run #" <> tshow runNumber
385 BL.hPutStr stderr out
390 BL.hPutStr stderr out
386 UTF8.hPutStr stderr "\n"
391 UTF8.hPutStr stderr "\n"
387 if runNumber < numRuns
392 if runNumber < numRuns
@@ -401,7 +406,7 @@ generic2pdf :: Verbosity
401generic2pdf verbosity program args source = do
406generic2pdf verbosity program args source = do
402 env' <- getEnvironment
407 env' <- getEnvironment
403 when (verbosity >= INFO) $
408 when (verbosity >= INFO) $
404− showVerboseInfo Nothing program args env' (T.unpack source)
409+ showVerboseInfo Nothing program args env' source
405 (exit, out) <- E.catch
410 (exit, out) <- E.catch
406 (pipeProcess (Just env') program args
411 (pipeProcess (Just env') program args
407 (BL.fromStrict $ UTF8.fromText source))
412 (BL.fromStrict $ UTF8.fromText source))
@@ -490,19 +495,32 @@ showVerboseInfo :: Maybe FilePath
490 -> String
495 -> String
491 -> [String]
496 -> [String]
492 -> [(String, String)]
497 -> [(String, String)]
493− -> String
498+ -> Text
494 -> IO ()
499 -> IO ()
495showVerboseInfo mbTmpDir program programArgs env source = do
500showVerboseInfo mbTmpDir program programArgs env source = do
496 case mbTmpDir of
501 case mbTmpDir of
497 Just tmpDir -> do
502 Just tmpDir -> do
498 UTF8.hPutStrLn stderr "[makePDF] temp dir:"
503 UTF8.hPutStrLn stderr "[makePDF] temp dir:"
499− UTF8.hPutStrLn stderr tmpDir
504+ UTF8.hPutStrLn stderr (T.pack tmpDir)
500 Nothing -> return ()
505 Nothing -> return ()
501 UTF8.hPutStrLn stderr "[makePDF] Command line:"
506 UTF8.hPutStrLn stderr "[makePDF] Command line:"
507+ UTF8.hPutStrLn stderr $
502− UTF8.hPutStrLn stderr $ program ++ " " ++ unwords (map show programArgs)
508+ T.pack program <> " " <> T.pack (unwords (map show programArgs))
503 UTF8.hPutStr stderr "\n"
509 UTF8.hPutStr stderr "\n"
504− UTF8.hPutStrLn stderr "[makePDF] Environment:"
510+ UTF8.hPutStrLn stderr "[makePDF] Relevant environment variables:"
511+ -- we filter out irrelevant stuff to avoid leaking passwords and keys!
512+ let isRelevant ("PATH",_) = True
513+ isRelevant ("TMPDIR",_) = True
514+ isRelevant ("PWD",_) = True
515+ isRelevant ("LANG",_) = True
516+ isRelevant ("HOME",_) = True
517+ isRelevant ("LUA_PATH",_) = True
518+ isRelevant ("LUA_CPATH",_) = True
519+ isRelevant ("SHELL",_) = True
520+ isRelevant ("TEXINPUTS",_) = True
521+ isRelevant ("TEXMFOUTPUT",_) = True
522+ isRelevant _ = False
505− mapM_ (UTF8.hPutStrLn stderr . show) env
523+ mapM_ (UTF8.hPutStrLn stderr . tshow) (filter isRelevant env)
506 UTF8.hPutStr stderr "\n"
524 UTF8.hPutStr stderr "\n"
507 UTF8.hPutStrLn stderr "[makePDF] Source:"
525 UTF8.hPutStrLn stderr "[makePDF] Source:"
508 UTF8.hPutStrLn stderr source
526 UTF8.hPutStrLn stderr source
@@ -513,8 +531,8 @@ handlePDFProgramNotFound program e
513 E.throwIO $ PandocPDFProgramNotFoundError $ T.pack program
531 E.throwIO $ PandocPDFProgramNotFoundError $ T.pack program
514 | otherwise = E.throwIO e
532 | otherwise = E.throwIO e
515
533
516−utf8ToString :: ByteString -> String
534+utf8ToText :: ByteString -> Text
517−utf8ToString lbs =
535+utf8ToText lbs =
518 case decodeUtf8' lbs of
536 case decodeUtf8' lbs of
519− Left _ -> BC.unpack lbs -- if decoding fails, treat as latin1
537+ Left _ -> T.pack $ BC.unpack lbs -- if decoding fails, treat as latin1
520− Right t -> TL.unpack t
538+ Right t -> TL.toStrict t
src/Text/Pandoc/Parsing.hs+361−243
@@ -5,7 +5,6 @@
5{-# LANGUAGE GeneralizedNewtypeDeriving #-}
5{-# LANGUAGE GeneralizedNewtypeDeriving #-}
6{-# LANGUAGE IncoherentInstances #-}
6{-# LANGUAGE IncoherentInstances #-}
7{-# LANGUAGE MultiParamTypeClasses #-}
7{-# LANGUAGE MultiParamTypeClasses #-}
8−{-# LANGUAGE ViewPatterns #-}
9{-# LANGUAGE OverloadedStrings #-}
8{-# LANGUAGE OverloadedStrings #-}
10{- |
9{- |
11 Module : Text.Pandoc.Parsing
10 Module : Text.Pandoc.Parsing
@@ -19,8 +18,7 @@
19A utility library with parsers used in pandoc readers.
18A utility library with parsers used in pandoc readers.
20-}
19-}
21
20
22−module Text.Pandoc.Parsing ( take1WhileP,
21+module Text.Pandoc.Parsing ( module Text.Pandoc.Sources,
23− takeP,
24 countChar,
22 countChar,
25 textStr,
23 textStr,
26 anyLine,
24 anyLine,
@@ -105,14 +103,14 @@ module Text.Pandoc.Parsing ( take1WhileP,
105 singleQuoteEnd,
103 singleQuoteEnd,
106 doubleQuoteStart,
104 doubleQuoteStart,
107 doubleQuoteEnd,
105 doubleQuoteEnd,
108− ellipses,
109 apostrophe,
106 apostrophe,
107+ doubleCloseQuote,
108+ ellipses,
110 dash,
109 dash,
111 nested,
110 nested,
112 citeKey,
111 citeKey,
113 Parser,
112 Parser,
114 ParserT,
113 ParserT,
115− F,
116 Future(..),
114 Future(..),
117 runF,
115 runF,
118 askF,
116 askF,
@@ -123,7 +121,6 @@ module Text.Pandoc.Parsing ( take1WhileP,
123 (<+?>),
121 (<+?>),
124 extractIdClass,
122 extractIdClass,
125 insertIncludedFile,
123 insertIncludedFile,
126− insertIncludedFileF,
127 -- * Re-exports from Text.Parsec
124 -- * Re-exports from Text.Parsec
128 Stream,
125 Stream,
129 runParser,
126 runParser,
@@ -134,22 +131,10 @@ module Text.Pandoc.Parsing ( take1WhileP,
134 getInput,
131 getInput,
135 setInput,
132 setInput,
136 unexpected,
133 unexpected,
137− char,
138− letter,
139− digit,
140− alphaNum,
141 skipMany,
134 skipMany,
142 skipMany1,
135 skipMany1,
143− spaces,
144− space,
145− anyChar,
146− satisfy,
147− newline,
148− string,
149 count,
136 count,
150 eof,
137 eof,
151− noneOf,
152− oneOf,
153 lookAhead,
138 lookAhead,
154 notFollowedBy,
139 notFollowedBy,
155 many,
140 many,
@@ -174,6 +159,8 @@ module Text.Pandoc.Parsing ( take1WhileP,
174 SourcePos,
159 SourcePos,
175 getPosition,
160 getPosition,
176 setPosition,
161 setPosition,
162+ sourceName,
163+ setSourceName,
177 sourceColumn,
164 sourceColumn,
178 sourceLine,
165 sourceLine,
179 setSourceColumn,
166 setSourceColumn,
@@ -189,48 +176,141 @@ module Text.Pandoc.Parsing ( take1WhileP,
189where
176where
190
177
191import Control.Monad.Identity
178import Control.Monad.Identity
179+ ( guard,
180+ join,
181+ unless,
182+ when,
183+ void,
184+ liftM2,
185+ liftM,
186+ Identity(..),
187+ MonadPlus(mzero) )
192import Control.Monad.Reader
188import Control.Monad.Reader
189+ ( asks, runReader, MonadReader(ask), Reader, ReaderT(ReaderT) )
193import Data.Char (chr, isAlphaNum, isAscii, isAsciiUpper, isAsciiLower,
190import Data.Char (chr, isAlphaNum, isAscii, isAsciiUpper, isAsciiLower,
194− isPunctuation, isSpace, ord, toLower, toUpper)
191+ isSpace, ord, toLower, toUpper)
195−import Data.Default
192+import Data.Default ( Default(..) )
196import Data.Functor (($>))
193import Data.Functor (($>))
197import Data.List (intercalate, transpose)
194import Data.List (intercalate, transpose)
198import qualified Data.Map as M
195import qualified Data.Map as M
199−import Data.Maybe (mapMaybe, fromMaybe)
196+import Data.Maybe (fromMaybe)
200import qualified Data.Set as Set
197import qualified Data.Set as Set
201−import Data.String
202import Data.Text (Text)
198import Data.Text (Text)
203import qualified Data.Text as T
199import qualified Data.Text as T
204import Text.HTML.TagSoup.Entity (lookupEntity)
200import Text.HTML.TagSoup.Entity (lookupEntity)
205−import Text.Pandoc.Asciify (toAsciiChar)
201+import Text.Pandoc.Asciify (toAsciiText)
206import Text.Pandoc.Builder (Blocks, HasMeta (..), Inlines, trimInlines)
202import Text.Pandoc.Builder (Blocks, HasMeta (..), Inlines, trimInlines)
207import qualified Text.Pandoc.Builder as B
203import qualified Text.Pandoc.Builder as B
208import Text.Pandoc.Class.PandocMonad (PandocMonad, readFileFromDirs, report)
204import Text.Pandoc.Class.PandocMonad (PandocMonad, readFileFromDirs, report)
209import Text.Pandoc.Definition
205import Text.Pandoc.Definition
206+ ( Target,
207+ nullMeta,
208+ nullAttr,
209+ Meta,
210+ ColWidth(ColWidthDefault, ColWidth),
211+ TableFoot(TableFoot),
212+ TableBody(TableBody),
213+ Attr,
214+ TableHead(TableHead),
215+ Row(..),
216+ Alignment(..),
217+ Inline(Str),
218+ ListNumberDelim(..),
219+ ListAttributes,
220+ ListNumberStyle(..) )
210import Text.Pandoc.Logging
221import Text.Pandoc.Logging
222+ ( LogMessage(CouldNotLoadIncludeFile, DuplicateIdentifier) )
211import Text.Pandoc.Options
223import Text.Pandoc.Options
224+ ( extensionEnabled,
225+ Extension(Ext_old_dashes, Ext_tex_math_dollars,
226+ Ext_tex_math_single_backslash, Ext_tex_math_double_backslash,
227+ Ext_auto_identifiers, Ext_ascii_identifiers, Ext_smart),
228+ ReaderOptions(readerTabStop, readerColumns, readerExtensions) )
212import Text.Pandoc.Readers.LaTeX.Types (Macro)
229import Text.Pandoc.Readers.LaTeX.Types (Macro)
213import Text.Pandoc.Shared
230import Text.Pandoc.Shared
231+ ( uniqueIdent,
232+ tshow,
233+ mapLeft,
234+ compactify,
235+ trim,
236+ trimr,
237+ splitTextByIndices,
238+ safeRead,
239+ trimMath,
240+ schemes,
241+ escapeURI )
242+import Text.Pandoc.Sources
214import qualified Text.Pandoc.UTF8 as UTF8 (putStrLn)
243import qualified Text.Pandoc.UTF8 as UTF8 (putStrLn)
215import Text.Pandoc.XML (fromEntities)
244import Text.Pandoc.XML (fromEntities)
216−import Text.Parsec hiding (token)
245+import Text.Parsec
246+ ( between,
247+ setSourceName,
248+ Parsec,
249+ Column,
250+ Line,
251+ incSourceLine,
252+ incSourceColumn,
253+ setSourceLine,
254+ setSourceColumn,
255+ sourceLine,
256+ sourceColumn,
257+ sourceName,
258+ setSourceName,
259+ setPosition,
260+ getPosition,
261+ updateState,
262+ setState,
263+ getState,
264+ optionMaybe,
265+ optional,
266+ option,
267+ endBy1,
268+ endBy,
269+ sepEndBy1,
270+ sepEndBy,
271+ sepBy1,
272+ sepBy,
273+ try,
274+ choice,
275+ (<?>),
276+ (<|>),
277+ manyTill,
278+ many1,
279+ many,
280+ notFollowedBy,
281+ lookAhead,
282+ eof,
283+ count,
284+ skipMany1,
285+ skipMany,
286+ unexpected,
287+ setInput,
288+ getInput,
289+ anyToken,
290+ tokenPrim,
291+ parse,
292+ runParserT,
293+ runParser,
294+ ParseError,
295+ ParsecT,
296+ SourcePos,
297+ Stream(..) )
217−import Text.Parsec.Pos (initialPos, newPos, updatePosString)
298+import Text.Parsec.Pos (initialPos, newPos)
218−
219−import Control.Monad.Except
299+import Control.Monad.Except ( MonadError(throwError) )
220import Text.Pandoc.Error
300import Text.Pandoc.Error
301+ ( PandocError(PandocParseError, PandocParsecError) )
221
302
222type Parser t s = Parsec t s
303type Parser t s = Parsec t s
223
304
224type ParserT = ParsecT
305type ParserT = ParsecT
225
306
307+
226-- | Reader monad wrapping the parser state. This is used to possibly delay
308-- | Reader monad wrapping the parser state. This is used to possibly delay
227-- evaluation until all relevant information has been parsed and made available
309-- evaluation until all relevant information has been parsed and made available
228-- in the parser state.
310-- in the parser state.
229newtype Future s a = Future { runDelayed :: Reader s a }
311newtype Future s a = Future { runDelayed :: Reader s a }
230 deriving (Monad, Applicative, Functor)
312 deriving (Monad, Applicative, Functor)
231
313
232−type F = Future ParserState
233−
234runF :: Future s a -> s -> a
314runF :: Future s a -> s -> a
235runF = runReader . runDelayed
315runF = runReader . runDelayed
236
316
@@ -253,70 +333,48 @@ instance (Semigroup a, Monoid a) => Monoid (Future s a) where
253 mappend = (<>)
333 mappend = (<>)
254
334
255-- | Like @count@, but packs its result
335-- | Like @count@, but packs its result
256−countChar :: (Stream s m Char, Monad m)
336+countChar :: (Stream s m Char, UpdateSourcePos s Char, Monad m)
257 => Int
337 => Int
258 -> ParsecT s st m Char
338 -> ParsecT s st m Char
259 -> ParsecT s st m Text
339 -> ParsecT s st m Text
260countChar n = fmap T.pack . count n
340countChar n = fmap T.pack . count n
261
341
262-- | Like @string@, but uses @Text@.
342-- | Like @string@, but uses @Text@.
343+textStr :: (Stream s m Char, UpdateSourcePos s Char)
263−textStr :: Stream s m Char => Text -> ParsecT s u m Text
344+ => Text -> ParsecT s u m Text
264textStr t = string (T.unpack t) $> t
345textStr t = string (T.unpack t) $> t
265
346
266−-- | Parse characters while a predicate is true.
347+
267−take1WhileP :: Monad m
268− => (Char -> Bool)
269− -> ParserT Text st m Text
270−take1WhileP f = do
271− -- needed to persuade parsec that this won't match an empty string:
272− c <- satisfy f
273− inp <- getInput
274− pos <- getPosition
275− let (t, rest) = T.span f inp
276− setInput rest
277− setPosition $
278− if f '\t' || f '\n'
279− then updatePosString pos $ T.unpack t
280− else incSourceColumn pos (T.length t)
281− return $ T.singleton c <> t
282−
283−-- Parse n characters of input (or the rest of the input if
284−-- there aren't n characters).
285−takeP :: Monad m => Int -> ParserT Text st m Text
286−takeP n = do
287− guard (n > 0)
288− -- faster than 'count n anyChar'
289− inp <- getInput
290− pos <- getPosition
291− let (xs, rest) = T.splitAt n inp
292− -- needed to persuade parsec that this won't match an empty string:
293− anyChar
294− setInput rest
295− setPosition $ updatePosString pos $ T.unpack xs
296− return xs
297−
298−-- | Parse any line of text
348+-- | Parse any line of text, returning the contents without the
349+-- final newline.
299−anyLine :: Monad m => ParserT Text st m Text
350+anyLine :: Monad m => ParserT Sources st m Text
300anyLine = do
351anyLine = do
301 -- This is much faster than:
352 -- This is much faster than:
302 -- manyTill anyChar newline
353 -- manyTill anyChar newline
303 inp <- getInput
354 inp <- getInput
304− pos <- getPosition
355+ case inp of
356+ Sources [] -> mzero
357+ Sources ((fp,t):inps) ->
358+ -- we assume that lines don't span different input files
305− case T.break (=='\n') inp of
359+ case T.break (=='\n') t of
306− (this, T.uncons -> Just ('\n', rest)) -> do
360+ (this, rest)
361+ | T.null rest
362+ , not (null inps) ->
363+ -- line may span different input files, so do it
364+ -- character by character
365+ T.pack <$> manyTill anyChar newline
366+ | otherwise -> do -- either end of inputs or newline in rest
367+ setInput $ Sources ((fp, rest):inps)
307− -- needed to persuade parsec that this won't match an empty string:
368+ char '\n' -- needed so parsec knows we won't match empty string
308− anyChar
369+ -- and so source pos is updated
309− setInput rest
310− setPosition $ incSourceLine (setSourceColumn pos 1) 1
311− return this
370+ return this
312− _ -> mzero
313
371
314-- | Parse any line, include the final newline in the output
372-- | Parse any line, include the final newline in the output
315−anyLineNewline :: Monad m => ParserT Text st m Text
373+anyLineNewline :: Monad m => ParserT Sources st m Text
316anyLineNewline = (<> "\n") <$> anyLine
374anyLineNewline = (<> "\n") <$> anyLine
317
375
318-- | Parse indent by specified number of spaces (or equiv. tabs)
376-- | Parse indent by specified number of spaces (or equiv. tabs)
319−indentWith :: Stream s m Char
377+indentWith :: (Stream s m Char, UpdateSourcePos s Char)
320 => HasReaderOptions st
378 => HasReaderOptions st
321 => Int -> ParserT s st m Text
379 => Int -> ParserT s st m Text
322indentWith num = do
380indentWith num = do
@@ -401,11 +459,13 @@ notFollowedBy' p = try $ join $ do a <- try p
401 return (return ())
459 return (return ())
402-- (This version due to Andrew Pimlott on the Haskell mailing list.)
460-- (This version due to Andrew Pimlott on the Haskell mailing list.)
403
461
462+oneOfStrings' :: (Stream s m Char, UpdateSourcePos s Char)
404−oneOfStrings' :: Stream s m Char => (Char -> Char -> Bool) -> [Text] -> ParserT s st m Text
463+ => (Char -> Char -> Bool) -> [Text] -> ParserT s st m Text
405oneOfStrings' f = fmap T.pack . oneOfStrings'' f . fmap T.unpack
464oneOfStrings' f = fmap T.pack . oneOfStrings'' f . fmap T.unpack
406
465
407-- TODO: This should be re-implemented in a Text-aware way
466-- TODO: This should be re-implemented in a Text-aware way
467+oneOfStrings'' :: (Stream s m Char, UpdateSourcePos s Char)
408−oneOfStrings'' :: Stream s m Char => (Char -> Char -> Bool) -> [String] -> ParserT s st m String
468+ => (Char -> Char -> Bool) -> [String] -> ParserT s st m String
409oneOfStrings'' _ [] = Prelude.fail "no strings"
469oneOfStrings'' _ [] = Prelude.fail "no strings"
410oneOfStrings'' matches strs = try $ do
470oneOfStrings'' matches strs = try $ do
411 c <- anyChar
471 c <- anyChar
@@ -420,14 +480,16 @@ oneOfStrings'' matches strs = try $ do
420-- | Parses one of a list of strings. If the list contains
480-- | Parses one of a list of strings. If the list contains
421-- two strings one of which is a prefix of the other, the longer
481-- two strings one of which is a prefix of the other, the longer
422-- string will be matched if possible.
482-- string will be matched if possible.
483+oneOfStrings :: (Stream s m Char, UpdateSourcePos s Char)
423−oneOfStrings :: Stream s m Char => [Text] -> ParserT s st m Text
484+ => [Text] -> ParserT s st m Text
424oneOfStrings = oneOfStrings' (==)
485oneOfStrings = oneOfStrings' (==)
425
486
426-- | Parses one of a list of strings (tried in order), case insensitive.
487-- | Parses one of a list of strings (tried in order), case insensitive.
427
488
428-- TODO: This will not be accurate with general Unicode (neither
489-- TODO: This will not be accurate with general Unicode (neither
429-- Text.toLower nor Text.toCaseFold can be implemented with a map)
490-- Text.toLower nor Text.toCaseFold can be implemented with a map)
491+oneOfStringsCI :: (Stream s m Char, UpdateSourcePos s Char)
430−oneOfStringsCI :: Stream s m Char => [Text] -> ParserT s st m Text
492+ => [Text] -> ParserT s st m Text
431oneOfStringsCI = oneOfStrings' ciMatch
493oneOfStringsCI = oneOfStrings' ciMatch
432 where ciMatch x y = toLower' x == toLower' y
494 where ciMatch x y = toLower' x == toLower' y
433 -- this optimizes toLower by checking common ASCII case
495 -- this optimizes toLower by checking common ASCII case
@@ -438,11 +500,13 @@ oneOfStringsCI = oneOfStrings' ciMatch
438 | otherwise = toLower c
500 | otherwise = toLower c
439
501
440-- | Parses a space or tab.
502-- | Parses a space or tab.
503+spaceChar :: (Stream s m Char, UpdateSourcePos s Char)
441−spaceChar :: Stream s m Char => ParserT s st m Char
504+ => ParserT s st m Char
442spaceChar = satisfy $ \c -> c == ' ' || c == '\t'
505spaceChar = satisfy $ \c -> c == ' ' || c == '\t'
443
506
444-- | Parses a nonspace, nonnewline character.
507-- | Parses a nonspace, nonnewline character.
445−nonspaceChar :: Stream s m Char => ParserT s st m Char
508+nonspaceChar :: (Stream s m Char, UpdateSourcePos s Char)
509+ => ParserT s st m Char
446nonspaceChar = satisfy (not . isSpaceChar)
510nonspaceChar = satisfy (not . isSpaceChar)
447
511
448isSpaceChar :: Char -> Bool
512isSpaceChar :: Char -> Bool
@@ -453,21 +517,24 @@ isSpaceChar '\r' = True
453isSpaceChar _ = False
517isSpaceChar _ = False
454
518
455-- | Skips zero or more spaces or tabs.
519-- | Skips zero or more spaces or tabs.
456−skipSpaces :: Stream s m Char => ParserT s st m ()
520+skipSpaces :: (Stream s m Char, UpdateSourcePos s Char)
521+ => ParserT s st m ()
457skipSpaces = skipMany spaceChar
522skipSpaces = skipMany spaceChar
458
523
459-- | Skips zero or more spaces or tabs, then reads a newline.
524-- | Skips zero or more spaces or tabs, then reads a newline.
525+blankline :: (Stream s m Char, UpdateSourcePos s Char)
460−blankline :: Stream s m Char => ParserT s st m Char
526+ => ParserT s st m Char
461blankline = try $ skipSpaces >> newline
527blankline = try $ skipSpaces >> newline
462
528
463-- | Parses one or more blank lines and returns a string of newlines.
529-- | Parses one or more blank lines and returns a string of newlines.
530+blanklines :: (Stream s m Char, UpdateSourcePos s Char)
464−blanklines :: Stream s m Char => ParserT s st m Text
531+ => ParserT s st m Text
465blanklines = T.pack <$> many1 blankline
532blanklines = T.pack <$> many1 blankline
466
533
467-- | Gobble n spaces; if tabs are encountered, expand them
534-- | Gobble n spaces; if tabs are encountered, expand them
468-- and gobble some or all of their spaces, leaving the rest.
535-- and gobble some or all of their spaces, leaving the rest.
469gobbleSpaces :: (HasReaderOptions st, Monad m)
536gobbleSpaces :: (HasReaderOptions st, Monad m)
470− => Int -> ParserT Text st m ()
537+ => Int -> ParserT Sources st m ()
471gobbleSpaces 0 = return ()
538gobbleSpaces 0 = return ()
472gobbleSpaces n
539gobbleSpaces n
473 | n < 0 = error "gobbleSpaces called with negative number"
540 | n < 0 = error "gobbleSpaces called with negative number"
@@ -475,18 +542,26 @@ gobbleSpaces n
475 char ' ' <|> eatOneSpaceOfTab
542 char ' ' <|> eatOneSpaceOfTab
476 gobbleSpaces (n - 1)
543 gobbleSpaces (n - 1)
477
544
478−eatOneSpaceOfTab :: (HasReaderOptions st, Monad m) => ParserT Text st m Char
545+eatOneSpaceOfTab :: (HasReaderOptions st, Monad m) => ParserT Sources st m Char
479eatOneSpaceOfTab = do
546eatOneSpaceOfTab = do
480− char '\t'
547+ lookAhead (char '\t')
548+ pos <- getPosition
481 tabstop <- getOption readerTabStop
549 tabstop <- getOption readerTabStop
550+ -- replace the tab on the input stream with spaces
551+ let numSpaces = tabstop - ((sourceColumn pos - 1) `mod` tabstop)
482 inp <- getInput
552 inp <- getInput
483− setInput $ T.replicate (tabstop - 1) " " <> inp
553+ setInput $
484− return ' '
554+ case inp of
555+ Sources [] -> error "eatOneSpaceOfTab - empty Sources list"
556+ Sources ((fp,t):rest) ->
557+ -- drop the tab and add spaces
558+ Sources ((fp, T.replicate numSpaces " " <> T.drop 1 t):rest)
559+ char ' '
485
560
486-- | Gobble up to n spaces; if tabs are encountered, expand them
561-- | Gobble up to n spaces; if tabs are encountered, expand them
487-- and gobble some or all of their spaces, leaving the rest.
562-- and gobble some or all of their spaces, leaving the rest.
488gobbleAtMostSpaces :: (HasReaderOptions st, Monad m)
563gobbleAtMostSpaces :: (HasReaderOptions st, Monad m)
489− => Int -> ParserT Text st m Int
564+ => Int -> ParserT Sources st m Int
490gobbleAtMostSpaces 0 = return 0
565gobbleAtMostSpaces 0 = return 0
491gobbleAtMostSpaces n
566gobbleAtMostSpaces n
492 | n < 0 = error "gobbleAtMostSpaces called with negative number"
567 | n < 0 = error "gobbleAtMostSpaces called with negative number"
@@ -495,7 +570,8 @@ gobbleAtMostSpaces n
495 (+ 1) <$> gobbleAtMostSpaces (n - 1)
570 (+ 1) <$> gobbleAtMostSpaces (n - 1)
496
571
497-- | Parses material enclosed between start and end parsers.
572-- | Parses material enclosed between start and end parsers.
573+enclosed :: (Show end, Stream s m Char, UpdateSourcePos s Char)
498−enclosed :: (Show end, Stream s m Char) => ParserT s st m t -- ^ start parser
574+ => ParserT s st m t -- ^ start parser
499 -> ParserT s st m end -- ^ end parser
575 -> ParserT s st m end -- ^ end parser
500 -> ParserT s st m a -- ^ content parser (to be used repeatedly)
576 -> ParserT s st m a -- ^ content parser (to be used repeatedly)
501 -> ParserT s st m [a]
577 -> ParserT s st m [a]
@@ -503,39 +579,41 @@ enclosed start end parser = try $
503 start >> notFollowedBy space >> many1Till parser end
579 start >> notFollowedBy space >> many1Till parser end
504
580
505-- | Parse string, case insensitive.
581-- | Parse string, case insensitive.
582+stringAnyCase :: (Stream s m Char, UpdateSourcePos s Char)
506−stringAnyCase :: Stream s m Char => Text -> ParserT s st m Text
583+ => Text -> ParserT s st m Text
507stringAnyCase = fmap T.pack . stringAnyCase' . T.unpack
584stringAnyCase = fmap T.pack . stringAnyCase' . T.unpack
508
585
586+stringAnyCase' :: (Stream s m Char, UpdateSourcePos s Char)
509−stringAnyCase' :: Stream s m Char => String -> ParserT s st m String
587+ => String -> ParserT s st m String
510stringAnyCase' [] = string ""
588stringAnyCase' [] = string ""
511stringAnyCase' (x:xs) = do
589stringAnyCase' (x:xs) = do
512 firstChar <- char (toUpper x) <|> char (toLower x)
590 firstChar <- char (toUpper x) <|> char (toLower x)
513 rest <- stringAnyCase' xs
591 rest <- stringAnyCase' xs
514 return (firstChar:rest)
592 return (firstChar:rest)
515
593
594+-- TODO rewrite by just adding to Sources stream?
516-- | Parse contents of 'str' using 'parser' and return result.
595-- | Parse contents of 'str' using 'parser' and return result.
517−parseFromString :: (Stream s m Char, IsString s)
596+parseFromString :: Monad m
518− => ParserT s st m r
597+ => ParserT Sources st m r
519 -> Text
598 -> Text
520− -> ParserT s st m r
599+ -> ParserT Sources st m r
521parseFromString parser str = do
600parseFromString parser str = do
522 oldPos <- getPosition
601 oldPos <- getPosition
523 setPosition $ initialPos "chunk"
602 setPosition $ initialPos "chunk"
524 oldInput <- getInput
603 oldInput <- getInput
525− setInput $ fromString $ T.unpack str
604+ setInput $ toSources str
526 result <- parser
605 result <- parser
527 spaces
606 spaces
528− eof
529 setInput oldInput
607 setInput oldInput
530 setPosition oldPos
608 setPosition oldPos
531 return result
609 return result
532
610
533-- | Like 'parseFromString' but specialized for 'ParserState'.
611-- | Like 'parseFromString' but specialized for 'ParserState'.
534-- This resets 'stateLastStrPos', which is almost always what we want.
612-- This resets 'stateLastStrPos', which is almost always what we want.
535−parseFromString' :: (Stream s m Char, IsString s, HasLastStrPosition u)
613+parseFromString' :: (Monad m, HasLastStrPosition u)
536− => ParserT s u m a
614+ => ParserT Sources u m a
537 -> Text
615 -> Text
538− -> ParserT s u m a
616+ -> ParserT Sources u m a
539parseFromString' parser str = do
617parseFromString' parser str = do
540 oldLastStrPos <- getLastStrPos <$> getState
618 oldLastStrPos <- getLastStrPos <$> getState
541 updateState $ setLastStrPos Nothing
619 updateState $ setLastStrPos Nothing
@@ -544,7 +622,7 @@ parseFromString' parser str = do
544 return res
622 return res
545
623
546-- | Parse raw line block up to and including blank lines.
624-- | Parse raw line block up to and including blank lines.
547−lineClump :: Monad m => ParserT Text st m Text
625+lineClump :: Monad m => ParserT Sources st m Text
548lineClump = blanklines
626lineClump = blanklines
549 <|> (T.unlines <$> many1 (notFollowedBy blankline >> anyLine))
627 <|> (T.unlines <$> many1 (notFollowedBy blankline >> anyLine))
550
628
@@ -553,7 +631,7 @@ lineClump = blanklines
553-- pairs of open and close, which must be different. For example,
631-- pairs of open and close, which must be different. For example,
554-- @charsInBalanced '(' ')' anyChar@ will parse "(hello (there))"
632-- @charsInBalanced '(' ')' anyChar@ will parse "(hello (there))"
555-- and return "hello (there)".
633-- and return "hello (there)".
556−charsInBalanced :: Stream s m Char => Char -> Char -> ParserT s st m Char
634+charsInBalanced :: (Stream s m Char, UpdateSourcePos s Char) => Char -> Char -> ParserT s st m Char
557 -> ParserT s st m Text
635 -> ParserT s st m Text
558charsInBalanced open close parser = try $ do
636charsInBalanced open close parser = try $ do
559 char open
637 char open
@@ -572,7 +650,7 @@ charsInBalanced open close parser = try $ do
572-- Auxiliary functions for romanNumeral:
650-- Auxiliary functions for romanNumeral:
573
651
574-- | Parses a roman numeral (uppercase or lowercase), returns number.
652-- | Parses a roman numeral (uppercase or lowercase), returns number.
575−romanNumeral :: Stream s m Char => Bool -- ^ Uppercase if true
653+romanNumeral :: (Stream s m Char, UpdateSourcePos s Char) => Bool -- ^ Uppercase if true
576 -> ParserT s st m Int
654 -> ParserT s st m Int
577romanNumeral upperCase = do
655romanNumeral upperCase = do
578 let rchar uc = char $ if upperCase then uc else toLower uc
656 let rchar uc = char $ if upperCase then uc else toLower uc
@@ -608,20 +686,19 @@ romanNumeral upperCase = do
608
686
609-- | Parses an email address; returns original and corresponding
687-- | Parses an email address; returns original and corresponding
610-- escaped mailto: URI.
688-- escaped mailto: URI.
611−emailAddress :: Stream s m Char => ParserT s st m (Text, Text)
689+emailAddress :: (Stream s m Char, UpdateSourcePos s Char) => ParserT s st m (Text, Text)
612emailAddress = try $ toResult <$> mailbox <*> (char '@' *> domain)
690emailAddress = try $ toResult <$> mailbox <*> (char '@' *> domain)
613 where toResult mbox dom = let full = fromEntities $ T.pack $ mbox ++ '@':dom
691 where toResult mbox dom = let full = fromEntities $ T.pack $ mbox ++ '@':dom
614 in (full, escapeURI $ "mailto:" <> full)
692 in (full, escapeURI $ "mailto:" <> full)
615 mailbox = intercalate "." <$> (emailWord `sepBy1'` dot)
693 mailbox = intercalate "." <$> (emailWord `sepBy1'` dot)
616 domain = intercalate "." <$> (subdomain `sepBy1'` dot)
694 domain = intercalate "." <$> (subdomain `sepBy1'` dot)
617 dot = char '.'
695 dot = char '.'
618− subdomain = many1 $ alphaNum <|> innerPunct
696+ subdomain = many1 $ alphaNum <|> innerPunct (=='-')
619 -- this excludes some valid email addresses, since an
697 -- this excludes some valid email addresses, since an
620 -- email could contain e.g. '__', but gives better results
698 -- email could contain e.g. '__', but gives better results
621 -- for our purposes, when combined with markdown parsing:
699 -- for our purposes, when combined with markdown parsing:
622− innerPunct = try (satisfy (\c -> isEmailPunct c || c == '@')
700+ innerPunct f = try (satisfy f
623− <* notFollowedBy space
701+ <* notFollowedBy (satisfy (not . isAlphaNum)))
624− <* notFollowedBy (satisfy isPunctuation))
625 -- technically an email address could begin with a symbol,
702 -- technically an email address could begin with a symbol,
626 -- but allowing this creates too many problems.
703 -- but allowing this creates too many problems.
627 -- See e.g. https://github.com/jgm/pandoc/issues/2940
704 -- See e.g. https://github.com/jgm/pandoc/issues/2940
@@ -632,11 +709,11 @@ emailAddress = try $ toResult <$> mailbox <*> (char '@' *> domain)
632 isEmailPunct c = T.any (== c) "!\"#$%&'*+-/=?^_{|}~;"
709 isEmailPunct c = T.any (== c) "!\"#$%&'*+-/=?^_{|}~;"
633
710
634
711
635−uriScheme :: Stream s m Char => ParserT s st m Text
712+uriScheme :: (Stream s m Char, UpdateSourcePos s Char) => ParserT s st m Text
636uriScheme = oneOfStringsCI (Set.toList schemes)
713uriScheme = oneOfStringsCI (Set.toList schemes)
637
714
638-- | Parses a URI. Returns pair of original and URI-escaped version.
715-- | Parses a URI. Returns pair of original and URI-escaped version.
639−uri :: Stream s m Char => ParserT s st m (Text, Text)
716+uri :: (Stream s m Char, UpdateSourcePos s Char) => ParserT s st m (Text, Text)
640uri = try $ do
717uri = try $ do
641 scheme <- uriScheme
718 scheme <- uriScheme
642 char ':'
719 char ':'
@@ -679,7 +756,7 @@ uri = try $ do
679 uriChunkBetween l r = try $ do chunk <- between (char l) (char r) uriChunk
756 uriChunkBetween l r = try $ do chunk <- between (char l) (char r) uriChunk
680 return (T.pack $ [l] ++ chunk ++ [r])
757 return (T.pack $ [l] ++ chunk ++ [r])
681
758
682−mathInlineWith :: Stream s m Char => Text -> Text -> ParserT s st m Text
759+mathInlineWith :: (Stream s m Char, UpdateSourcePos s Char) => Text -> Text -> ParserT s st m Text
683mathInlineWith op cl = try $ do
760mathInlineWith op cl = try $ do
684 textStr op
761 textStr op
685 when (op == "$") $ notFollowedBy space
762 when (op == "$") $ notFollowedBy space
@@ -700,10 +777,10 @@ mathInlineWith op cl = try $ do
700 notFollowedBy digit -- to prevent capture of $5
777 notFollowedBy digit -- to prevent capture of $5
701 return $ trimMath $ T.concat words'
778 return $ trimMath $ T.concat words'
702 where
779 where
703− inBalancedBraces :: Stream s m Char => Int -> Text -> ParserT s st m Text
780+ inBalancedBraces :: (Stream s m Char, UpdateSourcePos s Char) => Int -> Text -> ParserT s st m Text
704 inBalancedBraces n = fmap T.pack . inBalancedBraces' n . T.unpack
781 inBalancedBraces n = fmap T.pack . inBalancedBraces' n . T.unpack
705
782
706− inBalancedBraces' :: Stream s m Char => Int -> String -> ParserT s st m String
783+ inBalancedBraces' :: (Stream s m Char, UpdateSourcePos s Char) => Int -> String -> ParserT s st m String
707 inBalancedBraces' 0 "" = do
784 inBalancedBraces' 0 "" = do
708 c <- anyChar
785 c <- anyChar
709 if c == '{'
786 if c == '{'
@@ -720,13 +797,13 @@ mathInlineWith op cl = try $ do
720 '{' -> inBalancedBraces' (numOpen + 1) (c:xs)
797 '{' -> inBalancedBraces' (numOpen + 1) (c:xs)
721 _ -> inBalancedBraces' numOpen (c:xs)
798 _ -> inBalancedBraces' numOpen (c:xs)
722
799
723−mathDisplayWith :: Stream s m Char => Text -> Text -> ParserT s st m Text
800+mathDisplayWith :: (Stream s m Char, UpdateSourcePos s Char) => Text -> Text -> ParserT s st m Text
724mathDisplayWith op cl = try $ fmap T.pack $ do
801mathDisplayWith op cl = try $ fmap T.pack $ do
725 textStr op
802 textStr op
726 many1Till (satisfy (/= '\n') <|> (newline <* notFollowedBy' blankline))
803 many1Till (satisfy (/= '\n') <|> (newline <* notFollowedBy' blankline))
727 (try $ textStr cl)
804 (try $ textStr cl)
728
805
729−mathDisplay :: (HasReaderOptions st, Stream s m Char)
806+mathDisplay :: (HasReaderOptions st, Stream s m Char, UpdateSourcePos s Char)
730 => ParserT s st m Text
807 => ParserT s st m Text
731mathDisplay =
808mathDisplay =
732 (guardEnabled Ext_tex_math_dollars >> mathDisplayWith "$$" "$$")
809 (guardEnabled Ext_tex_math_dollars >> mathDisplayWith "$$" "$$")
@@ -735,7 +812,7 @@ mathDisplay =
735 <|> (guardEnabled Ext_tex_math_double_backslash >>
812 <|> (guardEnabled Ext_tex_math_double_backslash >>
736 mathDisplayWith "\\\\[" "\\\\]")
813 mathDisplayWith "\\\\[" "\\\\]")
737
814
738−mathInline :: (HasReaderOptions st , Stream s m Char)
815+mathInline :: (HasReaderOptions st, Stream s m Char, UpdateSourcePos s Char)
739 => ParserT s st m Text
816 => ParserT s st m Text
740mathInline =
817mathInline =
741 (guardEnabled Ext_tex_math_dollars >> mathInlineWith "$" "$")
818 (guardEnabled Ext_tex_math_dollars >> mathInlineWith "$" "$")
@@ -748,7 +825,7 @@ mathInline =
748-- displacement (the difference between the source column at the end
825-- displacement (the difference between the source column at the end
749-- and the source column at the beginning). Vertical displacement
826-- and the source column at the beginning). Vertical displacement
750-- (source row) is ignored.
827-- (source row) is ignored.
751−withHorizDisplacement :: Stream s m Char
828+withHorizDisplacement :: (Stream s m Char, UpdateSourcePos s Char)
752 => ParserT s st m a -- ^ Parser to apply
829 => ParserT s st m a -- ^ Parser to apply
753 -> ParserT s st m (a, Int) -- ^ (result, displacement)
830 -> ParserT s st m (a, Int) -- ^ (result, displacement)
754withHorizDisplacement parser = do
831withHorizDisplacement parser = do
@@ -760,30 +837,37 @@ withHorizDisplacement parser = do
760-- | Applies a parser and returns the raw string that was parsed,
837-- | Applies a parser and returns the raw string that was parsed,
761-- along with the value produced by the parser.
838-- along with the value produced by the parser.
762withRaw :: Monad m
839withRaw :: Monad m
763− => ParsecT Text st m a
840+ => ParsecT Sources st m a
764− -> ParsecT Text st m (a, Text)
841+ -> ParsecT Sources st m (a, Text)
765withRaw parser = do
842withRaw parser = do
766− pos1 <- getPosition
767− inp <- getInput
843+ inps1 <- getInput
768 result <- parser
844 result <- parser
769− pos2 <- getPosition
845+ inps2 <- getInput
770− let (l1,c1) = (sourceLine pos1, sourceColumn pos1)
846+ -- 'raw' is the difference between inps1 and inps2
771− let (l2,c2) = (sourceLine pos2, sourceColumn pos2)
847+ return (result, sourcesDifference inps1 inps2)
772− let inplines = take ((l2 - l1) + 1) $ T.lines inp
848+
773− let raw = case inplines of
849+sourcesDifference :: Sources -> Sources -> Text
850+sourcesDifference (Sources is1) (Sources is2) = go is1 is2
851+ where
852+ go inps1 inps2 =
853+ case (inps1, inps2) of
774− [] -> ""
854+ ([], _) -> mempty
775− [l] -> T.take (c2 - c1) l
855+ (_, []) -> mconcat $ map snd inps1
776− ls -> T.unlines (init ls) <> T.take (c2 - 1) (last ls)
856+ ((p1,t1):rest1, (p2, t2):rest2)
777− return (result, raw)
857+ | p1 == p2
858+ , t1 == t2 -> go rest1 rest2
859+ | p1 == p2
860+ , t1 /= t2 -> fromMaybe mempty $ T.stripSuffix t2 t1
861+ | otherwise -> t1 <> go rest1 inps2
778
862
779-- | Parses backslash, then applies character parser.
863-- | Parses backslash, then applies character parser.
780−escaped :: Stream s m Char
864+escaped :: (Stream s m Char, UpdateSourcePos s Char)
781 => ParserT s st m Char -- ^ Parser for character to escape
865 => ParserT s st m Char -- ^ Parser for character to escape
782 -> ParserT s st m Char
866 -> ParserT s st m Char
783escaped parser = try $ char '\\' >> parser
867escaped parser = try $ char '\\' >> parser
784
868
785-- | Parse character entity.
869-- | Parse character entity.
786−characterReference :: Stream s m Char => ParserT s st m Char
870+characterReference :: (Stream s m Char, UpdateSourcePos s Char) => ParserT s st m Char
787characterReference = try $ do
871characterReference = try $ do
788 char '&'
872 char '&'
789 ent <- many1Till nonspaceChar (char ';')
873 ent <- many1Till nonspaceChar (char ';')
@@ -796,19 +880,19 @@ characterReference = try $ do
796 _ -> Prelude.fail "entity not found"
880 _ -> Prelude.fail "entity not found"
797
881
798-- | Parses an uppercase roman numeral and returns (UpperRoman, number).
882-- | Parses an uppercase roman numeral and returns (UpperRoman, number).
799−upperRoman :: Stream s m Char => ParserT s st m (ListNumberStyle, Int)
883+upperRoman :: (Stream s m Char, UpdateSourcePos s Char) => ParserT s st m (ListNumberStyle, Int)
800upperRoman = do
884upperRoman = do
801 num <- romanNumeral True
885 num <- romanNumeral True
802 return (UpperRoman, num)
886 return (UpperRoman, num)
803
887
804-- | Parses a lowercase roman numeral and returns (LowerRoman, number).
888-- | Parses a lowercase roman numeral and returns (LowerRoman, number).
805−lowerRoman :: Stream s m Char => ParserT s st m (ListNumberStyle, Int)
889+lowerRoman :: (Stream s m Char, UpdateSourcePos s Char) => ParserT s st m (ListNumberStyle, Int)
806lowerRoman = do
890lowerRoman = do
807 num <- romanNumeral False
891 num <- romanNumeral False
808 return (LowerRoman, num)
892 return (LowerRoman, num)
809
893
810-- | Parses a decimal numeral and returns (Decimal, number).
894-- | Parses a decimal numeral and returns (Decimal, number).
811−decimal :: Stream s m Char => ParserT s st m (ListNumberStyle, Int)
895+decimal :: (Stream s m Char, UpdateSourcePos s Char) => ParserT s st m (ListNumberStyle, Int)
812decimal = do
896decimal = do
813 num <- many1 digit
897 num <- many1 digit
814 return (Decimal, fromMaybe 1 $ safeRead $ T.pack num)
898 return (Decimal, fromMaybe 1 $ safeRead $ T.pack num)
@@ -817,7 +901,7 @@ decimal = do
817-- returns (DefaultStyle, [next example number]). The next
901-- returns (DefaultStyle, [next example number]). The next
818-- example number is incremented in parser state, and the label
902-- example number is incremented in parser state, and the label
819-- (if present) is added to the label table.
903-- (if present) is added to the label table.
820−exampleNum :: Stream s m Char
904+exampleNum :: (Stream s m Char, UpdateSourcePos s Char)
821 => ParserT s ParserState m (ListNumberStyle, Int)
905 => ParserT s ParserState m (ListNumberStyle, Int)
822exampleNum = do
906exampleNum = do
823 char '@'
907 char '@'
@@ -836,37 +920,37 @@ exampleNum = do
836 return (Example, num)
920 return (Example, num)
837
921
838-- | Parses a '#' returns (DefaultStyle, 1).
922-- | Parses a '#' returns (DefaultStyle, 1).
839−defaultNum :: Stream s m Char => ParserT s st m (ListNumberStyle, Int)
923+defaultNum :: (Stream s m Char, UpdateSourcePos s Char) => ParserT s st m (ListNumberStyle, Int)
840defaultNum = do
924defaultNum = do
841 char '#'
925 char '#'
842 return (DefaultStyle, 1)
926 return (DefaultStyle, 1)
843
927
844-- | Parses a lowercase letter and returns (LowerAlpha, number).
928-- | Parses a lowercase letter and returns (LowerAlpha, number).
845−lowerAlpha :: Stream s m Char => ParserT s st m (ListNumberStyle, Int)
929+lowerAlpha :: (Stream s m Char, UpdateSourcePos s Char) => ParserT s st m (ListNumberStyle, Int)
846lowerAlpha = do
930lowerAlpha = do
847 ch <- satisfy isAsciiLower
931 ch <- satisfy isAsciiLower
848 return (LowerAlpha, ord ch - ord 'a' + 1)
932 return (LowerAlpha, ord ch - ord 'a' + 1)
849
933
850-- | Parses an uppercase letter and returns (UpperAlpha, number).
934-- | Parses an uppercase letter and returns (UpperAlpha, number).
851−upperAlpha :: Stream s m Char => ParserT s st m (ListNumberStyle, Int)
935+upperAlpha :: (Stream s m Char, UpdateSourcePos s Char) => ParserT s st m (ListNumberStyle, Int)
852upperAlpha = do
936upperAlpha = do
853 ch <- satisfy isAsciiUpper
937 ch <- satisfy isAsciiUpper
854 return (UpperAlpha, ord ch - ord 'A' + 1)
938 return (UpperAlpha, ord ch - ord 'A' + 1)
855
939
856-- | Parses a roman numeral i or I
940-- | Parses a roman numeral i or I
857−romanOne :: Stream s m Char => ParserT s st m (ListNumberStyle, Int)
941+romanOne :: (Stream s m Char, UpdateSourcePos s Char) => ParserT s st m (ListNumberStyle, Int)
858romanOne = (char 'i' >> return (LowerRoman, 1)) <|>
942romanOne = (char 'i' >> return (LowerRoman, 1)) <|>
859 (char 'I' >> return (UpperRoman, 1))
943 (char 'I' >> return (UpperRoman, 1))
860
944
861-- | Parses an ordered list marker and returns list attributes.
945-- | Parses an ordered list marker and returns list attributes.
862−anyOrderedListMarker :: Stream s m Char => ParserT s ParserState m ListAttributes
946+anyOrderedListMarker :: (Stream s m Char, UpdateSourcePos s Char) => ParserT s ParserState m ListAttributes
863anyOrderedListMarker = choice
947anyOrderedListMarker = choice
864 [delimParser numParser | delimParser <- [inPeriod, inOneParen, inTwoParens],
948 [delimParser numParser | delimParser <- [inPeriod, inOneParen, inTwoParens],
865 numParser <- [decimal, exampleNum, defaultNum, romanOne,
949 numParser <- [decimal, exampleNum, defaultNum, romanOne,
866 lowerAlpha, lowerRoman, upperAlpha, upperRoman]]
950 lowerAlpha, lowerRoman, upperAlpha, upperRoman]]
867
951
868-- | Parses a list number (num) followed by a period, returns list attributes.
952-- | Parses a list number (num) followed by a period, returns list attributes.
869−inPeriod :: Stream s m Char
953+inPeriod :: (Stream s m Char, UpdateSourcePos s Char)
870 => ParserT s st m (ListNumberStyle, Int)
954 => ParserT s st m (ListNumberStyle, Int)
871 -> ParserT s st m ListAttributes
955 -> ParserT s st m ListAttributes
872inPeriod num = try $ do
956inPeriod num = try $ do
@@ -878,7 +962,7 @@ inPeriod num = try $ do
878 return (start, style, delim)
962 return (start, style, delim)
879
963
880-- | Parses a list number (num) followed by a paren, returns list attributes.
964-- | Parses a list number (num) followed by a paren, returns list attributes.
881−inOneParen :: Stream s m Char
965+inOneParen :: (Stream s m Char, UpdateSourcePos s Char)
882 => ParserT s st m (ListNumberStyle, Int)
966 => ParserT s st m (ListNumberStyle, Int)
883 -> ParserT s st m ListAttributes
967 -> ParserT s st m ListAttributes
884inOneParen num = try $ do
968inOneParen num = try $ do
@@ -887,7 +971,7 @@ inOneParen num = try $ do
887 return (start, style, OneParen)
971 return (start, style, OneParen)
888
972
889-- | Parses a list number (num) enclosed in parens, returns list attributes.
973-- | Parses a list number (num) enclosed in parens, returns list attributes.
890−inTwoParens :: Stream s m Char
974+inTwoParens :: (Stream s m Char, UpdateSourcePos s Char)
891 => ParserT s st m (ListNumberStyle, Int)
975 => ParserT s st m (ListNumberStyle, Int)
892 -> ParserT s st m ListAttributes
976 -> ParserT s st m ListAttributes
893inTwoParens num = try $ do
977inTwoParens num = try $ do
@@ -898,7 +982,7 @@ inTwoParens num = try $ do
898
982
899-- | Parses an ordered list marker with a given style and delimiter,
983-- | Parses an ordered list marker with a given style and delimiter,
900-- returns number.
984-- returns number.
901−orderedListMarker :: Stream s m Char
985+orderedListMarker :: (Stream s m Char, UpdateSourcePos s Char)
902 => ListNumberStyle
986 => ListNumberStyle
903 -> ListNumberDelim
987 -> ListNumberDelim
904 -> ParserT s ParserState m Int
988 -> ParserT s ParserState m Int
@@ -921,10 +1005,10 @@ orderedListMarker style delim = do
921 return start
1005 return start
922
1006
923-- | Parses a character reference and returns a Str element.
1007-- | Parses a character reference and returns a Str element.
924−charRef :: Stream s m Char => ParserT s st m Inline
1008+charRef :: (Stream s m Char, UpdateSourcePos s Char) => ParserT s st m Inline
925charRef = Str . T.singleton <$> characterReference
1009charRef = Str . T.singleton <$> characterReference
926
1010
927−lineBlockLine :: Monad m => ParserT Text st m Text
1011+lineBlockLine :: Monad m => ParserT Sources st m Text
928lineBlockLine = try $ do
1012lineBlockLine = try $ do
929 char '|'
1013 char '|'
930 char ' '
1014 char ' '
@@ -934,11 +1018,11 @@ lineBlockLine = try $ do
934 continuations <- many (try $ char ' ' >> anyLine)
1018 continuations <- many (try $ char ' ' >> anyLine)
935 return $ white <> T.unwords (line : continuations)
1019 return $ white <> T.unwords (line : continuations)
936
1020
937−blankLineBlockLine :: Stream s m Char => ParserT s st m Char
1021+blankLineBlockLine :: (Stream s m Char, UpdateSourcePos s Char) => ParserT s st m Char
938blankLineBlockLine = try (char '|' >> blankline)
1022blankLineBlockLine = try (char '|' >> blankline)
939
1023
940-- | Parses an RST-style line block and returns a list of strings.
1024-- | Parses an RST-style line block and returns a list of strings.
941−lineBlockLines :: Monad m => ParserT Text st m [Text]
1025+lineBlockLines :: Monad m => ParserT Sources st m [Text]
942lineBlockLines = try $ do
1026lineBlockLines = try $ do
943 lines' <- many1 (lineBlockLine <|> (T.singleton <$> blankLineBlockLine))
1027 lines' <- many1 (lineBlockLine <|> (T.singleton <$> blankLineBlockLine))
944 skipMany blankline
1028 skipMany blankline
@@ -946,7 +1030,8 @@ lineBlockLines = try $ do
946
1030
947-- | Parse a table using 'headerParser', 'rowParser',
1031-- | Parse a table using 'headerParser', 'rowParser',
948-- 'lineParser', and 'footerParser'.
1032-- 'lineParser', and 'footerParser'.
1033+tableWith :: (Stream s m Char, UpdateSourcePos s Char,
949−tableWith :: (Stream s m Char, HasReaderOptions st, Monad mf)
1034+ HasReaderOptions st, Monad mf)
950 => ParserT s st m (mf [Blocks], [Alignment], [Int])
1035 => ParserT s st m (mf [Blocks], [Alignment], [Int])
951 -> ([Int] -> ParserT s st m (mf [Blocks]))
1036 -> ([Int] -> ParserT s st m (mf [Blocks]))
952 -> ParserT s st m sep
1037 -> ParserT s st m sep
@@ -966,7 +1051,8 @@ tableWith headerParser rowParser lineParser footerParser = try $ do
966
1051
967type TableComponents mf = ([Alignment], [Double], mf [Row], mf [Row])
1052type TableComponents mf = ([Alignment], [Double], mf [Row], mf [Row])
968
1053
1054+tableWith' :: (Stream s m Char, UpdateSourcePos s Char,
969−tableWith' :: (Stream s m Char, HasReaderOptions st, Monad mf)
1055+ HasReaderOptions st, Monad mf)
970 => ParserT s st m (mf [Blocks], [Alignment], [Int])
1056 => ParserT s st m (mf [Blocks], [Alignment], [Int])
971 -> ([Int] -> ParserT s st m (mf [Blocks]))
1057 -> ([Int] -> ParserT s st m (mf [Blocks]))
972 -> ParserT s st m sep
1058 -> ParserT s st m sep
@@ -1015,20 +1101,19 @@ widthsFromIndices numColumns' indices =
1015-- (which may be grid), then the rows,
1101-- (which may be grid), then the rows,
1016-- which may be grid, separated by blank lines, and
1102-- which may be grid, separated by blank lines, and
1017-- ending with a footer (dashed line followed by blank line).
1103-- ending with a footer (dashed line followed by blank line).
1018−gridTableWith :: (Stream s m Char, HasReaderOptions st, HasLastStrPosition st,
1104+gridTableWith :: (Monad m, HasReaderOptions st, HasLastStrPosition st, Monad mf)
1019− Monad mf, IsString s)
1020− => ParserT s st m (mf Blocks) -- ^ Block list parser
1105+ => ParserT Sources st m (mf Blocks) -- ^ Block list parser
1021 -> Bool -- ^ Headerless table
1106 -> Bool -- ^ Headerless table
1022− -> ParserT s st m (mf Blocks)
1107+ -> ParserT Sources st m (mf Blocks)
1023gridTableWith blocks headless =
1108gridTableWith blocks headless =
1024 tableWith (gridTableHeader headless blocks) (gridTableRow blocks)
1109 tableWith (gridTableHeader headless blocks) (gridTableRow blocks)
1025 (gridTableSep '-') gridTableFooter
1110 (gridTableSep '-') gridTableFooter
1026
1111
1027−gridTableWith' :: (Stream s m Char, HasReaderOptions st, HasLastStrPosition st,
1112+gridTableWith' :: (Monad m, HasReaderOptions st, HasLastStrPosition st,
1028− Monad mf, IsString s)
1113+ Monad mf)
1029− => ParserT s st m (mf Blocks) -- ^ Block list parser
1114+ => ParserT Sources st m (mf Blocks) -- ^ Block list parser
1030 -> Bool -- ^ Headerless table
1115 -> Bool -- ^ Headerless table
1031− -> ParserT s st m (TableComponents mf)
1116+ -> ParserT Sources st m (TableComponents mf)
1032gridTableWith' blocks headless =
1117gridTableWith' blocks headless =
1033 tableWith' (gridTableHeader headless blocks) (gridTableRow blocks)
1118 tableWith' (gridTableHeader headless blocks) (gridTableRow blocks)
1034 (gridTableSep '-') gridTableFooter
1119 (gridTableSep '-') gridTableFooter
@@ -1037,7 +1122,7 @@ gridTableSplitLine :: [Int] -> Text -> [Text]
1037gridTableSplitLine indices line = map removeFinalBar $ tail $
1122gridTableSplitLine indices line = map removeFinalBar $ tail $
1038 splitTextByIndices (init indices) $ trimr line
1123 splitTextByIndices (init indices) $ trimr line
1039
1124
1040−gridPart :: Stream s m Char => Char -> ParserT s st m ((Int, Int), Alignment)
1125+gridPart :: Monad m => Char -> ParserT Sources st m ((Int, Int), Alignment)
1041gridPart ch = do
1126gridPart ch = do
1042 leftColon <- option False (True <$ char ':')
1127 leftColon <- option False (True <$ char ':')
1043 dashes <- many1 (char ch)
1128 dashes <- many1 (char ch)
@@ -1052,7 +1137,7 @@ gridPart ch = do
1052 (False, False) -> AlignDefault
1137 (False, False) -> AlignDefault
1053 return ((lengthDashes, lengthDashes + 1), alignment)
1138 return ((lengthDashes, lengthDashes + 1), alignment)
1054
1139
1055−gridDashedLines :: Stream s m Char => Char -> ParserT s st m [((Int, Int), Alignment)]
1140+gridDashedLines :: Monad m => Char -> ParserT Sources st m [((Int, Int), Alignment)]
1056gridDashedLines ch = try $ char '+' >> many1 (gridPart ch) <* blankline
1141gridDashedLines ch = try $ char '+' >> many1 (gridPart ch) <* blankline
1057
1142
1058removeFinalBar :: Text -> Text
1143removeFinalBar :: Text -> Text
@@ -1061,14 +1146,14 @@ removeFinalBar = T.dropWhileEnd go . T.dropWhileEnd (=='|')
1061 go c = T.any (== c) " \t"
1146 go c = T.any (== c) " \t"
1062
1147
1063-- | Separator between rows of grid table.
1148-- | Separator between rows of grid table.
1064−gridTableSep :: Stream s m Char => Char -> ParserT s st m Char
1149+gridTableSep :: Monad m => Char -> ParserT Sources st m Char
1065gridTableSep ch = try $ gridDashedLines ch >> return '\n'
1150gridTableSep ch = try $ gridDashedLines ch >> return '\n'
1066
1151
1067-- | Parse header for a grid table.
1152-- | Parse header for a grid table.
1068−gridTableHeader :: (Stream s m Char, Monad mf, IsString s, HasLastStrPosition st)
1153+gridTableHeader :: (Monad m, Monad mf, HasLastStrPosition st)
1069 => Bool -- ^ Headerless table
1154 => Bool -- ^ Headerless table
1070− -> ParserT s st m (mf Blocks)
1155+ -> ParserT Sources st m (mf Blocks)
1071− -> ParserT s st m (mf [Blocks], [Alignment], [Int])
1156+ -> ParserT Sources st m (mf [Blocks], [Alignment], [Int])
1072gridTableHeader True _ = do
1157gridTableHeader True _ = do
1073 optional blanklines
1158 optional blanklines
1074 dashes <- gridDashedLines '-'
1159 dashes <- gridDashedLines '-'
@@ -1091,17 +1176,17 @@ gridTableHeader False blocks = try $ do
1091 heads <- sequence <$> mapM (parseFromString' blocks . trim) rawHeads
1176 heads <- sequence <$> mapM (parseFromString' blocks . trim) rawHeads
1092 return (heads, aligns, indices)
1177 return (heads, aligns, indices)
1093
1178
1094−gridTableRawLine :: Stream s m Char => [Int] -> ParserT s st m [Text]
1179+gridTableRawLine :: (Stream s m Char, UpdateSourcePos s Char) => [Int] -> ParserT s st m [Text]
1095gridTableRawLine indices = do
1180gridTableRawLine indices = do
1096 char '|'
1181 char '|'
1097 line <- many1Till anyChar newline
1182 line <- many1Till anyChar newline
1098 return (gridTableSplitLine indices $ T.pack line)
1183 return (gridTableSplitLine indices $ T.pack line)
1099
1184
1100-- | Parse row of grid table.
1185-- | Parse row of grid table.
1101−gridTableRow :: (Stream s m Char, Monad mf, IsString s, HasLastStrPosition st)
1186+gridTableRow :: (Monad m, Monad mf, HasLastStrPosition st)
1102− => ParserT s st m (mf Blocks)
1187+ => ParserT Sources st m (mf Blocks)
1103 -> [Int]
1188 -> [Int]
1104− -> ParserT s st m (mf [Blocks])
1189+ -> ParserT Sources st m (mf [Blocks])
1105gridTableRow blocks indices = do
1190gridTableRow blocks indices = do
1106 colLines <- many1 (gridTableRawLine indices)
1191 colLines <- many1 (gridTableRawLine indices)
1107 let cols = map ((<> "\n") . T.unlines . removeOneLeadingSpace) $
1192 let cols = map ((<> "\n") . T.unlines . removeOneLeadingSpace) $
@@ -1122,34 +1207,38 @@ removeOneLeadingSpace xs =
1122 Just (c, _) -> c == ' '
1207 Just (c, _) -> c == ' '
1123
1208
1124-- | Parse footer for a grid table.
1209-- | Parse footer for a grid table.
1125−gridTableFooter :: Stream s m Char => ParserT s st m ()
1210+gridTableFooter :: (Stream s m Char, UpdateSourcePos s Char) => ParserT s st m ()
1126gridTableFooter = optional blanklines
1211gridTableFooter = optional blanklines
1127
1212
1128---
1213---
1129
1214
1130-- | Removes the ParsecT layer from the monad transformer stack
1215-- | Removes the ParsecT layer from the monad transformer stack
1131−readWithM :: (Stream s m Char, ToText s)
1216+readWithM :: (Monad m, ToSources t)
1132− => ParserT s st m a -- ^ parser
1217+ => ParserT Sources st m a -- ^ parser
1133− -> st -- ^ initial state
1218+ -> st -- ^ initial state
1134− -> s -- ^ input
1219+ -> t -- ^ input
1135 -> m (Either PandocError a)
1220 -> m (Either PandocError a)
1136readWithM parser state input =
1221readWithM parser state input =
1137− mapLeft (PandocParsecError $ toText input) `liftM` runParserT parser state "source" input
1222+ mapLeft (PandocParsecError sources)
1223+ <$> runParserT parser state (initialSourceName sources) sources
1224+ where
1225+ sources = toSources input
1138
1226
1139-- | Parse a string with a given parser and state
1227-- | Parse a string with a given parser and state
1228+readWith :: ToSources t
1140−readWith :: Parser Text st a
1229+ => Parser Sources st a
1141 -> st
1230 -> st
1142− -> Text
1231+ -> t
1143 -> Either PandocError a
1232 -> Either PandocError a
1144readWith p t inp = runIdentity $ readWithM p t inp
1233readWith p t inp = runIdentity $ readWithM p t inp
1145
1234
1146-- | Parse a string with @parser@ (for testing).
1235-- | Parse a string with @parser@ (for testing).
1147testStringWith :: Show a
1236testStringWith :: Show a
1148− => ParserT Text ParserState Identity a
1237+ => ParserT Sources ParserState Identity a
1149 -> Text
1238 -> Text
1150 -> IO ()
1239 -> IO ()
1151−testStringWith parser str = UTF8.putStrLn $ show $
1240+testStringWith parser str = UTF8.putStrLn $ tshow $
1152− readWith parser defaultParserState str
1241+ readWith parser defaultParserState (toSources str)
1153
1242
1154-- | Parsing options.
1243-- | Parsing options.
1155data ParserState = ParserState
1244data ParserState = ParserState
@@ -1169,7 +1258,7 @@ data ParserState = ParserState
1169 stateInNote :: Bool, -- ^ True if parsing note contents
1258 stateInNote :: Bool, -- ^ True if parsing note contents
1170 stateNoteNumber :: Int, -- ^ Last note number for citations
1259 stateNoteNumber :: Int, -- ^ Last note number for citations
1171 stateMeta :: Meta, -- ^ Document metadata
1260 stateMeta :: Meta, -- ^ Document metadata
1172− stateMeta' :: F Meta, -- ^ Document metadata
1261+ stateMeta' :: Future ParserState Meta, -- ^ Document metadata
1173 stateCitations :: M.Map Text Text, -- ^ RST-style citations
1262 stateCitations :: M.Map Text Text, -- ^ RST-style citations
1174 stateHeaderTable :: [HeaderType], -- ^ Ordered list of header types used
1263 stateHeaderTable :: [HeaderType], -- ^ Ordered list of header types used
1175 stateIdentifiers :: Set.Set Text, -- ^ Header identifiers used
1264 stateIdentifiers :: Set.Set Text, -- ^ Header identifiers used
@@ -1348,7 +1437,7 @@ data QuoteContext
1348
1437
1349type NoteTable = [(Text, Text)]
1438type NoteTable = [(Text, Text)]
1350
1439
1351−type NoteTable' = M.Map Text (SourcePos, F Blocks)
1440+type NoteTable' = M.Map Text (SourcePos, Future ParserState Blocks)
1352-- used in markdown reader
1441-- used in markdown reader
1353
1442
1354newtype Key = Key Text deriving (Show, Read, Eq, Ord)
1443newtype Key = Key Text deriving (Show, Read, Eq, Ord)
@@ -1383,7 +1472,7 @@ registerHeader (ident,classes,kvs) header' = do
1383 then do
1472 then do
1384 let id' = uniqueIdent exts (B.toList header') ids
1473 let id' = uniqueIdent exts (B.toList header') ids
1385 let id'' = if Ext_ascii_identifiers `extensionEnabled` exts
1474 let id'' = if Ext_ascii_identifiers `extensionEnabled` exts
1386− then T.pack $ mapMaybe toAsciiChar $ T.unpack id'
1475+ then toAsciiText id'
1387 else id'
1476 else id'
1388 updateState $ updateIdentifierList $ Set.insert id'
1477 updateState $ updateIdentifierList $ Set.insert id'
1389 updateState $ updateIdentifierList $ Set.insert id''
1478 updateState $ updateIdentifierList $ Set.insert id''
@@ -1396,34 +1485,42 @@ registerHeader (ident,classes,kvs) header' = do
1396 updateState $ updateIdentifierList $ Set.insert ident
1485 updateState $ updateIdentifierList $ Set.insert ident
1397 return (ident,classes,kvs)
1486 return (ident,classes,kvs)
1398
1487
1399−smartPunctuation :: (HasReaderOptions st, HasLastStrPosition st, HasQuoteContext st m, Stream s m Char)
1488+smartPunctuation :: (HasReaderOptions st, HasLastStrPosition st,
1489+ HasQuoteContext st m,
1490+ Stream s m Char, UpdateSourcePos s Char)
1400 => ParserT s st m Inlines
1491 => ParserT s st m Inlines
1401 -> ParserT s st m Inlines
1492 -> ParserT s st m Inlines
1402smartPunctuation inlineParser = do
1493smartPunctuation inlineParser = do
1403 guardEnabled Ext_smart
1494 guardEnabled Ext_smart
1404− choice [ quoted inlineParser, apostrophe, dash, ellipses ]
1495+ choice [ quoted inlineParser, apostrophe, doubleCloseQuote, dash, ellipses ]
1405
1496
1406−apostrophe :: Stream s m Char => ParserT s st m Inlines
1407−apostrophe = (char '\'' <|> char '\8217') >> return (B.str "\x2019")
1408−
1409−quoted :: (HasLastStrPosition st, HasQuoteContext st m, Stream s m Char)
1497+quoted :: (HasLastStrPosition st, HasQuoteContext st m,
1498+ Stream s m Char, UpdateSourcePos s Char)
1410 => ParserT s st m Inlines
1499 => ParserT s st m Inlines
1411 -> ParserT s st m Inlines
1500 -> ParserT s st m Inlines
1412quoted inlineParser = doubleQuoted inlineParser <|> singleQuoted inlineParser
1501quoted inlineParser = doubleQuoted inlineParser <|> singleQuoted inlineParser
1413
1502
1414−singleQuoted :: (HasLastStrPosition st, HasQuoteContext st m, Stream s m Char)
1503+singleQuoted :: (HasLastStrPosition st, HasQuoteContext st m,
1504+ Stream s m Char, UpdateSourcePos s Char)
1415 => ParserT s st m Inlines
1505 => ParserT s st m Inlines
1416 -> ParserT s st m Inlines
1506 -> ParserT s st m Inlines
1417−singleQuoted inlineParser = try $ B.singleQuoted . mconcat
1507+singleQuoted inlineParser = do
1418− <$ singleQuoteStart
1508+ singleQuoteStart
1509+ (B.singleQuoted . mconcat <$>
1510+ try
1419− <*> withQuoteContext InSingleQuote (many1Till inlineParser singleQuoteEnd)
1511+ (withQuoteContext InSingleQuote (many1Till inlineParser singleQuoteEnd)))
1420−
1512+ <|> pure "\8217"
1513+
1421−doubleQuoted :: (HasQuoteContext st m, Stream s m Char)
1514+doubleQuoted :: (HasQuoteContext st m, HasLastStrPosition st,
1515+ Stream s m Char, UpdateSourcePos s Char)
1422 => ParserT s st m Inlines
1516 => ParserT s st m Inlines
1423 -> ParserT s st m Inlines
1517 -> ParserT s st m Inlines
1424−doubleQuoted inlineParser = try $ B.doubleQuoted . mconcat
1518+doubleQuoted inlineParser = do
1425− <$ doubleQuoteStart
1519+ doubleQuoteStart
1520+ (B.doubleQuoted . mconcat <$>
1521+ try
1426− <*> withQuoteContext InDoubleQuote (manyTill inlineParser doubleQuoteEnd)
1522+ (withQuoteContext InDoubleQuote (manyTill inlineParser doubleQuoteEnd)))
1523+ <|> pure (B.str "\8220")
1427
1524
1428failIfInQuoteContext :: (HasQuoteContext st m, Stream s m t)
1525failIfInQuoteContext :: (HasQuoteContext st m, Stream s m t)
1429 => QuoteContext
1526 => QuoteContext
@@ -1432,13 +1529,14 @@ failIfInQuoteContext context = do
1432 context' <- getQuoteContext
1529 context' <- getQuoteContext
1433 when (context' == context) $ Prelude.fail "already inside quotes"
1530 when (context' == context) $ Prelude.fail "already inside quotes"
1434
1531
1435−charOrRef :: Stream s m Char => [Char] -> ParserT s st m Char
1532+charOrRef :: (Stream s m Char, UpdateSourcePos s Char) => [Char] -> ParserT s st m Char
1436charOrRef cs =
1533charOrRef cs =
1437 oneOf cs <|> try (do c <- characterReference
1534 oneOf cs <|> try (do c <- characterReference
1438 guard (c `elem` cs)
1535 guard (c `elem` cs)
1439 return c)
1536 return c)
1440
1537
1441−singleQuoteStart :: (HasLastStrPosition st, HasQuoteContext st m, Stream s m Char)
1538+singleQuoteStart :: (HasLastStrPosition st, HasQuoteContext st m,
1539+ Stream s m Char, UpdateSourcePos s Char)
1442 => ParserT s st m ()
1540 => ParserT s st m ()
1443singleQuoteStart = do
1541singleQuoteStart = do
1444 failIfInQuoteContext InSingleQuote
1542 failIfInQuoteContext InSingleQuote
@@ -1446,30 +1544,39 @@ singleQuoteStart = do
1446 guard =<< notAfterString
1544 guard =<< notAfterString
1447 try $ do
1545 try $ do
1448 charOrRef "'\8216\145"
1546 charOrRef "'\8216\145"
1449− notFollowedBy (oneOf [' ', '\t', '\n'])
1547+ void $ lookAhead (satisfy (not . isSpaceChar))
1450
1548
1451−singleQuoteEnd :: Stream s m Char
1549+singleQuoteEnd :: (Stream s m Char, UpdateSourcePos s Char)
1452 => ParserT s st m ()
1550 => ParserT s st m ()
1453singleQuoteEnd = try $ do
1551singleQuoteEnd = try $ do
1454 charOrRef "'\8217\146"
1552 charOrRef "'\8217\146"
1455 notFollowedBy alphaNum
1553 notFollowedBy alphaNum
1456
1554
1555+doubleQuoteStart :: (HasLastStrPosition st,
1457−doubleQuoteStart :: (HasQuoteContext st m, Stream s m Char)
1556+ HasQuoteContext st m,
1557+ Stream s m Char, UpdateSourcePos s Char)
1458 => ParserT s st m ()
1558 => ParserT s st m ()
1459doubleQuoteStart = do
1559doubleQuoteStart = do
1460 failIfInQuoteContext InDoubleQuote
1560 failIfInQuoteContext InDoubleQuote
1561+ guard =<< notAfterString
1461 try $ do charOrRef "\"\8220\147"
1562 try $ do charOrRef "\"\8220\147"
1462− notFollowedBy (oneOf [' ', '\t', '\n'])
1563+ void $ lookAhead (satisfy (not . isSpaceChar))
1463
1564
1464−doubleQuoteEnd :: Stream s m Char
1565+doubleQuoteEnd :: (Stream s m Char, UpdateSourcePos s Char)
1465 => ParserT s st m ()
1566 => ParserT s st m ()
1466doubleQuoteEnd = void (charOrRef "\"\8221\148")
1567doubleQuoteEnd = void (charOrRef "\"\8221\148")
1467
1568
1569+apostrophe :: (Stream s m Char, UpdateSourcePos s Char) => ParserT s st m Inlines
1570+apostrophe = (char '\'' <|> char '\8217') >> return (B.str "\8217")
1571+
1572+doubleCloseQuote :: (Stream s m Char, UpdateSourcePos s Char) => ParserT s st m Inlines
1573+doubleCloseQuote = B.str "\8221" <$ char '"'
1574+
1468−ellipses :: Stream s m Char
1575+ellipses :: (Stream s m Char, UpdateSourcePos s Char)
1469 => ParserT s st m Inlines
1576 => ParserT s st m Inlines
1470ellipses = try (string "..." >> return (B.str "\8230"))
1577ellipses = try (string "..." >> return (B.str "\8230"))
1471
1578
1472−dash :: (HasReaderOptions st, Stream s m Char)
1579+dash :: (HasReaderOptions st, Stream s m Char, UpdateSourcePos s Char)
1473 => ParserT s st m Inlines
1580 => ParserT s st m Inlines
1474dash = try $ do
1581dash = try $ do
1475 oldDashes <- extensionEnabled Ext_old_dashes <$> getOption readerExtensions
1582 oldDashes <- extensionEnabled Ext_old_dashes <$> getOption readerExtensions
@@ -1496,20 +1603,28 @@ nested p = do
1496 updateState $ \st -> st{ stateMaxNestingLevel = nestlevel }
1603 updateState $ \st -> st{ stateMaxNestingLevel = nestlevel }
1497 return res
1604 return res
1498
1605
1499−citeKey :: (Stream s m Char, HasLastStrPosition st)
1606+citeKey :: (Stream s m Char, UpdateSourcePos s Char, HasLastStrPosition st)
1607+ => Bool -- ^ If True, allow expanded @{..} syntax.
1500− => ParserT s st m (Bool, Text)
1608+ -> ParserT s st m (Bool, Text)
1501−citeKey = try $ do
1609+citeKey allowBraced = try $ do
1502 guard =<< notAfterString
1610 guard =<< notAfterString
1503 suppress_author <- option False (True <$ char '-')
1611 suppress_author <- option False (True <$ char '-')
1504 char '@'
1612 char '@'
1613+ key <- simpleCiteIdentifier
1614+ <|> if allowBraced
1615+ then charsInBalanced '{' '}' (satisfy (not . isSpace))
1616+ else mzero
1617+ return (suppress_author, key)
1618+
1619+simpleCiteIdentifier :: (Stream s m Char, UpdateSourcePos s Char)
1620+ => ParserT s st m Text
1621+simpleCiteIdentifier = do
1505 firstChar <- alphaNum <|> char '_' <|> char '*' -- @* for wildcard in nocite
1622 firstChar <- alphaNum <|> char '_' <|> char '*' -- @* for wildcard in nocite
1506 let regchar = satisfy (\c -> isAlphaNum c || c == '_')
1623 let regchar = satisfy (\c -> isAlphaNum c || c == '_')
1507 let internal p = try $ p <* lookAhead regchar
1624 let internal p = try $ p <* lookAhead regchar
1508 rest <- many $ regchar <|> internal (oneOf ":.#$%&-+?<>~/") <|>
1625 rest <- many $ regchar <|> internal (oneOf ":.#$%&-+?<>~/") <|>
1509 try (oneOf ":/" <* lookAhead (char '/'))
1626 try (oneOf ":/" <* lookAhead (char '/'))
1510− let key = firstChar:rest
1511− return (suppress_author, T.pack key)
1627+ return $ T.pack $ firstChar:rest
1512−
1513
1628
1514token :: (Stream s m t)
1629token :: (Stream s m t)
1515 => (t -> Text)
1630 => (t -> Text)
@@ -1529,12 +1644,15 @@ extractIdClass (ident, cls, kvs) = (ident', cls', kvs')
1529 cls' = maybe cls T.words $ lookup "class" kvs
1644 cls' = maybe cls T.words $ lookup "class" kvs
1530 kvs' = filter (\(k,_) -> k /= "id" || k /= "class") kvs
1645 kvs' = filter (\(k,_) -> k /= "id" || k /= "class") kvs
1531
1646
1532−insertIncludedFile' :: (PandocMonad m, HasIncludeFiles st)
1647+insertIncludedFile :: (PandocMonad m, HasIncludeFiles st)
1533− => ParserT a st m (mf Blocks)
1648+ => ParserT a st m b -- ^ parser to apply
1534− -> (Text -> a)
1649+ -> (Text -> a) -- ^ convert Text to stream type
1535− -> [FilePath] -> FilePath
1650+ -> [FilePath] -- ^ search path (directories)
1651+ -> FilePath -- ^ path of file to include
1652+ -> Maybe Int -- ^ start line (negative counts from end)
1653+ -> Maybe Int -- ^ end line (negative counts from end)
1536− -> ParserT a st m (mf Blocks)
1654+ -> ParserT a st m b
1537−insertIncludedFile' blocks totoks dirs f = do
1655+insertIncludedFile parser toStream dirs f mbstartline mbendline = do
1538 oldPos <- getPosition
1656 oldPos <- getPosition
1539 oldInput <- getInput
1657 oldInput <- getInput
1540 containers <- getIncludeFiles <$> getState
1658 containers <- getIncludeFiles <$> getState
@@ -1543,32 +1661,32 @@ insertIncludedFile' blocks totoks dirs f = do
1543 updateState $ addIncludeFile $ T.pack f
1661 updateState $ addIncludeFile $ T.pack f
1544 mbcontents <- readFileFromDirs dirs f
1662 mbcontents <- readFileFromDirs dirs f
1545 contents <- case mbcontents of
1663 contents <- case mbcontents of
1546− Just s -> return s
1664+ Just s -> return $ exciseLines mbstartline mbendline s
1547 Nothing -> do
1665 Nothing -> do
1548 report $ CouldNotLoadIncludeFile (T.pack f) oldPos
1666 report $ CouldNotLoadIncludeFile (T.pack f) oldPos
1549 return ""
1667 return ""
1668+ setInput $ toStream contents
1550− setPosition $ newPos f 1 1
1669+ setPosition $ newPos f (fromMaybe 1 mbstartline) 1
1551− setInput $ totoks contents
1670+ result <- parser
1552− bs <- blocks
1553 setInput oldInput
1671 setInput oldInput
1554 setPosition oldPos
1672 setPosition oldPos
1555 updateState dropLatestIncludeFile
1673 updateState dropLatestIncludeFile
1556− return bs
1674+ return result
1675+
1676+exciseLines :: Maybe Int -> Maybe Int -> Text -> Text
1677+exciseLines Nothing Nothing t = t
1678+exciseLines mbstartline mbendline t =
1679+ T.unlines $ take (endline' - (startline' - 1))
1680+ $ drop (startline' - 1) contentLines
1681+ where
1682+ contentLines = T.lines t
1683+ numLines = length contentLines
1684+ startline' = case mbstartline of
1685+ Nothing -> 1
1686+ Just x | x >= 0 -> x
1687+ | otherwise -> numLines + x -- negative from end
1688+ endline' = case mbendline of
1689+ Nothing -> numLines
1690+ Just x | x >= 0 -> x
1691+ | otherwise -> numLines + x -- negative from end
1557
1692
1558−-- | Parse content of include file as blocks. Circular includes result in an
1559−-- @PandocParseError@.
1560−insertIncludedFile :: (PandocMonad m, HasIncludeFiles st)
1561− => ParserT [a] st m Blocks
1562− -> (Text -> [a])
1563− -> [FilePath] -> FilePath
1564− -> ParserT [a] st m Blocks
1565−insertIncludedFile blocks totoks dirs f =
1566− runIdentity <$> insertIncludedFile' (Identity <$> blocks) totoks dirs f
1567−
1568−-- | Parse content of include file as future blocks. Circular includes result in
1569−-- an @PandocParseError@.
1570−insertIncludedFileF :: (PandocMonad m, HasIncludeFiles st)
1571− => ParserT Text st m (Future st Blocks)
1572− -> [FilePath] -> FilePath
1573− -> ParserT Text st m (Future st Blocks)
1574−insertIncludedFileF p = insertIncludedFile' p id
src/Text/Pandoc/Readers.hs+55−48
@@ -1,4 +1,5 @@
1{-# LANGUAGE MonoLocalBinds #-}
1{-# LANGUAGE MonoLocalBinds #-}
2+{-# LANGUAGE RankNTypes #-}
2{-# LANGUAGE ScopedTypeVariables #-}
3{-# LANGUAGE ScopedTypeVariables #-}
3{-# LANGUAGE OverloadedStrings #-}
4{-# LANGUAGE OverloadedStrings #-}
4{- |
5{- |
@@ -65,12 +66,14 @@ import Data.Aeson
65import qualified Data.ByteString.Lazy as BL
66import qualified Data.ByteString.Lazy as BL
66import Data.Text (Text)
67import Data.Text (Text)
67import qualified Data.Text as T
68import qualified Data.Text as T
69+import Text.Pandoc.Shared (tshow)
68import Text.Pandoc.Class
70import Text.Pandoc.Class
69import Text.Pandoc.Definition
71import Text.Pandoc.Definition
70import Text.Pandoc.Error
72import Text.Pandoc.Error
71import Text.Pandoc.Extensions
73import Text.Pandoc.Extensions
72import Text.Pandoc.Options
74import Text.Pandoc.Options
73import Text.Pandoc.Readers.CommonMark
75import Text.Pandoc.Readers.CommonMark
76+import Text.Pandoc.Readers.Markdown
74import Text.Pandoc.Readers.Creole
77import Text.Pandoc.Readers.Creole
75import Text.Pandoc.Readers.DocBook
78import Text.Pandoc.Readers.DocBook
76import Text.Pandoc.Readers.Docx
79import Text.Pandoc.Readers.Docx
@@ -83,7 +86,6 @@ import Text.Pandoc.Readers.HTML (readHtml)
83import Text.Pandoc.Readers.JATS (readJATS)
86import Text.Pandoc.Readers.JATS (readJATS)
84import Text.Pandoc.Readers.Jira (readJira)
87import Text.Pandoc.Readers.Jira (readJira)
85import Text.Pandoc.Readers.LaTeX
88import Text.Pandoc.Readers.LaTeX
86−import Text.Pandoc.Readers.Markdown
87import Text.Pandoc.Readers.MediaWiki
89import Text.Pandoc.Readers.MediaWiki
88import Text.Pandoc.Readers.Muse
90import Text.Pandoc.Readers.Muse
89import Text.Pandoc.Readers.Native
91import Text.Pandoc.Readers.Native
@@ -101,59 +103,60 @@ import Text.Pandoc.Readers.CSV
101import Text.Pandoc.Readers.CslJson
103import Text.Pandoc.Readers.CslJson
102import Text.Pandoc.Readers.BibTeX
104import Text.Pandoc.Readers.BibTeX
103import qualified Text.Pandoc.UTF8 as UTF8
105import qualified Text.Pandoc.UTF8 as UTF8
104−import Text.Parsec.Error
106+import Text.Pandoc.Sources (ToSources(..), sourcesToText)
105
107
108+data Reader m = TextReader (forall a . ToSources a =>
106−data Reader m = TextReader (ReaderOptions -> Text -> m Pandoc)
109+ ReaderOptions -> a -> m Pandoc)
107 | ByteStringReader (ReaderOptions -> BL.ByteString -> m Pandoc)
110 | ByteStringReader (ReaderOptions -> BL.ByteString -> m Pandoc)
108
111
109-- | Association list of formats and readers.
112-- | Association list of formats and readers.
110readers :: PandocMonad m => [(Text, Reader m)]
113readers :: PandocMonad m => [(Text, Reader m)]
111−readers = [ ("native" , TextReader readNative)
114+readers = [("native" , TextReader readNative)
112− ,("json" , TextReader readJSON)
115+ ,("json" , TextReader readJSON)
113− ,("markdown" , TextReader readMarkdown)
116+ ,("markdown" , TextReader readMarkdown)
114− ,("markdown_strict" , TextReader readMarkdown)
117+ ,("markdown_strict" , TextReader readMarkdown)
115− ,("markdown_phpextra" , TextReader readMarkdown)
118+ ,("markdown_phpextra" , TextReader readMarkdown)
116− ,("markdown_github" , TextReader readMarkdown)
119+ ,("markdown_github" , TextReader readMarkdown)
117− ,("markdown_mmd", TextReader readMarkdown)
120+ ,("markdown_mmd", TextReader readMarkdown)
118− ,("commonmark" , TextReader readCommonMark)
121+ ,("commonmark" , TextReader readCommonMark)
119− ,("commonmark_x" , TextReader readCommonMark)
122+ ,("commonmark_x" , TextReader readCommonMark)
120− ,("creole" , TextReader readCreole)
123+ ,("creole" , TextReader readCreole)
121− ,("dokuwiki" , TextReader readDokuWiki)
124+ ,("dokuwiki" , TextReader readDokuWiki)
122− ,("gfm" , TextReader readCommonMark)
125+ ,("gfm" , TextReader readCommonMark)
123− ,("rst" , TextReader readRST)
126+ ,("rst" , TextReader readRST)
124− ,("mediawiki" , TextReader readMediaWiki)
127+ ,("mediawiki" , TextReader readMediaWiki)
125− ,("vimwiki" , TextReader readVimwiki)
128+ ,("vimwiki" , TextReader readVimwiki)
126− ,("docbook" , TextReader readDocBook)
129+ ,("docbook" , TextReader readDocBook)
127− ,("opml" , TextReader readOPML)
130+ ,("opml" , TextReader readOPML)
128− ,("org" , TextReader readOrg)
131+ ,("org" , TextReader readOrg)
129− ,("textile" , TextReader readTextile) -- TODO : textile+lhs
132+ ,("textile" , TextReader readTextile) -- TODO : textile+lhs
130− ,("html" , TextReader readHtml)
133+ ,("html" , TextReader readHtml)
131− ,("jats" , TextReader readJATS)
134+ ,("jats" , TextReader readJATS)
132− ,("jira" , TextReader readJira)
135+ ,("jira" , TextReader readJira)
133− ,("latex" , TextReader readLaTeX)
136+ ,("latex" , TextReader readLaTeX)
134− ,("haddock" , TextReader readHaddock)
137+ ,("haddock" , TextReader readHaddock)
135− ,("twiki" , TextReader readTWiki)
138+ ,("twiki" , TextReader readTWiki)
136− ,("tikiwiki" , TextReader readTikiWiki)
139+ ,("tikiwiki" , TextReader readTikiWiki)
137− ,("docx" , ByteStringReader readDocx)
140+ ,("docx" , ByteStringReader readDocx)
138− ,("odt" , ByteStringReader readOdt)
141+ ,("odt" , ByteStringReader readOdt)
139− ,("t2t" , TextReader readTxt2Tags)
142+ ,("t2t" , TextReader readTxt2Tags)
140− ,("epub" , ByteStringReader readEPUB)
143+ ,("epub" , ByteStringReader readEPUB)
141− ,("muse" , TextReader readMuse)
144+ ,("muse" , TextReader readMuse)
142− ,("man" , TextReader readMan)
145+ ,("man" , TextReader readMan)
143− ,("fb2" , TextReader readFB2)
146+ ,("fb2" , TextReader readFB2)
144− ,("ipynb" , TextReader readIpynb)
147+ ,("ipynb" , TextReader readIpynb)
145− ,("csv" , TextReader readCSV)
148+ ,("csv" , TextReader readCSV)
146− ,("csljson" , TextReader readCslJson)
149+ ,("csljson" , TextReader readCslJson)
147− ,("bibtex" , TextReader readBibTeX)
150+ ,("bibtex" , TextReader readBibTeX)
148− ,("biblatex" , TextReader readBibLaTeX)
151+ ,("biblatex" , TextReader readBibLaTeX)
149 ]
152 ]
150
153
151-- | Retrieve reader, extensions based on formatSpec (format+extensions).
154-- | Retrieve reader, extensions based on formatSpec (format+extensions).
152getReader :: PandocMonad m => Text -> m (Reader m, Extensions)
155getReader :: PandocMonad m => Text -> m (Reader m, Extensions)
153getReader s =
156getReader s =
154 case parseFormatSpec s of
157 case parseFormatSpec s of
155− Left e -> throwError $ PandocAppError
158+ Left e -> throwError $ PandocAppError $
156− $ T.intercalate "\n" [T.pack m | Message m <- errorMessages e]
159+ "Error parsing reader format " <> tshow s <> ": " <> tshow e
157 Right (readerName, extsToEnable, extsToDisable) ->
160 Right (readerName, extsToEnable, extsToDisable) ->
158 case lookup readerName readers of
161 case lookup readerName readers of
159 Nothing -> throwError $ PandocUnknownReaderError
162 Nothing -> throwError $ PandocUnknownReaderError
@@ -173,9 +176,13 @@ getReader s =
173 return (r, exts)
176 return (r, exts)
174
177
175-- | Read pandoc document from JSON format.
178-- | Read pandoc document from JSON format.
176−readJSON :: PandocMonad m
179+readJSON :: (PandocMonad m, ToSources a)
177− => ReaderOptions -> Text -> m Pandoc
180+ => ReaderOptions
181+ -> a
182+ -> m Pandoc
178−readJSON _ t =
183+readJSON _ s =
179− case eitherDecode' . BL.fromStrict . UTF8.fromText $ t of
184+ case eitherDecode' . BL.fromStrict . UTF8.fromText
185+ . sourcesToText . toSources $ s of
180 Right doc -> return doc
186 Right doc -> return doc
181− Left e -> throwError $ PandocParseError ("JSON parse error: " <> T.pack e)
187+ Left e -> throwError $ PandocParseError ("JSON parse error: "
188+ <> T.pack e)
src/Text/Pandoc/Readers/BibTeX.hs+14−8
@@ -23,41 +23,47 @@ where
23import Text.Pandoc.Options
23import Text.Pandoc.Options
24import Text.Pandoc.Definition
24import Text.Pandoc.Definition
25import Text.Pandoc.Builder (setMeta, cite, str)
25import Text.Pandoc.Builder (setMeta, cite, str)
26−import Data.Text (Text)
27import Citeproc (Lang(..), parseLang)
26import Citeproc (Lang(..), parseLang)
28import Citeproc.Locale (getLocale)
27import Citeproc.Locale (getLocale)
29import Text.Pandoc.Error (PandocError(..))
28import Text.Pandoc.Error (PandocError(..))
30import Text.Pandoc.Class (PandocMonad, lookupEnv)
29import Text.Pandoc.Class (PandocMonad, lookupEnv)
31import Text.Pandoc.Citeproc.BibTeX as BibTeX
30import Text.Pandoc.Citeproc.BibTeX as BibTeX
32import Text.Pandoc.Citeproc.MetaValue (referenceToMetaValue)
31import Text.Pandoc.Citeproc.MetaValue (referenceToMetaValue)
32+import Text.Pandoc.Sources (ToSources(..))
33import Control.Monad.Except (throwError)
33import Control.Monad.Except (throwError)
34
34
35-- | Read BibTeX from an input string and return a Pandoc document.
35-- | Read BibTeX from an input string and return a Pandoc document.
36-- The document will have only metadata, with an empty body.
36-- The document will have only metadata, with an empty body.
37-- The metadata will contain a `references` field with the
37-- The metadata will contain a `references` field with the
38-- bibliography entries, and a `nocite` field with the wildcard `[@*]`.
38-- bibliography entries, and a `nocite` field with the wildcard `[@*]`.
39+readBibTeX :: (PandocMonad m, ToSources a)
39−readBibTeX :: PandocMonad m => ReaderOptions -> Text -> m Pandoc
40+ => ReaderOptions -> a -> m Pandoc
40readBibTeX = readBibTeX' BibTeX.Bibtex
41readBibTeX = readBibTeX' BibTeX.Bibtex
41
42
42-- | Read BibLaTeX from an input string and return a Pandoc document.
43-- | Read BibLaTeX from an input string and return a Pandoc document.
43-- The document will have only metadata, with an empty body.
44-- The document will have only metadata, with an empty body.
44-- The metadata will contain a `references` field with the
45-- The metadata will contain a `references` field with the
45-- bibliography entries, and a `nocite` field with the wildcard `[@*]`.
46-- bibliography entries, and a `nocite` field with the wildcard `[@*]`.
46−readBibLaTeX :: PandocMonad m => ReaderOptions -> Text -> m Pandoc
47+readBibLaTeX :: (PandocMonad m, ToSources a)
48+ => ReaderOptions -> a -> m Pandoc
47readBibLaTeX = readBibTeX' BibTeX.Biblatex
49readBibLaTeX = readBibTeX' BibTeX.Biblatex
48
50
51+readBibTeX' :: (PandocMonad m, ToSources a)
49−readBibTeX' :: PandocMonad m => Variant -> ReaderOptions -> Text -> m Pandoc
52+ => Variant -> ReaderOptions -> a -> m Pandoc
50readBibTeX' variant _opts t = do
53readBibTeX' variant _opts t = do
51− lang <- maybe (Lang "en" (Just "US")) parseLang
54+ mblangEnv <- lookupEnv "LANG"
52− <$> lookupEnv "LANG"
55+ let defaultLang = Lang "en" Nothing (Just "US") [] [] []
56+ let lang = case mblangEnv of
57+ Nothing -> defaultLang
58+ Just l -> either (const defaultLang) id $ parseLang l
53 locale <- case getLocale lang of
59 locale <- case getLocale lang of
54 Left e ->
60 Left e ->
55− case getLocale (Lang "en" (Just "US")) of
61+ case getLocale (Lang "en" Nothing (Just "US") [] [] []) of
56 Right l -> return l
62 Right l -> return l
57 Left _ -> throwError $ PandocCiteprocError e
63 Left _ -> throwError $ PandocCiteprocError e
58 Right l -> return l
64 Right l -> return l
59 case BibTeX.readBibtexString variant locale (const True) t of
65 case BibTeX.readBibtexString variant locale (const True) t of
60− Left e -> throwError $ PandocParsecError t e
66+ Left e -> throwError $ PandocParsecError (toSources t) e
61 Right refs -> return $ setMeta "references"
67 Right refs -> return $ setMeta "references"
62 (map referenceToMetaValue refs)
68 (map referenceToMetaValue refs)
63 . setMeta "nocite"
69 . setMeta "nocite"
src/Text/Pandoc/Readers/CSV.hs+7−7
@@ -13,23 +13,23 @@
13Conversion from CSV to a 'Pandoc' table.
13Conversion from CSV to a 'Pandoc' table.
14-}
14-}
15module Text.Pandoc.Readers.CSV ( readCSV ) where
15module Text.Pandoc.Readers.CSV ( readCSV ) where
16−import Data.Text (Text)
17import qualified Data.Text as T
16import qualified Data.Text as T
18import Text.Pandoc.CSV (parseCSV, defaultCSVOptions)
17import Text.Pandoc.CSV (parseCSV, defaultCSVOptions)
19import Text.Pandoc.Definition
18import Text.Pandoc.Definition
20import qualified Text.Pandoc.Builder as B
19import qualified Text.Pandoc.Builder as B
21import Text.Pandoc.Class (PandocMonad)
20import Text.Pandoc.Class (PandocMonad)
22−import Text.Pandoc.Shared (crFilter)
23import Text.Pandoc.Error
21import Text.Pandoc.Error
22+import Text.Pandoc.Sources (ToSources(..), sourcesToText)
24import Text.Pandoc.Options (ReaderOptions)
23import Text.Pandoc.Options (ReaderOptions)
25import Control.Monad.Except (throwError)
24import Control.Monad.Except (throwError)
26
25
27−readCSV :: PandocMonad m
26+readCSV :: (PandocMonad m, ToSources a)
28 => ReaderOptions -- ^ Reader options
27 => ReaderOptions -- ^ Reader options
29− -> Text -- ^ Text to parse (assuming @'\n'@ line endings)
28+ -> a
30 -> m Pandoc
29 -> m Pandoc
31−readCSV _opts s =
30+readCSV _opts s = do
31+ let txt = sourcesToText $ toSources s
32− case parseCSV defaultCSVOptions (crFilter s) of
32+ case parseCSV defaultCSVOptions txt of
33 Right (r:rs) -> return $ B.doc $ B.table capt
33 Right (r:rs) -> return $ B.doc $ B.table capt
34 (zip aligns widths)
34 (zip aligns widths)
35 (TableHead nullAttr hdrs)
35 (TableHead nullAttr hdrs)
@@ -45,4 +45,4 @@ readCSV _opts s =
45 aligns = replicate numcols AlignDefault
45 aligns = replicate numcols AlignDefault
46 widths = replicate numcols ColWidthDefault
46 widths = replicate numcols ColWidthDefault
47 Right [] -> return $ B.doc mempty
47 Right [] -> return $ B.doc mempty
48− Left e -> throwError $ PandocParsecError s e
48+ Left e -> throwError $ PandocParsecError (toSources [("",txt)]) e
src/Text/Pandoc/Readers/CommonMark.hs+46−4
@@ -26,19 +26,59 @@ import Text.Pandoc.Definition
26import Text.Pandoc.Builder as B
26import Text.Pandoc.Builder as B
27import Text.Pandoc.Options
27import Text.Pandoc.Options
28import Text.Pandoc.Error
28import Text.Pandoc.Error
29+import Text.Pandoc.Readers.Metadata (yamlMetaBlock)
29import Control.Monad.Except
30import Control.Monad.Except
30import Data.Functor.Identity (runIdentity)
31import Data.Functor.Identity (runIdentity)
31import Data.Typeable
32import Data.Typeable
33+import Text.Pandoc.Parsing (runParserT, getInput,
34+ runF, defaultParserState, option, many1, anyChar,
35+ Sources(..), ToSources(..), ParserT, Future,
36+ sourceName)
37+import qualified Data.Text as T
32
38
33-- | Parse a CommonMark formatted string into a 'Pandoc' structure.
39-- | Parse a CommonMark formatted string into a 'Pandoc' structure.
34−readCommonMark :: PandocMonad m => ReaderOptions -> Text -> m Pandoc
40+readCommonMark :: (PandocMonad m, ToSources a)
41+ => ReaderOptions -> a -> m Pandoc
35readCommonMark opts s
42readCommonMark opts s
43+ | isEnabled Ext_yaml_metadata_block opts = do
44+ let sources = toSources s
45+ let toks = concatMap sourceToToks (unSources sources)
46+ res <- runParserT (do meta <- yamlMetaBlock (metaValueParser opts)
47+ rest <- getInput
48+ return (meta, rest))
49+ defaultParserState "YAML metadata" (toSources s)
50+ case res of
51+ Left _ -> readCommonMarkBody opts sources toks
52+ Right (meta, rest) -> do
53+ -- strip off metadata section and parse body
54+ let body = concatMap sourceToToks (unSources rest)
55+ Pandoc _ bs <- readCommonMarkBody opts sources body
56+ return $ Pandoc (runF meta defaultParserState) bs
57+ | otherwise = do
58+ let sources = toSources s
59+ let toks = concatMap sourceToToks (unSources sources)
60+ readCommonMarkBody opts sources toks
61+
62+sourceToToks :: (SourcePos, Text) -> [Tok]
63+sourceToToks (pos, s) = tokenize (sourceName pos) s
64+
65+metaValueParser :: Monad m
66+ => ReaderOptions -> ParserT Sources st m (Future st MetaValue)
67+metaValueParser opts = do
68+ inp <- option "" $ T.pack <$> many1 anyChar
69+ let toks = concatMap sourceToToks (unSources (toSources inp))
70+ case runIdentity (parseCommonmarkWith (specFor opts) toks) of
71+ Left _ -> mzero
72+ Right (Cm bls :: Cm () Blocks) -> return $ return $ B.toMetaValue bls
73+
74+readCommonMarkBody :: PandocMonad m => ReaderOptions -> Sources -> [Tok] -> m Pandoc
75+readCommonMarkBody opts s toks
36 | isEnabled Ext_sourcepos opts =
76 | isEnabled Ext_sourcepos opts =
37− case runIdentity (commonmarkWith (specFor opts) "" s) of
77+ case runIdentity (parseCommonmarkWith (specFor opts) toks) of
38 Left err -> throwError $ PandocParsecError s err
78 Left err -> throwError $ PandocParsecError s err
39 Right (Cm bls :: Cm SourceRange Blocks) -> return $ B.doc bls
79 Right (Cm bls :: Cm SourceRange Blocks) -> return $ B.doc bls
40 | otherwise =
80 | otherwise =
41− case runIdentity (commonmarkWith (specFor opts) "" s) of
81+ case runIdentity (parseCommonmarkWith (specFor opts) toks) of
42 Left err -> throwError $ PandocParsecError s err
82 Left err -> throwError $ PandocParsecError s err
43 Right (Cm bls :: Cm () Blocks) -> return $ B.doc bls
83 Right (Cm bls :: Cm () Blocks) -> return $ B.doc bls
44
84
@@ -72,5 +112,7 @@ specFor opts = foldr ($) defaultSyntaxSpec exts
72 | isEnabled Ext_implicit_header_references opts ] ++
112 | isEnabled Ext_implicit_header_references opts ] ++
73 [ (footnoteSpec <>) | isEnabled Ext_footnotes opts ] ++
113 [ (footnoteSpec <>) | isEnabled Ext_footnotes opts ] ++
74 [ (definitionListSpec <>) | isEnabled Ext_definition_lists opts ] ++
114 [ (definitionListSpec <>) | isEnabled Ext_definition_lists opts ] ++
75− [ (taskListSpec <>) | isEnabled Ext_task_lists opts ]
115+ [ (taskListSpec <>) | isEnabled Ext_task_lists opts ] ++
116+ [ (rebaseRelativePathsSpec <>)
117+ | isEnabled Ext_rebase_relative_paths opts ]
76
118
src/Text/Pandoc/Readers/Creole.hs+5−6
@@ -23,21 +23,20 @@ import Text.Pandoc.Class.PandocMonad (PandocMonad (..))
23import Text.Pandoc.Definition
23import Text.Pandoc.Definition
24import Text.Pandoc.Options
24import Text.Pandoc.Options
25import Text.Pandoc.Parsing hiding (enclosed)
25import Text.Pandoc.Parsing hiding (enclosed)
26−import Text.Pandoc.Shared (crFilter)
27−
28
26
29-- | Read creole from an input string and return a Pandoc document.
27-- | Read creole from an input string and return a Pandoc document.
30−readCreole :: PandocMonad m
28+readCreole :: (PandocMonad m, ToSources a)
31 => ReaderOptions
29 => ReaderOptions
32− -> Text
30+ -> a
33 -> m Pandoc
31 -> m Pandoc
34readCreole opts s = do
32readCreole opts s = do
33+ let sources = ensureFinalNewlines 2 (toSources s)
35− res <- readWithM parseCreole def{ stateOptions = opts } $ crFilter s <> "\n\n"
34+ res <- readWithM parseCreole def{ stateOptions = opts } sources
36 case res of
35 case res of
37 Left e -> throwError e
36 Left e -> throwError e
38 Right d -> return d
37 Right d -> return d
39
38
40−type CRLParser = ParserT Text ParserState
39+type CRLParser = ParserT Sources ParserState
41
40
42--
41--
43-- Utility functions
42-- Utility functions
src/Text/Pandoc/Readers/CslJson.hs+5−4
@@ -24,21 +24,22 @@ import Text.Pandoc.Options
24import Text.Pandoc.Definition
24import Text.Pandoc.Definition
25import Text.Pandoc.Builder (setMeta, cite, str)
25import Text.Pandoc.Builder (setMeta, cite, str)
26import qualified Text.Pandoc.UTF8 as UTF8
26import qualified Text.Pandoc.UTF8 as UTF8
27−import Data.Text (Text)
28import qualified Data.Text as T
27import qualified Data.Text as T
29import Text.Pandoc.Error (PandocError(..))
28import Text.Pandoc.Error (PandocError(..))
30import Text.Pandoc.Class (PandocMonad)
29import Text.Pandoc.Class (PandocMonad)
31import Text.Pandoc.Citeproc.CslJson (cslJsonToReferences)
30import Text.Pandoc.Citeproc.CslJson (cslJsonToReferences)
32import Text.Pandoc.Citeproc.MetaValue (referenceToMetaValue)
31import Text.Pandoc.Citeproc.MetaValue (referenceToMetaValue)
33import Control.Monad.Except (throwError)
32import Control.Monad.Except (throwError)
33+import Text.Pandoc.Sources (ToSources(..), sourcesToText)
34
34
35-- | Read CSL JSON from an input string and return a Pandoc document.
35-- | Read CSL JSON from an input string and return a Pandoc document.
36-- The document will have only metadata, with an empty body.
36-- The document will have only metadata, with an empty body.
37-- The metadata will contain a `references` field with the
37-- The metadata will contain a `references` field with the
38-- bibliography entries, and a `nocite` field with the wildcard `[@*]`.
38-- bibliography entries, and a `nocite` field with the wildcard `[@*]`.
39+readCslJson :: (PandocMonad m, ToSources a)
39−readCslJson :: PandocMonad m => ReaderOptions -> Text -> m Pandoc
40+ => ReaderOptions -> a -> m Pandoc
40−readCslJson _opts t =
41+readCslJson _opts x =
41− case cslJsonToReferences (UTF8.fromText t) of
42+ case cslJsonToReferences (UTF8.fromText $ sourcesToText $ toSources x) of
42 Left e -> throwError $ PandocParseError $ T.pack e
43 Left e -> throwError $ PandocParseError $ T.pack e
43 Right refs -> return $ setMeta "references"
44 Right refs -> return $ setMeta "references"
44 (map referenceToMetaValue refs)
45 (map referenceToMetaValue refs)
src/Text/Pandoc/Readers/DocBook.hs+94−81
@@ -12,23 +12,28 @@ Conversion of DocBook XML to 'Pandoc' document.
12-}
12-}
13module Text.Pandoc.Readers.DocBook ( readDocBook ) where
13module Text.Pandoc.Readers.DocBook ( readDocBook ) where
14import Control.Monad.State.Strict
14import Control.Monad.State.Strict
15−import Data.Char (isSpace, toUpper)
15+import Data.Char (isSpace, isLetter)
16import Data.Default
16import Data.Default
17import Data.Either (rights)
17import Data.Either (rights)
18import Data.Foldable (asum)
18import Data.Foldable (asum)
19import Data.Generics
19import Data.Generics
20import Data.List (intersperse,elemIndex)
20import Data.List (intersperse,elemIndex)
21+import Data.List.NonEmpty (nonEmpty)
21import Data.Maybe (fromMaybe,mapMaybe)
22import Data.Maybe (fromMaybe,mapMaybe)
22import Data.Text (Text)
23import Data.Text (Text)
23import qualified Data.Text as T
24import qualified Data.Text as T
25+import qualified Data.Text.Lazy as TL
26+import Control.Monad.Except (throwError)
24import Text.HTML.TagSoup.Entity (lookupEntity)
27import Text.HTML.TagSoup.Entity (lookupEntity)
28+import Text.Pandoc.Error (PandocError(..))
25import Text.Pandoc.Builder
29import Text.Pandoc.Builder
26import Text.Pandoc.Class.PandocMonad (PandocMonad, report)
30import Text.Pandoc.Class.PandocMonad (PandocMonad, report)
27import Text.Pandoc.Options
31import Text.Pandoc.Options
28import Text.Pandoc.Logging (LogMessage(..))
32import Text.Pandoc.Logging (LogMessage(..))
29−import Text.Pandoc.Shared (crFilter, safeRead, extractSpaces)
33+import Text.Pandoc.Shared (safeRead, extractSpaces)
34+import Text.Pandoc.Sources (ToSources(..), sourcesToText)
30import Text.TeXMath (readMathML, writeTeX)
35import Text.TeXMath (readMathML, writeTeX)
31−import Text.XML.Light
36+import Text.Pandoc.XML.Light
32
37
33{-
38{-
34
39
@@ -129,6 +134,7 @@ List of all DocBook tags, with [x] indicating implemented,
129[ ] corpcredit - A corporation or organization credited in a document
134[ ] corpcredit - A corporation or organization credited in a document
130[ ] corpname - The name of a corporation
135[ ] corpname - The name of a corporation
131[ ] country - The name of a country
136[ ] country - The name of a country
137+[x] danger - An admonition set off from the text indicating hazardous situation
132[ ] database - The name of a database, or part of a database
138[ ] database - The name of a database, or part of a database
133[x] date - The date of publication or revision of a document
139[x] date - The date of publication or revision of a document
134[ ] dedication - A wrapper for the dedication section of a book
140[ ] dedication - A wrapper for the dedication section of a book
@@ -206,7 +212,7 @@ List of all DocBook tags, with [x] indicating implemented,
206[x] info - A wrapper for information about a component or other block. (DocBook v5)
212[x] info - A wrapper for information about a component or other block. (DocBook v5)
207[x] informalequation - A displayed mathematical equation without a title
213[x] informalequation - A displayed mathematical equation without a title
208[x] informalexample - A displayed example without a title
214[x] informalexample - A displayed example without a title
209−[ ] informalfigure - A untitled figure
215+[x] informalfigure - An untitled figure
210[ ] informaltable - A table without a title
216[ ] informaltable - A table without a title
211[ ] initializer - The initializer for a FieldSynopsis
217[ ] initializer - The initializer for a FieldSynopsis
212[x] inlineequation - A mathematical equation or expression occurring inline
218[x] inlineequation - A mathematical equation or expression occurring inline
@@ -535,24 +541,32 @@ instance Default DBState where
535 , dbContent = [] }
541 , dbContent = [] }
536
542
537
543
538−readDocBook :: PandocMonad m => ReaderOptions -> Text -> m Pandoc
544+readDocBook :: (PandocMonad m, ToSources a)
545+ => ReaderOptions
546+ -> a
547+ -> m Pandoc
539readDocBook _ inp = do
548readDocBook _ inp = do
540− let tree = normalizeTree . parseXML . handleInstructions $ crFilter inp
549+ let sources = toSources inp
550+ tree <- either (throwError . PandocXMLError "") return $
551+ parseXMLContents
552+ (TL.fromStrict . handleInstructions . sourcesToText $ sources)
541 (bs, st') <- flip runStateT (def{ dbContent = tree }) $ mapM parseBlock tree
553 (bs, st') <- flip runStateT (def{ dbContent = tree }) $ mapM parseBlock tree
542 return $ Pandoc (dbMeta st') (toList . mconcat $ bs)
554 return $ Pandoc (dbMeta st') (toList . mconcat $ bs)
543
555
544−-- We treat <?asciidoc-br?> specially (issue #1236), converting it
556+-- We treat certain processing instructions by converting them to tags
545−-- to <br/>, since xml-light doesn't parse the instruction correctly.
557+-- beginning "pi-".
546−-- Other xml instructions are simply removed from the input stream.
547handleInstructions :: Text -> Text
558handleInstructions :: Text -> Text
548−handleInstructions = T.pack . handleInstructions' . T.unpack
559+handleInstructions t =
549−
560+ let (x,y) = T.breakOn "<?" t
550−handleInstructions' :: String -> String
561+ in if T.null y
551−handleInstructions' ('<':'?':'a':'s':'c':'i':'i':'d':'o':'c':'-':'b':'r':'?':'>':xs) = '<':'b':'r':'/':'>': handleInstructions' xs
562+ then x
552−handleInstructions' xs = case break (=='<') xs of
563+ else
553− (ys, []) -> ys
564+ let (w,z) = T.breakOn "?>" y
554− ([], '<':zs) -> '<' : handleInstructions' zs
565+ in (if T.takeWhile (\c -> isLetter c || c == '-')
566+ (T.drop 2 w) `elem` ["asciidoc-br", "dbfo"]
567+ then x <> "<pi-" <> T.drop 2 w <> "/>"
568+ else x <> w <> T.take 2 z) <>
555− (ys, zs) -> ys ++ handleInstructions' zs
569+ handleInstructions (T.drop 2 z)
556
570
557getFigure :: PandocMonad m => Element -> DB m Blocks
571getFigure :: PandocMonad m => Element -> DB m Blocks
558getFigure e = do
572getFigure e = do
@@ -565,32 +579,14 @@ getFigure e = do
565 modify $ \st -> st{ dbFigureTitle = mempty, dbFigureId = mempty }
579 modify $ \st -> st{ dbFigureTitle = mempty, dbFigureId = mempty }
566 return res
580 return res
567
581
568−-- normalize input, consolidating adjacent Text and CRef elements
569−normalizeTree :: [Content] -> [Content]
570−normalizeTree = everywhere (mkT go)
571− where go :: [Content] -> [Content]
572− go (Text (CData CDataRaw _ _):xs) = xs
573− go (Text (CData CDataText s1 z):Text (CData CDataText s2 _):xs) =
574− Text (CData CDataText (s1 ++ s2) z):xs
575− go (Text (CData CDataText s1 z):CRef r:xs) =
576− Text (CData CDataText (s1 ++ convertEntity r) z):xs
577− go (CRef r:Text (CData CDataText s1 z):xs) =
578− Text (CData CDataText (convertEntity r ++ s1) z):xs
579− go (CRef r1:CRef r2:xs) =
580− Text (CData CDataText (convertEntity r1 ++ convertEntity r2) Nothing):xs
581− go xs = xs
582−
583−convertEntity :: String -> String
584−convertEntity e = Data.Maybe.fromMaybe (map toUpper e) (lookupEntity e)
585−
586-- convenience function to get an attribute value, defaulting to ""
582-- convenience function to get an attribute value, defaulting to ""
587−attrValue :: String -> Element -> Text
583+attrValue :: Text -> Element -> Text
588attrValue attr elt =
584attrValue attr elt =
589− maybe "" T.pack (lookupAttrBy (\x -> qName x == attr) (elAttribs elt))
585+ fromMaybe "" (lookupAttrBy (\x -> qName x == attr) (elAttribs elt))
590
586
591-- convenience function
587-- convenience function
592named :: Text -> Element -> Bool
588named :: Text -> Element -> Bool
593−named s e = qName (elName e) == T.unpack s
589+named s e = qName (elName e) == s
594
590
595--
591--
596
592
@@ -605,16 +601,24 @@ addMetadataFromElement e = do
605 Nothing -> return ()
601 Nothing -> return ()
606 Just z -> addMetaField "author" z
602 Just z -> addMetaField "author" z
607 addMetaField "subtitle" e
603 addMetaField "subtitle" e
608− addMetaField "author" e
604+ addAuthor e
609 addMetaField "date" e
605 addMetaField "date" e
610 addMetaField "release" e
606 addMetaField "release" e
611 addMetaField "releaseinfo" e
607 addMetaField "releaseinfo" e
612 return mempty
608 return mempty
609+ where
610+ addAuthor elt =
611+ case filterChildren (named "author") elt of
612+ [] -> return ()
613+ [z] -> fromAuthor z >>= addMeta "author"
614+ zs -> mapM fromAuthor zs >>= addMeta "author"
615+ fromAuthor elt =
616+ mconcat . intersperse space <$> mapM getInlines (elChildren elt)
613− where addMetaField fieldname elt =
617+ addMetaField fieldname elt =
614− case filterChildren (named fieldname) elt of
618+ case filterChildren (named fieldname) elt of
615− [] -> return ()
619+ [] -> return ()
616− [z] -> getInlines z >>= addMeta fieldname
620+ [z] -> getInlines z >>= addMeta fieldname
617− zs -> mapM getInlines zs >>= addMeta fieldname
621+ zs -> mapM getInlines zs >>= addMeta fieldname
618
622
619addMeta :: PandocMonad m => ToMetaValue a => Text -> a -> DB m ()
623addMeta :: PandocMonad m => ToMetaValue a => Text -> a -> DB m ()
620addMeta field val = modify (setMeta field val)
624addMeta field val = modify (setMeta field val)
@@ -627,7 +631,7 @@ isBlockElement :: Content -> Bool
627isBlockElement (Elem e) = qName (elName e) `elem` blockTags
631isBlockElement (Elem e) = qName (elName e) `elem` blockTags
628isBlockElement _ = False
632isBlockElement _ = False
629
633
630−blockTags :: [String]
634+blockTags :: [Text]
631blockTags =
635blockTags =
632 [ "abstract"
636 [ "abstract"
633 , "ackno"
637 , "ackno"
@@ -669,6 +673,7 @@ blockTags =
669 , "index"
673 , "index"
670 , "info"
674 , "info"
671 , "informalexample"
675 , "informalexample"
676+ , "informalfigure"
672 , "informaltable"
677 , "informaltable"
673 , "itemizedlist"
678 , "itemizedlist"
674 , "linegroup"
679 , "linegroup"
@@ -713,8 +718,8 @@ blockTags =
713 , "variablelist"
718 , "variablelist"
714 ] ++ admonitionTags
719 ] ++ admonitionTags
715
720
716−admonitionTags :: [String]
721+admonitionTags :: [Text]
717−admonitionTags = ["important","caution","note","tip","warning"]
722+admonitionTags = ["caution","danger","important","note","tip","warning"]
718
723
719-- Trim leading and trailing newline characters
724-- Trim leading and trailing newline characters
720trimNl :: Text -> Text
725trimNl :: Text -> Text
@@ -771,10 +776,10 @@ getBlocks e = mconcat <$>
771
776
772parseBlock :: PandocMonad m => Content -> DB m Blocks
777parseBlock :: PandocMonad m => Content -> DB m Blocks
773parseBlock (Text (CData CDataRaw _ _)) = return mempty -- DOCTYPE
778parseBlock (Text (CData CDataRaw _ _)) = return mempty -- DOCTYPE
774−parseBlock (Text (CData _ s _)) = if all isSpace s
779+parseBlock (Text (CData _ s _)) = if T.all isSpace s
775 then return mempty
780 then return mempty
776− else return $ plain $ trimInlines $ text $ T.pack s
781+ else return $ plain $ trimInlines $ text s
777−parseBlock (CRef x) = return $ plain $ str $ T.toUpper $ T.pack x
782+parseBlock (CRef x) = return $ plain $ str $ T.toUpper x
778parseBlock (Elem e) =
783parseBlock (Elem e) =
779 case qName (elName e) of
784 case qName (elName e) of
780 "toc" -> skip -- skip TOC, since in pandoc it's autogenerated
785 "toc" -> skip -- skip TOC, since in pandoc it's autogenerated
@@ -829,7 +834,7 @@ parseBlock (Elem e) =
829 "refsect2" -> sect 2
834 "refsect2" -> sect 2
830 "refsect3" -> sect 3
835 "refsect3" -> sect 3
831 "refsection" -> gets dbSectionLevel >>= sect . (+1)
836 "refsection" -> gets dbSectionLevel >>= sect . (+1)
832− l | l `elem` admonitionTags -> parseAdmonition $ T.pack l
837+ l | l `elem` admonitionTags -> parseAdmonition l
833 "area" -> skip
838 "area" -> skip
834 "areaset" -> skip
839 "areaset" -> skip
835 "areaspec" -> skip
840 "areaspec" -> skip
@@ -855,6 +860,7 @@ parseBlock (Elem e) =
855 "variablelist" -> definitionList <$> deflistitems
860 "variablelist" -> definitionList <$> deflistitems
856 "procedure" -> bulletList <$> steps
861 "procedure" -> bulletList <$> steps
857 "figure" -> getFigure e
862 "figure" -> getFigure e
863+ "informalfigure" -> getFigure e
858 "mediaobject" -> para <$> getMediaobject e
864 "mediaobject" -> para <$> getMediaobject e
859 "caption" -> skip
865 "caption" -> skip
860 "info" -> addMetadataFromElement e
866 "info" -> addMetadataFromElement e
@@ -890,7 +896,11 @@ parseBlock (Elem e) =
890 "subtitle" -> return mempty -- handled in parent element
896 "subtitle" -> return mempty -- handled in parent element
891 _ -> skip >> getBlocks e
897 _ -> skip >> getBlocks e
892 where skip = do
898 where skip = do
899+ let qn = qName $ elName e
900+ let name = if "pi-" `T.isPrefixOf` qn
901+ then "<?" <> qn <> "?>"
902+ else qn
893− lift $ report $ IgnoredElement $ T.pack $ qName (elName e)
903+ lift $ report $ IgnoredElement name
894 return mempty
904 return mempty
895
905
896 codeBlockWithLang = do
906 codeBlockWithLang = do
@@ -898,7 +908,7 @@ parseBlock (Elem e) =
898 "" -> []
908 "" -> []
899 x -> [x]
909 x -> [x]
900 return $ codeBlockWith (attrValue "id" e, classes', [])
910 return $ codeBlockWith (attrValue "id" e, classes', [])
901− $ trimNl $ T.pack $ strContentRecursive e
911+ $ trimNl $ strContentRecursive e
902 parseBlockquote = do
912 parseBlockquote = do
903 attrib <- case filterChild (named "attribution") e of
913 attrib <- case filterChild (named "attribution") e of
904 Nothing -> return mempty
914 Nothing -> return mempty
@@ -952,17 +962,16 @@ parseBlock (Elem e) =
952 w <- findAttr (unqual "colwidth") c
962 w <- findAttr (unqual "colwidth") c
953 n <- safeRead $ "0" <> T.filter (\x ->
963 n <- safeRead $ "0" <> T.filter (\x ->
954 (x >= '0' && x <= '9')
964 (x >= '0' && x <= '9')
955− || x == '.') (T.pack w)
965+ || x == '.') w
956 if n > 0 then Just n else Nothing
966 if n > 0 then Just n else Nothing
957− let numrows = case bodyrows of
967+ let numrows = maybe 0 maximum $ nonEmpty
958− [] -> 0
959− xs -> maximum $ map length xs
968+ $ map length bodyrows
960 let aligns = case colspecs of
969 let aligns = case colspecs of
961 [] -> replicate numrows AlignDefault
970 [] -> replicate numrows AlignDefault
962 cs -> map toAlignment cs
971 cs -> map toAlignment cs
963 let parseWidth s = safeRead (T.filter (\x -> (x >= '0' && x <= '9')
972 let parseWidth s = safeRead (T.filter (\x -> (x >= '0' && x <= '9')
964 || x == '.') s)
973 || x == '.') s)
965− let textWidth = case filterChild (named "?dbfo") e of
974+ let textWidth = case filterChild (named "pi-dbfo") e of
966 Just d -> case attrValue "table-width" d of
975 Just d -> case attrValue "table-width" d of
967 "" -> 1.0
976 "" -> 1.0
968 w -> fromMaybe 100.0 (parseWidth w) / 100.0
977 w -> fromMaybe 100.0 (parseWidth w) / 100.0
@@ -1035,12 +1044,12 @@ parseMixed container conts = do
1035 x <- parseMixed container rs
1044 x <- parseMixed container rs
1036 return $ p <> b <> x
1045 return $ p <> b <> x
1037
1046
1038−parseRow :: PandocMonad m => [String] -> Element -> DB m [Cell]
1047+parseRow :: PandocMonad m => [Text] -> Element -> DB m [Cell]
1039parseRow cn = do
1048parseRow cn = do
1040 let isEntry x = named "entry" x || named "td" x || named "th" x
1049 let isEntry x = named "entry" x || named "td" x || named "th" x
1041 mapM (parseEntry cn) . filterChildren isEntry
1050 mapM (parseEntry cn) . filterChildren isEntry
1042
1051
1043−parseEntry :: PandocMonad m => [String] -> Element -> DB m Cell
1052+parseEntry :: PandocMonad m => [Text] -> Element -> DB m Cell
1044parseEntry cn el = do
1053parseEntry cn el = do
1045 let colDistance sa ea = do
1054 let colDistance sa ea = do
1046 let iStrt = elemIndex sa cn
1055 let iStrt = elemIndex sa cn
@@ -1062,7 +1071,7 @@ getInlines :: PandocMonad m => Element -> DB m Inlines
1062getInlines e' = trimInlines . mconcat <$>
1071getInlines e' = trimInlines . mconcat <$>
1063 mapM parseInline (elContent e')
1072 mapM parseInline (elContent e')
1064
1073
1065−strContentRecursive :: Element -> String
1074+strContentRecursive :: Element -> Text
1066strContentRecursive = strContent .
1075strContentRecursive = strContent .
1067 (\e' -> e'{ elContent = map elementToStr $ elContent e' })
1076 (\e' -> e'{ elContent = map elementToStr $ elContent e' })
1068
1077
@@ -1071,16 +1080,16 @@ elementToStr (Elem e') = Text $ CData CDataText (strContentRecursive e') Nothing
1071elementToStr x = x
1080elementToStr x = x
1072
1081
1073parseInline :: PandocMonad m => Content -> DB m Inlines
1082parseInline :: PandocMonad m => Content -> DB m Inlines
1074−parseInline (Text (CData _ s _)) = return $ text $ T.pack s
1083+parseInline (Text (CData _ s _)) = return $ text s
1075parseInline (CRef ref) =
1084parseInline (CRef ref) =
1076− return $ text $ maybe (T.toUpper $ T.pack ref) T.pack $ lookupEntity ref
1085+ return $ text $ maybe (T.toUpper ref) T.pack $ lookupEntity (T.unpack ref)
1077parseInline (Elem e) =
1086parseInline (Elem e) =
1078 case qName (elName e) of
1087 case qName (elName e) of
1079 "anchor" -> do
1088 "anchor" -> do
1080 return $ spanWith (attrValue "id" e, [], []) mempty
1089 return $ spanWith (attrValue "id" e, [], []) mempty
1081 "phrase" -> do
1090 "phrase" -> do
1082 let ident = attrValue "id" e
1091 let ident = attrValue "id" e
1083− let classes = T.words $ attrValue "class" e
1092+ let classes = T.words $ attrValue "role" e
1084 if ident /= "" || classes /= []
1093 if ident /= "" || classes /= []
1085 then innerInlines (spanWith (ident,classes,[]))
1094 then innerInlines (spanWith (ident,classes,[]))
1086 else innerInlines id
1095 else innerInlines id
@@ -1125,7 +1134,7 @@ parseInline (Elem e) =
1125 "userinput" -> codeWithLang
1134 "userinput" -> codeWithLang
1126 "systemitem" -> codeWithLang
1135 "systemitem" -> codeWithLang
1127 "varargs" -> return $ code "(...)"
1136 "varargs" -> return $ code "(...)"
1128− "keycap" -> return (str $ T.pack $ strContent e)
1137+ "keycap" -> return (str $ strContent e)
1129 "keycombo" -> keycombo <$>
1138 "keycombo" -> keycombo <$>
1130 mapM parseInline (elContent e)
1139 mapM parseInline (elContent e)
1131 "menuchoice" -> menuchoice <$>
1140 "menuchoice" -> menuchoice <$>
@@ -1137,17 +1146,17 @@ parseInline (Elem e) =
1137 let title = case attrValue "endterm" e of
1146 let title = case attrValue "endterm" e of
1138 "" -> maybe "???" xrefTitleByElem
1147 "" -> maybe "???" xrefTitleByElem
1139 (findElementById linkend content)
1148 (findElementById linkend content)
1140− endterm -> maybe "???" (T.pack . strContent)
1149+ endterm -> maybe "???" strContent
1141 (findElementById endterm content)
1150 (findElementById endterm content)
1142 return $ link ("#" <> linkend) "" (text title)
1151 return $ link ("#" <> linkend) "" (text title)
1143− "email" -> return $ link ("mailto:" <> T.pack (strContent e)) ""
1152+ "email" -> return $ link ("mailto:" <> strContent e) ""
1144− $ str $ T.pack $ strContent e
1153+ $ str $ strContent e
1145− "uri" -> return $ link (T.pack $ strContent e) "" $ str $ T.pack $ strContent e
1154+ "uri" -> return $ link (strContent e) "" $ str $ strContent e
1146 "ulink" -> innerInlines (link (attrValue "url" e) "")
1155 "ulink" -> innerInlines (link (attrValue "url" e) "")
1147 "link" -> do
1156 "link" -> do
1148 ils <- innerInlines id
1157 ils <- innerInlines id
1149 let href = case findAttr (QName "href" (Just "http://www.w3.org/1999/xlink") Nothing) e of
1158 let href = case findAttr (QName "href" (Just "http://www.w3.org/1999/xlink") Nothing) e of
1150− Just h -> T.pack h
1159+ Just h -> h
1151 _ -> "#" <> attrValue "linkend" e
1160 _ -> "#" <> attrValue "linkend" e
1152 let ils' = if ils == mempty then str href else ils
1161 let ils' = if ils == mempty then str href else ils
1153 let attr = (attrValue "id" e, T.words $ attrValue "role" e, [])
1162 let attr = (attrValue "id" e, T.words $ attrValue "role" e, [])
@@ -1163,12 +1172,15 @@ parseInline (Elem e) =
1163 "title" -> return mempty
1172 "title" -> return mempty
1164 "affiliation" -> skip
1173 "affiliation" -> skip
1165 -- Note: this isn't a real docbook tag; it's what we convert
1174 -- Note: this isn't a real docbook tag; it's what we convert
1166− -- <?asciidor-br?> to in handleInstructions, above. A kludge to
1175+ -- <?asciidor-br?> to in handleInstructions, above.
1167− -- work around xml-light's inability to parse an instruction.
1168− "br" -> return linebreak
1176+ "pi-asciidoc-br" -> return linebreak
1169 _ -> skip >> innerInlines id
1177 _ -> skip >> innerInlines id
1170 where skip = do
1178 where skip = do
1179+ let qn = qName $ elName e
1180+ let name = if "pi-" `T.isPrefixOf` qn
1181+ then "<?" <> qn <> "?>"
1182+ else qn
1171− lift $ report $ IgnoredElement $ T.pack $ qName (elName e)
1183+ lift $ report $ IgnoredElement name
1172 return mempty
1184 return mempty
1173
1185
1174 innerInlines f = extractSpaces f . mconcat <$>
1186 innerInlines f = extractSpaces f . mconcat <$>
@@ -1177,7 +1189,7 @@ parseInline (Elem e) =
1177 let classes' = case attrValue "language" e of
1189 let classes' = case attrValue "language" e of
1178 "" -> []
1190 "" -> []
1179 l -> [l]
1191 l -> [l]
1180− return $ codeWith (attrValue "id" e,classes',[]) $ T.pack $ strContentRecursive e
1192+ return $ codeWith (attrValue "id" e,classes',[]) $ strContentRecursive e
1181 simpleList = mconcat . intersperse (str "," <> space) <$> mapM getInlines
1193 simpleList = mconcat . intersperse (str "," <> space) <$> mapM getInlines
1182 (filterChildren (named "member") e)
1194 (filterChildren (named "member") e)
1183 segmentedList = do
1195 segmentedList = do
@@ -1218,10 +1230,10 @@ parseInline (Elem e) =
1218 "sect5" -> descendantContent "title" el
1230 "sect5" -> descendantContent "title" el
1219 "cmdsynopsis" -> descendantContent "command" el
1231 "cmdsynopsis" -> descendantContent "command" el
1220 "funcsynopsis" -> descendantContent "function" el
1232 "funcsynopsis" -> descendantContent "function" el
1221− _ -> T.pack $ qName (elName el) ++ "_title"
1233+ _ -> qName (elName el) <> "_title"
1222 where
1234 where
1223 xrefLabel = attrValue "xreflabel" el
1235 xrefLabel = attrValue "xreflabel" el
1224− descendantContent name = maybe "???" (T.pack . strContent)
1236+ descendantContent name = maybe "???" strContent
1225 . filterElementName (\n -> qName n == name)
1237 . filterElementName (\n -> qName n == name)
1226
1238
1227-- | Extract a math equation from an element
1239-- | Extract a math equation from an element
@@ -1241,8 +1253,9 @@ equation e constructor =
1241 where
1253 where
1242 mathMLEquations :: [Text]
1254 mathMLEquations :: [Text]
1243 mathMLEquations = map writeTeX $ rights $ readMath
1255 mathMLEquations = map writeTeX $ rights $ readMath
1244− (\x -> qName (elName x) == "math" && qPrefix (elName x) == Just "mml")
1256+ (\x -> qName (elName x) == "math" &&
1257+ qURI (elName x) == Just "http://www.w3.org/1998/Math/MathML")
1245− (readMathML . T.pack . showElement)
1258+ (readMathML . showElement)
1246
1259
1247 latexEquations :: [Text]
1260 latexEquations :: [Text]
1248 latexEquations = readMath (\x -> qName (elName x) == "mathphrase")
1261 latexEquations = readMath (\x -> qName (elName x) == "mathphrase")
@@ -1256,8 +1269,8 @@ equation e constructor =
1256-- | Get the actual text stored in a CData block. 'showContent'
1269-- | Get the actual text stored in a CData block. 'showContent'
1257-- returns the text still surrounded by the [[CDATA]] tags.
1270-- returns the text still surrounded by the [[CDATA]] tags.
1258showVerbatimCData :: Content -> Text
1271showVerbatimCData :: Content -> Text
1259−showVerbatimCData (Text (CData _ d _)) = T.pack d
1272+showVerbatimCData (Text (CData _ d _)) = d
1260−showVerbatimCData c = T.pack $ showContent c
1273+showVerbatimCData c = showContent c
1261
1274
1262
1275
1263-- | Set the prefix of a name to 'Nothing'
1276-- | Set the prefix of a name to 'Nothing'
src/Text/Pandoc/Readers/Docx.hs+59−42
@@ -61,13 +61,14 @@ module Text.Pandoc.Readers.Docx
61import Codec.Archive.Zip
61import Codec.Archive.Zip
62import Control.Monad.Reader
62import Control.Monad.Reader
63import Control.Monad.State.Strict
63import Control.Monad.State.Strict
64+import Data.Bifunctor (bimap, first)
64import qualified Data.ByteString.Lazy as B
65import qualified Data.ByteString.Lazy as B
65import Data.Default (Default)
66import Data.Default (Default)
66−import Data.List (delete, intersect)
67+import Data.List (delete, intersect, foldl')
67import Data.Char (isSpace)
68import Data.Char (isSpace)
68import qualified Data.Map as M
69import qualified Data.Map as M
69import qualified Data.Text as T
70import qualified Data.Text as T
70−import Data.Maybe (isJust, fromMaybe)
71+import Data.Maybe (catMaybes, isJust, fromMaybe)
71import Data.Sequence (ViewL (..), viewl)
72import Data.Sequence (ViewL (..), viewl)
72import qualified Data.Sequence as Seq
73import qualified Data.Sequence as Seq
73import qualified Data.Set as Set
74import qualified Data.Set as Set
@@ -85,6 +86,7 @@ import Text.Pandoc.Class.PandocMonad (PandocMonad)
85import qualified Text.Pandoc.Class.PandocMonad as P
86import qualified Text.Pandoc.Class.PandocMonad as P
86import Text.Pandoc.Error
87import Text.Pandoc.Error
87import Text.Pandoc.Logging
88import Text.Pandoc.Logging
89+import Data.List.NonEmpty (nonEmpty)
88
90
89readDocx :: PandocMonad m
91readDocx :: PandocMonad m
90 => ReaderOptions
92 => ReaderOptions
@@ -112,6 +114,7 @@ data DState = DState { docxAnchorMap :: M.Map T.Text T.Text
112 -- restarting
114 -- restarting
113 , docxListState :: M.Map (T.Text, T.Text) Integer
115 , docxListState :: M.Map (T.Text, T.Text) Integer
114 , docxPrevPara :: Inlines
116 , docxPrevPara :: Inlines
117+ , docxTableCaptions :: [Blocks]
115 }
118 }
116
119
117instance Default DState where
120instance Default DState where
@@ -122,6 +125,7 @@ instance Default DState where
122 , docxDropCap = mempty
125 , docxDropCap = mempty
123 , docxListState = M.empty
126 , docxListState = M.empty
124 , docxPrevPara = mempty
127 , docxPrevPara = mempty
128+ , docxTableCaptions = []
125 }
129 }
126
130
127data DEnv = DEnv { docxOptions :: ReaderOptions
131data DEnv = DEnv { docxOptions :: ReaderOptions
@@ -490,15 +494,32 @@ singleParaToPlain blks
490 singleton $ Plain ils
494 singleton $ Plain ils
491singleParaToPlain blks = blks
495singleParaToPlain blks = blks
492
496
493−cellToBlocks :: PandocMonad m => Docx.Cell -> DocxContext m Blocks
497+cellToCell :: PandocMonad m => RowSpan -> Docx.Cell -> DocxContext m Pandoc.Cell
494−cellToBlocks (Docx.Cell bps) = do
498+cellToCell rowSpan (Docx.Cell gridSpan _ bps) = do
495 blks <- smushBlocks <$> mapM bodyPartToBlocks bps
499 blks <- smushBlocks <$> mapM bodyPartToBlocks bps
496− return $ fromList $ blocksToDefinitions $ blocksToBullets $ toList blks
500+ let blks' = singleParaToPlain $ fromList $ blocksToDefinitions $ blocksToBullets $ toList blks
501+ return (cell AlignDefault rowSpan (ColSpan (fromIntegral gridSpan)) blks')
502+
503+rowsToRows :: PandocMonad m => [Docx.Row] -> DocxContext m [Pandoc.Row]
504+rowsToRows rows = do
505+ let rowspans = (fmap . fmap) (first RowSpan) (Docx.rowsToRowspans rows)
506+ cells <- traverse (traverse (uncurry cellToCell)) rowspans
507+ return (fmap (Pandoc.Row nullAttr) cells)
508+
509+splitHeaderRows :: Bool -> [Docx.Row] -> ([Docx.Row], [Docx.Row])
510+splitHeaderRows hasFirstRowFormatting rs = bimap reverse reverse $ fst
511+ $ if hasFirstRowFormatting
512+ then foldl' f ((take 1 rs, []), True) (drop 1 rs)
513+ else foldl' f (([], []), False) rs
514+ where
515+ f ((headerRows, bodyRows), previousRowWasHeader) r@(Docx.Row h cs)
516+ | h == HasTblHeader || (previousRowWasHeader && any isContinuationCell cs)
517+ = ((r : headerRows, bodyRows), True)
518+ | otherwise
519+ = ((headerRows, r : bodyRows), False)
520+
521+ isContinuationCell (Docx.Cell _ vm _) = vm == Docx.Continue
497
522
498−rowToBlocksList :: PandocMonad m => Docx.Row -> DocxContext m [Blocks]
499−rowToBlocksList (Docx.Row cells) = do
500− blksList <- mapM cellToBlocks cells
501− return $ map singleParaToPlain blksList
502
523
503-- like trimInlines, but also take out linebreaks
524-- like trimInlines, but also take out linebreaks
504trimSps :: Inlines -> Inlines
525trimSps :: Inlines -> Inlines
@@ -545,6 +566,11 @@ normalizeToClassName = T.map go . fromStyleName
545 where go c | isSpace c = '-'
566 where go c | isSpace c = '-'
546 | otherwise = c
567 | otherwise = c
547
568
569+bodyPartToTableCaption :: PandocMonad m => BodyPart -> DocxContext m (Maybe Blocks)
570+bodyPartToTableCaption (TblCaption pPr parparts) =
571+ Just <$> bodyPartToBlocks (Paragraph pPr parparts)
572+bodyPartToTableCaption _ = pure Nothing
573+
548bodyPartToBlocks :: PandocMonad m => BodyPart -> DocxContext m Blocks
574bodyPartToBlocks :: PandocMonad m => BodyPart -> DocxContext m Blocks
549bodyPartToBlocks (Paragraph pPr parparts)
575bodyPartToBlocks (Paragraph pPr parparts)
550 | Just True <- pBidi pPr = do
576 | Just True <- pBidi pPr = do
@@ -636,54 +662,43 @@ bodyPartToBlocks (ListItem pPr _ _ _ parparts) =
636 let pPr' = pPr {pStyle = constructBogusParStyleData "list-paragraph": pStyle pPr}
662 let pPr' = pPr {pStyle = constructBogusParStyleData "list-paragraph": pStyle pPr}
637 in
663 in
638 bodyPartToBlocks $ Paragraph pPr' parparts
664 bodyPartToBlocks $ Paragraph pPr' parparts
665+bodyPartToBlocks (TblCaption _ _) =
666+ return $ para mempty -- collected separately
639bodyPartToBlocks (Tbl _ _ _ []) =
667bodyPartToBlocks (Tbl _ _ _ []) =
640 return $ para mempty
668 return $ para mempty
641−bodyPartToBlocks (Tbl cap _ look parts@(r:rs)) = do
669+bodyPartToBlocks (Tbl cap grid look parts) = do
642− let cap' = simpleCaption $ plain $ text cap
670+ captions <- gets docxTableCaptions
643− (hdr, rows) = case firstRowFormatting look of
671+ fullCaption <- case captions of
644− True | null rs -> (Nothing, [r])
672+ c : cs -> do
645− | otherwise -> (Just r, rs)
673+ modify (\s -> s { docxTableCaptions = cs })
646− False -> (Nothing, r:rs)
674+ return c
647−
675+ [] -> return $ if T.null cap then mempty else plain (text cap)
648− cells <- mapM rowToBlocksList rows
676+ let shortCaption = if T.null cap then Nothing else Just (toList (text cap))
677+ cap' = caption shortCaption fullCaption
678+ (hdr, rows) = splitHeaderRows (firstRowFormatting look) parts
649
679
650 let width = maybe 0 maximum $ nonEmpty $ map rowLength parts
680 let width = maybe 0 maximum $ nonEmpty $ map rowLength parts
651− -- Data.List.NonEmpty is not available with ghc 7.10 so we roll out
652− -- our own, see
653− -- https://github.com/jgm/pandoc/pull/4361#issuecomment-365416155
654− nonEmpty [] = Nothing
655− nonEmpty l = Just l
656 rowLength :: Docx.Row -> Int
681 rowLength :: Docx.Row -> Int
657− rowLength (Docx.Row c) = length c
682+ rowLength (Docx.Row _ c) = sum (fmap (\(Docx.Cell gridSpan _ _) -> fromIntegral gridSpan) c)
658
683
659− let toRow = Pandoc.Row nullAttr . map simpleCell
684+ headerCells <- rowsToRows hdr
660− toHeaderRow l = [toRow l | not (null l)]
685+ bodyCells <- rowsToRows rows
661
686
662− -- pad cells. New Text.Pandoc.Builder will do that for us,
687+ -- Horizontal column alignment goes to the default at the moment. Getting
663− -- so this is for compatibility while we switch over.
664− let cells' = map (\row -> toRow $ take width (row ++ repeat mempty)) cells
665−
666− hdrCells <- case hdr of
667− Just r' -> toHeaderRow <$> rowToBlocksList r'
668− Nothing -> return []
669−
670− -- The two following variables (horizontal column alignment and
671− -- relative column widths) go to the default at the
672− -- moment. Width information is in the TblGrid field of the Tbl,
673− -- so should be possible. Alignment might be more difficult,
674− -- since there doesn't seem to be a column entity in docx.
688+ -- it might be difficult, since there doesn't seem to be a column entity
689+ -- in docx.
675 let alignments = replicate width AlignDefault
690 let alignments = replicate width AlignDefault
676− widths = replicate width ColWidthDefault
691+ totalWidth = sum grid
692+ widths = (\w -> ColWidth (fromInteger w / fromInteger totalWidth)) <$> grid
677
693
678 return $ table cap'
694 return $ table cap'
679 (zip alignments widths)
695 (zip alignments widths)
680− (TableHead nullAttr hdrCells)
696+ (TableHead nullAttr headerCells)
681− [TableBody nullAttr 0 [] cells']
697+ [TableBody nullAttr 0 [] bodyCells]
682 (TableFoot nullAttr [])
698 (TableFoot nullAttr [])
683bodyPartToBlocks (OMathPara e) =
699bodyPartToBlocks (OMathPara e) =
684 return $ para $ displayMath (writeTeX e)
700 return $ para $ displayMath (writeTeX e)
685
701
686−
687-- replace targets with generated anchors.
702-- replace targets with generated anchors.
688rewriteLink' :: PandocMonad m => Inline -> DocxContext m Inline
703rewriteLink' :: PandocMonad m => Inline -> DocxContext m Inline
689rewriteLink' l@(Link attr ils (T.uncons -> Just ('#',target), title)) = do
704rewriteLink' l@(Link attr ils (T.uncons -> Just ('#',target), title)) = do
@@ -719,6 +734,8 @@ bodyToOutput :: PandocMonad m => Body -> DocxContext m (Meta, [Block])
719bodyToOutput (Body bps) = do
734bodyToOutput (Body bps) = do
720 let (metabps, blkbps) = sepBodyParts bps
735 let (metabps, blkbps) = sepBodyParts bps
721 meta <- bodyPartsToMeta metabps
736 meta <- bodyPartsToMeta metabps
737+ captions <- catMaybes <$> mapM bodyPartToTableCaption blkbps
738+ modify (\s -> s { docxTableCaptions = captions })
722 blks <- smushBlocks <$> mapM bodyPartToBlocks blkbps
739 blks <- smushBlocks <$> mapM bodyPartToBlocks blkbps
723 blks' <- rewriteLinks $ blocksToDefinitions $ blocksToBullets $ toList blks
740 blks' <- rewriteLinks $ blocksToDefinitions $ blocksToBullets $ toList blks
724 blks'' <- removeOrphanAnchors blks'
741 blks'' <- removeOrphanAnchors blks'
src/Text/Pandoc/Readers/Docx/Combine.hs+5−5
@@ -61,7 +61,7 @@ import Data.List
61import Data.Bifunctor
61import Data.Bifunctor
62import Data.Sequence ( ViewL (..), ViewR (..), viewl, viewr, spanr, spanl
62import Data.Sequence ( ViewL (..), ViewR (..), viewl, viewr, spanr, spanl
63 , (><), (|>) )
63 , (><), (|>) )
64−import Text.Pandoc.Builder
64+import Text.Pandoc.Builder as B
65
65
66data Modifier a = Modifier (a -> a)
66data Modifier a = Modifier (a -> a)
67 | AttrModifier (Attr -> a -> a) Attr
67 | AttrModifier (Attr -> a -> a) Attr
@@ -116,12 +116,12 @@ ilModifierAndInnards ils = case viewl $ unMany ils of
116
116
117inlinesL :: Inlines -> (Inlines, Inlines)
117inlinesL :: Inlines -> (Inlines, Inlines)
118inlinesL ils = case viewl $ unMany ils of
118inlinesL ils = case viewl $ unMany ils of
119− (s :< sq) -> (singleton s, Many sq)
119+ (s :< sq) -> (B.singleton s, Many sq)
120 _ -> (mempty, ils)
120 _ -> (mempty, ils)
121
121
122inlinesR :: Inlines -> (Inlines, Inlines)
122inlinesR :: Inlines -> (Inlines, Inlines)
123inlinesR ils = case viewr $ unMany ils of
123inlinesR ils = case viewr $ unMany ils of
124− (sq :> s) -> (Many sq, singleton s)
124+ (sq :> s) -> (Many sq, B.singleton s)
125 _ -> (ils, mempty)
125 _ -> (ils, mempty)
126
126
127combineInlines :: Inlines -> Inlines -> Inlines
127combineInlines :: Inlines -> Inlines -> Inlines
@@ -182,7 +182,7 @@ isAttrModifier _ = False
182
182
183smushInlines :: [Inlines] -> Inlines
183smushInlines :: [Inlines] -> Inlines
184smushInlines xs = combineInlines xs' mempty
184smushInlines xs = combineInlines xs' mempty
185− where xs' = foldl combineInlines mempty xs
185+ where xs' = foldl' combineInlines mempty xs
186
186
187smushBlocks :: [Blocks] -> Blocks
187smushBlocks :: [Blocks] -> Blocks
188−smushBlocks xs = foldl combineBlocks mempty xs
188+smushBlocks xs = foldl' combineBlocks mempty xs
src/Text/Pandoc/Readers/Docx/Parse.hs+214−120
@@ -33,7 +33,9 @@ module Text.Pandoc.Readers.Docx.Parse ( Docx(..)
33 , ParStyle
33 , ParStyle
34 , CharStyle(cStyleData)
34 , CharStyle(cStyleData)
35 , Row(..)
35 , Row(..)
36+ , TblHeader(..)
36 , Cell(..)
37 , Cell(..)
38+ , VMerge(..)
37 , TrackedChange(..)
39 , TrackedChange(..)
38 , ChangeType(..)
40 , ChangeType(..)
39 , ChangeInfo(..)
41 , ChangeInfo(..)
@@ -50,6 +52,7 @@ module Text.Pandoc.Readers.Docx.Parse ( Docx(..)
50 , pHeading
52 , pHeading
51 , constructBogusParStyleData
53 , constructBogusParStyleData
52 , leftBiasedMergeRunStyle
54 , leftBiasedMergeRunStyle
55+ , rowsToRowspans
53 ) where
56 ) where
54import Text.Pandoc.Readers.Docx.Parse.Styles
57import Text.Pandoc.Readers.Docx.Parse.Styles
55import Codec.Archive.Zip
58import Codec.Archive.Zip
@@ -63,6 +66,7 @@ import Data.Char (chr, ord, readLitChar)
63import Data.List
66import Data.List
64import qualified Data.Map as M
67import qualified Data.Map as M
65import qualified Data.Text as T
68import qualified Data.Text as T
69+import Data.Text (Text)
66import Data.Maybe
70import Data.Maybe
67import System.FilePath
71import System.FilePath
68import Text.Pandoc.Readers.Docx.Util
72import Text.Pandoc.Readers.Docx.Util
@@ -72,8 +76,7 @@ import qualified Text.Pandoc.UTF8 as UTF8
72import Text.TeXMath (Exp)
76import Text.TeXMath (Exp)
73import Text.TeXMath.Readers.OMML (readOMML)
77import Text.TeXMath.Readers.OMML (readOMML)
74import Text.TeXMath.Unicode.Fonts (Font (..), getUnicode, textToFont)
78import Text.TeXMath.Unicode.Fonts (Font (..), getUnicode, textToFont)
75−import Text.XML.Light
79+import Text.Pandoc.XML.Light
76−import qualified Text.XML.Light.Cursor as XMLC
77
80
78data ReaderEnv = ReaderEnv { envNotes :: Notes
81data ReaderEnv = ReaderEnv { envNotes :: Notes
79 , envComments :: Comments
82 , envComments :: Comments
@@ -127,37 +130,23 @@ mapD f xs =
127 in
130 in
128 concatMapM handler xs
131 concatMapM handler xs
129
132
130−unwrap :: NameSpaces -> Content -> [Content]
133+unwrapElement :: NameSpaces -> Element -> [Element]
131−unwrap ns (Elem element)
134+unwrapElement ns element
132 | isElem ns "w" "sdt" element
135 | isElem ns "w" "sdt" element
133 , Just sdtContent <- findChildByName ns "w" "sdtContent" element
136 , Just sdtContent <- findChildByName ns "w" "sdtContent" element
134− = concatMap (unwrap ns . Elem) (elChildren sdtContent)
137+ = concatMap (unwrapElement ns) (elChildren sdtContent)
135 | isElem ns "w" "smartTag" element
138 | isElem ns "w" "smartTag" element
136− = concatMap (unwrap ns . Elem) (elChildren element)
139+ = concatMap (unwrapElement ns) (elChildren element)
137−unwrap _ content = [content]
140+ | otherwise
141+ = [element{ elContent = concatMap (unwrapContent ns) (elContent element) }]
138
142
139−unwrapChild :: NameSpaces -> Content -> Content
143+unwrapContent :: NameSpaces -> Content -> [Content]
140−unwrapChild ns (Elem element) =
144+unwrapContent ns (Elem element) = map Elem $ unwrapElement ns element
141− Elem $ element { elContent = concatMap (unwrap ns) (elContent element) }
142−unwrapChild _ content = content
145+unwrapContent _ content = [content]
143
146
144−walkDocument' :: NameSpaces -> XMLC.Cursor -> XMLC.Cursor
147+walkDocument :: NameSpaces -> Element -> Element
145−walkDocument' ns cur =
146− let modifiedCur = XMLC.modifyContent (unwrapChild ns) cur
147− in
148− case XMLC.nextDF modifiedCur of
149− Just cur' -> walkDocument' ns cur'
150− Nothing -> XMLC.root modifiedCur
151−
152−walkDocument :: NameSpaces -> Element -> Maybe Element
153walkDocument ns element =
148walkDocument ns element =
154− let cur = XMLC.fromContent (Elem element)
149+ element{ elContent = concatMap (unwrapContent ns) (elContent element) }
155− cur' = walkDocument' ns cur
156− in
157− case XMLC.toTree cur' of
158− Elem element' -> Just element'
159− _ -> Nothing
160−
161
150
162newtype Docx = Docx Document
151newtype Docx = Docx Document
163 deriving Show
152 deriving Show
@@ -239,6 +228,7 @@ defaultParagraphStyle = ParagraphStyle { pStyle = []
239data BodyPart = Paragraph ParagraphStyle [ParPart]
228data BodyPart = Paragraph ParagraphStyle [ParPart]
240 | ListItem ParagraphStyle T.Text T.Text (Maybe Level) [ParPart]
229 | ListItem ParagraphStyle T.Text T.Text (Maybe Level) [ParPart]
241 | Tbl T.Text TblGrid TblLook [Row]
230 | Tbl T.Text TblGrid TblLook [Row]
231+ | TblCaption ParagraphStyle [ParPart]
242 | OMathPara [Exp]
232 | OMathPara [Exp]
243 deriving Show
233 deriving Show
244
234
@@ -250,12 +240,61 @@ newtype TblLook = TblLook {firstRowFormatting::Bool}
250defaultTblLook :: TblLook
240defaultTblLook :: TblLook
251defaultTblLook = TblLook{firstRowFormatting = False}
241defaultTblLook = TblLook{firstRowFormatting = False}
252
242
253−newtype Row = Row [Cell]
254− deriving Show
243+data Row = Row TblHeader [Cell] deriving Show
255
244
245+data TblHeader = HasTblHeader | NoTblHeader deriving (Show, Eq)
246+
256−newtype Cell = Cell [BodyPart]
247+data Cell = Cell GridSpan VMerge [BodyPart]
257 deriving Show
248 deriving Show
258
249
250+type GridSpan = Integer
251+
252+data VMerge = Continue
253+ -- ^ This cell should be merged with the one above it
254+ | Restart
255+ -- ^ This cell should not be merged with the one above it
256+ deriving (Show, Eq)
257+
258+rowsToRowspans :: [Row] -> [[(Int, Cell)]]
259+rowsToRowspans rows = let
260+ removeMergedCells = fmap (filter (\(_, Cell _ vmerge _) -> vmerge == Restart))
261+ in removeMergedCells (foldr f [] rows)
262+ where
263+ f :: Row -> [[(Int, Cell)]] -> [[(Int, Cell)]]
264+ f (Row _ cells) acc = let
265+ spans = g cells Nothing (listToMaybe acc)
266+ in spans : acc
267+
268+ g ::
269+ -- | The current row
270+ [Cell] ->
271+ -- | Number of columns left below
272+ Maybe Integer ->
273+ -- | (rowspan so far, cell) for the row below this one
274+ Maybe [(Int, Cell)] ->
275+ -- | (rowspan so far, cell) for this row
276+ [(Int, Cell)]
277+ g cells _ Nothing = zip (repeat 1) cells
278+ g cells columnsLeftBelow (Just rowBelow) =
279+ case cells of
280+ [] -> []
281+ thisCell@(Cell thisGridSpan _ _) : restOfRow -> case rowBelow of
282+ [] -> zip (repeat 1) cells
283+ (spanSoFarBelow, Cell gridSpanBelow vmerge _) : _ ->
284+ let spanSoFar = case vmerge of
285+ Restart -> 1
286+ Continue -> 1 + spanSoFarBelow
287+ columnsToDrop = thisGridSpan + (gridSpanBelow - fromMaybe gridSpanBelow columnsLeftBelow)
288+ (newColumnsLeftBelow, restOfRowBelow) = dropColumns columnsToDrop rowBelow
289+ in (spanSoFar, thisCell) : g restOfRow (Just newColumnsLeftBelow) (Just restOfRowBelow)
290+
291+ dropColumns :: Integer -> [(a, Cell)] -> (Integer, [(a, Cell)])
292+ dropColumns n [] = (n, [])
293+ dropColumns n cells@((_, Cell gridSpan _ _) : otherCells) =
294+ if n < gridSpan
295+ then (gridSpan - n, cells)
296+ else dropColumns (n - gridSpan) otherCells
297+
259leftBiasedMergeRunStyle :: RunStyle -> RunStyle -> RunStyle
298leftBiasedMergeRunStyle :: RunStyle -> RunStyle -> RunStyle
260leftBiasedMergeRunStyle a b = RunStyle
299leftBiasedMergeRunStyle a b = RunStyle
261 { isBold = isBold a <|> isBold b
300 { isBold = isBold a <|> isBold b
@@ -343,10 +382,16 @@ archiveToDocxWithWarnings archive = do
343 Right doc -> Right (Docx doc, stateWarnings st)
382 Right doc -> Right (Docx doc, stateWarnings st)
344 Left e -> Left e
383 Left e -> Left e
345
384
385+parseXMLFromEntry :: Entry -> Maybe Element
386+parseXMLFromEntry entry =
387+ case parseXMLElement (UTF8.toTextLazy (fromEntry entry)) of
388+ Left _ -> Nothing
389+ Right el -> Just el
390+
346getDocumentXmlPath :: Archive -> Maybe FilePath
391getDocumentXmlPath :: Archive -> Maybe FilePath
347getDocumentXmlPath zf = do
392getDocumentXmlPath zf = do
348 entry <- findEntryByPath "_rels/.rels" zf
393 entry <- findEntryByPath "_rels/.rels" zf
349− relsElem <- (parseXMLDoc . UTF8.toStringLazy . fromEntry) entry
394+ relsElem <- parseXMLFromEntry entry
350 let rels = filterChildrenName (\n -> qName n == "Relationship") relsElem
395 let rels = filterChildrenName (\n -> qName n == "Relationship") relsElem
351 rel <- find (\e -> findAttr (QName "Type" Nothing Nothing) e ==
396 rel <- find (\e -> findAttr (QName "Type" Nothing Nothing) e ==
352 Just "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument")
397 Just "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument")
@@ -354,18 +399,18 @@ getDocumentXmlPath zf = do
354 fp <- findAttr (QName "Target" Nothing Nothing) rel
399 fp <- findAttr (QName "Target" Nothing Nothing) rel
355 -- sometimes there will be a leading slash, which windows seems to
400 -- sometimes there will be a leading slash, which windows seems to
356 -- have trouble with.
401 -- have trouble with.
357− return $ case fp of
402+ return $ case T.unpack fp of
358 '/' : fp' -> fp'
403 '/' : fp' -> fp'
359− _ -> fp
404+ fp' -> fp'
360
405
361archiveToDocument :: Archive -> D Document
406archiveToDocument :: Archive -> D Document
362archiveToDocument zf = do
407archiveToDocument zf = do
363 docPath <- asks envDocXmlPath
408 docPath <- asks envDocXmlPath
364 entry <- maybeToD $ findEntryByPath docPath zf
409 entry <- maybeToD $ findEntryByPath docPath zf
365− docElem <- maybeToD $ (parseXMLDoc . UTF8.toStringLazy . fromEntry) entry
410+ docElem <- maybeToD $ parseXMLFromEntry entry
366 let namespaces = elemToNameSpaces docElem
411 let namespaces = elemToNameSpaces docElem
367 bodyElem <- maybeToD $ findChildByName namespaces "w" "body" docElem
412 bodyElem <- maybeToD $ findChildByName namespaces "w" "body" docElem
368− let bodyElem' = fromMaybe bodyElem (walkDocument namespaces bodyElem)
413+ let bodyElem' = walkDocument namespaces bodyElem
369 body <- elemToBody namespaces bodyElem'
414 body <- elemToBody namespaces bodyElem'
370 return $ Document namespaces body
415 return $ Document namespaces body
371
416
@@ -401,23 +446,24 @@ constructBogusParStyleData stName = ParStyle
401archiveToNotes :: Archive -> Notes
446archiveToNotes :: Archive -> Notes
402archiveToNotes zf =
447archiveToNotes zf =
403 let fnElem = findEntryByPath "word/footnotes.xml" zf
448 let fnElem = findEntryByPath "word/footnotes.xml" zf
404− >>= (parseXMLDoc . UTF8.toStringLazy . fromEntry)
449+ >>= parseXMLFromEntry
405 enElem = findEntryByPath "word/endnotes.xml" zf
450 enElem = findEntryByPath "word/endnotes.xml" zf
406− >>= (parseXMLDoc . UTF8.toStringLazy . fromEntry)
451+ >>= parseXMLFromEntry
407− fn_namespaces = maybe [] elemToNameSpaces fnElem
452+ fn_namespaces = maybe mempty elemToNameSpaces fnElem
408− en_namespaces = maybe [] elemToNameSpaces enElem
453+ en_namespaces = maybe mempty elemToNameSpaces enElem
409− ns = unionBy (\x y -> fst x == fst y) fn_namespaces en_namespaces
454+ ns = M.union fn_namespaces en_namespaces
410− fn = fnElem >>= walkDocument ns >>= elemToNotes ns "footnote"
455+ fn = fnElem >>= elemToNotes ns "footnote" . walkDocument ns
411− en = enElem >>= walkDocument ns >>= elemToNotes ns "endnote"
456+ en = enElem >>= elemToNotes ns "endnote" . walkDocument ns
412 in
457 in
413 Notes ns fn en
458 Notes ns fn en
414
459
415archiveToComments :: Archive -> Comments
460archiveToComments :: Archive -> Comments
416archiveToComments zf =
461archiveToComments zf =
417 let cmtsElem = findEntryByPath "word/comments.xml" zf
462 let cmtsElem = findEntryByPath "word/comments.xml" zf
418− >>= (parseXMLDoc . UTF8.toStringLazy . fromEntry)
463+ >>= parseXMLFromEntry
419− cmts_namespaces = maybe [] elemToNameSpaces cmtsElem
464+ cmts_namespaces = maybe mempty elemToNameSpaces cmtsElem
420− cmts = elemToComments cmts_namespaces <$> (cmtsElem >>= walkDocument cmts_namespaces)
465+ cmts = elemToComments cmts_namespaces . walkDocument cmts_namespaces <$>
466+ cmtsElem
421 in
467 in
422 case cmts of
468 case cmts of
423 Just c -> Comments cmts_namespaces c
469 Just c -> Comments cmts_namespaces c
@@ -433,20 +479,26 @@ filePathToRelType path docXmlPath =
433 then Just InDocument
479 then Just InDocument
434 else Nothing
480 else Nothing
435
481
436−relElemToRelationship :: DocumentLocation -> Element -> Maybe Relationship
482+relElemToRelationship :: FilePath -> DocumentLocation -> Element
483+ -> Maybe Relationship
437−relElemToRelationship relType element | qName (elName element) == "Relationship" =
484+relElemToRelationship fp relType element | qName (elName element) == "Relationship" =
438 do
485 do
439− relId <- findAttrText (QName "Id" Nothing Nothing) element
486+ relId <- findAttr (QName "Id" Nothing Nothing) element
440− target <- findAttrText (QName "Target" Nothing Nothing) element
487+ target <- findAttr (QName "Target" Nothing Nothing) element
488+ -- target may be relative (media/image1.jpeg) or absolute
489+ -- (/word/media/image1.jpeg); we need to relativize it (see #7374)
490+ let frontOfFp = T.pack $ takeWhile (/= '_') fp
491+ let target' = fromMaybe target $
492+ T.stripPrefix frontOfFp $ T.dropWhile (== '/') target
441− return $ Relationship relType relId target
493+ return $ Relationship relType relId target'
442−relElemToRelationship _ _ = Nothing
494+relElemToRelationship _ _ _ = Nothing
443
495
444filePathToRelationships :: Archive -> FilePath -> FilePath -> [Relationship]
496filePathToRelationships :: Archive -> FilePath -> FilePath -> [Relationship]
445filePathToRelationships ar docXmlPath fp
497filePathToRelationships ar docXmlPath fp
446 | Just relType <- filePathToRelType fp docXmlPath
498 | Just relType <- filePathToRelType fp docXmlPath
447 , Just entry <- findEntryByPath fp ar
499 , Just entry <- findEntryByPath fp ar
448− , Just relElems <- (parseXMLDoc . UTF8.toStringLazy . fromEntry) entry =
500+ , Just relElems <- parseXMLFromEntry entry =
449− mapMaybe (relElemToRelationship relType) $ elChildren relElems
501+ mapMaybe (relElemToRelationship fp relType) $ elChildren relElems
450filePathToRelationships _ _ _ = []
502filePathToRelationships _ _ _ = []
451
503
452archiveToRelationships :: Archive -> FilePath -> [Relationship]
504archiveToRelationships :: Archive -> FilePath -> [Relationship]
@@ -478,10 +530,10 @@ lookupLevel numId ilvl (Numbering _ numbs absNumbs) = do
478loElemToLevelOverride :: NameSpaces -> Element -> Maybe LevelOverride
530loElemToLevelOverride :: NameSpaces -> Element -> Maybe LevelOverride
479loElemToLevelOverride ns element
531loElemToLevelOverride ns element
480 | isElem ns "w" "lvlOverride" element = do
532 | isElem ns "w" "lvlOverride" element = do
481− ilvl <- findAttrTextByName ns "w" "ilvl" element
533+ ilvl <- findAttrByName ns "w" "ilvl" element
482 let startOverride = findChildByName ns "w" "startOverride" element
534 let startOverride = findChildByName ns "w" "startOverride" element
483 >>= findAttrByName ns "w" "val"
535 >>= findAttrByName ns "w" "val"
484− >>= (\s -> listToMaybe (map fst (reads s :: [(Integer, String)])))
536+ >>= stringToInteger
485 lvl = findChildByName ns "w" "lvl" element
537 lvl = findChildByName ns "w" "lvl" element
486 >>= levelElemToLevel ns
538 >>= levelElemToLevel ns
487 return $ LevelOverride ilvl startOverride lvl
539 return $ LevelOverride ilvl startOverride lvl
@@ -490,9 +542,9 @@ loElemToLevelOverride _ _ = Nothing
490numElemToNum :: NameSpaces -> Element -> Maybe Numb
542numElemToNum :: NameSpaces -> Element -> Maybe Numb
491numElemToNum ns element
543numElemToNum ns element
492 | isElem ns "w" "num" element = do
544 | isElem ns "w" "num" element = do
493− numId <- findAttrTextByName ns "w" "numId" element
545+ numId <- findAttrByName ns "w" "numId" element
494 absNumId <- findChildByName ns "w" "abstractNumId" element
546 absNumId <- findChildByName ns "w" "abstractNumId" element
495− >>= findAttrTextByName ns "w" "val"
547+ >>= findAttrByName ns "w" "val"
496 let lvlOverrides = mapMaybe
548 let lvlOverrides = mapMaybe
497 (loElemToLevelOverride ns)
549 (loElemToLevelOverride ns)
498 (findChildrenByName ns "w" "lvlOverride" element)
550 (findChildrenByName ns "w" "lvlOverride" element)
@@ -502,7 +554,7 @@ numElemToNum _ _ = Nothing
502absNumElemToAbsNum :: NameSpaces -> Element -> Maybe AbstractNumb
554absNumElemToAbsNum :: NameSpaces -> Element -> Maybe AbstractNumb
503absNumElemToAbsNum ns element
555absNumElemToAbsNum ns element
504 | isElem ns "w" "abstractNum" element = do
556 | isElem ns "w" "abstractNum" element = do
505− absNumId <- findAttrTextByName ns "w" "abstractNumId" element
557+ absNumId <- findAttrByName ns "w" "abstractNumId" element
506 let levelElems = findChildrenByName ns "w" "lvl" element
558 let levelElems = findChildrenByName ns "w" "lvl" element
507 levels = mapMaybe (levelElemToLevel ns) levelElems
559 levels = mapMaybe (levelElemToLevel ns) levelElems
508 return $ AbstractNumb absNumId levels
560 return $ AbstractNumb absNumId levels
@@ -511,23 +563,23 @@ absNumElemToAbsNum _ _ = Nothing
511levelElemToLevel :: NameSpaces -> Element -> Maybe Level
563levelElemToLevel :: NameSpaces -> Element -> Maybe Level
512levelElemToLevel ns element
564levelElemToLevel ns element
513 | isElem ns "w" "lvl" element = do
565 | isElem ns "w" "lvl" element = do
514− ilvl <- findAttrTextByName ns "w" "ilvl" element
566+ ilvl <- findAttrByName ns "w" "ilvl" element
515 fmt <- findChildByName ns "w" "numFmt" element
567 fmt <- findChildByName ns "w" "numFmt" element
516− >>= findAttrTextByName ns "w" "val"
568+ >>= findAttrByName ns "w" "val"
517 txt <- findChildByName ns "w" "lvlText" element
569 txt <- findChildByName ns "w" "lvlText" element
518− >>= findAttrTextByName ns "w" "val"
570+ >>= findAttrByName ns "w" "val"
519 let start = findChildByName ns "w" "start" element
571 let start = findChildByName ns "w" "start" element
520 >>= findAttrByName ns "w" "val"
572 >>= findAttrByName ns "w" "val"
521− >>= (\s -> listToMaybe (map fst (reads s :: [(Integer, String)])))
573+ >>= stringToInteger
522 return (Level ilvl fmt txt start)
574 return (Level ilvl fmt txt start)
523levelElemToLevel _ _ = Nothing
575levelElemToLevel _ _ = Nothing
524
576
525archiveToNumbering' :: Archive -> Maybe Numbering
577archiveToNumbering' :: Archive -> Maybe Numbering
526archiveToNumbering' zf =
578archiveToNumbering' zf =
527 case findEntryByPath "word/numbering.xml" zf of
579 case findEntryByPath "word/numbering.xml" zf of
528− Nothing -> Just $ Numbering [] [] []
580+ Nothing -> Just $ Numbering mempty [] []
529 Just entry -> do
581 Just entry -> do
530− numberingElem <- (parseXMLDoc . UTF8.toStringLazy . fromEntry) entry
582+ numberingElem <- parseXMLFromEntry entry
531 let namespaces = elemToNameSpaces numberingElem
583 let namespaces = elemToNameSpaces numberingElem
532 numElems = findChildrenByName namespaces "w" "num" numberingElem
584 numElems = findChildrenByName namespaces "w" "num" numberingElem
533 absNumElems = findChildrenByName namespaces "w" "abstractNum" numberingElem
585 absNumElems = findChildrenByName namespaces "w" "abstractNum" numberingElem
@@ -537,13 +589,13 @@ archiveToNumbering' zf =
537
589
538archiveToNumbering :: Archive -> Numbering
590archiveToNumbering :: Archive -> Numbering
539archiveToNumbering archive =
591archiveToNumbering archive =
540− fromMaybe (Numbering [] [] []) (archiveToNumbering' archive)
592+ fromMaybe (Numbering mempty [] []) (archiveToNumbering' archive)
541
593
542−elemToNotes :: NameSpaces -> String -> Element -> Maybe (M.Map T.Text Element)
594+elemToNotes :: NameSpaces -> Text -> Element -> Maybe (M.Map T.Text Element)
543elemToNotes ns notetype element
595elemToNotes ns notetype element
544 | isElem ns "w" (notetype <> "s") element =
596 | isElem ns "w" (notetype <> "s") element =
545 let pairs = mapMaybe
597 let pairs = mapMaybe
546− (\e -> findAttrTextByName ns "w" "id" e >>=
598+ (\e -> findAttrByName ns "w" "id" e >>=
547 (\a -> Just (a, e)))
599 (\a -> Just (a, e)))
548 (findChildrenByName ns "w" notetype element)
600 (findChildrenByName ns "w" notetype element)
549 in
601 in
@@ -555,7 +607,7 @@ elemToComments :: NameSpaces -> Element -> M.Map T.Text Element
555elemToComments ns element
607elemToComments ns element
556 | isElem ns "w" "comments" element =
608 | isElem ns "w" "comments" element =
557 let pairs = mapMaybe
609 let pairs = mapMaybe
558− (\e -> findAttrTextByName ns "w" "id" e >>=
610+ (\e -> findAttrByName ns "w" "id" e >>=
559 (\a -> Just (a, e)))
611 (\a -> Just (a, e)))
560 (findChildrenByName ns "w" "comment" element)
612 (findChildrenByName ns "w" "comment" element)
561 in
613 in
@@ -570,7 +622,7 @@ elemToTblGrid :: NameSpaces -> Element -> D TblGrid
570elemToTblGrid ns element | isElem ns "w" "tblGrid" element =
622elemToTblGrid ns element | isElem ns "w" "tblGrid" element =
571 let cols = findChildrenByName ns "w" "gridCol" element
623 let cols = findChildrenByName ns "w" "gridCol" element
572 in
624 in
573− mapD (\e -> maybeToD (findAttrByName ns "w" "val" e >>= stringToInteger))
625+ mapD (\e -> maybeToD (findAttrByName ns "w" "w" e >>= stringToInteger))
574 cols
626 cols
575elemToTblGrid _ _ = throwError WrongElem
627elemToTblGrid _ _ = throwError WrongElem
576
628
@@ -594,14 +646,31 @@ elemToRow ns element | isElem ns "w" "tr" element =
594 do
646 do
595 let cellElems = findChildrenByName ns "w" "tc" element
647 let cellElems = findChildrenByName ns "w" "tc" element
596 cells <- mapD (elemToCell ns) cellElems
648 cells <- mapD (elemToCell ns) cellElems
649+ let hasTblHeader = maybe NoTblHeader (const HasTblHeader)
650+ (findChildByName ns "w" "trPr" element
651+ >>= findChildByName ns "w" "tblHeader")
597− return $ Row cells
652+ return $ Row hasTblHeader cells
598elemToRow _ _ = throwError WrongElem
653elemToRow _ _ = throwError WrongElem
599
654
600elemToCell :: NameSpaces -> Element -> D Cell
655elemToCell :: NameSpaces -> Element -> D Cell
601elemToCell ns element | isElem ns "w" "tc" element =
656elemToCell ns element | isElem ns "w" "tc" element =
602 do
657 do
658+ let properties = findChildByName ns "w" "tcPr" element
659+ let gridSpan = properties
660+ >>= findChildByName ns "w" "gridSpan"
661+ >>= findAttrByName ns "w" "val"
662+ >>= stringToInteger
663+ let vMerge = case properties >>= findChildByName ns "w" "vMerge" of
664+ Nothing -> Restart
665+ Just e ->
666+ fromMaybe Continue $ do
667+ s <- findAttrByName ns "w" "val" e
668+ case s of
669+ "continue" -> Just Continue
670+ "restart" -> Just Restart
671+ _ -> Nothing
603 cellContents <- mapD (elemToBodyPart ns) (elChildren element)
672 cellContents <- mapD (elemToBodyPart ns) (elChildren element)
604− return $ Cell cellContents
673+ return $ Cell (fromMaybe 1 gridSpan) vMerge cellContents
605elemToCell _ _ = throwError WrongElem
674elemToCell _ _ = throwError WrongElem
606
675
607elemToParIndentation :: NameSpaces -> Element -> Maybe ParIndentation
676elemToParIndentation :: NameSpaces -> Element -> Maybe ParIndentation
@@ -615,12 +684,12 @@ elemToParIndentation ns element | isElem ns "w" "ind" element =
615 stringToInteger
684 stringToInteger
616 , hangingParIndent =
685 , hangingParIndent =
617 findAttrByName ns "w" "hanging" element >>=
686 findAttrByName ns "w" "hanging" element >>=
618− stringToInteger}
687+ stringToInteger }
619elemToParIndentation _ _ = Nothing
688elemToParIndentation _ _ = Nothing
620
689
621−testBitMask :: String -> Int -> Bool
690+testBitMask :: Text -> Int -> Bool
622testBitMask bitMaskS n =
691testBitMask bitMaskS n =
623− case (reads ("0x" ++ bitMaskS) :: [(Int, String)]) of
692+ case (reads ("0x" ++ T.unpack bitMaskS) :: [(Int, String)]) of
624 [] -> False
693 [] -> False
625 ((n', _) : _) -> (n' .|. n) /= 0
694 ((n', _) : _) -> (n' .|. n) /= 0
626
695
@@ -633,10 +702,9 @@ pNumInfo = getParStyleField numInfo . pStyle
633elemToBodyPart :: NameSpaces -> Element -> D BodyPart
702elemToBodyPart :: NameSpaces -> Element -> D BodyPart
634elemToBodyPart ns element
703elemToBodyPart ns element
635 | isElem ns "w" "p" element
704 | isElem ns "w" "p" element
636− , (c:_) <- findChildrenByName ns "m" "oMathPara" element =
705+ , (c:_) <- findChildrenByName ns "m" "oMathPara" element = do
637− do
638− expsLst <- eitherToD $ readOMML $ T.pack $ showElement c
706+ expsLst <- eitherToD $ readOMML $ showElement c
639− return $ OMathPara expsLst
707+ return $ OMathPara expsLst
640elemToBodyPart ns element
708elemToBodyPart ns element
641 | isElem ns "w" "p" element
709 | isElem ns "w" "p" element
642 , Just (numId, lvl) <- getNumInfo ns element = do
710 , Just (numId, lvl) <- getNumInfo ns element = do
@@ -654,13 +722,31 @@ elemToBodyPart ns element
654 Nothing | Just (numId, lvl) <- pNumInfo parstyle -> do
722 Nothing | Just (numId, lvl) <- pNumInfo parstyle -> do
655 levelInfo <- lookupLevel numId lvl <$> asks envNumbering
723 levelInfo <- lookupLevel numId lvl <$> asks envNumbering
656 return $ ListItem parstyle numId lvl levelInfo parparts
724 return $ ListItem parstyle numId lvl levelInfo parparts
725+ _ -> let
726+ hasCaptionStyle = elem "Caption" (pStyleId <$> pStyle parstyle)
727+
728+ hasSimpleTableField = fromMaybe False $ do
729+ fldSimple <- findChildByName ns "w" "fldSimple" element
730+ instr <- findAttrByName ns "w" "instr" fldSimple
731+ pure ("Table" `elem` T.words instr)
732+
733+ hasComplexTableField = fromMaybe False $ do
734+ instrText <- findElementByName ns "w" "instrText" element
735+ pure ("Table" `elem` T.words (strContent instrText))
736+
737+ in if hasCaptionStyle && (hasSimpleTableField || hasComplexTableField)
738+ then return $ TblCaption parstyle parparts
657− _ -> return $ Paragraph parstyle parparts
739+ else return $ Paragraph parstyle parparts
740+
658elemToBodyPart ns element
741elemToBodyPart ns element
659 | isElem ns "w" "tbl" element = do
742 | isElem ns "w" "tbl" element = do
660− let caption' = findChildByName ns "w" "tblPr" element
743+ let tblProperties = findChildByName ns "w" "tblPr" element
744+ caption = fromMaybe "" $ tblProperties
661 >>= findChildByName ns "w" "tblCaption"
745 >>= findChildByName ns "w" "tblCaption"
662− >>= findAttrTextByName ns "w" "val"
746+ >>= findAttrByName ns "w" "val"
663− caption = fromMaybe "" caption'
747+ description = fromMaybe "" $ tblProperties
748+ >>= findChildByName ns "w" "tblDescription"
749+ >>= findAttrByName ns "w" "val"
664 grid' = case findChildByName ns "w" "tblGrid" element of
750 grid' = case findChildByName ns "w" "tblGrid" element of
665 Just g -> elemToTblGrid ns g
751 Just g -> elemToTblGrid ns g
666 Nothing -> return []
752 Nothing -> return []
@@ -673,7 +759,7 @@ elemToBodyPart ns element
673 grid <- grid'
759 grid <- grid'
674 tblLook <- tblLook'
760 tblLook <- tblLook'
675 rows <- mapD (elemToRow ns) (elChildren element)
761 rows <- mapD (elemToRow ns) (elChildren element)
676− return $ Tbl caption grid tblLook rows
762+ return $ Tbl (caption <> description) grid tblLook rows
677elemToBodyPart _ _ = throwError WrongElem
763elemToBodyPart _ _ = throwError WrongElem
678
764
679lookupRelationship :: DocumentLocation -> RelId -> [Relationship] -> Maybe Target
765lookupRelationship :: DocumentLocation -> RelId -> [Relationship] -> Maybe Target
@@ -698,8 +784,8 @@ getTitleAndAlt :: NameSpaces -> Element -> (T.Text, T.Text)
698getTitleAndAlt ns element =
784getTitleAndAlt ns element =
699 let mbDocPr = findChildByName ns "wp" "inline" element >>=
785 let mbDocPr = findChildByName ns "wp" "inline" element >>=
700 findChildByName ns "wp" "docPr"
786 findChildByName ns "wp" "docPr"
701− title = fromMaybe "" (mbDocPr >>= findAttrTextByName ns "" "title")
787+ title = fromMaybe "" (mbDocPr >>= findAttrByName ns "" "title")
702− alt = fromMaybe "" (mbDocPr >>= findAttrTextByName ns "" "descr")
788+ alt = fromMaybe "" (mbDocPr >>= findAttrByName ns "" "descr")
703 in (title, alt)
789 in (title, alt)
704
790
705elemToParPart :: NameSpaces -> Element -> D ParPart
791elemToParPart :: NameSpaces -> Element -> D ParPart
@@ -711,22 +797,29 @@ elemToParPart ns element
711 = let (title, alt) = getTitleAndAlt ns drawingElem
797 = let (title, alt) = getTitleAndAlt ns drawingElem
712 a_ns = "http://schemas.openxmlformats.org/drawingml/2006/main"
798 a_ns = "http://schemas.openxmlformats.org/drawingml/2006/main"
713 drawing = findElement (QName "blip" (Just a_ns) (Just "a")) picElem
799 drawing = findElement (QName "blip" (Just a_ns) (Just "a")) picElem
714− >>= findAttrTextByName ns "r" "embed"
800+ >>= findAttrByName ns "r" "embed"
715 in
801 in
716 case drawing of
802 case drawing of
717 Just s -> expandDrawingId s >>= (\(fp, bs) -> return $ Drawing fp title alt bs $ elemToExtent drawingElem)
803 Just s -> expandDrawingId s >>= (\(fp, bs) -> return $ Drawing fp title alt bs $ elemToExtent drawingElem)
718 Nothing -> throwError WrongElem
804 Nothing -> throwError WrongElem
719−-- The below is an attempt to deal with images in deprecated vml format.
805+-- The two cases below are an attempt to deal with images in deprecated vml format.
806+-- Todo: check out title and attr for deprecated format.
720elemToParPart ns element
807elemToParPart ns element
721 | isElem ns "w" "r" element
808 | isElem ns "w" "r" element
722 , Just _ <- findChildByName ns "w" "pict" element =
809 , Just _ <- findChildByName ns "w" "pict" element =
723 let drawing = findElement (elemName ns "v" "imagedata") element
810 let drawing = findElement (elemName ns "v" "imagedata") element
724− >>= findAttrTextByName ns "r" "id"
811+ >>= findAttrByName ns "r" "id"
725 in
812 in
726 case drawing of
813 case drawing of
727− -- Todo: check out title and attr for deprecated format.
728 Just s -> expandDrawingId s >>= (\(fp, bs) -> return $ Drawing fp "" "" bs Nothing)
814 Just s -> expandDrawingId s >>= (\(fp, bs) -> return $ Drawing fp "" "" bs Nothing)
729 Nothing -> throwError WrongElem
815 Nothing -> throwError WrongElem
816+elemToParPart ns element
817+ | isElem ns "w" "r" element
818+ , Just objectElem <- findChildByName ns "w" "object" element
819+ , Just shapeElem <- findChildByName ns "v" "shape" objectElem
820+ , Just imagedataElem <- findChildByName ns "v" "imagedata" shapeElem
821+ , Just drawingId <- findAttrByName ns "r" "id" imagedataElem
822+ = expandDrawingId drawingId >>= (\(fp, bs) -> return $ Drawing fp "" "" bs Nothing)
730-- Chart
823-- Chart
731elemToParPart ns element
824elemToParPart ns element
732 | isElem ns "w" "r" element
825 | isElem ns "w" "r" element
@@ -790,7 +883,7 @@ elemToParPart ns element
790 fldCharState <- gets stateFldCharState
883 fldCharState <- gets stateFldCharState
791 case fldCharState of
884 case fldCharState of
792 FldCharOpen -> do
885 FldCharOpen -> do
793− info <- eitherToD $ parseFieldInfo $ T.pack $ strContent instrText
886+ info <- eitherToD $ parseFieldInfo $ strContent instrText
794 modify $ \st -> st{stateFldCharState = FldCharFieldInfo info}
887 modify $ \st -> st{stateFldCharState = FldCharFieldInfo info}
795 return NullParPart
888 return NullParPart
796 _ -> return NullParPart
889 _ -> return NullParPart
@@ -811,48 +904,48 @@ elemToParPart ns element
811 return $ ChangedRuns change runs
904 return $ ChangedRuns change runs
812elemToParPart ns element
905elemToParPart ns element
813 | isElem ns "w" "bookmarkStart" element
906 | isElem ns "w" "bookmarkStart" element
814− , Just bmId <- findAttrTextByName ns "w" "id" element
907+ , Just bmId <- findAttrByName ns "w" "id" element
815− , Just bmName <- findAttrTextByName ns "w" "name" element =
908+ , Just bmName <- findAttrByName ns "w" "name" element =
816 return $ BookMark bmId bmName
909 return $ BookMark bmId bmName
817elemToParPart ns element
910elemToParPart ns element
818 | isElem ns "w" "hyperlink" element
911 | isElem ns "w" "hyperlink" element
819− , Just relId <- findAttrTextByName ns "r" "id" element = do
912+ , Just relId <- findAttrByName ns "r" "id" element = do
820 location <- asks envLocation
913 location <- asks envLocation
821 runs <- mapD (elemToRun ns) (elChildren element)
914 runs <- mapD (elemToRun ns) (elChildren element)
822 rels <- asks envRelationships
915 rels <- asks envRelationships
823 case lookupRelationship location relId rels of
916 case lookupRelationship location relId rels of
824 Just target ->
917 Just target ->
825− case findAttrTextByName ns "w" "anchor" element of
918+ case findAttrByName ns "w" "anchor" element of
826 Just anchor -> return $ ExternalHyperLink (target <> "#" <> anchor) runs
919 Just anchor -> return $ ExternalHyperLink (target <> "#" <> anchor) runs
827 Nothing -> return $ ExternalHyperLink target runs
920 Nothing -> return $ ExternalHyperLink target runs
828 Nothing -> return $ ExternalHyperLink "" runs
921 Nothing -> return $ ExternalHyperLink "" runs
829elemToParPart ns element
922elemToParPart ns element
830 | isElem ns "w" "hyperlink" element
923 | isElem ns "w" "hyperlink" element
831− , Just anchor <- findAttrTextByName ns "w" "anchor" element = do
924+ , Just anchor <- findAttrByName ns "w" "anchor" element = do
832 runs <- mapD (elemToRun ns) (elChildren element)
925 runs <- mapD (elemToRun ns) (elChildren element)
833 return $ InternalHyperLink anchor runs
926 return $ InternalHyperLink anchor runs
834elemToParPart ns element
927elemToParPart ns element
835 | isElem ns "w" "commentRangeStart" element
928 | isElem ns "w" "commentRangeStart" element
836− , Just cmtId <- findAttrTextByName ns "w" "id" element = do
929+ , Just cmtId <- findAttrByName ns "w" "id" element = do
837 (Comments _ commentMap) <- asks envComments
930 (Comments _ commentMap) <- asks envComments
838 case M.lookup cmtId commentMap of
931 case M.lookup cmtId commentMap of
839 Just cmtElem -> elemToCommentStart ns cmtElem
932 Just cmtElem -> elemToCommentStart ns cmtElem
840 Nothing -> throwError WrongElem
933 Nothing -> throwError WrongElem
841elemToParPart ns element
934elemToParPart ns element
842 | isElem ns "w" "commentRangeEnd" element
935 | isElem ns "w" "commentRangeEnd" element
843− , Just cmtId <- findAttrTextByName ns "w" "id" element =
936+ , Just cmtId <- findAttrByName ns "w" "id" element =
844 return $ CommentEnd cmtId
937 return $ CommentEnd cmtId
845elemToParPart ns element
938elemToParPart ns element
846 | isElem ns "m" "oMath" element =
939 | isElem ns "m" "oMath" element =
847− fmap PlainOMath (eitherToD $ readOMML $ T.pack $ showElement element)
940+ fmap PlainOMath (eitherToD $ readOMML $ showElement element)
848elemToParPart _ _ = throwError WrongElem
941elemToParPart _ _ = throwError WrongElem
849
942
850elemToCommentStart :: NameSpaces -> Element -> D ParPart
943elemToCommentStart :: NameSpaces -> Element -> D ParPart
851elemToCommentStart ns element
944elemToCommentStart ns element
852 | isElem ns "w" "comment" element
945 | isElem ns "w" "comment" element
853− , Just cmtId <- findAttrTextByName ns "w" "id" element
946+ , Just cmtId <- findAttrByName ns "w" "id" element
854− , Just cmtAuthor <- findAttrTextByName ns "w" "author" element
947+ , Just cmtAuthor <- findAttrByName ns "w" "author" element
855− , cmtDate <- findAttrTextByName ns "w" "date" element = do
948+ , cmtDate <- findAttrByName ns "w" "date" element = do
856 bps <- mapD (elemToBodyPart ns) (elChildren element)
949 bps <- mapD (elemToBodyPart ns) (elChildren element)
857 return $ CommentStart cmtId cmtAuthor cmtDate bps
950 return $ CommentStart cmtId cmtAuthor cmtDate bps
858elemToCommentStart _ _ = throwError WrongElem
951elemToCommentStart _ _ = throwError WrongElem
@@ -871,7 +964,7 @@ elemToExtent drawingElem =
871 where
964 where
872 wp_ns = "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
965 wp_ns = "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
873 getDim at = findElement (QName "extent" (Just wp_ns) (Just "wp")) drawingElem
966 getDim at = findElement (QName "extent" (Just wp_ns) (Just "wp")) drawingElem
874− >>= findAttr (QName at Nothing Nothing) >>= safeRead . T.pack
967+ >>= findAttr (QName at Nothing Nothing) >>= safeRead
875
968
876
969
877childElemToRun :: NameSpaces -> Element -> D Run
970childElemToRun :: NameSpaces -> Element -> D Run
@@ -882,7 +975,7 @@ childElemToRun ns element
882 = let (title, alt) = getTitleAndAlt ns element
975 = let (title, alt) = getTitleAndAlt ns element
883 a_ns = "http://schemas.openxmlformats.org/drawingml/2006/main"
976 a_ns = "http://schemas.openxmlformats.org/drawingml/2006/main"
884 drawing = findElement (QName "blip" (Just a_ns) (Just "a")) picElem
977 drawing = findElement (QName "blip" (Just a_ns) (Just "a")) picElem
885− >>= findAttrText (QName "embed" (lookup "r" ns) (Just "r"))
978+ >>= findAttr (QName "embed" (M.lookup "r" ns) (Just "r"))
886 in
979 in
887 case drawing of
980 case drawing of
888 Just s -> expandDrawingId s >>=
981 Just s -> expandDrawingId s >>=
@@ -895,7 +988,7 @@ childElemToRun ns element
895 = return InlineChart
988 = return InlineChart
896childElemToRun ns element
989childElemToRun ns element
897 | isElem ns "w" "footnoteReference" element
990 | isElem ns "w" "footnoteReference" element
898− , Just fnId <- findAttrTextByName ns "w" "id" element = do
991+ , Just fnId <- findAttrByName ns "w" "id" element = do
899 notes <- asks envNotes
992 notes <- asks envNotes
900 case lookupFootnote fnId notes of
993 case lookupFootnote fnId notes of
901 Just e -> do bps <- local (\r -> r {envLocation=InFootnote}) $ mapD (elemToBodyPart ns) (elChildren e)
994 Just e -> do bps <- local (\r -> r {envLocation=InFootnote}) $ mapD (elemToBodyPart ns) (elChildren e)
@@ -903,7 +996,7 @@ childElemToRun ns element
903 Nothing -> return $ Footnote []
996 Nothing -> return $ Footnote []
904childElemToRun ns element
997childElemToRun ns element
905 | isElem ns "w" "endnoteReference" element
998 | isElem ns "w" "endnoteReference" element
906− , Just enId <- findAttrTextByName ns "w" "id" element = do
999+ , Just enId <- findAttrByName ns "w" "id" element = do
907 notes <- asks envNotes
1000 notes <- asks envNotes
908 case lookupEndnote enId notes of
1001 case lookupEndnote enId notes of
909 Just e -> do bps <- local (\r -> r {envLocation=InEndnote}) $ mapD (elemToBodyPart ns) (elChildren e)
1002 Just e -> do bps <- local (\r -> r {envLocation=InEndnote}) $ mapD (elemToBodyPart ns) (elChildren e)
@@ -956,15 +1049,15 @@ getParStyleField _ _ = Nothing
956getTrackedChange :: NameSpaces -> Element -> Maybe TrackedChange
1049getTrackedChange :: NameSpaces -> Element -> Maybe TrackedChange
957getTrackedChange ns element
1050getTrackedChange ns element
958 | isElem ns "w" "ins" element || isElem ns "w" "moveTo" element
1051 | isElem ns "w" "ins" element || isElem ns "w" "moveTo" element
959− , Just cId <- findAttrTextByName ns "w" "id" element
1052+ , Just cId <- findAttrByName ns "w" "id" element
960− , Just cAuthor <- findAttrTextByName ns "w" "author" element
1053+ , Just cAuthor <- findAttrByName ns "w" "author" element
961− , mcDate <- findAttrTextByName ns "w" "date" element =
1054+ , mcDate <- findAttrByName ns "w" "date" element =
962 Just $ TrackedChange Insertion (ChangeInfo cId cAuthor mcDate)
1055 Just $ TrackedChange Insertion (ChangeInfo cId cAuthor mcDate)
963getTrackedChange ns element
1056getTrackedChange ns element
964 | isElem ns "w" "del" element || isElem ns "w" "moveFrom" element
1057 | isElem ns "w" "del" element || isElem ns "w" "moveFrom" element
965− , Just cId <- findAttrTextByName ns "w" "id" element
1058+ , Just cId <- findAttrByName ns "w" "id" element
966− , Just cAuthor <- findAttrTextByName ns "w" "author" element
1059+ , Just cAuthor <- findAttrByName ns "w" "author" element
967− , mcDate <- findAttrTextByName ns "w" "date" element =
1060+ , mcDate <- findAttrByName ns "w" "date" element =
968 Just $ TrackedChange Deletion (ChangeInfo cId cAuthor mcDate)
1061 Just $ TrackedChange Deletion (ChangeInfo cId cAuthor mcDate)
969getTrackedChange _ _ = Nothing
1062getTrackedChange _ _ = Nothing
970
1063
@@ -973,7 +1066,7 @@ elemToParagraphStyle ns element sty
973 | Just pPr <- findChildByName ns "w" "pPr" element =
1066 | Just pPr <- findChildByName ns "w" "pPr" element =
974 let style =
1067 let style =
975 mapMaybe
1068 mapMaybe
976− (fmap ParaStyleId . findAttrTextByName ns "w" "val")
1069+ (fmap ParaStyleId . findAttrByName ns "w" "val")
977 (findChildrenByName ns "w" "pStyle" pPr)
1070 (findChildrenByName ns "w" "pStyle" pPr)
978 in ParagraphStyle
1071 in ParagraphStyle
979 {pStyle = mapMaybe (`M.lookup` sty) style
1072 {pStyle = mapMaybe (`M.lookup` sty) style
@@ -1005,7 +1098,7 @@ elemToRunStyleD ns element
1005 charStyles <- asks envCharStyles
1098 charStyles <- asks envCharStyles
1006 let parentSty =
1099 let parentSty =
1007 findChildByName ns "w" "rStyle" rPr >>=
1100 findChildByName ns "w" "rStyle" rPr >>=
1008− findAttrTextByName ns "w" "val" >>=
1101+ findAttrByName ns "w" "val" >>=
1009 flip M.lookup charStyles . CharStyleId
1102 flip M.lookup charStyles . CharStyleId
1010 return $ elemToRunStyle ns element parentSty
1103 return $ elemToRunStyle ns element parentSty
1011elemToRunStyleD _ _ = return defaultRunStyle
1104elemToRunStyleD _ _ = return defaultRunStyle
@@ -1015,7 +1108,7 @@ elemToRunElem ns element
1015 | isElem ns "w" "t" element
1108 | isElem ns "w" "t" element
1016 || isElem ns "w" "delText" element
1109 || isElem ns "w" "delText" element
1017 || isElem ns "m" "t" element = do
1110 || isElem ns "m" "t" element = do
1018− let str = T.pack $ strContent element
1111+ let str = strContent element
1019 font <- asks envFont
1112 font <- asks envFont
1020 case font of
1113 case font of
1021 Nothing -> return $ TextRun str
1114 Nothing -> return $ TextRun str
@@ -1037,14 +1130,14 @@ getSymChar :: NameSpaces -> Element -> RunElem
1037getSymChar ns element
1130getSymChar ns element
1038 | Just s <- lowerFromPrivate <$> getCodepoint
1131 | Just s <- lowerFromPrivate <$> getCodepoint
1039 , Just font <- getFont =
1132 , Just font <- getFont =
1040− case readLitChar ("\\x" ++ s) of
1133+ case readLitChar ("\\x" ++ T.unpack s) of
1041 [(char, _)] -> TextRun . maybe "" T.singleton $ getUnicode font char
1134 [(char, _)] -> TextRun . maybe "" T.singleton $ getUnicode font char
1042 _ -> TextRun ""
1135 _ -> TextRun ""
1043 where
1136 where
1044 getCodepoint = findAttrByName ns "w" "char" element
1137 getCodepoint = findAttrByName ns "w" "char" element
1045− getFont = textToFont . T.pack =<< findAttrByName ns "w" "font" element
1138+ getFont = textToFont =<< findAttrByName ns "w" "font" element
1046− lowerFromPrivate ('F':xs) = '0':xs
1139+ lowerFromPrivate t | "F" `T.isPrefixOf` t = "0" <> T.drop 1 t
1047− lowerFromPrivate xs = xs
1140+ | otherwise = t
1048getSymChar _ _ = TextRun ""
1141getSymChar _ _ = TextRun ""
1049
1142
1050elemToRunElems :: NameSpaces -> Element -> D [RunElem]
1143elemToRunElems :: NameSpaces -> Element -> D [RunElem]
@@ -1054,8 +1147,9 @@ elemToRunElems ns element
1054 let qualName = elemName ns "w"
1147 let qualName = elemName ns "w"
1055 let font = do
1148 let font = do
1056 fontElem <- findElement (qualName "rFonts") element
1149 fontElem <- findElement (qualName "rFonts") element
1057− textToFont . T.pack =<<
1150+ textToFont =<<
1058− foldr ((<|>) . (flip findAttr fontElem . qualName)) Nothing ["ascii", "hAnsi"]
1151+ foldr ((<|>) . (flip findAttr fontElem . qualName))
1152+ Nothing ["ascii", "hAnsi"]
1059 local (setFont font) (mapD (elemToRunElem ns) (elChildren element))
1153 local (setFont font) (mapD (elemToRunElem ns) (elChildren element))
1060elemToRunElems _ _ = throwError WrongElem
1154elemToRunElems _ _ = throwError WrongElem
1061
1155
src/Text/Pandoc/Readers/Docx/Parse/Styles.hs+32−25
@@ -48,11 +48,13 @@ import Data.Function (on)
48import Data.String (IsString(..))
48import Data.String (IsString(..))
49import qualified Data.Map as M
49import qualified Data.Map as M
50import qualified Data.Text as T
50import qualified Data.Text as T
51+import qualified Data.Text.Read
52+import Data.Text (Text)
51import Data.Maybe
53import Data.Maybe
52import Data.Coerce
54import Data.Coerce
53import Text.Pandoc.Readers.Docx.Util
55import Text.Pandoc.Readers.Docx.Util
54import qualified Text.Pandoc.UTF8 as UTF8
56import qualified Text.Pandoc.UTF8 as UTF8
55−import Text.XML.Light
57+import Text.Pandoc.XML.Light
56
58
57newtype CharStyleId = CharStyleId T.Text
59newtype CharStyleId = CharStyleId T.Text
58 deriving (Show, Eq, Ord, IsString, FromStyleId)
60 deriving (Show, Eq, Ord, IsString, FromStyleId)
@@ -108,7 +110,7 @@ data RunStyle = RunStyle { isBold :: Maybe Bool
108 , isRTL :: Maybe Bool
110 , isRTL :: Maybe Bool
109 , isForceCTL :: Maybe Bool
111 , isForceCTL :: Maybe Bool
110 , rVertAlign :: Maybe VertAlign
112 , rVertAlign :: Maybe VertAlign
111− , rUnderline :: Maybe String
113+ , rUnderline :: Maybe Text
112 , rParentStyle :: Maybe CharStyle
114 , rParentStyle :: Maybe CharStyle
113 }
115 }
114 deriving Show
116 deriving Show
@@ -135,19 +137,22 @@ defaultRunStyle = RunStyle { isBold = Nothing
135 , rParentStyle = Nothing
137 , rParentStyle = Nothing
136 }
138 }
137
139
140+archiveToStyles'
138−archiveToStyles' :: (Ord k1, Ord k2, ElemToStyle a1, ElemToStyle a2) =>
141+ :: (Ord k1, Ord k2, ElemToStyle a1, ElemToStyle a2)
139− (a1 -> k1) -> (a2 -> k2) -> Archive -> (M.Map k1 a1, M.Map k2 a2)
142+ => (a1 -> k1) -> (a2 -> k2) -> Archive -> (M.Map k1 a1, M.Map k2 a2)
140archiveToStyles' conv1 conv2 zf =
143archiveToStyles' conv1 conv2 zf =
141− let stylesElem = findEntryByPath "word/styles.xml" zf >>=
144+ case findEntryByPath "word/styles.xml" zf of
142− (parseXMLDoc . UTF8.toStringLazy . fromEntry)
145+ Nothing -> (M.empty, M.empty)
143− in
146+ Just entry ->
144− case stylesElem of
147+ case parseXMLElement . UTF8.toTextLazy . fromEntry $ entry of
145− Nothing -> (M.empty, M.empty)
148+ Left _ -> (M.empty, M.empty)
146− Just styElem ->
149+ Right styElem ->
147− let namespaces = elemToNameSpaces styElem
150+ let namespaces = elemToNameSpaces styElem
148− in
151+ in
152+ ( M.fromList $ map (\r -> (conv1 r, r)) $
149− ( M.fromList $ map (\r -> (conv1 r, r)) $ buildBasedOnList namespaces styElem Nothing,
153+ buildBasedOnList namespaces styElem Nothing,
154+ M.fromList $ map (\p -> (conv2 p, p)) $
150− M.fromList $ map (\p -> (conv2 p, p)) $ buildBasedOnList namespaces styElem Nothing)
155+ buildBasedOnList namespaces styElem Nothing)
151
156
152isBasedOnStyle :: (ElemToStyle a, FromStyleId (StyleId a)) => NameSpaces -> Element -> Maybe a -> Bool
157isBasedOnStyle :: (ElemToStyle a, FromStyleId (StyleId a)) => NameSpaces -> Element -> Maybe a -> Bool
153isBasedOnStyle ns element parentStyle
158isBasedOnStyle ns element parentStyle
@@ -155,7 +160,7 @@ isBasedOnStyle ns element parentStyle
155 , Just styleType <- findAttrByName ns "w" "type" element
160 , Just styleType <- findAttrByName ns "w" "type" element
156 , styleType == cStyleType parentStyle
161 , styleType == cStyleType parentStyle
157 , Just basedOnVal <- findChildByName ns "w" "basedOn" element >>=
162 , Just basedOnVal <- findChildByName ns "w" "basedOn" element >>=
158− findAttrTextByName ns "w" "val"
163+ findAttrByName ns "w" "val"
159 , Just ps <- parentStyle = basedOnVal == fromStyleId (getStyleId ps)
164 , Just ps <- parentStyle = basedOnVal == fromStyleId (getStyleId ps)
160 | isElem ns "w" "style" element
165 | isElem ns "w" "style" element
161 , Just styleType <- findAttrByName ns "w" "type" element
166 , Just styleType <- findAttrByName ns "w" "type" element
@@ -165,7 +170,7 @@ isBasedOnStyle ns element parentStyle
165 | otherwise = False
170 | otherwise = False
166
171
167class HasStyleId a => ElemToStyle a where
172class HasStyleId a => ElemToStyle a where
168− cStyleType :: Maybe a -> String
173+ cStyleType :: Maybe a -> Text
169 elemToStyle :: NameSpaces -> Element -> Maybe a -> Maybe a
174 elemToStyle :: NameSpaces -> Element -> Maybe a -> Maybe a
170
175
171class FromStyleId (StyleId a) => HasStyleId a where
176class FromStyleId (StyleId a) => HasStyleId a where
@@ -222,8 +227,10 @@ buildBasedOnList ns element rootStyle =
222 stys -> stys ++
227 stys -> stys ++
223 concatMap (buildBasedOnList ns element . Just) stys
228 concatMap (buildBasedOnList ns element . Just) stys
224
229
225−stringToInteger :: String -> Maybe Integer
230+stringToInteger :: Text -> Maybe Integer
226−stringToInteger s = listToMaybe $ map fst (reads s :: [(Integer, String)])
231+stringToInteger s = case Data.Text.Read.decimal s of
232+ Right (x,_) -> Just x
233+ Left _ -> Nothing
227
234
228checkOnOff :: NameSpaces -> Element -> QName -> Maybe Bool
235checkOnOff :: NameSpaces -> Element -> QName -> Maybe Bool
229checkOnOff ns rPr tag
236checkOnOff ns rPr tag
@@ -243,7 +250,7 @@ checkOnOff _ _ _ = Nothing
243elemToCharStyle :: NameSpaces
250elemToCharStyle :: NameSpaces
244 -> Element -> Maybe CharStyle -> Maybe CharStyle
251 -> Element -> Maybe CharStyle -> Maybe CharStyle
245elemToCharStyle ns element parentStyle
252elemToCharStyle ns element parentStyle
246− = CharStyle <$> (CharStyleId <$> findAttrTextByName ns "w" "styleId" element)
253+ = CharStyle <$> (CharStyleId <$> findAttrByName ns "w" "styleId" element)
247 <*> getElementStyleName ns element
254 <*> getElementStyleName ns element
248 <*> Just (elemToRunStyle ns element parentStyle)
255 <*> Just (elemToRunStyle ns element parentStyle)
249
256
@@ -277,7 +284,7 @@ elemToRunStyle _ _ _ = defaultRunStyle
277getHeaderLevel :: NameSpaces -> Element -> Maybe (ParaStyleName, Int)
284getHeaderLevel :: NameSpaces -> Element -> Maybe (ParaStyleName, Int)
278getHeaderLevel ns element
285getHeaderLevel ns element
279 | Just styleName <- getElementStyleName ns element
286 | Just styleName <- getElementStyleName ns element
280− , Just n <- stringToInteger . T.unpack =<<
287+ , Just n <- stringToInteger =<<
281 (T.stripPrefix "heading " . T.toLower $
288 (T.stripPrefix "heading " . T.toLower $
282 fromStyleName styleName)
289 fromStyleName styleName)
283 , n > 0 = Just (styleName, fromInteger n)
290 , n > 0 = Just (styleName, fromInteger n)
@@ -285,8 +292,8 @@ getHeaderLevel _ _ = Nothing
285
292
286getElementStyleName :: Coercible T.Text a => NameSpaces -> Element -> Maybe a
293getElementStyleName :: Coercible T.Text a => NameSpaces -> Element -> Maybe a
287getElementStyleName ns el = coerce <$>
294getElementStyleName ns el = coerce <$>
288− ((findChildByName ns "w" "name" el >>= findAttrTextByName ns "w" "val")
295+ ((findChildByName ns "w" "name" el >>= findAttrByName ns "w" "val")
289− <|> findAttrTextByName ns "w" "styleId" el)
296+ <|> findAttrByName ns "w" "styleId" el)
290
297
291getNumInfo :: NameSpaces -> Element -> Maybe (T.Text, T.Text)
298getNumInfo :: NameSpaces -> Element -> Maybe (T.Text, T.Text)
292getNumInfo ns element = do
299getNumInfo ns element = do
@@ -294,15 +301,15 @@ getNumInfo ns element = do
294 findChildByName ns "w" "numPr"
301 findChildByName ns "w" "numPr"
295 lvl = fromMaybe "0" (numPr >>=
302 lvl = fromMaybe "0" (numPr >>=
296 findChildByName ns "w" "ilvl" >>=
303 findChildByName ns "w" "ilvl" >>=
297− findAttrTextByName ns "w" "val")
304+ findAttrByName ns "w" "val")
298 numId <- numPr >>=
305 numId <- numPr >>=
299 findChildByName ns "w" "numId" >>=
306 findChildByName ns "w" "numId" >>=
300− findAttrTextByName ns "w" "val"
307+ findAttrByName ns "w" "val"
301 return (numId, lvl)
308 return (numId, lvl)
302
309
303elemToParStyleData :: NameSpaces -> Element -> Maybe ParStyle -> Maybe ParStyle
310elemToParStyleData :: NameSpaces -> Element -> Maybe ParStyle -> Maybe ParStyle
304elemToParStyleData ns element parentStyle
311elemToParStyleData ns element parentStyle
305− | Just styleId <- findAttrTextByName ns "w" "styleId" element
312+ | Just styleId <- findAttrByName ns "w" "styleId" element
306 , Just styleName <- getElementStyleName ns element
313 , Just styleName <- getElementStyleName ns element
307 = Just $ ParStyle
314 = Just $ ParStyle
308 {
315 {
src/Text/Pandoc/Readers/Docx/Util.hs+27−25
@@ -1,3 +1,4 @@
1+{-# LANGUAGE OverloadedStrings #-}
1{- |
2{- |
2 Module : Text.Pandoc.Readers.Docx.StyleMaps
3 Module : Text.Pandoc.Readers.Docx.StyleMaps
3 Copyright : © 2014-2020 Jesse Rosenthal <jrosenthal@jhu.edu>,
4 Copyright : © 2014-2020 Jesse Rosenthal <jrosenthal@jhu.edu>,
@@ -18,51 +19,52 @@ module Text.Pandoc.Readers.Docx.Util (
18 , elemToNameSpaces
19 , elemToNameSpaces
19 , findChildByName
20 , findChildByName
20 , findChildrenByName
21 , findChildrenByName
21− , findAttrText
22+ , findElementByName
22 , findAttrByName
23 , findAttrByName
23− , findAttrTextByName
24 ) where
24 ) where
25
25
26−import Data.Maybe (mapMaybe)
27import qualified Data.Text as T
26import qualified Data.Text as T
27+import Data.Text (Text)
28−import Text.XML.Light
28+import Text.Pandoc.XML.Light
29+import qualified Data.Map as M
29
30
30−type NameSpaces = [(String, String)]
31+type NameSpaces = M.Map Text Text
31
32
32elemToNameSpaces :: Element -> NameSpaces
33elemToNameSpaces :: Element -> NameSpaces
33−elemToNameSpaces = mapMaybe attrToNSPair . elAttribs
34+elemToNameSpaces = foldr (\(Attr qn val) ->
35+ case qn of
36+ QName s _ (Just "xmlns") -> M.insert s val
37+ _ -> id) mempty . elAttribs
34
38
35−attrToNSPair :: Attr -> Maybe (String, String)
36−attrToNSPair (Attr (QName s _ (Just "xmlns")) val) = Just (s, val)
37−attrToNSPair _ = Nothing
38−
39−elemName :: NameSpaces -> String -> String -> QName
39+elemName :: NameSpaces -> Text -> Text -> QName
40elemName ns prefix name =
40elemName ns prefix name =
41+ QName name (M.lookup prefix ns)
41− QName name (lookup prefix ns) (if null prefix then Nothing else Just prefix)
42+ (if T.null prefix then Nothing else Just prefix)
42
43
43−isElem :: NameSpaces -> String -> String -> Element -> Bool
44+isElem :: NameSpaces -> Text -> Text -> Element -> Bool
44isElem ns prefix name element =
45isElem ns prefix name element =
45− let ns' = ns ++ elemToNameSpaces element
46+ let ns' = ns <> elemToNameSpaces element
46 in qName (elName element) == name &&
47 in qName (elName element) == name &&
47− qURI (elName element) == lookup prefix ns'
48+ qURI (elName element) == M.lookup prefix ns'
48
49
49−findChildByName :: NameSpaces -> String -> String -> Element -> Maybe Element
50+findChildByName :: NameSpaces -> Text -> Text -> Element -> Maybe Element
50findChildByName ns pref name el =
51findChildByName ns pref name el =
51− let ns' = ns ++ elemToNameSpaces el
52+ let ns' = ns <> elemToNameSpaces el
52 in findChild (elemName ns' pref name) el
53 in findChild (elemName ns' pref name) el
53
54
54−findChildrenByName :: NameSpaces -> String -> String -> Element -> [Element]
55+findChildrenByName :: NameSpaces -> Text -> Text -> Element -> [Element]
55findChildrenByName ns pref name el =
56findChildrenByName ns pref name el =
56− let ns' = ns ++ elemToNameSpaces el
57+ let ns' = ns <> elemToNameSpaces el
57 in findChildren (elemName ns' pref name) el
58 in findChildren (elemName ns' pref name) el
58
59
59−findAttrText :: QName -> Element -> Maybe T.Text
60+-- | Like 'findChildrenByName', but searches descendants.
60−findAttrText x = fmap T.pack . findAttr x
61+findElementByName :: NameSpaces -> Text -> Text -> Element -> Maybe Element
62+findElementByName ns pref name el =
63+ let ns' = ns <> elemToNameSpaces el
64+ in findElement (elemName ns' pref name) el
61
65
62−findAttrByName :: NameSpaces -> String -> String -> Element -> Maybe String
66+findAttrByName :: NameSpaces -> Text -> Text -> Element -> Maybe Text
63findAttrByName ns pref name el =
67findAttrByName ns pref name el =
64− let ns' = ns ++ elemToNameSpaces el
68+ let ns' = ns <> elemToNameSpaces el
65 in findAttr (elemName ns' pref name) el
69 in findAttr (elemName ns' pref name) el
66
70
67−findAttrTextByName :: NameSpaces -> String -> String -> Element -> Maybe T.Text
68−findAttrTextByName a b c = fmap T.pack . findAttrByName a b c
src/Text/Pandoc/Readers/DokuWiki.hs+9−8
@@ -29,26 +29,27 @@ import Text.Pandoc.Definition
29import Text.Pandoc.Error (PandocError (PandocParsecError))
29import Text.Pandoc.Error (PandocError (PandocParsecError))
30import Text.Pandoc.Options
30import Text.Pandoc.Options
31import Text.Pandoc.Parsing hiding (enclosed, nested)
31import Text.Pandoc.Parsing hiding (enclosed, nested)
32−import Text.Pandoc.Shared (crFilter, trim, stringify, tshow)
32+import Text.Pandoc.Shared (trim, stringify, tshow)
33
33
34-- | Read DokuWiki from an input string and return a Pandoc document.
34-- | Read DokuWiki from an input string and return a Pandoc document.
35−readDokuWiki :: PandocMonad m
35+readDokuWiki :: (PandocMonad m, ToSources a)
36 => ReaderOptions
36 => ReaderOptions
37− -> Text
37+ -> a
38 -> m Pandoc
38 -> m Pandoc
39readDokuWiki opts s = do
39readDokuWiki opts s = do
40− let input = crFilter s
40+ let sources = toSources s
41− res <- runParserT parseDokuWiki def {stateOptions = opts } "source" input
41+ res <- runParserT parseDokuWiki def {stateOptions = opts }
42+ (initialSourceName sources) sources
42 case res of
43 case res of
43− Left e -> throwError $ PandocParsecError input e
44+ Left e -> throwError $ PandocParsecError sources e
44 Right d -> return d
45 Right d -> return d
45
46
46−type DWParser = ParserT Text ParserState
47+type DWParser = ParserT Sources ParserState
47
48
48-- * Utility functions
49-- * Utility functions
49
50
50-- | Parse end-of-line, which can be either a newline or end-of-file.
51-- | Parse end-of-line, which can be either a newline or end-of-file.
51−eol :: Stream s m Char => ParserT s st m ()
52+eol :: (Stream s m Char, UpdateSourcePos s Char) => ParserT s st m ()
52eol = void newline <|> eof
53eol = void newline <|> eof
53
54
54nested :: PandocMonad m => DWParser m a -> DWParser m a
55nested :: PandocMonad m => DWParser m a -> DWParser m a
src/Text/Pandoc/Readers/EPUB.hs+44−36
@@ -17,14 +17,14 @@ module Text.Pandoc.Readers.EPUB
17 (readEPUB)
17 (readEPUB)
18 where
18 where
19
19
20−import Codec.Archive.Zip (Archive (..), Entry, findEntryByPath, fromEntry,
20+import Codec.Archive.Zip (Archive (..), Entry(..), findEntryByPath, fromEntry,
21 toArchiveOrFail)
21 toArchiveOrFail)
22import Control.DeepSeq (NFData, deepseq)
22import Control.DeepSeq (NFData, deepseq)
23import Control.Monad (guard, liftM, liftM2, mplus)
23import Control.Monad (guard, liftM, liftM2, mplus)
24import Control.Monad.Except (throwError)
24import Control.Monad.Except (throwError)
25import qualified Data.ByteString.Lazy as BL (ByteString)
25import qualified Data.ByteString.Lazy as BL (ByteString)
26−import Data.List (isInfixOf)
27import qualified Data.Text as T
26import qualified Data.Text as T
27+import Data.Text (Text)
28import qualified Data.Map as M (Map, elems, fromList, lookup)
28import qualified Data.Map as M (Map, elems, fromList, lookup)
29import Data.Maybe (mapMaybe)
29import Data.Maybe (mapMaybe)
30import qualified Data.Text.Lazy as TL
30import qualified Data.Text.Lazy as TL
@@ -40,12 +40,12 @@ import Text.Pandoc.Extensions (Extension (Ext_raw_html), enableExtension)
40import Text.Pandoc.MIME (MimeType)
40import Text.Pandoc.MIME (MimeType)
41import Text.Pandoc.Options (ReaderOptions (..))
41import Text.Pandoc.Options (ReaderOptions (..))
42import Text.Pandoc.Readers.HTML (readHtml)
42import Text.Pandoc.Readers.HTML (readHtml)
43−import Text.Pandoc.Shared (addMetaField, collapseFilePath, escapeURI)
43+import Text.Pandoc.Shared (addMetaField, collapseFilePath, escapeURI, tshow)
44−import qualified Text.Pandoc.UTF8 as UTF8 (toStringLazy)
44+import qualified Text.Pandoc.UTF8 as UTF8 (toTextLazy)
45import Text.Pandoc.Walk (query, walk)
45import Text.Pandoc.Walk (query, walk)
46−import Text.XML.Light
46+import Text.Pandoc.XML.Light
47
47
48−type Items = M.Map String (FilePath, MimeType)
48+type Items = M.Map Text (FilePath, MimeType)
49
49
50readEPUB :: PandocMonad m => ReaderOptions -> BL.ByteString -> m Pandoc
50readEPUB :: PandocMonad m => ReaderOptions -> BL.ByteString -> m Pandoc
51readEPUB opts bytes = case toArchiveOrFail bytes of
51readEPUB opts bytes = case toArchiveOrFail bytes of
@@ -125,26 +125,27 @@ imageToPandoc s = B.doc . B.para $ B.image (T.pack s) "" mempty
125imageMimes :: [MimeType]
125imageMimes :: [MimeType]
126imageMimes = ["image/gif", "image/jpeg", "image/png"]
126imageMimes = ["image/gif", "image/jpeg", "image/png"]
127
127
128−type CoverId = String
128+type CoverId = Text
129
129
130type CoverImage = FilePath
130type CoverImage = FilePath
131
131
132+parseManifest :: (PandocMonad m)
132−parseManifest :: (PandocMonad m) => Element -> Maybe CoverId -> m (Maybe CoverImage, Items)
133+ => Element -> Maybe CoverId -> m (Maybe CoverImage, Items)
133parseManifest content coverId = do
134parseManifest content coverId = do
134 manifest <- findElementE (dfName "manifest") content
135 manifest <- findElementE (dfName "manifest") content
135 let items = findChildren (dfName "item") manifest
136 let items = findChildren (dfName "item") manifest
136 r <- mapM parseItem items
137 r <- mapM parseItem items
137 let cover = findAttr (emptyName "href") =<< filterChild findCover manifest
138 let cover = findAttr (emptyName "href") =<< filterChild findCover manifest
138− return (cover `mplus` coverId, M.fromList r)
139+ return (T.unpack <$> (cover `mplus` coverId), M.fromList r)
139 where
140 where
140− findCover e = maybe False (isInfixOf "cover-image")
141+ findCover e = maybe False (T.isInfixOf "cover-image")
141 (findAttr (emptyName "properties") e)
142 (findAttr (emptyName "properties") e)
142 || Just True == liftM2 (==) coverId (findAttr (emptyName "id") e)
143 || Just True == liftM2 (==) coverId (findAttr (emptyName "id") e)
143 parseItem e = do
144 parseItem e = do
144 uid <- findAttrE (emptyName "id") e
145 uid <- findAttrE (emptyName "id") e
145 href <- findAttrE (emptyName "href") e
146 href <- findAttrE (emptyName "href") e
146 mime <- findAttrE (emptyName "media-type") e
147 mime <- findAttrE (emptyName "media-type") e
147− return (uid, (href, T.pack mime))
148+ return (uid, (T.unpack href, mime))
148
149
149parseSpine :: PandocMonad m => Items -> Element -> m [(FilePath, MimeType)]
150parseSpine :: PandocMonad m => Items -> Element -> m [(FilePath, MimeType)]
150parseSpine is e = do
151parseSpine is e = do
@@ -172,25 +173,25 @@ parseMeta content = do
172-- http://www.idpf.org/epub/30/spec/epub30-publications.html#sec-metadata-elem
173-- http://www.idpf.org/epub/30/spec/epub30-publications.html#sec-metadata-elem
173parseMetaItem :: Element -> Meta -> Meta
174parseMetaItem :: Element -> Meta -> Meta
174parseMetaItem e@(stripNamespace . elName -> field) meta =
175parseMetaItem e@(stripNamespace . elName -> field) meta =
175− addMetaField (renameMeta field) (B.str $ T.pack $ strContent e) meta
176+ addMetaField (renameMeta field) (B.str $ strContent e) meta
176
177
177−renameMeta :: String -> T.Text
178+renameMeta :: Text -> Text
178renameMeta "creator" = "author"
179renameMeta "creator" = "author"
179−renameMeta s = T.pack s
180+renameMeta s = s
180
181
181getManifest :: PandocMonad m => Archive -> m (String, Element)
182getManifest :: PandocMonad m => Archive -> m (String, Element)
182getManifest archive = do
183getManifest archive = do
183 metaEntry <- findEntryByPathE ("META-INF" </> "container.xml") archive
184 metaEntry <- findEntryByPathE ("META-INF" </> "container.xml") archive
184− docElem <- (parseXMLDocE . UTF8.toStringLazy . fromEntry) metaEntry
185+ docElem <- parseXMLDocE metaEntry
185 let namespaces = mapMaybe attrToNSPair (elAttribs docElem)
186 let namespaces = mapMaybe attrToNSPair (elAttribs docElem)
186 ns <- mkE "xmlns not in namespaces" (lookup "xmlns" namespaces)
187 ns <- mkE "xmlns not in namespaces" (lookup "xmlns" namespaces)
187 as <- fmap (map attrToPair . elAttribs)
188 as <- fmap (map attrToPair . elAttribs)
188 (findElementE (QName "rootfile" (Just ns) Nothing) docElem)
189 (findElementE (QName "rootfile" (Just ns) Nothing) docElem)
189− manifestFile <- mkE "Root not found" (lookup "full-path" as)
190+ manifestFile <- T.unpack <$> mkE "Root not found" (lookup "full-path" as)
190 let rootdir = dropFileName manifestFile
191 let rootdir = dropFileName manifestFile
191 --mime <- lookup "media-type" as
192 --mime <- lookup "media-type" as
192 manifest <- findEntryByPathE manifestFile archive
193 manifest <- findEntryByPathE manifestFile archive
193− (rootdir,) <$> (parseXMLDocE . UTF8.toStringLazy . fromEntry $ manifest)
194+ (rootdir,) <$> parseXMLDocE manifest
194
195
195-- Fixup
196-- Fixup
196
197
@@ -200,7 +201,8 @@ fixInternalReferences pathToFile =
200 . walk (fixBlockIRs filename)
201 . walk (fixBlockIRs filename)
201 . walk (fixInlineIRs filename)
202 . walk (fixInlineIRs filename)
202 where
203 where
203− (root, T.unpack . escapeURI . T.pack -> filename) = splitFileName pathToFile
204+ (root, T.unpack . escapeURI . T.pack -> filename) =
205+ splitFileName pathToFile
204
206
205fixInlineIRs :: String -> Inline -> Inline
207fixInlineIRs :: String -> Inline -> Inline
206fixInlineIRs s (Span as v) =
208fixInlineIRs s (Span as v) =
@@ -213,7 +215,7 @@ fixInlineIRs s (Link as is t) =
213 Link (fixAttrs s as) is t
215 Link (fixAttrs s as) is t
214fixInlineIRs _ v = v
216fixInlineIRs _ v = v
215
217
216−prependHash :: [T.Text] -> Inline -> Inline
218+prependHash :: [Text] -> Inline -> Inline
217prependHash ps l@(Link attr is (url, tit))
219prependHash ps l@(Link attr is (url, tit))
218 | or [s `T.isPrefixOf` url | s <- ps] =
220 | or [s `T.isPrefixOf` url | s <- ps] =
219 Link attr is ("#" <> url, tit)
221 Link attr is ("#" <> url, tit)
@@ -230,16 +232,17 @@ fixBlockIRs s (CodeBlock as code) =
230fixBlockIRs _ b = b
232fixBlockIRs _ b = b
231
233
232fixAttrs :: FilePath -> B.Attr -> B.Attr
234fixAttrs :: FilePath -> B.Attr -> B.Attr
235+fixAttrs s (ident, cs, kvs) =
233−fixAttrs s (ident, cs, kvs) = (addHash s ident, filter (not . T.null) cs, removeEPUBAttrs kvs)
236+ (addHash s ident, filter (not . T.null) cs, removeEPUBAttrs kvs)
234
237
235−addHash :: String -> T.Text -> T.Text
238+addHash :: FilePath -> Text -> Text
236addHash _ "" = ""
239addHash _ "" = ""
237addHash s ident = T.pack (takeFileName s) <> "#" <> ident
240addHash s ident = T.pack (takeFileName s) <> "#" <> ident
238
241
239−removeEPUBAttrs :: [(T.Text, T.Text)] -> [(T.Text, T.Text)]
242+removeEPUBAttrs :: [(Text, Text)] -> [(Text, Text)]
240removeEPUBAttrs kvs = filter (not . isEPUBAttr) kvs
243removeEPUBAttrs kvs = filter (not . isEPUBAttr) kvs
241
244
242−isEPUBAttr :: (T.Text, a) -> Bool
245+isEPUBAttr :: (Text, a) -> Bool
243isEPUBAttr (k, _) = "epub:" `T.isPrefixOf` k
246isEPUBAttr (k, _) = "epub:" `T.isPrefixOf` k
244
247
245-- Library
248-- Library
@@ -256,39 +259,44 @@ uncurry3 f (a, b, c) = f a b c
256
259
257-- Utility
260-- Utility
258
261
259−stripNamespace :: QName -> String
262+stripNamespace :: QName -> Text
260stripNamespace (QName v _ _) = v
263stripNamespace (QName v _ _) = v
261
264
262−attrToNSPair :: Attr -> Maybe (String, String)
265+attrToNSPair :: Attr -> Maybe (Text, Text)
263attrToNSPair (Attr (QName "xmlns" _ _) val) = Just ("xmlns", val)
266attrToNSPair (Attr (QName "xmlns" _ _) val) = Just ("xmlns", val)
264attrToNSPair _ = Nothing
267attrToNSPair _ = Nothing
265
268
266−attrToPair :: Attr -> (String, String)
269+attrToPair :: Attr -> (Text, Text)
267attrToPair (Attr (QName name _ _) val) = (name, val)
270attrToPair (Attr (QName name _ _) val) = (name, val)
268
271
269−defaultNameSpace :: Maybe String
272+defaultNameSpace :: Maybe Text
270defaultNameSpace = Just "http://www.idpf.org/2007/opf"
273defaultNameSpace = Just "http://www.idpf.org/2007/opf"
271
274
272−dfName :: String -> QName
275+dfName :: Text -> QName
273dfName s = QName s defaultNameSpace Nothing
276dfName s = QName s defaultNameSpace Nothing
274
277
275−emptyName :: String -> QName
278+emptyName :: Text -> QName
276emptyName s = QName s Nothing Nothing
279emptyName s = QName s Nothing Nothing
277
280
278-- Convert Maybe interface to Either
281-- Convert Maybe interface to Either
279
282
280−findAttrE :: PandocMonad m => QName -> Element -> m String
283+findAttrE :: PandocMonad m => QName -> Element -> m Text
281findAttrE q e = mkE "findAttr" $ findAttr q e
284findAttrE q e = mkE "findAttr" $ findAttr q e
282
285
283findEntryByPathE :: PandocMonad m => FilePath -> Archive -> m Entry
286findEntryByPathE :: PandocMonad m => FilePath -> Archive -> m Entry
284findEntryByPathE (normalise . unEscapeString -> path) a =
287findEntryByPathE (normalise . unEscapeString -> path) a =
285− mkE ("No entry on path: " ++ path) $ findEntryByPath path a
288+ mkE ("No entry on path: " <> T.pack path) $ findEntryByPath path a
286
289
287−parseXMLDocE :: PandocMonad m => String -> m Element
290+parseXMLDocE :: PandocMonad m => Entry -> m Element
288−parseXMLDocE doc = mkE "Unable to parse XML doc" $ parseXMLDoc doc
291+parseXMLDocE entry =
292+ either (throwError . PandocXMLError fp) return $ parseXMLElement doc
293+ where
294+ doc = UTF8.toTextLazy . fromEntry $ entry
295+ fp = T.pack $ eRelativePath entry
289
296
290findElementE :: PandocMonad m => QName -> Element -> m Element
297findElementE :: PandocMonad m => QName -> Element -> m Element
298+findElementE e x =
291−findElementE e x = mkE ("Unable to find element: " ++ show e) $ findElement e x
299+ mkE ("Unable to find element: " <> tshow e) $ findElement e x
292
300
293−mkE :: PandocMonad m => String -> Maybe a -> m a
301+mkE :: PandocMonad m => Text -> Maybe a -> m a
294−mkE s = maybe (throwError . PandocParseError $ T.pack s) return
302+mkE s = maybe (throwError . PandocParseError $ s) return
src/Text/Pandoc/Readers/FB2.hs+57−51
@@ -25,13 +25,13 @@ TODO:
25module Text.Pandoc.Readers.FB2 ( readFB2 ) where
25module Text.Pandoc.Readers.FB2 ( readFB2 ) where
26import Control.Monad.Except (throwError)
26import Control.Monad.Except (throwError)
27import Control.Monad.State.Strict
27import Control.Monad.State.Strict
28−import Data.ByteString.Lazy.Char8 ( pack )
29import Data.ByteString.Base64.Lazy
28import Data.ByteString.Base64.Lazy
30import Data.Functor
29import Data.Functor
31import Data.List (intersperse)
30import Data.List (intersperse)
32import qualified Data.Map as M
31import qualified Data.Map as M
33import Data.Text (Text)
32import Data.Text (Text)
34import qualified Data.Text as T
33import qualified Data.Text as T
34+import qualified Data.Text.Lazy as TL
35import Data.Default
35import Data.Default
36import Data.Maybe
36import Data.Maybe
37import Text.HTML.TagSoup.Entity (lookupEntity)
37import Text.HTML.TagSoup.Entity (lookupEntity)
@@ -40,8 +40,9 @@ import Text.Pandoc.Class.PandocMonad (PandocMonad, insertMedia, report)
40import Text.Pandoc.Error
40import Text.Pandoc.Error
41import Text.Pandoc.Logging
41import Text.Pandoc.Logging
42import Text.Pandoc.Options
42import Text.Pandoc.Options
43−import Text.Pandoc.Shared (crFilter)
43+import Text.Pandoc.XML.Light
44−import Text.XML.Light
44+import qualified Text.Pandoc.UTF8 as UTF8
45+import Text.Pandoc.Sources (ToSources(..), sourcesToText)
45
46
46type FB2 m = StateT FB2State m
47type FB2 m = StateT FB2State m
47
48
@@ -62,12 +63,15 @@ instance HasMeta FB2State where
62 setMeta field v s = s {fb2Meta = setMeta field v (fb2Meta s)}
63 setMeta field v s = s {fb2Meta = setMeta field v (fb2Meta s)}
63 deleteMeta field s = s {fb2Meta = deleteMeta field (fb2Meta s)}
64 deleteMeta field s = s {fb2Meta = deleteMeta field (fb2Meta s)}
64
65
65−readFB2 :: PandocMonad m => ReaderOptions -> Text -> m Pandoc
66+readFB2 :: (PandocMonad m, ToSources a)
67+ => ReaderOptions
68+ -> a
69+ -> m Pandoc
66readFB2 _ inp =
70readFB2 _ inp =
67− case parseXMLDoc $ crFilter inp of
71+ case parseXMLElement $ TL.fromStrict $ sourcesToText $ toSources inp of
68− Nothing -> throwError $ PandocParseError "Not an XML document"
72+ Left msg -> throwError $ PandocXMLError "" msg
69− Just e -> do
73+ Right el -> do
70− (bs, st) <- runStateT (parseRootElement e) def
74+ (bs, st) <- runStateT (parseRootElement el) def
71 let authors = if null $ fb2Authors st
75 let authors = if null $ fb2Authors st
72 then id
76 then id
73 else setMeta "author" (map text $ reverse $ fb2Authors st)
77 else setMeta "author" (map text $ reverse $ fb2Authors st)
@@ -83,12 +87,12 @@ removeHash t = case T.uncons t of
83 Just ('#', xs) -> xs
87 Just ('#', xs) -> xs
84 _ -> t
88 _ -> t
85
89
86−convertEntity :: String -> Text
90+convertEntity :: Text -> Text
87−convertEntity e = maybe (T.toUpper $ T.pack e) T.pack $ lookupEntity e
91+convertEntity e = maybe (T.toUpper e) T.pack $ lookupEntity (T.unpack e)
88
92
89parseInline :: PandocMonad m => Content -> FB2 m Inlines
93parseInline :: PandocMonad m => Content -> FB2 m Inlines
90parseInline (Elem e) =
94parseInline (Elem e) =
91− case T.pack $ qName $ elName e of
95+ case qName $ elName e of
92 "strong" -> strong <$> parseStyleType e
96 "strong" -> strong <$> parseStyleType e
93 "emphasis" -> emph <$> parseStyleType e
97 "emphasis" -> emph <$> parseStyleType e
94 "style" -> parseNamedStyle e
98 "style" -> parseNamedStyle e
@@ -96,12 +100,12 @@ parseInline (Elem e) =
96 "strikethrough" -> strikeout <$> parseStyleType e
100 "strikethrough" -> strikeout <$> parseStyleType e
97 "sub" -> subscript <$> parseStyleType e
101 "sub" -> subscript <$> parseStyleType e
98 "sup" -> superscript <$> parseStyleType e
102 "sup" -> superscript <$> parseStyleType e
99− "code" -> pure $ code $ T.pack $ strContent e
103+ "code" -> pure $ code $ strContent e
100 "image" -> parseInlineImageElement e
104 "image" -> parseInlineImageElement e
101 name -> do
105 name -> do
102 report $ IgnoredElement name
106 report $ IgnoredElement name
103 pure mempty
107 pure mempty
104−parseInline (Text x) = pure $ text $ T.pack $ cdData x
108+parseInline (Text x) = pure $ text $ cdData x
105parseInline (CRef r) = pure $ str $ convertEntity r
109parseInline (CRef r) = pure $ str $ convertEntity r
106
110
107parseSubtitle :: PandocMonad m => Element -> FB2 m Blocks
111parseSubtitle :: PandocMonad m => Element -> FB2 m Blocks
@@ -111,7 +115,7 @@ parseSubtitle e = headerWith ("", ["unnumbered"], []) <$> gets fb2SectionLevel <
111
115
112parseRootElement :: PandocMonad m => Element -> FB2 m Blocks
116parseRootElement :: PandocMonad m => Element -> FB2 m Blocks
113parseRootElement e =
117parseRootElement e =
114− case T.pack $ qName $ elName e of
118+ case qName $ elName e of
115 "FictionBook" -> do
119 "FictionBook" -> do
116 -- Parse notes before parsing the rest of the content.
120 -- Parse notes before parsing the rest of the content.
117 case filterChild isNotesBody e of
121 case filterChild isNotesBody e of
@@ -144,7 +148,7 @@ parseNote e =
144 Just sectionId -> do
148 Just sectionId -> do
145 content <- mconcat <$> mapM parseSectionChild (dropTitle $ elChildren e)
149 content <- mconcat <$> mapM parseSectionChild (dropTitle $ elChildren e)
146 oldNotes <- gets fb2Notes
150 oldNotes <- gets fb2Notes
147− modify $ \s -> s { fb2Notes = M.insert ("#" <> T.pack sectionId) content oldNotes }
151+ modify $ \s -> s { fb2Notes = M.insert ("#" <> sectionId) content oldNotes }
148 pure ()
152 pure ()
149 where
153 where
150 isTitle x = qName (elName x) == "title"
154 isTitle x = qName (elName x) == "title"
@@ -156,7 +160,7 @@ parseNote e =
156-- | Parse a child of @\<FictionBook>@ element.
160-- | Parse a child of @\<FictionBook>@ element.
157parseFictionBookChild :: PandocMonad m => Element -> FB2 m Blocks
161parseFictionBookChild :: PandocMonad m => Element -> FB2 m Blocks
158parseFictionBookChild e =
162parseFictionBookChild e =
159− case T.pack $ qName $ elName e of
163+ case qName $ elName e of
160 "stylesheet" -> pure mempty -- stylesheet is ignored
164 "stylesheet" -> pure mempty -- stylesheet is ignored
161 "description" -> mempty <$ mapM_ parseDescriptionChild (elChildren e)
165 "description" -> mempty <$ mapM_ parseDescriptionChild (elChildren e)
162 "body" -> if isNotesBody e
166 "body" -> if isNotesBody e
@@ -168,7 +172,7 @@ parseFictionBookChild e =
168-- | Parse a child of @\<description>@ element.
172-- | Parse a child of @\<description>@ element.
169parseDescriptionChild :: PandocMonad m => Element -> FB2 m ()
173parseDescriptionChild :: PandocMonad m => Element -> FB2 m ()
170parseDescriptionChild e =
174parseDescriptionChild e =
171− case T.pack $ qName $ elName e of
175+ case qName $ elName e of
172 "title-info" -> mapM_ parseTitleInfoChild (elChildren e)
176 "title-info" -> mapM_ parseTitleInfoChild (elChildren e)
173 "src-title-info" -> pure () -- ignore
177 "src-title-info" -> pure () -- ignore
174 "document-info" -> pure ()
178 "document-info" -> pure ()
@@ -182,7 +186,7 @@ parseDescriptionChild e =
182-- | Parse a child of @\<body>@ element.
186-- | Parse a child of @\<body>@ element.
183parseBodyChild :: PandocMonad m => Element -> FB2 m Blocks
187parseBodyChild :: PandocMonad m => Element -> FB2 m Blocks
184parseBodyChild e =
188parseBodyChild e =
185− case T.pack $ qName $ elName e of
189+ case qName $ elName e of
186 "image" -> parseImageElement e
190 "image" -> parseImageElement e
187 "title" -> header <$> gets fb2SectionLevel <*> parseTitleType (elContent e)
191 "title" -> header <$> gets fb2SectionLevel <*> parseTitleType (elContent e)
188 "epigraph" -> parseEpigraph e
192 "epigraph" -> parseEpigraph e
@@ -196,7 +200,10 @@ parseBinaryElement e =
196 (Nothing, _) -> report $ IgnoredElement "binary without id attribute"
200 (Nothing, _) -> report $ IgnoredElement "binary without id attribute"
197 (Just _, Nothing) ->
201 (Just _, Nothing) ->
198 report $ IgnoredElement "binary without content-type attribute"
202 report $ IgnoredElement "binary without content-type attribute"
199− (Just filename, contentType) -> insertMedia filename (T.pack <$> contentType) (decodeLenient (pack (strContent e)))
203+ (Just filename, contentType) ->
204+ insertMedia (T.unpack filename) contentType
205+ (decodeLenient
206+ (UTF8.fromTextLazy . TL.fromStrict . strContent $ e))
200
207
201-- * Type parsers
208-- * Type parsers
202
209
@@ -206,13 +213,13 @@ parseAuthor e = T.unwords . catMaybes <$> mapM parseAuthorChild (elChildren e)
206
213
207parseAuthorChild :: PandocMonad m => Element -> FB2 m (Maybe Text)
214parseAuthorChild :: PandocMonad m => Element -> FB2 m (Maybe Text)
208parseAuthorChild e =
215parseAuthorChild e =
209− case T.pack $ qName $ elName e of
216+ case qName $ elName e of
210− "first-name" -> pure $ Just $ T.pack $ strContent e
217+ "first-name" -> pure $ Just $ strContent e
211− "middle-name" -> pure $ Just $ T.pack $ strContent e
218+ "middle-name" -> pure $ Just $ strContent e
212− "last-name" -> pure $ Just $ T.pack $ strContent e
219+ "last-name" -> pure $ Just $ strContent e
213− "nickname" -> pure $ Just $ T.pack $ strContent e
220+ "nickname" -> pure $ Just $ strContent e
214− "home-page" -> pure $ Just $ T.pack $ strContent e
221+ "home-page" -> pure $ Just $ strContent e
215− "email" -> pure $ Just $ T.pack $ strContent e
222+ "email" -> pure $ Just $ strContent e
216 name -> do
223 name -> do
217 report $ IgnoredElement $ name <> " in author"
224 report $ IgnoredElement $ name <> " in author"
218 pure Nothing
225 pure Nothing
@@ -236,13 +243,13 @@ parseTitleContent _ = pure Nothing
236parseImageElement :: PandocMonad m => Element -> FB2 m Blocks
243parseImageElement :: PandocMonad m => Element -> FB2 m Blocks
237parseImageElement e =
244parseImageElement e =
238 case href of
245 case href of
239− Just src -> pure $ para $ imageWith (imgId, [], []) (removeHash $ T.pack src) title alt
246+ Just src -> pure $ para $ imageWith (imgId, [], []) (removeHash src) title alt
240 Nothing -> do
247 Nothing -> do
241 report $ IgnoredElement " image without href"
248 report $ IgnoredElement " image without href"
242 pure mempty
249 pure mempty
243− where alt = maybe mempty (str . T.pack) $ findAttr (unqual "alt") e
250+ where alt = maybe mempty str $ findAttr (unqual "alt") e
244− title = maybe "" T.pack $ findAttr (unqual "title") e
251+ title = fromMaybe "" $ findAttr (unqual "title") e
245− imgId = maybe "" T.pack $ findAttr (unqual "id") e
252+ imgId = fromMaybe "" $ findAttr (unqual "id") e
246 href = findAttr (QName "href" (Just "http://www.w3.org/1999/xlink") Nothing) e
253 href = findAttr (QName "href" (Just "http://www.w3.org/1999/xlink") Nothing) e
247
254
248-- | Parse @pType@
255-- | Parse @pType@
@@ -256,7 +263,7 @@ parseCite e = blockQuote . mconcat <$> mapM parseCiteChild (elChildren e)
256-- | Parse @citeType@ child
263-- | Parse @citeType@ child
257parseCiteChild :: PandocMonad m => Element -> FB2 m Blocks
264parseCiteChild :: PandocMonad m => Element -> FB2 m Blocks
258parseCiteChild e =
265parseCiteChild e =
259− case T.pack $ qName $ elName e of
266+ case qName $ elName e of
260 "p" -> para <$> parsePType e
267 "p" -> para <$> parsePType e
261 "poem" -> parsePoem e
268 "poem" -> parsePoem e
262 "empty-line" -> pure horizontalRule
269 "empty-line" -> pure horizontalRule
@@ -271,13 +278,13 @@ parsePoem e = mconcat <$> mapM parsePoemChild (elChildren e)
271
278
272parsePoemChild :: PandocMonad m => Element -> FB2 m Blocks
279parsePoemChild :: PandocMonad m => Element -> FB2 m Blocks
273parsePoemChild e =
280parsePoemChild e =
274− case T.pack $ qName $ elName e of
281+ case qName $ elName e of
275 "title" -> parseTitle e
282 "title" -> parseTitle e
276 "subtitle" -> parseSubtitle e
283 "subtitle" -> parseSubtitle e
277 "epigraph" -> parseEpigraph e
284 "epigraph" -> parseEpigraph e
278 "stanza" -> parseStanza e
285 "stanza" -> parseStanza e
279 "text-author" -> para <$> parsePType e
286 "text-author" -> para <$> parsePType e
280− "date" -> pure $ para $ text $ T.pack $ strContent e
287+ "date" -> pure $ para $ text $ strContent e
281 name -> report (UnexpectedXmlElement name "poem") $> mempty
288 name -> report (UnexpectedXmlElement name "poem") $> mempty
282
289
283parseStanza :: PandocMonad m => Element -> FB2 m Blocks
290parseStanza :: PandocMonad m => Element -> FB2 m Blocks
@@ -290,7 +297,7 @@ joinLineBlocks [] = []
290
297
291parseStanzaChild :: PandocMonad m => Element -> FB2 m Blocks
298parseStanzaChild :: PandocMonad m => Element -> FB2 m Blocks
292parseStanzaChild e =
299parseStanzaChild e =
293− case T.pack $ qName $ elName e of
300+ case qName $ elName e of
294 "title" -> parseTitle e
301 "title" -> parseTitle e
295 "subtitle" -> parseSubtitle e
302 "subtitle" -> parseSubtitle e
296 "v" -> lineBlock . (:[]) <$> parsePType e
303 "v" -> lineBlock . (:[]) <$> parsePType e
@@ -300,11 +307,11 @@ parseStanzaChild e =
300parseEpigraph :: PandocMonad m => Element -> FB2 m Blocks
307parseEpigraph :: PandocMonad m => Element -> FB2 m Blocks
301parseEpigraph e =
308parseEpigraph e =
302 divWith (divId, ["epigraph"], []) . mconcat <$> mapM parseEpigraphChild (elChildren e)
309 divWith (divId, ["epigraph"], []) . mconcat <$> mapM parseEpigraphChild (elChildren e)
303− where divId = maybe "" T.pack $ findAttr (unqual "id") e
310+ where divId = fromMaybe "" $ findAttr (unqual "id") e
304
311
305parseEpigraphChild :: PandocMonad m => Element -> FB2 m Blocks
312parseEpigraphChild :: PandocMonad m => Element -> FB2 m Blocks
306parseEpigraphChild e =
313parseEpigraphChild e =
307− case T.pack $ qName $ elName e of
314+ case qName $ elName e of
308 "p" -> para <$> parsePType e
315 "p" -> para <$> parsePType e
309 "poem" -> parsePoem e
316 "poem" -> parsePoem e
310 "cite" -> parseCite e
317 "cite" -> parseCite e
@@ -318,7 +325,7 @@ parseAnnotation e = mconcat <$> mapM parseAnnotationChild (elChildren e)
318
325
319parseAnnotationChild :: PandocMonad m => Element -> FB2 m Blocks
326parseAnnotationChild :: PandocMonad m => Element -> FB2 m Blocks
320parseAnnotationChild e =
327parseAnnotationChild e =
321− case T.pack $ qName $ elName e of
328+ case qName $ elName e of
322 "p" -> para <$> parsePType e
329 "p" -> para <$> parsePType e
323 "poem" -> parsePoem e
330 "poem" -> parsePoem e
324 "cite" -> parseCite e
331 "cite" -> parseCite e
@@ -332,14 +339,14 @@ parseSection :: PandocMonad m => Element -> FB2 m Blocks
332parseSection e = do
339parseSection e = do
333 n <- gets fb2SectionLevel
340 n <- gets fb2SectionLevel
334 modify $ \st -> st{ fb2SectionLevel = n + 1 }
341 modify $ \st -> st{ fb2SectionLevel = n + 1 }
335− let sectionId = maybe "" T.pack $ findAttr (unqual "id") e
342+ let sectionId = fromMaybe "" $ findAttr (unqual "id") e
336 bs <- divWith (sectionId, ["section"], []) . mconcat <$> mapM parseSectionChild (elChildren e)
343 bs <- divWith (sectionId, ["section"], []) . mconcat <$> mapM parseSectionChild (elChildren e)
337 modify $ \st -> st{ fb2SectionLevel = n }
344 modify $ \st -> st{ fb2SectionLevel = n }
338 pure bs
345 pure bs
339
346
340parseSectionChild :: PandocMonad m => Element -> FB2 m Blocks
347parseSectionChild :: PandocMonad m => Element -> FB2 m Blocks
341parseSectionChild e =
348parseSectionChild e =
342− case T.pack $ qName $ elName e of
349+ case qName $ elName e of
343 "title" -> parseBodyChild e
350 "title" -> parseBodyChild e
344 "epigraph" -> parseEpigraph e
351 "epigraph" -> parseEpigraph e
345 "image" -> parseImageElement e
352 "image" -> parseImageElement e
@@ -361,16 +368,16 @@ parseStyleType e = mconcat <$> mapM parseInline (elContent e)
361parseNamedStyle :: PandocMonad m => Element -> FB2 m Inlines
368parseNamedStyle :: PandocMonad m => Element -> FB2 m Inlines
362parseNamedStyle e = do
369parseNamedStyle e = do
363 content <- mconcat <$> mapM parseNamedStyleChild (elContent e)
370 content <- mconcat <$> mapM parseNamedStyleChild (elContent e)
364− let lang = maybeToList $ ("lang",) . T.pack <$> findAttr (QName "lang" Nothing (Just "xml")) e
371+ let lang = maybeToList $ ("lang",) <$> findAttr (QName "lang" Nothing (Just "xml")) e
365 case findAttr (unqual "name") e of
372 case findAttr (unqual "name") e of
366− Just name -> pure $ spanWith ("", [T.pack name], lang) content
373+ Just name -> pure $ spanWith ("", [name], lang) content
367 Nothing -> do
374 Nothing -> do
368 report $ IgnoredElement "link without required name"
375 report $ IgnoredElement "link without required name"
369 pure mempty
376 pure mempty
370
377
371parseNamedStyleChild :: PandocMonad m => Content -> FB2 m Inlines
378parseNamedStyleChild :: PandocMonad m => Content -> FB2 m Inlines
372parseNamedStyleChild (Elem e) =
379parseNamedStyleChild (Elem e) =
373− case T.pack $ qName (elName e) of
380+ case qName (elName e) of
374 "strong" -> strong <$> parseStyleType e
381 "strong" -> strong <$> parseStyleType e
375 "emphasis" -> emph <$> parseStyleType e
382 "emphasis" -> emph <$> parseStyleType e
376 "style" -> parseNamedStyle e
383 "style" -> parseNamedStyle e
@@ -378,7 +385,7 @@ parseNamedStyleChild (Elem e) =
378 "strikethrough" -> strikeout <$> parseStyleType e
385 "strikethrough" -> strikeout <$> parseStyleType e
379 "sub" -> subscript <$> parseStyleType e
386 "sub" -> subscript <$> parseStyleType e
380 "sup" -> superscript <$> parseStyleType e
387 "sup" -> superscript <$> parseStyleType e
381− "code" -> pure $ code $ T.pack $ strContent e
388+ "code" -> pure $ code $ strContent e
382 "image" -> parseInlineImageElement e
389 "image" -> parseInlineImageElement e
383 name -> do
390 name -> do
384 report $ IgnoredElement $ name <> " in style"
391 report $ IgnoredElement $ name <> " in style"
@@ -390,7 +397,7 @@ parseLinkType :: PandocMonad m => Element -> FB2 m Inlines
390parseLinkType e = do
397parseLinkType e = do
391 content <- mconcat <$> mapM parseStyleLinkType (elContent e)
398 content <- mconcat <$> mapM parseStyleLinkType (elContent e)
392 notes <- gets fb2Notes
399 notes <- gets fb2Notes
393− case T.pack <$> findAttr (QName "href" (Just "http://www.w3.org/1999/xlink") Nothing) e of
400+ case findAttr (QName "href" (Just "http://www.w3.org/1999/xlink") Nothing) e of
394 Just href -> case findAttr (QName "type" Nothing Nothing) e of
401 Just href -> case findAttr (QName "type" Nothing Nothing) e of
395 Just "note" -> case M.lookup href notes of
402 Just "note" -> case M.lookup href notes of
396 Nothing -> pure $ link href "" content
403 Nothing -> pure $ link href "" content
@@ -417,15 +424,14 @@ parseTable _ = pure mempty -- TODO: tables are not supported yet
417-- | Parse @title-infoType@
424-- | Parse @title-infoType@
418parseTitleInfoChild :: PandocMonad m => Element -> FB2 m ()
425parseTitleInfoChild :: PandocMonad m => Element -> FB2 m ()
419parseTitleInfoChild e =
426parseTitleInfoChild e =
420− case T.pack $ qName (elName e) of
427+ case qName (elName e) of
421 "genre" -> pure ()
428 "genre" -> pure ()
422 "author" -> parseAuthor e >>= \author -> modify (\st -> st {fb2Authors = author:fb2Authors st})
429 "author" -> parseAuthor e >>= \author -> modify (\st -> st {fb2Authors = author:fb2Authors st})
423− "book-title" -> modify (setMeta "title" (text $ T.pack $ strContent e))
430+ "book-title" -> modify (setMeta "title" (text $ strContent e))
424 "annotation" -> parseAnnotation e >>= modify . setMeta "abstract"
431 "annotation" -> parseAnnotation e >>= modify . setMeta "abstract"
425 "keywords" -> modify (setMeta "keywords" (map (MetaString . trim) $ T.splitOn ","
432 "keywords" -> modify (setMeta "keywords" (map (MetaString . trim) $ T.splitOn ","
426− $ T.pack
427 $ strContent e))
433 $ strContent e))
428− "date" -> modify (setMeta "date" (text $ T.pack $ strContent e))
434+ "date" -> modify (setMeta "date" (text $ strContent e))
429 "coverpage" -> parseCoverPage e
435 "coverpage" -> parseCoverPage e
430 "lang" -> pure ()
436 "lang" -> pure ()
431 "src-lang" -> pure ()
437 "src-lang" -> pure ()
@@ -439,7 +445,7 @@ parseCoverPage e =
439 Just img -> case href of
445 Just img -> case href of
440 Just src -> modify (setMeta "cover-image" (MetaString $ removeHash src))
446 Just src -> modify (setMeta "cover-image" (MetaString $ removeHash src))
441 Nothing -> pure ()
447 Nothing -> pure ()
442− where href = T.pack <$> findAttr (QName "href" (Just "http://www.w3.org/1999/xlink") Nothing) img
448+ where href = findAttr (QName "href" (Just "http://www.w3.org/1999/xlink") Nothing) img
443 Nothing -> pure ()
449 Nothing -> pure ()
444
450
445-- | Parse @inlineImageType@ element
451-- | Parse @inlineImageType@ element
@@ -452,5 +458,5 @@ parseInlineImageElement e =
452 Nothing -> do
458 Nothing -> do
453 report $ IgnoredElement "inline image without href"
459 report $ IgnoredElement "inline image without href"
454 pure mempty
460 pure mempty
455− where alt = maybe mempty (str . T.pack) $ findAttr (unqual "alt") e
461+ where alt = maybe mempty str $ findAttr (unqual "alt") e
456− href = T.pack <$> findAttr (QName "href" (Just "http://www.w3.org/1999/xlink") Nothing) e
462+ href = findAttr (QName "href" (Just "http://www.w3.org/1999/xlink") Nothing) e
src/Text/Pandoc/Readers/HTML.hs+199−136
@@ -19,20 +19,20 @@ module Text.Pandoc.Readers.HTML ( readHtml
19 , htmlInBalanced
19 , htmlInBalanced
20 , isInlineTag
20 , isInlineTag
21 , isBlockTag
21 , isBlockTag
22− , NamedTag(..)
23 , isTextTag
22 , isTextTag
24 , isCommentTag
23 , isCommentTag
25 ) where
24 ) where
26
25
27import Control.Applicative ((<|>))
26import Control.Applicative ((<|>))
28import Control.Monad (guard, msum, mzero, unless, void)
27import Control.Monad (guard, msum, mzero, unless, void)
29−import Control.Monad.Except (throwError)
28+import Control.Monad.Except (throwError, catchError)
30import Control.Monad.Reader (ask, asks, lift, local, runReaderT)
29import Control.Monad.Reader (ask, asks, lift, local, runReaderT)
31import Data.ByteString.Base64 (encode)
30import Data.ByteString.Base64 (encode)
32import Data.Char (isAlphaNum, isLetter)
31import Data.Char (isAlphaNum, isLetter)
33import Data.Default (Default (..), def)
32import Data.Default (Default (..), def)
34import Data.Foldable (for_)
33import Data.Foldable (for_)
35import Data.List.Split (splitWhen)
34import Data.List.Split (splitWhen)
35+import Data.List (foldl')
36import qualified Data.Map as M
36import qualified Data.Map as M
37import Data.Maybe (fromMaybe, isJust, isNothing)
37import Data.Maybe (fromMaybe, isJust, isNothing)
38import Data.Monoid (First (..))
38import Data.Monoid (First (..))
@@ -62,21 +62,21 @@ import Text.Pandoc.Options (
62 extensionEnabled)
62 extensionEnabled)
63import Text.Pandoc.Parsing hiding ((<|>))
63import Text.Pandoc.Parsing hiding ((<|>))
64import Text.Pandoc.Shared (
64import Text.Pandoc.Shared (
65− addMetaField, blocksToInlines', crFilter, escapeURI, extractSpaces,
65+ addMetaField, blocksToInlines', escapeURI, extractSpaces,
66 htmlSpanLikeElements, renderTags', safeRead, tshow)
66 htmlSpanLikeElements, renderTags', safeRead, tshow)
67import Text.Pandoc.Walk
67import Text.Pandoc.Walk
68import Text.Parsec.Error
68import Text.Parsec.Error
69import Text.TeXMath (readMathML, writeTeX)
69import Text.TeXMath (readMathML, writeTeX)
70
70
71-- | Convert HTML-formatted string to 'Pandoc' document.
71-- | Convert HTML-formatted string to 'Pandoc' document.
72−readHtml :: PandocMonad m
72+readHtml :: (PandocMonad m, ToSources a)
73 => ReaderOptions -- ^ Reader options
73 => ReaderOptions -- ^ Reader options
74− -> Text -- ^ String to parse (assumes @'\n'@ line endings)
74+ -> a -- ^ Input to parse
75 -> m Pandoc
75 -> m Pandoc
76readHtml opts inp = do
76readHtml opts inp = do
77 let tags = stripPrefixes $ canonicalizeTags $
77 let tags = stripPrefixes $ canonicalizeTags $
78 parseTagsOptions parseOptions{ optTagPosition = True }
78 parseTagsOptions parseOptions{ optTagPosition = True }
79− (crFilter inp)
79+ (sourcesToText $ toSources inp)
80 parseDoc = do
80 parseDoc = do
81 blocks <- fixPlains False . mconcat <$> manyTill block eof
81 blocks <- fixPlains False . mconcat <$> manyTill block eof
82 meta <- stateMeta . parserState <$> getState
82 meta <- stateMeta . parserState <$> getState
@@ -158,32 +158,65 @@ pHead = pInTags "head" $ pTitle <|> pMetaTag <|> pBaseTag <|> (mempty <$ pAny)
158 return mempty
158 return mempty
159
159
160block :: PandocMonad m => TagParser m Blocks
160block :: PandocMonad m => TagParser m Blocks
161−block = do
161+block = ((do
162− res <- choice
162+ tag <- lookAhead (pSatisfy isBlockTag)
163− [ eSection
163+ exts <- getOption readerExtensions
164− , eSwitch B.para block
164+ case tag of
165+ TagOpen name attr ->
166+ let type' = fromMaybe "" $
167+ lookup "type" attr <|> lookup "epub:type" attr
168+ epubExts = extensionEnabled Ext_epub_html_exts exts
169+ in
170+ case name of
171+ _ | name `elem` sectioningContent
172+ , epubExts
173+ , "chapter" `T.isInfixOf` type'
174+ -> eSection
175+ _ | epubExts
176+ , type' `elem` ["footnote", "rearnote"]
165− , mempty <$ eFootnote
177+ -> mempty <$ eFootnote
178+ _ | epubExts
179+ , type' == "toc"
166− , mempty <$ eTOC
180+ -> mempty <$ eTOC
181+ _ | "titlepage" `T.isInfixOf` type'
182+ , name `elem` ("section" : groupingContent)
167− , mempty <$ eTitlePage
183+ -> mempty <$ eTitlePage
168− , pPara
184+ "p" -> pPara
169− , pHeader
185+ "h1" -> pHeader
186+ "h2" -> pHeader
187+ "h3" -> pHeader
188+ "h4" -> pHeader
189+ "h5" -> pHeader
190+ "h6" -> pHeader
170− , pBlockQuote
191+ "blockquote" -> pBlockQuote
171− , pCodeBlock
192+ "pre" -> pCodeBlock
172− , pList
193+ "ul" -> pBulletList
173− , pHrule
194+ "ol" -> pOrderedList
195+ "dl" -> pDefinitionList
174− , pTable block
196+ "table" -> pTable block
197+ "hr" -> pHrule
175− , pHtml
198+ "html" -> pHtml
176− , pHead
199+ "head" -> pHead
177− , pBody
200+ "body" -> pBody
201+ "div"
202+ | extensionEnabled Ext_line_blocks exts
203+ , Just "line-block" <- lookup "class" attr
178− , pLineBlock
204+ -> pLineBlock
205+ | otherwise
179− , pDiv
206+ -> pDiv
180− , pPlain
207+ "section" -> pDiv
208+ "header" -> pDiv
209+ "main" -> pDiv
181− , pFigure
210+ "figure" -> pFigure
182− , pIframe
211+ "iframe" -> pIframe
183− , pRawHtmlBlock
212+ "style" -> pRawHtmlBlock
184− ]
213+ "textarea" -> pRawHtmlBlock
214+ "switch"
215+ | epubExts
216+ -> eSwitch B.para block
217+ _ -> mzero
218+ _ -> mzero) <|> pPlain <|> pRawHtmlBlock) >>= \res ->
185− trace (T.take 60 $ tshow $ B.toList res)
219+ res <$ trace (T.take 60 $ tshow $ B.toList res)
186− return res
187
220
188namespaces :: PandocMonad m => [(Text, TagParser m Inlines)]
221namespaces :: PandocMonad m => [(Text, TagParser m Inlines)]
189namespaces = [(mathMLNamespace, pMath True)]
222namespaces = [(mathMLNamespace, pMath True)]
@@ -256,9 +289,6 @@ eTOC = try $ do
256 guard $ (lookup "type" attr <|> lookup "epub:type" attr) == Just "toc"
289 guard $ (lookup "type" attr <|> lookup "epub:type" attr) == Just "toc"
257 void (pInTags tag block)
290 void (pInTags tag block)
258
291
259−pList :: PandocMonad m => TagParser m Blocks
260−pList = pBulletList <|> pOrderedList <|> pDefinitionList
261−
262pBulletList :: PandocMonad m => TagParser m Blocks
292pBulletList :: PandocMonad m => TagParser m Blocks
263pBulletList = try $ do
293pBulletList = try $ do
264 pSatisfy (matchTagOpen "ul" [])
294 pSatisfy (matchTagOpen "ul" [])
@@ -332,7 +362,10 @@ pDefListItem = try $ do
332 terms <- many1 (try $ skipMany nonItem >> pInTags "dt" inline)
362 terms <- many1 (try $ skipMany nonItem >> pInTags "dt" inline)
333 defs <- many1 (try $ skipMany nonItem >> pInTags "dd" block)
363 defs <- many1 (try $ skipMany nonItem >> pInTags "dd" block)
334 skipMany nonItem
364 skipMany nonItem
335− let term = foldl1 (\x y -> x <> B.linebreak <> y) $ map trimInlines terms
365+ let term = foldl' (\x y -> if null x
366+ then trimInlines y
367+ else x <> B.linebreak <> trimInlines y)
368+ mempty terms
336 return (term, map (fixPlains True) defs)
369 return (term, map (fixPlains True) defs)
337
370
338fixPlains :: Bool -> Blocks -> Blocks
371fixPlains :: Bool -> Blocks -> Blocks
@@ -369,13 +402,16 @@ pLineBlock = try $ do
369 B.toList ils
402 B.toList ils
370 return $ B.lineBlock lns
403 return $ B.lineBlock lns
371
404
405+isDivLike :: Text -> Bool
406+isDivLike "div" = True
407+isDivLike "section" = True
408+isDivLike "header" = True
409+isDivLike "main" = True
410+isDivLike _ = False
411+
372pDiv :: PandocMonad m => TagParser m Blocks
412pDiv :: PandocMonad m => TagParser m Blocks
373pDiv = try $ do
413pDiv = try $ do
374 guardEnabled Ext_native_divs
414 guardEnabled Ext_native_divs
375− let isDivLike "div" = True
376− isDivLike "section" = True
377− isDivLike "main" = True
378− isDivLike _ = False
379 TagOpen tag attr' <- lookAhead $ pSatisfy $ tagOpen isDivLike (const True)
415 TagOpen tag attr' <- lookAhead $ pSatisfy $ tagOpen isDivLike (const True)
380 let (ident, classes, kvs) = toAttr attr'
416 let (ident, classes, kvs) = toAttr attr'
381 contents <- pInTags tag block
417 contents <- pInTags tag block
@@ -393,11 +429,17 @@ pIframe = try $ do
393 tag <- pSatisfy (tagOpen (=="iframe") (isJust . lookup "src"))
429 tag <- pSatisfy (tagOpen (=="iframe") (isJust . lookup "src"))
394 pCloses "iframe" <|> eof
430 pCloses "iframe" <|> eof
395 url <- canonicalizeUrl $ fromAttrib "src" tag
431 url <- canonicalizeUrl $ fromAttrib "src" tag
432+ if T.null url
433+ then ignore $ renderTags' [tag, TagClose "iframe"]
434+ else catchError
396− (bs, _) <- openURL url
435+ (do (bs, _) <- openURL url
397− let inp = UTF8.toText bs
436+ let inp = UTF8.toText bs
398− opts <- readerOpts <$> getState
437+ opts <- readerOpts <$> getState
399− Pandoc _ contents <- readHtml opts inp
438+ Pandoc _ contents <- readHtml opts inp
400− return $ B.divWith ("",["iframe"],[]) $ B.fromList contents
439+ return $ B.divWith ("",["iframe"],[]) $ B.fromList contents)
440+ (\e -> do
441+ logMessage $ CouldNotFetchResource url (renderError e)
442+ ignore $ renderTags' [tag, TagClose "iframe"])
401
443
402pRawHtmlBlock :: PandocMonad m => TagParser m Blocks
444pRawHtmlBlock :: PandocMonad m => TagParser m Blocks
403pRawHtmlBlock = do
445pRawHtmlBlock = do
@@ -459,17 +501,13 @@ pHeader = try $ do
459 tagOpen (`elem` ["h1","h2","h3","h4","h5","h6"])
501 tagOpen (`elem` ["h1","h2","h3","h4","h5","h6"])
460 (const True)
502 (const True)
461 let attr = toStringAttr attr'
503 let attr = toStringAttr attr'
462− let bodyTitle = TagOpen tagtype attr' ~== TagOpen ("h1" :: Text)
463− [("class","title")]
464 level <- headerLevel tagtype
504 level <- headerLevel tagtype
465 contents <- trimInlines . mconcat <$> manyTill inline (pCloses tagtype <|> eof)
505 contents <- trimInlines . mconcat <$> manyTill inline (pCloses tagtype <|> eof)
466 let ident = fromMaybe "" $ lookup "id" attr
506 let ident = fromMaybe "" $ lookup "id" attr
467 let classes = maybe [] T.words $ lookup "class" attr
507 let classes = maybe [] T.words $ lookup "class" attr
468 let keyvals = [(k,v) | (k,v) <- attr, k /= "class", k /= "id"]
508 let keyvals = [(k,v) | (k,v) <- attr, k /= "class", k /= "id"]
469 attr'' <- registerHeader (ident, classes, keyvals) contents
509 attr'' <- registerHeader (ident, classes, keyvals) contents
470− return $ if bodyTitle
471− then mempty -- skip a representation of the title in the body
472− else B.headerWith attr'' level contents
510+ return $ B.headerWith attr'' level contents
473
511
474pHrule :: PandocMonad m => TagParser m Blocks
512pHrule :: PandocMonad m => TagParser m Blocks
475pHrule = do
513pHrule = do
@@ -519,7 +557,18 @@ pFigure = try $ do
519pCodeBlock :: PandocMonad m => TagParser m Blocks
557pCodeBlock :: PandocMonad m => TagParser m Blocks
520pCodeBlock = try $ do
558pCodeBlock = try $ do
521 TagOpen _ attr' <- pSatisfy (matchTagOpen "pre" [])
559 TagOpen _ attr' <- pSatisfy (matchTagOpen "pre" [])
522− let attr = toAttr attr'
560+ -- if the `pre` has no attributes, try if it is followed by a `code`
561+ -- element and use those attributes if possible.
562+ attr <- case attr' of
563+ _:_ -> pure (toAttr attr')
564+ [] -> option nullAttr $ do
565+ TagOpen _ codeAttr <- pSatisfy (matchTagOpen "code" [])
566+ pure $ toAttr
567+ [ (k, v') | (k, v) <- codeAttr
568+ -- strip language from class
569+ , let v' = if k == "class"
570+ then fromMaybe v (T.stripPrefix "language-" v)
571+ else v ]
523 contents <- manyTill pAny (pCloses "pre" <|> eof)
572 contents <- manyTill pAny (pCloses "pre" <|> eof)
524 let rawText = T.concat $ map tagToText contents
573 let rawText = T.concat $ map tagToText contents
525 -- drop leading newline if any
574 -- drop leading newline if any
@@ -538,31 +587,47 @@ tagToText (TagOpen "br" _) = "\n"
538tagToText _ = ""
587tagToText _ = ""
539
588
540inline :: PandocMonad m => TagParser m Inlines
589inline :: PandocMonad m => TagParser m Inlines
541−inline = choice
590+inline = pTagText <|> do
591+ tag <- lookAhead (pSatisfy isInlineTag)
592+ exts <- getOption readerExtensions
593+ case tag of
594+ TagOpen name attr ->
595+ case name of
596+ "a" | extensionEnabled Ext_epub_html_exts exts
597+ , Just "noteref" <- lookup "type" attr <|> lookup "epub:type" attr
598+ , Just ('#',_) <- lookup "href" attr >>= T.uncons
542− [ eNoteref
599+ -> eNoteref
600+ | otherwise -> pLink
543− , eSwitch id inline
601+ "switch" -> eSwitch id inline
544− , pTagText
545− , pQ
602+ "q" -> pQ
546− , pEmph
603+ "em" -> pEmph
604+ "i" -> pEmph
605+ "strong" -> pStrong
547− , pStrong
606+ "b" -> pStrong
548− , pSuperscript
607+ "sup" -> pSuperscript
549− , pSubscript
608+ "sub" -> pSubscript
550− , pSpanLike
551− , pSmall
609+ "small" -> pSmall
610+ "s" -> pStrikeout
552− , pStrikeout
611+ "strike" -> pStrikeout
612+ "del" -> pStrikeout
553− , pUnderline
613+ "u" -> pUnderline
614+ "ins" -> pUnderline
554− , pLineBreak
615+ "br" -> pLineBreak
555− , pLink
556− , pImage
616+ "img" -> pImage
557− , pSvg
617+ "svg" -> pSvg
558− , pBdo
618+ "bdo" -> pBdo
559− , pCode
619+ "code" -> pCode
560− , pCodeWithClass [("samp","sample"),("var","variable")]
620+ "samp" -> pCodeWithClass "samp" "sample"
621+ "var" -> pCodeWithClass "var" "variable"
561− , pSpan
622+ "span" -> pSpan
562− , pMath False
623+ "math" -> pMath False
624+ "script"
625+ | Just x <- lookup "type" attr
563− , pScriptMath
626+ , "math/tex" `T.isPrefixOf` x -> pScriptMath
627+ _ | name `elem` htmlSpanLikeElements -> pSpanLike
564− , pRawHtmlInline
628+ _ -> pRawHtmlInline
565− ]
629+ TagText _ -> pTagText
630+ _ -> pRawHtmlInline
566
631
567pSelfClosing :: PandocMonad m
632pSelfClosing :: PandocMonad m
568 => (Text -> Bool) -> ([Attribute Text] -> Bool)
633 => (Text -> Bool) -> ([Attribute Text] -> Bool)
@@ -573,27 +638,25 @@ pSelfClosing f g = do
573 return open
638 return open
574
639
575pQ :: PandocMonad m => TagParser m Inlines
640pQ :: PandocMonad m => TagParser m Inlines
576−pQ = choice $ map try [citedQuote, normalQuote]
641+pQ = do
577− where citedQuote = do
578− tag <- pSatisfy $ tagOpenLit "q" (any ((=="cite") . fst))
642+ TagOpen _ attrs <- pSatisfy $ tagOpenLit "q" (const True)
579−
643+ case lookup "cite" attrs of
580− url <- canonicalizeUrl $ fromAttrib "cite" tag
644+ Just url -> do
581− let uid = fromMaybe (fromAttrib "name" tag) $
645+ let uid = fromMaybe mempty $
582− maybeFromAttrib "id" tag
646+ lookup "name" attrs <> lookup "id" attrs
583− let cls = T.words $ fromAttrib "class" tag
647+ let cls = maybe [] T.words $ lookup "class" attrs
584−
648+ url' <- canonicalizeUrl url
585− makeQuote $ B.spanWith (uid, cls, [("cite", escapeURI url)])
649+ makeQuote $ B.spanWith (uid, cls, [("cite", escapeURI url')])
586− normalQuote = do
650+ Nothing -> makeQuote id
587− pSatisfy $ tagOpenLit "q" (const True)
651+ where
588− makeQuote id
589− makeQuote wrapper = do
652+ makeQuote wrapper = do
590− ctx <- asks quoteContext
653+ ctx <- asks quoteContext
591− let (constructor, innerContext) = case ctx of
654+ let (constructor, innerContext) = case ctx of
592− InDoubleQuote -> (B.singleQuoted, InSingleQuote)
655+ InDoubleQuote -> (B.singleQuoted, InSingleQuote)
593− _ -> (B.doubleQuoted, InDoubleQuote)
656+ _ -> (B.doubleQuoted, InDoubleQuote)
594−
595− content <- withQuoteContext innerContext (mconcat <$> manyTill inline (pCloses "q"))
657+ content <- withQuoteContext innerContext
658+ (mconcat <$> manyTill inline (pCloses "q"))
596− return $ extractSpaces (constructor . wrapper) content
659+ return $ extractSpaces (constructor . wrapper) content
597
660
598pEmph :: PandocMonad m => TagParser m Inlines
661pEmph :: PandocMonad m => TagParser m Inlines
599pEmph = pInlinesInTags "em" B.emph <|> pInlinesInTags "i" B.emph
662pEmph = pInlinesInTags "em" B.emph <|> pInlinesInTags "i" B.emph
@@ -684,13 +747,12 @@ pSvg = do
684 UTF8.toText (encode $ UTF8.fromText rawText)
747 UTF8.toText (encode $ UTF8.fromText rawText)
685 return $ B.imageWith (ident,cls,[]) svgData mempty mempty
748 return $ B.imageWith (ident,cls,[]) svgData mempty mempty
686
749
687−pCodeWithClass :: PandocMonad m => [(T.Text,Text)] -> TagParser m Inlines
750+pCodeWithClass :: PandocMonad m => Text -> Text -> TagParser m Inlines
688−pCodeWithClass elemToClass = try $ do
751+pCodeWithClass name class' = try $ do
689− let tagTest = flip elem . fmap fst $ elemToClass
690− TagOpen open attr' <- pSatisfy $ tagOpen tagTest (const True)
752+ TagOpen open attr' <- pSatisfy $ tagOpen (== name) (const True)
691 result <- manyTill pAny (pCloses open)
753 result <- manyTill pAny (pCloses open)
692 let (ids,cs,kvs) = toAttr attr'
754 let (ids,cs,kvs) = toAttr attr'
693− cs' = maybe cs (:cs) . lookup open $ elemToClass
755+ cs' = class' : cs
694 return . B.codeWith (ids,cs',kvs) .
756 return . B.codeWith (ids,cs',kvs) .
695 T.unwords . T.lines . innerText $ result
757 T.unwords . T.lines . innerText $ result
696
758
@@ -777,17 +839,19 @@ pInlinesInTags tagtype f = extractSpaces f <$> pInTags tagtype inline
777
839
778pTagText :: PandocMonad m => TagParser m Inlines
840pTagText :: PandocMonad m => TagParser m Inlines
779pTagText = try $ do
841pTagText = try $ do
842+ pos <- getPosition
780 (TagText str) <- pSatisfy isTagText
843 (TagText str) <- pSatisfy isTagText
781 st <- getState
844 st <- getState
782 qu <- ask
845 qu <- ask
783 parsed <- lift $ lift $
846 parsed <- lift $ lift $
784− flip runReaderT qu $ runParserT (many pTagContents) st "text" str
847+ flip runReaderT qu $ runParserT (many pTagContents) st "text"
848+ (Sources [(pos, str)])
785 case parsed of
849 case parsed of
786 Left _ -> throwError $ PandocParseError $
850 Left _ -> throwError $ PandocParseError $
787 "Could not parse `" <> str <> "'"
851 "Could not parse `" <> str <> "'"
788 Right result -> return $ mconcat result
852 Right result -> return $ mconcat result
789
853
790−type InlinesParser m = HTMLParser m Text
854+type InlinesParser m = HTMLParser m Sources
791
855
792pTagContents :: PandocMonad m => InlinesParser m Inlines
856pTagContents :: PandocMonad m => InlinesParser m Inlines
793pTagContents =
857pTagContents =
@@ -881,27 +945,23 @@ pSpace = many1 (satisfy isSpace) >>= \xs ->
881 then return B.softbreak
945 then return B.softbreak
882 else return B.space
946 else return B.space
883
947
884−class NamedTag a where
885− getTagName :: a -> Maybe Text
948+getTagName :: Tag Text -> Maybe Text
886−
887−instance NamedTag (Tag Text) where
888− getTagName (TagOpen t _) = Just t
949+getTagName (TagOpen t _) = Just t
889− getTagName (TagClose t) = Just t
950+getTagName (TagClose t) = Just t
890− getTagName _ = Nothing
951+getTagName _ = Nothing
891−
952+
892−instance NamedTag (Tag String) where
893− getTagName (TagOpen t _) = Just (T.pack t)
894− getTagName (TagClose t) = Just (T.pack t)
895− getTagName _ = Nothing
896−
897−isInlineTag :: NamedTag (Tag a) => Tag a -> Bool
953+isInlineTag :: Tag Text -> Bool
898−isInlineTag t =
899− isCommentTag t || case getTagName t of
954+isInlineTag t = isCommentTag t || case t of
955+ TagOpen "script" _ -> "math/tex" `T.isPrefixOf` fromAttrib "type" t
956+ TagClose "script" -> True
957+ TagOpen name _ -> isInlineTagName name
958+ TagClose name -> isInlineTagName name
900− Nothing -> False
959+ _ -> False
960+ where isInlineTagName x =
901− Just x -> x `Set.notMember` blockTags ||
961+ x `Set.notMember` blockTags ||
902− T.take 1 x == "?" -- processing instr.
962+ T.take 1 x == "?" -- processing instr.
903−
963+
904−isBlockTag :: NamedTag (Tag a) => Tag a -> Bool
964+isBlockTag :: Tag Text -> Bool
905isBlockTag t = isBlockTagName || isTagComment t
965isBlockTag t = isBlockTagName || isTagComment t
906 where isBlockTagName =
966 where isBlockTagName =
907 case getTagName t of
967 case getTagName t of
@@ -912,10 +972,10 @@ isBlockTag t = isBlockTagName || isTagComment t
912 || x `Set.member` eitherBlockOrInline
972 || x `Set.member` eitherBlockOrInline
913 Nothing -> False
973 Nothing -> False
914
974
915−isTextTag :: Tag a -> Bool
975+isTextTag :: Tag Text -> Bool
916isTextTag = tagText (const True)
976isTextTag = tagText (const True)
917
977
918−isCommentTag :: Tag a -> Bool
978+isCommentTag :: Tag Text -> Bool
919isCommentTag = tagComment (const True)
979isCommentTag = tagComment (const True)
920
980
921--- parsers for use in markdown, textile readers
981--- parsers for use in markdown, textile readers
@@ -923,13 +983,14 @@ isCommentTag = tagComment (const True)
923-- | Matches a stretch of HTML in balanced tags.
983-- | Matches a stretch of HTML in balanced tags.
924htmlInBalanced :: Monad m
984htmlInBalanced :: Monad m
925 => (Tag Text -> Bool)
985 => (Tag Text -> Bool)
926− -> ParserT Text st m Text
986+ -> ParserT Sources st m Text
927htmlInBalanced f = try $ do
987htmlInBalanced f = try $ do
928 lookAhead (char '<')
988 lookAhead (char '<')
929− inp <- getInput
989+ sources <- getInput
930− let ts = canonicalizeTags $
990+ let ts = canonicalizeTags
931− parseTagsOptions parseOptions{ optTagWarning = True,
991+ $ parseTagsOptions parseOptions{ optTagWarning = True,
932− optTagPosition = True } inp
992+ optTagPosition = True }
993+ $ sourcesToText sources
933 case ts of
994 case ts of
934 (TagPosition sr sc : t@(TagOpen tn _) : rest) -> do
995 (TagPosition sr sc : t@(TagOpen tn _) : rest) -> do
935 guard $ f t
996 guard $ f t
@@ -964,22 +1025,24 @@ htmlInBalanced' tagname ts = fromMaybe [] $ go 0 ts
964 go n (t:ts') = (t :) <$> go n ts'
1025 go n (t:ts') = (t :) <$> go n ts'
965 go _ [] = mzero
1026 go _ [] = mzero
966
1027
967−hasTagWarning :: [Tag a] -> Bool
1028+hasTagWarning :: [Tag Text] -> Bool
968hasTagWarning (TagWarning _:_) = True
1029hasTagWarning (TagWarning _:_) = True
969hasTagWarning _ = False
1030hasTagWarning _ = False
970
1031
971-- | Matches a tag meeting a certain condition.
1032-- | Matches a tag meeting a certain condition.
972htmlTag :: (HasReaderOptions st, Monad m)
1033htmlTag :: (HasReaderOptions st, Monad m)
973 => (Tag Text -> Bool)
1034 => (Tag Text -> Bool)
974− -> ParserT Text st m (Tag Text, Text)
1035+ -> ParserT Sources st m (Tag Text, Text)
975htmlTag f = try $ do
1036htmlTag f = try $ do
976 lookAhead (char '<')
1037 lookAhead (char '<')
977 startpos <- getPosition
1038 startpos <- getPosition
978− inp <- getInput
1039+ sources <- getInput
1040+ let inp = sourcesToText sources
979 let ts = canonicalizeTags $ parseTagsOptions
1041 let ts = canonicalizeTags $ parseTagsOptions
980 parseOptions{ optTagWarning = False
1042 parseOptions{ optTagWarning = False
981 , optTagPosition = True }
1043 , optTagPosition = True }
1044+ (inp <> " ")
982− (inp <> " ") -- add space to ensure that
1045+ -- add space to ensure that
983 -- we get a TagPosition after the tag
1046 -- we get a TagPosition after the tag
984 (next, ln, col) <- case ts of
1047 (next, ln, col) <- case ts of
985 (TagPosition{} : next : TagPosition ln col : _)
1048 (TagPosition{} : next : TagPosition ln col : _)
src/Text/Pandoc/Readers/HTML/Table.hs+35−15
@@ -17,6 +17,8 @@ module Text.Pandoc.Readers.HTML.Table (pTable) where
17
17
18import Control.Applicative ((<|>))
18import Control.Applicative ((<|>))
19import Data.Maybe (fromMaybe)
19import Data.Maybe (fromMaybe)
20+import Data.Either (lefts, rights)
21+import Data.List.NonEmpty (nonEmpty)
20import Data.Text (Text)
22import Data.Text (Text)
21import Text.HTML.TagSoup
23import Text.HTML.TagSoup
22import Text.Pandoc.Builder (Blocks)
24import Text.Pandoc.Builder (Blocks)
@@ -32,34 +34,51 @@ import Text.Pandoc.Shared (onlySimpleTableCells, safeRead)
32import qualified Data.Text as T
34import qualified Data.Text as T
33import qualified Text.Pandoc.Builder as B
35import qualified Text.Pandoc.Builder as B
34
36
35−-- | Parses a @<col>@ element, returning the column's width. Defaults to
37+-- | Parses a @<col>@ element, returning the column's width.
38+-- An Either value is used: Left i means a "relative length" with
39+-- integral value i (see https://www.w3.org/TR/html4/types.html#h-6.6);
40+-- Right w means a regular width. Defaults to @'Right ColWidthDefault'@
36−-- @'ColWidthDefault'@ if the width is not set or cannot be determined.
41+-- if the width is not set or cannot be determined.
37−pCol :: PandocMonad m => TagParser m ColWidth
42+pCol :: PandocMonad m => TagParser m (Either Int ColWidth)
38pCol = try $ do
43pCol = try $ do
39 TagOpen _ attribs' <- pSatisfy (matchTagOpen "col" [])
44 TagOpen _ attribs' <- pSatisfy (matchTagOpen "col" [])
40 let attribs = toStringAttr attribs'
45 let attribs = toStringAttr attribs'
41 skipMany pBlank
46 skipMany pBlank
42 optional $ pSatisfy (matchTagClose "col")
47 optional $ pSatisfy (matchTagClose "col")
43 skipMany pBlank
48 skipMany pBlank
44− let width = case lookup "width" attribs of
49+ return $ case lookup "width" attribs of
45 Nothing -> case lookup "style" attribs of
50 Nothing -> case lookup "style" attribs of
46 Just (T.stripPrefix "width:" -> Just xs) | T.any (== '%') xs ->
51 Just (T.stripPrefix "width:" -> Just xs) | T.any (== '%') xs ->
52+ maybe (Right ColWidthDefault) (Right . ColWidth . (/ 100.0))
47− fromMaybe 0.0 $ safeRead (T.filter
53+ $ safeRead (T.filter
48− (`notElem` (" \t\r\n%'\";" :: [Char])) xs)
54+ (`notElem` (" \t\r\n%'\";" :: [Char])) xs)
49− _ -> 0.0
55+ _ -> Right ColWidthDefault
56+ Just (T.unsnoc -> Just (xs, '*')) ->
57+ maybe (Left 1) Left $ safeRead xs
50 Just (T.unsnoc -> Just (xs, '%')) ->
58 Just (T.unsnoc -> Just (xs, '%')) ->
59+ maybe (Right ColWidthDefault)
51− fromMaybe 0.0 $ safeRead xs
60+ (Right . ColWidth . (/ 100.0)) $ safeRead xs
52− _ -> 0.0
53− if width > 0.0
54− then return $ ColWidth $ width / 100.0
55− else return ColWidthDefault
61+ _ -> Right ColWidthDefault
56
62
57−pColgroup :: PandocMonad m => TagParser m [ColWidth]
63+pColgroup :: PandocMonad m => TagParser m [Either Int ColWidth]
58pColgroup = try $ do
64pColgroup = try $ do
59 pSatisfy (matchTagOpen "colgroup" [])
65 pSatisfy (matchTagOpen "colgroup" [])
60 skipMany pBlank
66 skipMany pBlank
61 manyTill pCol (pCloses "colgroup" <|> eof) <* skipMany pBlank
67 manyTill pCol (pCloses "colgroup" <|> eof) <* skipMany pBlank
62
68
69+resolveRelativeLengths :: [Either Int ColWidth] -> [ColWidth]
70+resolveRelativeLengths ws =
71+ let remaining = 1 - sum (map getColWidth $ rights ws)
72+ relatives = sum $ lefts ws
73+ relUnit = remaining / fromIntegral relatives
74+ toColWidth (Right x) = x
75+ toColWidth (Left i) = ColWidth (fromIntegral i * relUnit)
76+ in map toColWidth ws
77+
78+getColWidth :: ColWidth -> Double
79+getColWidth ColWidthDefault = 0
80+getColWidth (ColWidth w) = w
81+
63data CellType
82data CellType
64 = HeaderCell
83 = HeaderCell
65 | BodyCell
84 | BodyCell
@@ -181,7 +200,8 @@ pTable :: PandocMonad m
181pTable block = try $ do
200pTable block = try $ do
182 TagOpen _ attribs <- pSatisfy (matchTagOpen "table" []) <* skipMany pBlank
201 TagOpen _ attribs <- pSatisfy (matchTagOpen "table" []) <* skipMany pBlank
183 caption <- option mempty $ pInTags "caption" block <* skipMany pBlank
202 caption <- option mempty $ pInTags "caption" block <* skipMany pBlank
203+ widths <- resolveRelativeLengths <$>
184− widths <- ((mconcat <$> many1 pColgroup) <|> many pCol) <* skipMany pBlank
204+ ((mconcat <$> many1 pColgroup) <|> many pCol) <* skipMany pBlank
185 thead <- pTableHead block <* skipMany pBlank
205 thead <- pTableHead block <* skipMany pBlank
186 topfoot <- optionMaybe (pTableFoot block) <* skipMany pBlank
206 topfoot <- optionMaybe (pTableFoot block) <* skipMany pBlank
187 tbodies <- many (pTableBody block) <* skipMany pBlank
207 tbodies <- many (pTableBody block) <* skipMany pBlank
@@ -216,7 +236,7 @@ normalize widths head' bodies foot = do
216 let rows = headRows head' <> concatMap bodyRows bodies <> footRows foot
236 let rows = headRows head' <> concatMap bodyRows bodies <> footRows foot
217 let cellWidth (Cell _ _ _ (ColSpan cs) _) = cs
237 let cellWidth (Cell _ _ _ (ColSpan cs) _) = cs
218 let rowLength = foldr (\cell acc -> cellWidth cell + acc) 0 . rowCells
238 let rowLength = foldr (\cell acc -> cellWidth cell + acc) 0 . rowCells
219− let ncols = maximum (map rowLength rows)
239+ let ncols = maybe 0 maximum $ nonEmpty $ map rowLength rows
220 let tblType = tableType (map rowCells rows)
240 let tblType = tableType (map rowCells rows)
221 -- fail on empty table
241 -- fail on empty table
222 if null rows
242 if null rows
src/Text/Pandoc/Readers/Haddock.hs+11−7
@@ -17,8 +17,9 @@ module Text.Pandoc.Readers.Haddock
17
17
18import Control.Monad.Except (throwError)
18import Control.Monad.Except (throwError)
19import Data.List (intersperse)
19import Data.List (intersperse)
20+import Data.List.NonEmpty (nonEmpty)
20import Data.Maybe (fromMaybe)
21import Data.Maybe (fromMaybe)
21−import Data.Text (Text, unpack)
22+import Data.Text (unpack)
22import qualified Data.Text as T
23import qualified Data.Text as T
23import Documentation.Haddock.Parser
24import Documentation.Haddock.Parser
24import Documentation.Haddock.Types as H
25import Documentation.Haddock.Types as H
@@ -28,15 +29,17 @@ import Text.Pandoc.Class.PandocMonad (PandocMonad)
28import Text.Pandoc.Definition
29import Text.Pandoc.Definition
29import Text.Pandoc.Error
30import Text.Pandoc.Error
30import Text.Pandoc.Options
31import Text.Pandoc.Options
32+import Text.Pandoc.Sources (ToSources(..), sourcesToText)
31−import Text.Pandoc.Shared (crFilter, splitTextBy, trim)
33+import Text.Pandoc.Shared (splitTextBy, trim)
32
34
33
35
34-- | Parse Haddock markup and return a 'Pandoc' document.
36-- | Parse Haddock markup and return a 'Pandoc' document.
35−readHaddock :: PandocMonad m
37+readHaddock :: (PandocMonad m, ToSources a)
36 => ReaderOptions
38 => ReaderOptions
37− -> Text
39+ -> a
38 -> m Pandoc
40 -> m Pandoc
39−readHaddock opts s = case readHaddockEither opts (unpack (crFilter s)) of
41+readHaddock opts s = case readHaddockEither opts
42+ (unpack . sourcesToText . toSources $ s) of
40 Right result -> return result
43 Right result -> return result
41 Left e -> throwError e
44 Left e -> throwError e
42
45
@@ -92,7 +95,7 @@ docHToBlocks d' =
92 then ([], map toCells bodyRows)
95 then ([], map toCells bodyRows)
93 else (toCells (head headerRows),
96 else (toCells (head headerRows),
94 map toCells (tail headerRows ++ bodyRows))
97 map toCells (tail headerRows ++ bodyRows))
95− colspecs = replicate (maximum (map length body))
98+ colspecs = replicate (maybe 0 maximum (nonEmpty (map length body)))
96 (AlignDefault, ColWidthDefault)
99 (AlignDefault, ColWidthDefault)
97 in B.table B.emptyCaption
100 in B.table B.emptyCaption
98 colspecs
101 colspecs
@@ -128,7 +131,8 @@ docHToInlines isCode d' =
128 DocIdentifier s -> B.codeWith ("",["haskell","identifier"],[]) $ T.pack s
131 DocIdentifier s -> B.codeWith ("",["haskell","identifier"],[]) $ T.pack s
129 _ -> mempty
132 _ -> mempty
130 DocIdentifierUnchecked s -> B.codeWith ("",["haskell","identifier"],[]) $ T.pack s
133 DocIdentifierUnchecked s -> B.codeWith ("",["haskell","identifier"],[]) $ T.pack s
131− DocModule s -> B.codeWith ("",["haskell","module"],[]) $ T.pack s
134+ DocModule s -> B.codeWith ("",["haskell","module"],[]) $
135+ T.pack (modLinkName s)
132 DocWarning _ -> mempty -- TODO
136 DocWarning _ -> mempty -- TODO
133 DocEmphasis d -> B.emph (docHToInlines isCode d)
137 DocEmphasis d -> B.emph (docHToInlines isCode d)
134 DocMonospaced (DocString s) -> B.code $ T.pack s
138 DocMonospaced (DocString s) -> B.code $ T.pack s
src/Text/Pandoc/Readers/Ipynb.hs+5−3
@@ -39,10 +39,12 @@ import Data.Aeson as Aeson
39import Control.Monad.Except (throwError)
39import Control.Monad.Except (throwError)
40import Text.Pandoc.Readers.Markdown (readMarkdown)
40import Text.Pandoc.Readers.Markdown (readMarkdown)
41import qualified Text.Pandoc.UTF8 as UTF8
41import qualified Text.Pandoc.UTF8 as UTF8
42+import Text.Pandoc.Sources (ToSources(..), sourcesToText)
42
43
44+readIpynb :: (PandocMonad m, ToSources a)
43−readIpynb :: PandocMonad m => ReaderOptions -> Text -> m Pandoc
45+ => ReaderOptions -> a -> m Pandoc
44−readIpynb opts t = do
46+readIpynb opts x = do
45− let src = BL.fromStrict (TE.encodeUtf8 t)
47+ let src = BL.fromStrict . TE.encodeUtf8 . sourcesToText $ toSources x
46 case eitherDecode src of
48 case eitherDecode src of
47 Right (notebook4 :: Notebook NbV4) -> notebookToPandoc opts notebook4
49 Right (notebook4 :: Notebook NbV4) -> notebookToPandoc opts notebook4
48 Left _ ->
50 Left _ ->
src/Text/Pandoc/Readers/JATS.hs+37−51
@@ -14,7 +14,9 @@ Conversion of JATS XML to 'Pandoc' document.
14
14
15module Text.Pandoc.Readers.JATS ( readJATS ) where
15module Text.Pandoc.Readers.JATS ( readJATS ) where
16import Control.Monad.State.Strict
16import Control.Monad.State.Strict
17+import Control.Monad.Except (throwError)
18+import Text.Pandoc.Error (PandocError(..))
17−import Data.Char (isDigit, isSpace, toUpper)
19+import Data.Char (isDigit, isSpace)
18import Data.Default
20import Data.Default
19import Data.Generics
21import Data.Generics
20import Data.List (foldl', intersperse)
22import Data.List (foldl', intersperse)
@@ -22,15 +24,17 @@ import qualified Data.Map as Map
22import Data.Maybe (maybeToList, fromMaybe)
24import Data.Maybe (maybeToList, fromMaybe)
23import Data.Text (Text)
25import Data.Text (Text)
24import qualified Data.Text as T
26import qualified Data.Text as T
27+import qualified Data.Text.Lazy as TL
25import Text.HTML.TagSoup.Entity (lookupEntity)
28import Text.HTML.TagSoup.Entity (lookupEntity)
26import Text.Pandoc.Builder
29import Text.Pandoc.Builder
27import Text.Pandoc.Class.PandocMonad (PandocMonad)
30import Text.Pandoc.Class.PandocMonad (PandocMonad)
28import Text.Pandoc.Options
31import Text.Pandoc.Options
29−import Text.Pandoc.Shared (crFilter, safeRead, extractSpaces)
32+import Text.Pandoc.Shared (safeRead, extractSpaces)
30import Text.TeXMath (readMathML, writeTeX)
33import Text.TeXMath (readMathML, writeTeX)
31−import Text.XML.Light
34+import Text.Pandoc.XML.Light
32import qualified Data.Set as S (fromList, member)
35import qualified Data.Set as S (fromList, member)
33import Data.Set ((\\))
36import Data.Set ((\\))
37+import Text.Pandoc.Sources (ToSources(..), sourcesToText)
34
38
35type JATS m = StateT JATSState m
39type JATS m = StateT JATSState m
36
40
@@ -49,42 +53,28 @@ instance Default JATSState where
49 , jatsContent = [] }
53 , jatsContent = [] }
50
54
51
55
52−readJATS :: PandocMonad m => ReaderOptions -> Text -> m Pandoc
56+readJATS :: (PandocMonad m, ToSources a)
57+ => ReaderOptions
58+ -> a
59+ -> m Pandoc
53readJATS _ inp = do
60readJATS _ inp = do
54− let tree = normalizeTree . parseXML
61+ let sources = toSources inp
55− $ T.unpack $ crFilter inp
62+ tree <- either (throwError . PandocXMLError "") return $
63+ parseXMLContents (TL.fromStrict . sourcesToText $ sources)
56 (bs, st') <- flip runStateT (def{ jatsContent = tree }) $ mapM parseBlock tree
64 (bs, st') <- flip runStateT (def{ jatsContent = tree }) $ mapM parseBlock tree
57 return $ Pandoc (jatsMeta st') (toList . mconcat $ bs)
65 return $ Pandoc (jatsMeta st') (toList . mconcat $ bs)
58
66
59−-- normalize input, consolidating adjacent Text and CRef elements
60−normalizeTree :: [Content] -> [Content]
61−normalizeTree = everywhere (mkT go)
62− where go :: [Content] -> [Content]
63− go (Text (CData CDataRaw _ _):xs) = xs
64− go (Text (CData CDataText s1 z):Text (CData CDataText s2 _):xs) =
65− Text (CData CDataText (s1 ++ s2) z):xs
66− go (Text (CData CDataText s1 z):CRef r:xs) =
67− Text (CData CDataText (s1 ++ convertEntity r) z):xs
68− go (CRef r:Text (CData CDataText s1 z):xs) =
69− Text (CData CDataText (convertEntity r ++ s1) z):xs
70− go (CRef r1:CRef r2:xs) =
71− Text (CData CDataText (convertEntity r1 ++ convertEntity r2) Nothing):xs
72− go xs = xs
73−
74−convertEntity :: String -> String
75−convertEntity e = Data.Maybe.fromMaybe (map toUpper e) (lookupEntity e)
76−
77-- convenience function to get an attribute value, defaulting to ""
67-- convenience function to get an attribute value, defaulting to ""
78−attrValue :: String -> Element -> Text
68+attrValue :: Text -> Element -> Text
79attrValue attr =
69attrValue attr =
80 fromMaybe "" . maybeAttrValue attr
70 fromMaybe "" . maybeAttrValue attr
81
71
82−maybeAttrValue :: String -> Element -> Maybe Text
72+maybeAttrValue :: Text -> Element -> Maybe Text
83maybeAttrValue attr elt =
73maybeAttrValue attr elt =
84− T.pack <$> lookupAttrBy (\x -> qName x == attr) (elAttribs elt)
74+ lookupAttrBy (\x -> qName x == attr) (elAttribs elt)
85
75
86-- convenience function
76-- convenience function
87−named :: String -> Element -> Bool
77+named :: Text -> Element -> Bool
88named s e = qName (elName e) == s
78named s e = qName (elName e) == s
89
79
90--
80--
@@ -150,10 +140,10 @@ getBlocks e = mconcat <$>
150
140
151parseBlock :: PandocMonad m => Content -> JATS m Blocks
141parseBlock :: PandocMonad m => Content -> JATS m Blocks
152parseBlock (Text (CData CDataRaw _ _)) = return mempty -- DOCTYPE
142parseBlock (Text (CData CDataRaw _ _)) = return mempty -- DOCTYPE
153−parseBlock (Text (CData _ s _)) = if all isSpace s
143+parseBlock (Text (CData _ s _)) = if T.all isSpace s
154 then return mempty
144 then return mempty
155− else return $ plain $ trimInlines $ text $ T.pack s
145+ else return $ plain $ trimInlines $ text s
156−parseBlock (CRef x) = return $ plain $ str $ T.toUpper $ T.pack x
146+parseBlock (CRef x) = return $ plain $ str $ T.toUpper x
157parseBlock (Elem e) =
147parseBlock (Elem e) =
158 case qName (elName e) of
148 case qName (elName e) of
159 "p" -> parseMixed para (elContent e)
149 "p" -> parseMixed para (elContent e)
@@ -202,7 +192,7 @@ parseBlock (Elem e) =
202 "" -> []
192 "" -> []
203 x -> [x]
193 x -> [x]
204 return $ codeBlockWith (attrValue "id" e, classes', [])
194 return $ codeBlockWith (attrValue "id" e, classes', [])
205− $ trimNl $ textContentRecursive e
195+ $ trimNl $ strContentRecursive e
206 parseBlockquote = do
196 parseBlockquote = do
207 attrib <- case filterChild (named "attribution") e of
197 attrib <- case filterChild (named "attribution") e of
208 Nothing -> return mempty
198 Nothing -> return mempty
@@ -266,7 +256,7 @@ parseBlock (Elem e) =
266 Just "center" -> AlignCenter
256 Just "center" -> AlignCenter
267 _ -> AlignDefault
257 _ -> AlignDefault
268 let toWidth c = do
258 let toWidth c = do
269− w <- findAttrText (unqual "colwidth") c
259+ w <- findAttr (unqual "colwidth") c
270 n <- safeRead $ "0" <> T.filter (\x -> isDigit x || x == '.') w
260 n <- safeRead $ "0" <> T.filter (\x -> isDigit x || x == '.') w
271 if n > 0 then Just n else Nothing
261 if n > 0 then Just n else Nothing
272 let numrows = foldl' max 0 $ map length bodyrows
262 let numrows = foldl' max 0 $ map length bodyrows
@@ -437,16 +427,10 @@ parseRef e = do
437 Nothing -> return $ Map.insert "id" (toMetaValue refId) mempty
427 Nothing -> return $ Map.insert "id" (toMetaValue refId) mempty
438 -- TODO handle mixed-citation
428 -- TODO handle mixed-citation
439
429
440−findAttrText :: QName -> Element -> Maybe Text
441−findAttrText x = fmap T.pack . findAttr x
442−
443textContent :: Element -> Text
430textContent :: Element -> Text
444−textContent = T.pack . strContent
431+textContent = strContent
445−
446−textContentRecursive :: Element -> Text
447−textContentRecursive = T.pack . strContentRecursive
448
432
449−strContentRecursive :: Element -> String
433+strContentRecursive :: Element -> Text
450strContentRecursive = strContent .
434strContentRecursive = strContent .
451 (\e' -> e'{ elContent = map elementToStr $ elContent e' })
435 (\e' -> e'{ elContent = map elementToStr $ elContent e' })
452
436
@@ -455,9 +439,9 @@ elementToStr (Elem e') = Text $ CData CDataText (strContentRecursive e') Nothing
455elementToStr x = x
439elementToStr x = x
456
440
457parseInline :: PandocMonad m => Content -> JATS m Inlines
441parseInline :: PandocMonad m => Content -> JATS m Inlines
458−parseInline (Text (CData _ s _)) = return $ text $ T.pack s
442+parseInline (Text (CData _ s _)) = return $ text s
459−parseInline (CRef ref) =
443+parseInline (CRef ref) = return $ maybe (text $ T.toUpper ref) (text . T.pack)
460− return . text . maybe (T.toUpper $ T.pack ref) T.pack $ lookupEntity ref
444+ $ lookupEntity (T.unpack ref)
461parseInline (Elem e) =
445parseInline (Elem e) =
462 case qName (elName e) of
446 case qName (elName e) of
463 "italic" -> innerInlines emph
447 "italic" -> innerInlines emph
@@ -502,9 +486,9 @@ parseInline (Elem e) =
502 else linkWith attr ("#" <> rid) "" ils
486 else linkWith attr ("#" <> rid) "" ils
503 "ext-link" -> do
487 "ext-link" -> do
504 ils <- innerInlines id
488 ils <- innerInlines id
505− let title = fromMaybe "" $ findAttrText (QName "title" (Just "http://www.w3.org/1999/xlink") Nothing) e
489+ let title = fromMaybe "" $ findAttr (QName "title" (Just "http://www.w3.org/1999/xlink") Nothing) e
506 let href = case findAttr (QName "href" (Just "http://www.w3.org/1999/xlink") Nothing) e of
490 let href = case findAttr (QName "href" (Just "http://www.w3.org/1999/xlink") Nothing) e of
507− Just h -> T.pack h
491+ Just h -> h
508 _ -> "#" <> attrValue "rid" e
492 _ -> "#" <> attrValue "rid" e
509 let ils' = if ils == mempty then str href else ils
493 let ils' = if ils == mempty then str href else ils
510 let attr = (attrValue "id" e, [], [])
494 let attr = (attrValue "id" e, [], [])
@@ -512,7 +496,8 @@ parseInline (Elem e) =
512
496
513 "disp-formula" -> formula displayMath
497 "disp-formula" -> formula displayMath
514 "inline-formula" -> formula math
498 "inline-formula" -> formula math
499+ "math" | qURI (elName e) == Just "http://www.w3.org/1998/Math/MathML"
515− "math" | qPrefix (elName e) == Just "mml" -> return . math $ mathML e
500+ -> return . math $ mathML e
516 "tex-math" -> return . math $ textContent e
501 "tex-math" -> return . math $ textContent e
517
502
518 "email" -> return $ link ("mailto:" <> textContent e) ""
503 "email" -> return $ link ("mailto:" <> textContent e) ""
@@ -524,7 +509,7 @@ parseInline (Elem e) =
524 where innerInlines f = extractSpaces f . mconcat <$>
509 where innerInlines f = extractSpaces f . mconcat <$>
525 mapM parseInline (elContent e)
510 mapM parseInline (elContent e)
526 mathML x =
511 mathML x =
527− case readMathML . T.pack . showElement $ everywhere (mkT removePrefix) x of
512+ case readMathML . showElement $ everywhere (mkT removePrefix) x of
528 Left _ -> mempty
513 Left _ -> mempty
529 Right m -> writeTeX m
514 Right m -> writeTeX m
530 formula constructor = do
515 formula constructor = do
@@ -535,11 +520,12 @@ parseInline (Elem e) =
535 filterChildren isMathML whereToLook
520 filterChildren isMathML whereToLook
536 return . mconcat . take 1 . map constructor $ texMaths ++ mathMLs
521 return . mconcat . take 1 . map constructor $ texMaths ++ mathMLs
537
522
538− isMathML x = qName (elName x) == "math" &&
523+ isMathML x = qName (elName x) == "math" &&
539− qPrefix (elName x) == Just "mml"
524+ qURI (elName x) ==
525+ Just "http://www.w3.org/1998/Math/MathML"
540 removePrefix elname = elname { qPrefix = Nothing }
526 removePrefix elname = elname { qPrefix = Nothing }
541 codeWithLang = do
527 codeWithLang = do
542 let classes' = case attrValue "language" e of
528 let classes' = case attrValue "language" e of
543 "" -> []
529 "" -> []
544 l -> [l]
530 l -> [l]
545− return $ codeWith (attrValue "id" e,classes',[]) $ textContentRecursive e
531+ return $ codeWith (attrValue "id" e,classes',[]) $ strContentRecursive e
src/Text/Pandoc/Readers/Jira.hs+13−9
@@ -20,18 +20,20 @@ import Text.Pandoc.Builder hiding (cell)
20import Text.Pandoc.Error (PandocError (PandocParseError))
20import Text.Pandoc.Error (PandocError (PandocParseError))
21import Text.Pandoc.Options (ReaderOptions)
21import Text.Pandoc.Options (ReaderOptions)
22import Text.Pandoc.Shared (stringify)
22import Text.Pandoc.Shared (stringify)
23−
23+import Text.Pandoc.Sources (ToSources(..), sourcesToText)
24import qualified Text.Jira.Markup as Jira
24import qualified Text.Jira.Markup as Jira
25
25
26-- | Read Jira wiki markup.
26-- | Read Jira wiki markup.
27−readJira :: PandocMonad m
27+readJira :: (PandocMonad m, ToSources a)
28 => ReaderOptions
28 => ReaderOptions
29− -> Text
29+ -> a
30 -> m Pandoc
30 -> m Pandoc
31−readJira _opts s = case parse s of
31+readJira _opts inp = do
32+ let sources = toSources inp
33+ case parse (sourcesToText sources) of
32− Right d -> return $ jiraToPandoc d
34+ Right d -> return $ jiraToPandoc d
33− Left e -> throwError . PandocParseError $
35+ Left e -> throwError . PandocParseError $
34− "Jira parse error" `append` pack (show e)
36+ "Jira parse error" `append` pack (show e)
35
37
36jiraToPandoc :: Jira.Doc -> Pandoc
38jiraToPandoc :: Jira.Doc -> Pandoc
37jiraToPandoc (Jira.Doc blks) = doc $ foldMap jiraToPandocBlocks blks
39jiraToPandoc (Jira.Doc blks) = doc $ foldMap jiraToPandocBlocks blks
@@ -71,10 +73,10 @@ toPandocCodeBlocks langMay params txt =
71 Nothing -> []
73 Nothing -> []
72 in codeBlockWith ("", classes, map paramToPair params) txt
74 in codeBlockWith ("", classes, map paramToPair params) txt
73
75
74−-- | Create a pandoc @'Div'@
76+-- | Create a pandoc @'Div'@ from a panel.
75toPandocDiv :: [Jira.Parameter] -> [Jira.Block] -> Blocks
77toPandocDiv :: [Jira.Parameter] -> [Jira.Block] -> Blocks
76toPandocDiv params =
78toPandocDiv params =
77− divWith ("", [], map paramToPair params) . foldMap jiraToPandocBlocks
79+ divWith ("", ["panel"], map paramToPair params) . foldMap jiraToPandocBlocks
78
80
79paramToPair :: Jira.Parameter -> (Text, Text)
81paramToPair :: Jira.Parameter -> (Text, Text)
80paramToPair (Jira.Parameter key value) = (key, value)
82paramToPair (Jira.Parameter key value) = (key, value)
@@ -170,6 +172,8 @@ jiraLinkToPandoc linkType alias url =
170 Jira.Email -> link ("mailto:" <> url') "" alias'
172 Jira.Email -> link ("mailto:" <> url') "" alias'
171 Jira.Attachment -> linkWith ("", ["attachment"], []) url' "" alias'
173 Jira.Attachment -> linkWith ("", ["attachment"], []) url' "" alias'
172 Jira.User -> linkWith ("", ["user-account"], []) url' "" alias'
174 Jira.User -> linkWith ("", ["user-account"], []) url' "" alias'
175+ Jira.SmartCard -> linkWith ("", ["smart-card"], []) url' "" alias'
176+ Jira.SmartLink -> linkWith ("", ["smart-link"], []) url' "" alias'
173
177
174-- | Get unicode representation of a Jira icon.
178-- | Get unicode representation of a Jira icon.
175iconUnicode :: Jira.Icon -> Text
179iconUnicode :: Jira.Icon -> Text
src/Text/Pandoc/Readers/LaTeX.hs+227−1410
@@ -1,7 +1,3 @@
1−{-# LANGUAGE BangPatterns #-}
2−{-# LANGUAGE CPP #-}
3−{-# LANGUAGE FlexibleInstances #-}
4−{-# LANGUAGE MultiParamTypeClasses #-}
5{-# LANGUAGE OverloadedStrings #-}
1{-# LANGUAGE OverloadedStrings #-}
6{-# LANGUAGE PatternGuards #-}
2{-# LANGUAGE PatternGuards #-}
7{-# LANGUAGE ScopedTypeVariables #-}
3{-# LANGUAGE ScopedTypeVariables #-}
@@ -22,50 +18,58 @@ module Text.Pandoc.Readers.LaTeX ( readLaTeX,
22 applyMacros,
18 applyMacros,
23 rawLaTeXInline,
19 rawLaTeXInline,
24 rawLaTeXBlock,
20 rawLaTeXBlock,
25− inlineCommand,
21+ inlineCommand
26− tokenize,
27− untokenize
28 ) where
22 ) where
29
23
30import Control.Applicative (many, optional, (<|>))
24import Control.Applicative (many, optional, (<|>))
31import Control.Monad
25import Control.Monad
32import Control.Monad.Except (throwError)
26import Control.Monad.Except (throwError)
33−import Data.Char (isDigit, isLetter, toUpper, chr)
27+import Data.Char (isDigit, isLetter, isAlphaNum, toUpper, chr)
34import Data.Default
28import Data.Default
35−import Data.Functor (($>))
36import Data.List (intercalate)
29import Data.List (intercalate)
37import qualified Data.Map as M
30import qualified Data.Map as M
38import Data.Maybe (fromMaybe, maybeToList)
31import Data.Maybe (fromMaybe, maybeToList)
39import qualified Data.Set as Set
32import qualified Data.Set as Set
40−import qualified Data.Sequence as Seq
41import Data.Text (Text)
33import Data.Text (Text)
42import qualified Data.Text as T
34import qualified Data.Text as T
35+import Skylighting (defaultSyntaxMap)
43import System.FilePath (addExtension, replaceExtension, takeExtension)
36import System.FilePath (addExtension, replaceExtension, takeExtension)
44−import Text.Pandoc.BCP47 (Lang (..), renderLang)
37+import Text.Collate.Lang (renderLang)
45−import Text.Pandoc.Builder
38+import Text.Pandoc.Builder as B
46import Text.Pandoc.Class.PandocPure (PandocPure)
39import Text.Pandoc.Class.PandocPure (PandocPure)
47import Text.Pandoc.Class.PandocMonad (PandocMonad (..), getResourcePath,
40import Text.Pandoc.Class.PandocMonad (PandocMonad (..), getResourcePath,
48− readFileFromDirs, report, setResourcePath,
41+ readFileFromDirs, report,
49− setTranslations, translateTerm)
42+ setResourcePath)
50import Text.Pandoc.Error (PandocError (PandocParseError, PandocParsecError))
43import Text.Pandoc.Error (PandocError (PandocParseError, PandocParsecError))
51−import Text.Pandoc.Highlighting (fromListingsLanguage, languagesByExtension)
44+import Text.Pandoc.Highlighting (languagesByExtension)
52import Text.Pandoc.ImageSize (numUnit, showFl)
45import Text.Pandoc.ImageSize (numUnit, showFl)
53import Text.Pandoc.Logging
46import Text.Pandoc.Logging
54import Text.Pandoc.Options
47import Text.Pandoc.Options
55import Text.Pandoc.Parsing hiding (blankline, many, mathDisplay, mathInline,
48import Text.Pandoc.Parsing hiding (blankline, many, mathDisplay, mathInline,
56 optional, space, spaces, withRaw, (<|>))
49 optional, space, spaces, withRaw, (<|>))
57−import Text.Pandoc.Readers.LaTeX.Types (ExpansionPoint (..), Macro (..),
50+import Text.Pandoc.Readers.LaTeX.Types (Tok (..), TokType (..))
58− ArgSpec (..), Tok (..), TokType (..))
59import Text.Pandoc.Readers.LaTeX.Parsing
51import Text.Pandoc.Readers.LaTeX.Parsing
60−import Text.Pandoc.Readers.LaTeX.Lang (polyglossiaLangToBCP47,
52+import Text.Pandoc.Readers.LaTeX.Citation (citationCommands, cites)
53+import Text.Pandoc.Readers.LaTeX.Math (dollarsMath, inlineEnvironments,
54+ inlineEnvironment,
55+ mathDisplay, mathInline,
56+ newtheorem, theoremstyle, proof,
57+ theoremEnvironment)
58+import Text.Pandoc.Readers.LaTeX.Table (tableEnvironments)
59+import Text.Pandoc.Readers.LaTeX.Macro (macroDef)
60+import Text.Pandoc.Readers.LaTeX.Lang (inlineLanguageCommands,
61+ enquoteCommands,
61− babelLangToBCP47)
62+ babelLangToBCP47, setDefaultLanguage)
62−import Text.Pandoc.Readers.LaTeX.SIunitx
63+import Text.Pandoc.Readers.LaTeX.SIunitx (siunitxCommands)
64+import Text.Pandoc.Readers.LaTeX.Inline (acronymCommands, refCommands,
65+ nameCommands, charCommands,
66+ accentCommands,
67+ biblatexInlineCommands,
68+ verbCommands, rawInlineOr,
69+ listingsLanguage)
63import Text.Pandoc.Shared
70import Text.Pandoc.Shared
64−import qualified Text.Pandoc.Translations as Translations
65import Text.Pandoc.Walk
71import Text.Pandoc.Walk
66−import qualified Text.Pandoc.Builder as B
72+import Data.List.NonEmpty (nonEmpty)
67−import qualified Data.Text.Normalize as Normalize
68−import Safe
69
73
70-- for debugging:
74-- for debugging:
71-- import Text.Pandoc.Extensions (getDefaultExtensions)
75-- import Text.Pandoc.Extensions (getDefaultExtensions)
@@ -73,16 +77,17 @@ import Safe
73-- import Debug.Trace (traceShowId)
77-- import Debug.Trace (traceShowId)
74
78
75-- | Parse LaTeX from string and return 'Pandoc' document.
79-- | Parse LaTeX from string and return 'Pandoc' document.
76−readLaTeX :: PandocMonad m
80+readLaTeX :: (PandocMonad m, ToSources a)
77 => ReaderOptions -- ^ Reader options
81 => ReaderOptions -- ^ Reader options
78− -> Text -- ^ String to parse (assumes @'\n'@ line endings)
82+ -> a -- ^ Input to parse
79 -> m Pandoc
83 -> m Pandoc
80readLaTeX opts ltx = do
84readLaTeX opts ltx = do
85+ let sources = toSources ltx
81 parsed <- runParserT parseLaTeX def{ sOptions = opts } "source"
86 parsed <- runParserT parseLaTeX def{ sOptions = opts } "source"
82− (tokenize "source" (crFilter ltx))
87+ (tokenizeSources sources)
83 case parsed of
88 case parsed of
84 Right result -> return result
89 Right result -> return result
85− Left e -> throwError $ PandocParsecError ltx e
90+ Left e -> throwError $ PandocParsecError sources e
86
91
87parseLaTeX :: PandocMonad m => LP m Pandoc
92parseLaTeX :: PandocMonad m => LP m Pandoc
88parseLaTeX = do
93parseLaTeX = do
@@ -93,11 +98,7 @@ parseLaTeX = do
93 let doc' = doc bs
98 let doc' = doc bs
94 let headerLevel (Header n _ _) = [n]
99 let headerLevel (Header n _ _) = [n]
95 headerLevel _ = []
100 headerLevel _ = []
96−#if MIN_VERSION_safe(0,3,18)
101+ let bottomLevel = maybe 1 minimum $ nonEmpty $ query headerLevel doc'
97− let bottomLevel = minimumBound 1 $ query headerLevel doc'
98−#else
99− let bottomLevel = minimumDef 1 $ query headerLevel doc'
100−#endif
101 let adjustHeaders m (Header n attr ils) = Header (n+m) attr ils
102 let adjustHeaders m (Header n attr ils) = Header (n+m) attr ils
102 adjustHeaders _ x = x
103 adjustHeaders _ x = x
103 let (Pandoc _ bs') =
104 let (Pandoc _ bs') =
@@ -132,11 +133,11 @@ resolveRefs _ x = x
132
133
133
134
134rawLaTeXBlock :: (PandocMonad m, HasMacros s, HasReaderOptions s)
135rawLaTeXBlock :: (PandocMonad m, HasMacros s, HasReaderOptions s)
135− => ParserT Text s m Text
136+ => ParserT Sources s m Text
136rawLaTeXBlock = do
137rawLaTeXBlock = do
137 lookAhead (try (char '\\' >> letter))
138 lookAhead (try (char '\\' >> letter))
138 inp <- getInput
139 inp <- getInput
139− let toks = tokenize "source" inp
140+ let toks = tokenizeSources inp
140 snd <$> (rawLaTeXParser toks False (macroDef (const mempty)) blocks
141 snd <$> (rawLaTeXParser toks False (macroDef (const mempty)) blocks
141 <|> rawLaTeXParser toks True
142 <|> rawLaTeXParser toks True
142 (do choice (map controlSeq
143 (do choice (map controlSeq
@@ -163,14 +164,14 @@ beginOrEndCommand = try $ do
163 (txt <> untokenize rawargs)
164 (txt <> untokenize rawargs)
164
165
165rawLaTeXInline :: (PandocMonad m, HasMacros s, HasReaderOptions s)
166rawLaTeXInline :: (PandocMonad m, HasMacros s, HasReaderOptions s)
166− => ParserT Text s m Text
167+ => ParserT Sources s m Text
167rawLaTeXInline = do
168rawLaTeXInline = do
168 lookAhead (try (char '\\' >> letter))
169 lookAhead (try (char '\\' >> letter))
169 inp <- getInput
170 inp <- getInput
170− let toks = tokenize "source" inp
171+ let toks = tokenizeSources inp
171 raw <- snd <$>
172 raw <- snd <$>
172 ( rawLaTeXParser toks True
173 ( rawLaTeXParser toks True
173− (mempty <$ (controlSeq "input" >> skipMany opt >> braced))
174+ (mempty <$ (controlSeq "input" >> skipMany rawopt >> braced))
174 inlines
175 inlines
175 <|> rawLaTeXParser toks True (inlineEnvironment <|> inlineCommand')
176 <|> rawLaTeXParser toks True (inlineEnvironment <|> inlineCommand')
176 inlines
177 inlines
@@ -178,11 +179,11 @@ rawLaTeXInline = do
178 finalbraces <- mconcat <$> many (try (string "{}")) -- see #5439
179 finalbraces <- mconcat <$> many (try (string "{}")) -- see #5439
179 return $ raw <> T.pack finalbraces
180 return $ raw <> T.pack finalbraces
180
181
181−inlineCommand :: PandocMonad m => ParserT Text ParserState m Inlines
182+inlineCommand :: PandocMonad m => ParserT Sources ParserState m Inlines
182inlineCommand = do
183inlineCommand = do
183 lookAhead (try (char '\\' >> letter))
184 lookAhead (try (char '\\' >> letter))
184 inp <- getInput
185 inp <- getInput
185− let toks = tokenize "source" inp
186+ let toks = tokenizeSources inp
186 fst <$> rawLaTeXParser toks True (inlineEnvironment <|> inlineCommand')
187 fst <$> rawLaTeXParser toks True (inlineEnvironment <|> inlineCommand')
187 inlines
188 inlines
188
189
@@ -191,12 +192,6 @@ inlineCommand = do
191word :: PandocMonad m => LP m Inlines
192word :: PandocMonad m => LP m Inlines
192word = str . untoken <$> satisfyTok isWordTok
193word = str . untoken <$> satisfyTok isWordTok
193
194
194−regularSymbol :: PandocMonad m => LP m Inlines
195−regularSymbol = str . untoken <$> satisfyTok isRegularSymbol
196− where isRegularSymbol (Tok _ Symbol t) = not $ T.any isSpecial t
197− isRegularSymbol _ = False
198− isSpecial c = c `Set.member` specialChars
199−
200inlineGroup :: PandocMonad m => LP m Inlines
195inlineGroup :: PandocMonad m => LP m Inlines
201inlineGroup = do
196inlineGroup = do
202 ils <- grouped inline
197 ils <- grouped inline
@@ -237,19 +232,6 @@ mkImage options (T.unpack -> src) = do
237 _ -> return src
232 _ -> return src
238 return $ imageWith attr (T.pack src') "" alt
233 return $ imageWith attr (T.pack src') "" alt
239
234
240−doxspace :: PandocMonad m => LP m Inlines
241−doxspace =
242− (space <$ lookAhead (satisfyTok startsWithLetter)) <|> return mempty
243− where startsWithLetter (Tok _ Word t) =
244− case T.uncons t of
245− Just (c, _) | isLetter c -> True
246− _ -> False
247− startsWithLetter _ = False
248−
249−
250−lit :: Text -> LP m Inlines
251−lit = pure . str
252−
253removeDoubleQuotes :: Text -> Text
235removeDoubleQuotes :: Text -> Text
254removeDoubleQuotes t =
236removeDoubleQuotes t =
255 Data.Maybe.fromMaybe t $ T.stripPrefix "\"" t >>= T.stripSuffix "\""
237 Data.Maybe.fromMaybe t $ T.stripPrefix "\"" t >>= T.stripSuffix "\""
@@ -296,23 +278,14 @@ quoted' f starter ender = do
296 cs -> cs)
278 cs -> cs)
297 else lit startchs
279 else lit startchs
298
280
299−enquote :: PandocMonad m => Bool -> Maybe Text -> LP m Inlines
281+lit :: Text -> LP m Inlines
300−enquote starred mblang = do
282+lit = pure . str
301− skipopts
302− let lang = mblang >>= babelLangToBCP47
303− let langspan = case lang of
304− Nothing -> id
305− Just l -> spanWith ("",[],[("lang", renderLang l)])
306− quoteContext <- sQuoteContext <$> getState
307− if starred || quoteContext == InDoubleQuote
308− then singleQuoted . langspan <$> withQuoteContext InSingleQuote tok
309− else doubleQuoted . langspan <$> withQuoteContext InDoubleQuote tok
310
283
311blockquote :: PandocMonad m => Bool -> Maybe Text -> LP m Blocks
284blockquote :: PandocMonad m => Bool -> Maybe Text -> LP m Blocks
312blockquote cvariant mblang = do
285blockquote cvariant mblang = do
313 citepar <- if cvariant
286 citepar <- if cvariant
314 then (\xs -> para (cite xs mempty))
287 then (\xs -> para (cite xs mempty))
315− <$> cites NormalCitation False
288+ <$> cites inline NormalCitation False
316 else option mempty $ para <$> bracketed inline
289 else option mempty $ para <$> bracketed inline
317 let lang = mblang >>= babelLangToBCP47
290 let lang = mblang >>= babelLangToBCP47
318 let langdiv = case lang of
291 let langdiv = case lang of
@@ -323,224 +296,13 @@ blockquote cvariant mblang = do
323 optional $ symbolIn (".:;?!" :: [Char]) -- currently ignored
296 optional $ symbolIn (".:;?!" :: [Char]) -- currently ignored
324 return $ blockQuote . langdiv $ (bs <> citepar)
297 return $ blockQuote . langdiv $ (bs <> citepar)
325
298
326−doAcronym :: PandocMonad m => Text -> LP m Inlines
327−doAcronym form = do
328− acro <- braced
329− return . mconcat $ [spanWith ("",[],[("acronym-label", untokenize acro),
330− ("acronym-form", "singular+" <> form)])
331− $ str $ untokenize acro]
332−
333−doAcronymPlural :: PandocMonad m => Text -> LP m Inlines
334−doAcronymPlural form = do
335− acro <- braced
336− plural <- lit "s"
337− return . mconcat $ [spanWith ("",[],[("acronym-label", untokenize acro),
338− ("acronym-form", "plural+" <> form)]) $
339− mconcat [str $ untokenize acro, plural]]
340−
341−doverb :: PandocMonad m => LP m Inlines
342−doverb = do
343− Tok _ Symbol t <- anySymbol
344− marker <- case T.uncons t of
345− Just (c, ts) | T.null ts -> return c
346− _ -> mzero
347− withVerbatimMode $
348− code . untokenize <$>
349− manyTill (notFollowedBy newlineTok >> verbTok marker) (symbol marker)
350−
351−verbTok :: PandocMonad m => Char -> LP m Tok
352−verbTok stopchar = do
353− t@(Tok pos toktype txt) <- anyTok
354− case T.findIndex (== stopchar) txt of
355− Nothing -> return t
356− Just i -> do
357− let (t1, t2) = T.splitAt i txt
358− inp <- getInput
359− setInput $ Tok (incSourceColumn pos i) Symbol (T.singleton stopchar)
360− : totoks (incSourceColumn pos (i + 1)) (T.drop 1 t2) ++ inp
361− return $ Tok pos toktype t1
362−
363−listingsLanguage :: [(Text, Text)] -> Maybe Text
364−listingsLanguage opts =
365− case lookup "language" opts of
366− Nothing -> Nothing
367− Just l -> fromListingsLanguage l `mplus` Just l
368−
369−dolstinline :: PandocMonad m => LP m Inlines
370−dolstinline = do
371− options <- option [] keyvals
372− let classes = maybeToList $ listingsLanguage options
373− doinlinecode classes
374−
375−domintinline :: PandocMonad m => LP m Inlines
376−domintinline = do
377− skipopts
378− cls <- untokenize <$> braced
379− doinlinecode [cls]
380−
381−doinlinecode :: PandocMonad m => [Text] -> LP m Inlines
382−doinlinecode classes = do
383− Tok _ Symbol t <- anySymbol
384− marker <- case T.uncons t of
385− Just (c, ts) | T.null ts -> return c
386− _ -> mzero
387− let stopchar = if marker == '{' then '}' else marker
388− withVerbatimMode $
389− codeWith ("",classes,[]) . T.map nlToSpace . untokenize <$>
390− manyTill (verbTok stopchar) (symbol stopchar)
391−
392−nlToSpace :: Char -> Char
393−nlToSpace '\n' = ' '
394−nlToSpace x = x
395−
396−mathDisplay :: Text -> Inlines
397−mathDisplay = displayMath . trimMath
398−
399−mathInline :: Text -> Inlines
400−mathInline = math . trimMath
401−
402−dollarsMath :: PandocMonad m => LP m Inlines
403−dollarsMath = do
404− symbol '$'
405− display <- option False (True <$ symbol '$')
406− (do contents <- try $ untokenize <$> pDollarsMath 0
407− if display
408− then mathDisplay contents <$ symbol '$'
409− else return $ mathInline contents)
410− <|> (guard display >> return (mathInline ""))
411−
412−-- Int is number of embedded groupings
413−pDollarsMath :: PandocMonad m => Int -> LP m [Tok]
414−pDollarsMath n = do
415− tk@(Tok _ toktype t) <- anyTok
416− case toktype of
417− Symbol | t == "$"
418− , n == 0 -> return []
419− | t == "\\" -> do
420− tk' <- anyTok
421− (tk :) . (tk' :) <$> pDollarsMath n
422− | t == "{" -> (tk :) <$> pDollarsMath (n+1)
423− | t == "}" ->
424− if n > 0
425− then (tk :) <$> pDollarsMath (n-1)
426− else mzero
427− _ -> (tk :) <$> pDollarsMath n
428−
429−-- citations
430−
431−addPrefix :: [Inline] -> [Citation] -> [Citation]
432−addPrefix p (k:ks) = k {citationPrefix = p ++ citationPrefix k} : ks
433−addPrefix _ _ = []
434−
435−addSuffix :: [Inline] -> [Citation] -> [Citation]
436−addSuffix s ks@(_:_) =
437− let k = last ks
438− in init ks ++ [k {citationSuffix = citationSuffix k ++ s}]
439−addSuffix _ _ = []
440−
441−simpleCiteArgs :: PandocMonad m => LP m [Citation]
442−simpleCiteArgs = try $ do
443− first <- optionMaybe $ toList <$> opt
444− second <- optionMaybe $ toList <$> opt
445− keys <- try $ bgroup *> manyTill citationLabel egroup
446− let (pre, suf) = case (first , second ) of
447− (Just s , Nothing) -> (mempty, s )
448− (Just s , Just t ) -> (s , t )
449− _ -> (mempty, mempty)
450− conv k = Citation { citationId = k
451− , citationPrefix = []
452− , citationSuffix = []
453− , citationMode = NormalCitation
454− , citationHash = 0
455− , citationNoteNum = 0
456− }
457− return $ addPrefix pre $ addSuffix suf $ map conv keys
458−
459−citationLabel :: PandocMonad m => LP m Text
460−citationLabel = do
461− sp
462− untokenize <$>
463− (many1 (satisfyTok isWordTok <|> symbolIn bibtexKeyChar)
464− <* sp
465− <* optional (symbol ',')
466− <* sp)
467− where bibtexKeyChar = ".:;?!`'()/*@_+=-&[]" :: [Char]
468−
469−cites :: PandocMonad m => CitationMode -> Bool -> LP m [Citation]
470−cites mode multi = try $ do
471− cits <- if multi
472− then do
473− multiprenote <- optionMaybe $ toList <$> paropt
474− multipostnote <- optionMaybe $ toList <$> paropt
475− let (pre, suf) = case (multiprenote, multipostnote) of
476− (Just s , Nothing) -> (mempty, s)
477− (Nothing , Just t) -> (mempty, t)
478− (Just s , Just t ) -> (s, t)
479− _ -> (mempty, mempty)
480− tempCits <- many1 simpleCiteArgs
481− case tempCits of
482− (k:ks) -> case ks of
483− (_:_) -> return $ (addMprenote pre k : init ks) ++
484− [addMpostnote suf (last ks)]
485− _ -> return [addMprenote pre (addMpostnote suf k)]
486− _ -> return [[]]
487− else count 1 simpleCiteArgs
488− let cs = concat cits
489− return $ case mode of
490− AuthorInText -> case cs of
491− (c:rest) -> c {citationMode = mode} : rest
492− [] -> []
493− _ -> map (\a -> a {citationMode = mode}) cs
494− where mprenote (k:ks) = (k:ks) ++ [Space]
495− mprenote _ = mempty
496− mpostnote (k:ks) = [Str ",", Space] ++ (k:ks)
497− mpostnote _ = mempty
498− addMprenote mpn (k:ks) =
499− let mpnfinal = case citationPrefix k of
500− (_:_) -> mprenote mpn
501− _ -> mpn
502− in addPrefix mpnfinal (k:ks)
503− addMprenote _ _ = []
504− addMpostnote = addSuffix . mpostnote
505−
506−citation :: PandocMonad m => Text -> CitationMode -> Bool -> LP m Inlines
507−citation name mode multi = do
508− (c,raw) <- withRaw $ cites mode multi
509− return $ cite c (rawInline "latex" $ "\\" <> name <> untokenize raw)
510−
511−handleCitationPart :: Inlines -> [Citation]
512−handleCitationPart ils =
513− let isCite Cite{} = True
514− isCite _ = False
515− (pref, rest) = break isCite (toList ils)
516− in case rest of
517− (Cite cs _:suff) -> addPrefix pref $ addSuffix suff cs
518− _ -> []
519−
520−complexNatbibCitation :: PandocMonad m => CitationMode -> LP m Inlines
521−complexNatbibCitation mode = try $ do
522− (cs, raw) <-
523− withRaw $ concat <$> do
524− bgroup
525− items <- mconcat <$>
526− many1 (notFollowedBy (symbol ';') >> inline)
527− `sepBy1` symbol ';'
528− egroup
529− return $ map handleCitationPart items
530− case cs of
531− [] -> mzero
532− (c:cits) -> return $ cite (c{ citationMode = mode }:cits)
533− (rawInline "latex" $ "\\citetext" <> untokenize raw)
534−
535−inNote :: Inlines -> Inlines
536−inNote ils =
537− note $ para $ ils <> str "."
538−
539inlineCommand' :: PandocMonad m => LP m Inlines
299inlineCommand' :: PandocMonad m => LP m Inlines
540inlineCommand' = try $ do
300inlineCommand' = try $ do
541 Tok _ (CtrlSeq name) cmd <- anyControlSeq
301 Tok _ (CtrlSeq name) cmd <- anyControlSeq
542 guard $ name /= "begin" && name /= "end" && name /= "and"
302 guard $ name /= "begin" && name /= "end" && name /= "and"
303+ star <- if T.all isAlphaNum name
543− star <- option "" ("*" <$ symbol '*' <* sp)
304+ then option "" ("*" <$ symbol '*' <* sp)
305+ else pure ""
544 overlay <- option "" overlaySpecification
306 overlay <- option "" overlaySpecification
545 let name' = name <> star <> overlay
307 let name' = name <> star <> overlay
546 let names = ordNub [name', name] -- check non-starred as fallback
308 let names = ordNub [name', name] -- check non-starred as fallback
@@ -551,28 +313,8 @@ inlineCommand' = try $ do
551 <|> ignore rawcommand
313 <|> ignore rawcommand
552 lookupListDefault raw names inlineCommands
314 lookupListDefault raw names inlineCommands
553
315
554−
555tok :: PandocMonad m => LP m Inlines
316tok :: PandocMonad m => LP m Inlines
556−tok = try $ spaces >> grouped inline <|> inlineCommand' <|> singleChar'
317+tok = tokWith inline
557− where singleChar' = do
558− Tok _ _ t <- singleChar
559− return $ str t
560−
561−opt :: PandocMonad m => LP m Inlines
562−opt = do
563− toks <- try (sp *> bracketedToks <* sp)
564− -- now parse the toks as inlines
565− st <- getState
566− parsed <- runParserT (mconcat <$> many inline) st "bracketed option" toks
567− case parsed of
568− Right result -> return result
569− Left e -> throwError $ PandocParsecError (untokenize toks) e
570−
571−paropt :: PandocMonad m => LP m Inlines
572−paropt = parenWrapped inline
573−
574−inBrackets :: Inlines -> Inlines
575−inBrackets x = str "[" <> x <> str "]"
576
318
577unescapeURL :: Text -> Text
319unescapeURL :: Text -> Text
578unescapeURL = T.concat . go . T.splitOn "\\"
320unescapeURL = T.concat . go . T.splitOn "\\"
@@ -585,379 +327,107 @@ unescapeURL = T.concat . go . T.splitOn "\\"
585 , isEscapable c = t
327 , isEscapable c = t
586 | otherwise = "\\" <> t
328 | otherwise = "\\" <> t
587
329
588−mathEnvWith :: PandocMonad m
589− => (Inlines -> a) -> Maybe Text -> Text -> LP m a
590−mathEnvWith f innerEnv name = f . mathDisplay . inner <$> mathEnv name
591− where inner x = case innerEnv of
592− Nothing -> x
593− Just y -> "\\begin{" <> y <> "}\n" <> x <>
594− "\\end{" <> y <> "}"
595−
596−mathEnv :: PandocMonad m => Text -> LP m Text
597−mathEnv name = do
598− skipopts
599− optional blankline
600− res <- manyTill anyTok (end_ name)
601− return $ stripTrailingNewlines $ untokenize res
602−
603−inlineEnvironment :: PandocMonad m => LP m Inlines
604−inlineEnvironment = try $ do
605− controlSeq "begin"
606− name <- untokenize <$> braced
607− M.findWithDefault mzero name inlineEnvironments
608−
609−inlineEnvironments :: PandocMonad m => M.Map Text (LP m Inlines)
610−inlineEnvironments = M.fromList [
611− ("displaymath", mathEnvWith id Nothing "displaymath")
612− , ("math", math <$> mathEnv "math")
613− , ("equation", mathEnvWith id Nothing "equation")
614− , ("equation*", mathEnvWith id Nothing "equation*")
615− , ("gather", mathEnvWith id (Just "gathered") "gather")
616− , ("gather*", mathEnvWith id (Just "gathered") "gather*")
617− , ("multline", mathEnvWith id (Just "gathered") "multline")
618− , ("multline*", mathEnvWith id (Just "gathered") "multline*")
619− , ("eqnarray", mathEnvWith id (Just "aligned") "eqnarray")
620− , ("eqnarray*", mathEnvWith id (Just "aligned") "eqnarray*")
621− , ("align", mathEnvWith id (Just "aligned") "align")
622− , ("align*", mathEnvWith id (Just "aligned") "align*")
623− , ("alignat", mathEnvWith id (Just "aligned") "alignat")
624− , ("alignat*", mathEnvWith id (Just "aligned") "alignat*")
625− , ("dmath", mathEnvWith id Nothing "dmath")
626− , ("dmath*", mathEnvWith id Nothing "dmath*")
627− , ("dgroup", mathEnvWith id (Just "aligned") "dgroup")
628− , ("dgroup*", mathEnvWith id (Just "aligned") "dgroup*")
629− , ("darray", mathEnvWith id (Just "aligned") "darray")
630− , ("darray*", mathEnvWith id (Just "aligned") "darray*")
631− ]
632−
633inlineCommands :: PandocMonad m => M.Map Text (LP m Inlines)
330inlineCommands :: PandocMonad m => M.Map Text (LP m Inlines)
634−inlineCommands = M.union inlineLanguageCommands $ M.fromList
331+inlineCommands = M.unions
635− [ ("emph", extractSpaces emph <$> tok)
332+ [ accentCommands tok
636− , ("textit", extractSpaces emph <$> tok)
333+ , citationCommands inline
637− , ("textsl", extractSpaces emph <$> tok)
334+ , siunitxCommands tok
638− , ("textsc", extractSpaces smallcaps <$> tok)
335+ , acronymCommands
639− , ("textsf", extractSpaces (spanWith ("",["sans-serif"],[])) <$> tok)
336+ , refCommands
640− , ("textmd", extractSpaces (spanWith ("",["medium"],[])) <$> tok)
337+ , nameCommands
641− , ("textrm", extractSpaces (spanWith ("",["roman"],[])) <$> tok)
338+ , verbCommands
642− , ("textup", extractSpaces (spanWith ("",["upright"],[])) <$> tok)
339+ , charCommands
643− , ("texttt", ttfamily)
340+ , enquoteCommands tok
644− , ("sout", extractSpaces strikeout <$> tok)
341+ , inlineLanguageCommands tok
645− , ("alert", skipopts >> spanWith ("",["alert"],[]) <$> tok) -- beamer
342+ , biblatexInlineCommands tok
646− , ("lq", return (str "‘"))
343+ , rest ]
647− , ("rq", return (str "’"))
344+ where
648− , ("textquoteleft", return (str "‘"))
345+ rest = M.fromList
649− , ("textquoteright", return (str "’"))
346+ [ ("emph", extractSpaces emph <$> tok)
650− , ("textquotedblleft", return (str "“"))
347+ , ("textit", extractSpaces emph <$> tok)
651− , ("textquotedblright", return (str "”"))
348+ , ("textsl", extractSpaces emph <$> tok)
652− , ("textsuperscript", extractSpaces superscript <$> tok)
349+ , ("textsc", extractSpaces smallcaps <$> tok)
653− , ("textsubscript", extractSpaces subscript <$> tok)
350+ , ("textsf", extractSpaces (spanWith ("",["sans-serif"],[])) <$> tok)
654− , ("textbackslash", lit "\\")
351+ , ("textmd", extractSpaces (spanWith ("",["medium"],[])) <$> tok)
655− , ("backslash", lit "\\")
352+ , ("textrm", extractSpaces (spanWith ("",["roman"],[])) <$> tok)
656− , ("slash", lit "/")
353+ , ("textup", extractSpaces (spanWith ("",["upright"],[])) <$> tok)
657− , ("textbf", extractSpaces strong <$> tok)
354+ , ("texttt", ttfamily)
658− , ("textnormal", extractSpaces (spanWith ("",["nodecor"],[])) <$> tok)
355+ , ("sout", extractSpaces strikeout <$> tok)
659− , ("underline", underline <$> tok)
356+ , ("alert", skipopts >> spanWith ("",["alert"],[]) <$> tok) -- beamer
660− , ("ldots", lit "…")
357+ , ("textsuperscript", extractSpaces superscript <$> tok)
661− , ("vdots", lit "\8942")
358+ , ("textsubscript", extractSpaces subscript <$> tok)
662− , ("dots", lit "…")
359+ , ("textbf", extractSpaces strong <$> tok)
663− , ("mdots", lit "…")
360+ , ("textnormal", extractSpaces (spanWith ("",["nodecor"],[])) <$> tok)
664− , ("sim", lit "~")
361+ , ("underline", underline <$> tok)
665− , ("sep", lit ",")
362+ , ("mbox", rawInlineOr "mbox" $ processHBox <$> tok)
666− , ("label", rawInlineOr "label" dolabel)
363+ , ("hbox", rawInlineOr "hbox" $ processHBox <$> tok)
667− , ("ref", rawInlineOr "ref" $ doref "ref")
364+ , ("lettrine", rawInlineOr "lettrine" lettrine)
668− , ("cref", rawInlineOr "cref" $ doref "ref") -- from cleveref.sty
365+ , ("(", mathInline . untokenize <$> manyTill anyTok (controlSeq ")"))
669− , ("vref", rawInlineOr "vref" $ doref "ref+page") -- from varioref.sty
366+ , ("[", mathDisplay . untokenize <$> manyTill anyTok (controlSeq "]"))
670− , ("eqref", rawInlineOr "eqref" $ doref "eqref") -- from amsmath.sty
367+ , ("ensuremath", mathInline . untokenize <$> braced)
671− , ("mbox", rawInlineOr "mbox" $ processHBox <$> tok)
368+ , ("texorpdfstring", const <$> tok <*> tok)
672− , ("hbox", rawInlineOr "hbox" $ processHBox <$> tok)
369+ -- old TeX commands
673− , ("lettrine", rawInlineOr "lettrine" lettrine)
370+ , ("em", extractSpaces emph <$> inlines)
674− , ("(", mathInline . untokenize <$> manyTill anyTok (controlSeq ")"))
371+ , ("it", extractSpaces emph <$> inlines)
675− , ("[", mathDisplay . untokenize <$> manyTill anyTok (controlSeq "]"))
372+ , ("sl", extractSpaces emph <$> inlines)
676− , ("ensuremath", mathInline . untokenize <$> braced)
373+ , ("bf", extractSpaces strong <$> inlines)
677− , ("texorpdfstring", const <$> tok <*> tok)
374+ , ("tt", code . stringify . toList <$> inlines)
678− , ("P", lit "¶")
375+ , ("rm", inlines)
679− , ("S", lit "§")
376+ , ("itshape", extractSpaces emph <$> inlines)
680− , ("$", lit "$")
377+ , ("slshape", extractSpaces emph <$> inlines)
681− , ("%", lit "%")
378+ , ("scshape", extractSpaces smallcaps <$> inlines)
682− , ("&", lit "&")
379+ , ("bfseries", extractSpaces strong <$> inlines)
683− , ("#", lit "#")
380+ , ("MakeUppercase", makeUppercase <$> tok)
684− , ("_", lit "_")
381+ , ("MakeTextUppercase", makeUppercase <$> tok) -- textcase
685− , ("{", lit "{")
382+ , ("uppercase", makeUppercase <$> tok)
686− , ("}", lit "}")
383+ , ("MakeLowercase", makeLowercase <$> tok)
687− , ("qed", lit "\a0\x25FB")
384+ , ("MakeTextLowercase", makeLowercase <$> tok)
688− -- old TeX commands
385+ , ("lowercase", makeLowercase <$> tok)
689− , ("em", extractSpaces emph <$> inlines)
386+ , ("thanks", skipopts >> note <$> grouped block)
690− , ("it", extractSpaces emph <$> inlines)
387+ , ("footnote", skipopts >> note <$> grouped block)
691− , ("sl", extractSpaces emph <$> inlines)
388+ , ("passthrough", tok) -- \passthrough macro used by latex writer
692− , ("bf", extractSpaces strong <$> inlines)
389+ -- for listings
693− , ("tt", code . stringify . toList <$> inlines)
390+ , ("includegraphics", do options <- option [] keyvals
694− , ("rm", inlines)
391+ src <- braced
695− , ("itshape", extractSpaces emph <$> inlines)
392+ mkImage options .
696− , ("slshape", extractSpaces emph <$> inlines)
393+ unescapeURL .
697− , ("scshape", extractSpaces smallcaps <$> inlines)
394+ removeDoubleQuotes $ untokenize src)
698− , ("bfseries", extractSpaces strong <$> inlines)
395+ -- hyperref
699− , ("MakeUppercase", makeUppercase <$> tok)
396+ , ("url", (\url -> link url "" (str url)) . unescapeURL . untokenize <$>
700− , ("MakeTextUppercase", makeUppercase <$> tok) -- textcase
397+ bracedUrl)
701− , ("uppercase", makeUppercase <$> tok)
398+ , ("nolinkurl", code . unescapeURL . untokenize <$> bracedUrl)
702− , ("MakeLowercase", makeLowercase <$> tok)
399+ , ("href", do url <- bracedUrl
703− , ("MakeTextLowercase", makeLowercase <$> tok)
400+ sp
704− , ("lowercase", makeLowercase <$> tok)
401+ link (unescapeURL $ untokenize url) "" <$> tok)
705− , ("/", pure mempty) -- italic correction
402+ , ("hyperlink", hyperlink)
706− , ("aa", lit "å")
403+ , ("hyperref", hyperref)
707− , ("AA", lit "Å")
404+ , ("hypertarget", hypertargetInline)
708− , ("ss", lit "ß")
405+ -- hyphenat
709− , ("o", lit "ø")
406+ , ("nohyphens", tok)
710− , ("O", lit "Ø")
407+ , ("textnhtt", ttfamily)
711− , ("L", lit "Ł")
408+ , ("nhttfamily", ttfamily)
712− , ("l", lit "ł")
409+ -- LaTeX colors
713− , ("ae", lit "æ")
410+ , ("textcolor", coloredInline "color")
714− , ("AE", lit "Æ")
411+ , ("colorbox", coloredInline "background-color")
715− , ("oe", lit "œ")
412+ -- etoolbox
716− , ("OE", lit "Œ")
413+ , ("ifstrequal", ifstrequal)
717− , ("pounds", lit "£")
414+ , ("newtoggle", braced >>= newToggle)
718− , ("euro", lit "€")
415+ , ("toggletrue", braced >>= setToggle True)
719− , ("copyright", lit "©")
416+ , ("togglefalse", braced >>= setToggle False)
720− , ("textasciicircum", lit "^")
417+ , ("iftoggle", try $ ifToggle >> inline)
721− , ("textasciitilde", lit "~")
418+ -- include
722− , ("H", accent '\779' Nothing) -- hungarumlaut
419+ , ("input", rawInlineOr "input" $ include "input")
723− , ("`", accent '\768' (Just '`')) -- grave
420+ -- soul package
724− , ("'", accent '\769' (Just '\'')) -- acute
421+ , ("ul", underline <$> tok)
725− , ("^", accent '\770' (Just '^')) -- circ
422+ -- ulem package
726− , ("~", accent '\771' (Just '~')) -- tilde
423+ , ("uline", underline <$> tok)
727− , ("\"", accent '\776' Nothing) -- umlaut
424+ -- plain tex stuff that should just be passed through as raw tex
728− , (".", accent '\775' Nothing) -- dot
425+ , ("ifdim", ifdim)
729− , ("=", accent '\772' Nothing) -- macron
426+ ]
730− , ("|", accent '\781' Nothing) -- vertical line above
731− , ("b", accent '\817' Nothing) -- macron below
732− , ("c", accent '\807' Nothing) -- cedilla
733− , ("G", accent '\783' Nothing) -- doublegrave
734− , ("h", accent '\777' Nothing) -- hookabove
735− , ("d", accent '\803' Nothing) -- dotbelow
736− , ("f", accent '\785' Nothing) -- inverted breve
737− , ("r", accent '\778' Nothing) -- ringabove
738− , ("t", accent '\865' Nothing) -- double inverted breve
739− , ("U", accent '\782' Nothing) -- double vertical line above
740− , ("v", accent '\780' Nothing) -- hacek
741− , ("u", accent '\774' Nothing) -- breve
742− , ("k", accent '\808' Nothing) -- ogonek
743− , ("textogonekcentered", accent '\808' Nothing) -- ogonek
744− , ("i", lit "ı") -- dotless i
745− , ("j", lit "ȷ") -- dotless j
746− , ("newtie", accent '\785' Nothing) -- inverted breve
747− , ("textcircled", accent '\8413' Nothing) -- combining circle
748− , ("\\", linebreak <$ (do inTableCell <- sInTableCell <$> getState
749− guard $ not inTableCell
750− optional opt
751− spaces))
752− , (",", lit "\8198")
753− , ("@", pure mempty)
754− , (" ", lit "\160")
755− , ("ps", pure $ str "PS." <> space)
756− , ("TeX", lit "TeX")
757− , ("LaTeX", lit "LaTeX")
758− , ("bar", lit "|")
759− , ("textless", lit "<")
760− , ("textgreater", lit ">")
761− , ("thanks", skipopts >> note <$> grouped block)
762− , ("footnote", skipopts >> note <$> grouped block)
763− , ("passthrough", tok) -- \passthrough macro used by latex writer
764− -- for listings
765− , ("verb", doverb)
766− , ("lstinline", dolstinline)
767− , ("mintinline", domintinline)
768− , ("Verb", doverb)
769− , ("url", (\url -> link url "" (str url)) . unescapeURL . untokenize <$>
770− bracedUrl)
771− , ("nolinkurl", code . unescapeURL . untokenize <$> bracedUrl)
772− , ("href", do url <- bracedUrl
773− sp
774− link (unescapeURL $ untokenize url) "" <$> tok)
775− , ("includegraphics", do options <- option [] keyvals
776− src <- braced
777− mkImage options . unescapeURL . removeDoubleQuotes $
778− untokenize src)
779− , ("enquote*", enquote True Nothing)
780− , ("enquote", enquote False Nothing)
781− -- foreignquote is supposed to use native quote marks
782− , ("foreignquote*", braced >>= enquote True . Just . untokenize)
783− , ("foreignquote", braced >>= enquote False . Just . untokenize)
784− -- hypehnquote uses regular quotes
785− , ("hyphenquote*", braced >>= enquote True . Just . untokenize)
786− , ("hyphenquote", braced >>= enquote False . Just . untokenize)
787− , ("figurename", doTerm Translations.Figure)
788− , ("prefacename", doTerm Translations.Preface)
789− , ("refname", doTerm Translations.References)
790− , ("bibname", doTerm Translations.Bibliography)
791− , ("chaptername", doTerm Translations.Chapter)
792− , ("partname", doTerm Translations.Part)
793− , ("contentsname", doTerm Translations.Contents)
794− , ("listfigurename", doTerm Translations.ListOfFigures)
795− , ("listtablename", doTerm Translations.ListOfTables)
796− , ("indexname", doTerm Translations.Index)
797− , ("abstractname", doTerm Translations.Abstract)
798− , ("tablename", doTerm Translations.Table)
799− , ("enclname", doTerm Translations.Encl)
800− , ("ccname", doTerm Translations.Cc)
801− , ("headtoname", doTerm Translations.To)
802− , ("pagename", doTerm Translations.Page)
803− , ("seename", doTerm Translations.See)
804− , ("seealsoname", doTerm Translations.SeeAlso)
805− , ("proofname", doTerm Translations.Proof)
806− , ("glossaryname", doTerm Translations.Glossary)
807− , ("lstlistingname", doTerm Translations.Listing)
808− , ("cite", citation "cite" NormalCitation False)
809− , ("Cite", citation "Cite" NormalCitation False)
810− , ("citep", citation "citep" NormalCitation False)
811− , ("citep*", citation "citep*" NormalCitation False)
812− , ("citeal", citation "citeal" NormalCitation False)
813− , ("citealp", citation "citealp" NormalCitation False)
814− , ("citealp*", citation "citealp*" NormalCitation False)
815− , ("autocite", citation "autocite" NormalCitation False)
816− , ("smartcite", citation "smartcite" NormalCitation False)
817− , ("footcite", inNote <$> citation "footcite" NormalCitation False)
818− , ("parencite", citation "parencite" NormalCitation False)
819− , ("supercite", citation "supercite" NormalCitation False)
820− , ("footcitetext", inNote <$> citation "footcitetext" NormalCitation False)
821− , ("citeyearpar", citation "citeyearpar" SuppressAuthor False)
822− , ("citeyear", citation "citeyear" SuppressAuthor False)
823− , ("autocite*", citation "autocite*" SuppressAuthor False)
824− , ("cite*", citation "cite*" SuppressAuthor False)
825− , ("parencite*", citation "parencite*" SuppressAuthor False)
826− , ("textcite", citation "textcite" AuthorInText False)
827− , ("citet", citation "citet" AuthorInText False)
828− , ("citet*", citation "citet*" AuthorInText False)
829− , ("citealt", citation "citealt" AuthorInText False)
830− , ("citealt*", citation "citealt*" AuthorInText False)
831− , ("textcites", citation "textcites" AuthorInText True)
832− , ("cites", citation "cites" NormalCitation True)
833− , ("autocites", citation "autocites" NormalCitation True)
834− , ("footcites", inNote <$> citation "footcites" NormalCitation True)
835− , ("parencites", citation "parencites" NormalCitation True)
836− , ("supercites", citation "supercites" NormalCitation True)
837− , ("footcitetexts", inNote <$> citation "footcitetexts" NormalCitation True)
838− , ("Autocite", citation "Autocite" NormalCitation False)
839− , ("Smartcite", citation "Smartcite" NormalCitation False)
840− , ("Footcite", inNote <$> citation "Footcite" NormalCitation False)
841− , ("Parencite", citation "Parencite" NormalCitation False)
842− , ("Supercite", citation "Supercite" NormalCitation False)
843− , ("Footcitetext", inNote <$> citation "Footcitetext" NormalCitation False)
844− , ("Citeyearpar", citation "Citeyearpar" SuppressAuthor False)
845− , ("Citeyear", citation "Citeyear" SuppressAuthor False)
846− , ("Autocite*", citation "Autocite*" SuppressAuthor False)
847− , ("Cite*", citation "Cite*" SuppressAuthor False)
848− , ("Parencite*", citation "Parencite*" SuppressAuthor False)
849− , ("Textcite", citation "Textcite" AuthorInText False)
850− , ("Textcites", citation "Textcites" AuthorInText True)
851− , ("Cites", citation "Cites" NormalCitation True)
852− , ("Autocites", citation "Autocites" NormalCitation True)
853− , ("Footcites", inNote <$> citation "Footcites" NormalCitation True)
854− , ("Parencites", citation "Parencites" NormalCitation True)
855− , ("Supercites", citation "Supercites" NormalCitation True)
856− , ("Footcitetexts", inNote <$> citation "Footcitetexts" NormalCitation True)
857− , ("citetext", complexNatbibCitation NormalCitation)
858− , ("citeauthor", (try (tok *> sp *> controlSeq "citetext") *>
859− complexNatbibCitation AuthorInText)
860− <|> citation "citeauthor" AuthorInText False)
861− , ("nocite", mempty <$ (citation "nocite" NormalCitation False >>=
862− addMeta "nocite"))
863− , ("hyperlink", hyperlink)
864− , ("hypertarget", hypertargetInline)
865− -- glossaries package
866− , ("gls", doAcronym "short")
867− , ("Gls", doAcronym "short")
868− , ("glsdesc", doAcronym "long")
869− , ("Glsdesc", doAcronym "long")
870− , ("GLSdesc", doAcronym "long")
871− , ("acrlong", doAcronym "long")
872− , ("Acrlong", doAcronym "long")
873− , ("acrfull", doAcronym "full")
874− , ("Acrfull", doAcronym "full")
875− , ("acrshort", doAcronym "abbrv")
876− , ("Acrshort", doAcronym "abbrv")
877− , ("glspl", doAcronymPlural "short")
878− , ("Glspl", doAcronymPlural "short")
879− , ("glsdescplural", doAcronymPlural "long")
880− , ("Glsdescplural", doAcronymPlural "long")
881− , ("GLSdescplural", doAcronymPlural "long")
882− -- acronyms package
883− , ("ac", doAcronym "short")
884− , ("acf", doAcronym "full")
885− , ("acs", doAcronym "abbrv")
886− , ("acl", doAcronym "long")
887− , ("acp", doAcronymPlural "short")
888− , ("acfp", doAcronymPlural "full")
889− , ("acsp", doAcronymPlural "abbrv")
890− , ("aclp", doAcronymPlural "long")
891− , ("Ac", doAcronym "short")
892− , ("Acf", doAcronym "full")
893− , ("Acs", doAcronym "abbrv")
894− , ("Acl", doAcronym "long")
895− , ("Acp", doAcronymPlural "short")
896− , ("Acfp", doAcronymPlural "full")
897− , ("Acsp", doAcronymPlural "abbrv")
898− , ("Aclp", doAcronymPlural "long")
899− -- siuntix
900− , ("si", skipopts *> dosi tok)
901− , ("SI", doSI tok)
902− , ("SIrange", doSIrange True tok)
903− , ("numrange", doSIrange False tok)
904− , ("numlist", doSInumlist)
905− , ("num", doSInum)
906− , ("ang", doSIang)
907− -- hyphenat
908− , ("bshyp", lit "\\\173")
909− , ("fshyp", lit "/\173")
910− , ("dothyp", lit ".\173")
911− , ("colonhyp", lit ":\173")
912− , ("hyp", lit "-")
913− , ("nohyphens", tok)
914− , ("textnhtt", ttfamily)
915− , ("nhttfamily", ttfamily)
916− -- LaTeX colors
917− , ("textcolor", coloredInline "color")
918− , ("colorbox", coloredInline "background-color")
919− -- fontawesome
920− , ("faCheck", lit "\10003")
921− , ("faClose", lit "\10007")
922− -- xspace
923− , ("xspace", doxspace)
924− -- etoolbox
925− , ("ifstrequal", ifstrequal)
926− , ("newtoggle", braced >>= newToggle)
927− , ("toggletrue", braced >>= setToggle True)
928− , ("togglefalse", braced >>= setToggle False)
929− , ("iftoggle", try $ ifToggle >> inline)
930− -- biblatex misc
931− , ("RN", romanNumeralUpper)
932− , ("Rn", romanNumeralLower)
933− -- babel
934− , ("foreignlanguage", foreignlanguage)
935− -- include
936− , ("input", rawInlineOr "input" $ include "input")
937− -- soul package
938− , ("ul", underline <$> tok)
939− -- ulem package
940− , ("uline", underline <$> tok)
941− -- plain tex stuff that should just be passed through as raw tex
942− , ("ifdim", ifdim)
943− ]
944−
945−accent :: PandocMonad m => Char -> Maybe Char -> LP m Inlines
946−accent combiningAccent fallBack = try $ do
947− ils <- tok
948− case toList ils of
949− (Str (T.uncons -> Just (x, xs)) : ys) -> return $ fromList $
950− -- try to normalize to the combined character:
951− Str (Normalize.normalize Normalize.NFC
952− (T.pack [x, combiningAccent]) <> xs) : ys
953− [Space] -> return $ str $ T.singleton $ fromMaybe combiningAccent fallBack
954− [] -> return $ str $ T.singleton $ fromMaybe combiningAccent fallBack
955− _ -> return ils
956−
957
427
958lettrine :: PandocMonad m => LP m Inlines
428lettrine :: PandocMonad m => LP m Inlines
959lettrine = do
429lettrine = do
960− optional opt
430+ optional rawopt
961 x <- tok
431 x <- tok
962 y <- tok
432 y <- tok
963 return $ extractSpaces (spanWith ("",["lettrine"],[])) x <> smallcaps y
433 return $ extractSpaces (spanWith ("",["lettrine"],[])) x <> smallcaps y
@@ -977,32 +447,18 @@ alterStr :: (Text -> Text) -> Inline -> Inline
977alterStr f (Str xs) = Str (f xs)
447alterStr f (Str xs) = Str (f xs)
978alterStr _ x = x
448alterStr _ x = x
979
449
980−foreignlanguage :: PandocMonad m => LP m Inlines
981−foreignlanguage = do
982− babelLang <- untokenize <$> braced
983− case babelLangToBCP47 babelLang of
984− Just lang -> spanWith ("", [], [("lang", renderLang lang)]) <$> tok
985− _ -> tok
986−
987−inlineLanguageCommands :: PandocMonad m => M.Map Text (LP m Inlines)
988−inlineLanguageCommands = M.fromList $ mk <$> M.toList polyglossiaLangToBCP47
989− where
990− mk (polyglossia, bcp47Func) =
991− ("text" <> polyglossia, inlineLanguage bcp47Func)
992−
993−inlineLanguage :: PandocMonad m => (Text -> Lang) -> LP m Inlines
994−inlineLanguage bcp47Func = do
995− o <- option "" $ T.filter (\c -> c /= '[' && c /= ']')
996− <$> rawopt
997− let lang = renderLang $ bcp47Func o
998− extractSpaces (spanWith ("", [], [("lang", lang)])) <$> tok
999−
1000hyperlink :: PandocMonad m => LP m Inlines
450hyperlink :: PandocMonad m => LP m Inlines
1001hyperlink = try $ do
451hyperlink = try $ do
1002 src <- untokenize <$> braced
452 src <- untokenize <$> braced
1003 lab <- tok
453 lab <- tok
1004 return $ link ("#" <> src) "" lab
454 return $ link ("#" <> src) "" lab
1005
455
456+hyperref :: PandocMonad m => LP m Inlines
457+hyperref = try $ do
458+ url <- (("#" <>) . untokenize <$> try (sp *> bracketedToks <* sp))
459+ <|> untokenize <$> (bracedUrl <* bracedUrl <* bracedUrl)
460+ link url "" <$> tok
461+
1006hypertargetBlock :: PandocMonad m => LP m Blocks
462hypertargetBlock :: PandocMonad m => LP m Blocks
1007hypertargetBlock = try $ do
463hypertargetBlock = try $ do
1008 ref <- untokenize <$> braced
464 ref <- untokenize <$> braced
@@ -1017,31 +473,6 @@ hypertargetInline = try $ do
1017 ils <- grouped inline
473 ils <- grouped inline
1018 return $ spanWith (ref, [], []) ils
474 return $ spanWith (ref, [], []) ils
1019
475
1020−romanNumeralUpper :: (PandocMonad m) => LP m Inlines
1021−romanNumeralUpper =
1022− str . toRomanNumeral <$> romanNumeralArg
1023−
1024−romanNumeralLower :: (PandocMonad m) => LP m Inlines
1025−romanNumeralLower =
1026− str . T.toLower . toRomanNumeral <$> romanNumeralArg
1027−
1028−romanNumeralArg :: (PandocMonad m) => LP m Int
1029−romanNumeralArg = spaces *> (parser <|> inBraces)
1030− where
1031− inBraces = do
1032− symbol '{'
1033− spaces
1034− res <- parser
1035− spaces
1036− symbol '}'
1037− return res
1038− parser = do
1039− Tok _ Word s <- satisfyTok isWordTok
1040− let (digits, rest) = T.span isDigit s
1041− unless (T.null rest) $
1042− Prelude.fail "Non-digits in argument to \\Rn or \\RN"
1043− safeRead digits
1044−
1045newToggle :: (Monoid a, PandocMonad m) => [Tok] -> LP m a
476newToggle :: (Monoid a, PandocMonad m) => [Tok] -> LP m a
1046newToggle name = do
477newToggle name = do
1047 updateState $ \st ->
478 updateState $ \st ->
@@ -1072,9 +503,6 @@ ifToggle = do
1072 report $ UndefinedToggle name' pos
503 report $ UndefinedToggle name' pos
1073 return ()
504 return ()
1074
505
1075−doTerm :: PandocMonad m => Translations.Term -> LP m Inlines
1076−doTerm term = str <$> translateTerm term
1077−
1078ifstrequal :: (PandocMonad m, Monoid a) => LP m a
506ifstrequal :: (PandocMonad m, Monoid a) => LP m a
1079ifstrequal = do
507ifstrequal = do
1080 str1 <- tok
508 str1 <- tok
@@ -1095,13 +523,6 @@ coloredInline stylename = do
1095ttfamily :: PandocMonad m => LP m Inlines
523ttfamily :: PandocMonad m => LP m Inlines
1096ttfamily = code . stringify . toList <$> tok
524ttfamily = code . stringify . toList <$> tok
1097
525
1098−rawInlineOr :: PandocMonad m => Text -> LP m Inlines -> LP m Inlines
1099−rawInlineOr name' fallback = do
1100− parseRaw <- extensionEnabled Ext_raw_tex <$> getOption readerExtensions
1101− if parseRaw
1102− then rawInline "latex" <$> getRawCommand name' ("\\" <> name')
1103− else fallback
1104−
1105processHBox :: Inlines -> Inlines
526processHBox :: Inlines -> Inlines
1106processHBox = walk convert
527processHBox = walk convert
1107 where
528 where
@@ -1152,67 +573,67 @@ treatAsInline = Set.fromList
1152 , "pagebreak"
573 , "pagebreak"
1153 ]
574 ]
1154
575
1155−label :: PandocMonad m => LP m ()
1156−label = do
1157− controlSeq "label"
1158− t <- braced
1159− updateState $ \st -> st{ sLastLabel = Just $ untokenize t }
1160−
1161−dolabel :: PandocMonad m => LP m Inlines
1162−dolabel = do
1163− v <- braced
1164− let refstr = untokenize v
1165− updateState $ \st ->
1166− st{ sLastLabel = Just refstr }
1167− return $ spanWith (refstr,[],[("label", refstr)])
1168− $ inBrackets $ str $ untokenize v
1169−
1170−doref :: PandocMonad m => Text -> LP m Inlines
1171−doref cls = do
1172− v <- braced
1173− let refstr = untokenize v
1174− return $ linkWith ("",[],[ ("reference-type", cls)
1175− , ("reference", refstr)])
1176− ("#" <> refstr)
1177− ""
1178− (inBrackets $ str refstr)
1179−
1180lookupListDefault :: (Ord k) => v -> [k] -> M.Map k v -> v
576lookupListDefault :: (Ord k) => v -> [k] -> M.Map k v -> v
1181lookupListDefault d = (fromMaybe d .) . lookupList
577lookupListDefault d = (fromMaybe d .) . lookupList
1182 where lookupList l m = msum $ map (`M.lookup` m) l
578 where lookupList l m = msum $ map (`M.lookup` m) l
1183
579
1184inline :: PandocMonad m => LP m Inlines
580inline :: PandocMonad m => LP m Inlines
581+inline = do
582+ Tok pos toktype t <- lookAhead anyTok
583+ let symbolAsString = str . untoken <$> anySymbol
584+ let unescapedSymbolAsString =
585+ do s <- untoken <$> anySymbol
586+ report $ ParsingUnescaped s pos
587+ return $ str s
588+ case toktype of
1185−inline = (mempty <$ comment)
589+ Comment -> mempty <$ comment
1186− <|> (space <$ whitespace)
590+ Spaces -> space <$ whitespace
1187− <|> (softbreak <$ endline)
591+ Newline -> softbreak <$ endline
1188− <|> word
592+ Word -> word
1189− <|> macroDef (rawInline "latex")
593+ Esc1 -> str . T.singleton <$> primEscape
1190− <|> inlineCommand'
594+ Esc2 -> str . T.singleton <$> primEscape
1191− <|> inlineEnvironment
595+ Symbol ->
1192− <|> inlineGroup
596+ case t of
1193− <|> (symbol '-' *>
597+ "-" -> symbol '-' *>
1194− option (str "-") (symbol '-' *>
598+ option (str "-") (symbol '-' *>
1195− option (str "–") (str "—" <$ symbol '-')))
599+ option (str "–") (str "—" <$ symbol '-'))
1196− <|> doubleQuote
1197− <|> singleQuote
1198− <|> (str "”" <$ try (symbol '\'' >> symbol '\''))
600+ "'" -> symbol '\'' *>
1199− <|> (str "”" <$ symbol '”')
1200− <|> (str "’" <$ symbol '\'')
601+ option (str "’") (str "”" <$ symbol '\'')
1201− <|> (str "’" <$ symbol '’')
1202− <|> (str "\160" <$ symbol '~')
602+ "~" -> str "\160" <$ symbol '~'
603+ "`" -> doubleQuote <|> singleQuote <|> symbolAsString
604+ "\"" -> doubleQuote <|> singleQuote <|> symbolAsString
605+ "“" -> doubleQuote <|> symbolAsString
606+ "‘" -> singleQuote <|> symbolAsString
1203− <|> dollarsMath
607+ "$" -> dollarsMath <|> unescapedSymbolAsString
1204− <|> (guardEnabled Ext_literate_haskell *> symbol '|' *> doLHSverb)
608+ "|" -> (guardEnabled Ext_literate_haskell *>
1205− <|> (str . T.singleton <$> primEscape)
609+ symbol '|' *> doLHSverb) <|> symbolAsString
1206− <|> regularSymbol
610+ "{" -> inlineGroup
1207− <|> (do res <- symbolIn "#^'`\"[]&"
611+ "#" -> unescapedSymbolAsString
1208− pos <- getPosition
612+ "&" -> unescapedSymbolAsString
1209− let s = untoken res
613+ "_" -> unescapedSymbolAsString
1210− report $ ParsingUnescaped s pos
614+ "^" -> unescapedSymbolAsString
1211− return $ str s)
615+ "\\" -> mzero
616+ "}" -> mzero
617+ _ -> symbolAsString
618+ CtrlSeq _ -> macroDef (rawInline "latex")
619+ <|> inlineCommand'
620+ <|> inlineEnvironment
621+ <|> inlineGroup
622+ _ -> mzero
1212
623
1213inlines :: PandocMonad m => LP m Inlines
624inlines :: PandocMonad m => LP m Inlines
1214inlines = mconcat <$> many inline
625inlines = mconcat <$> many inline
1215
626
627+opt :: PandocMonad m => LP m Inlines
628+opt = do
629+ toks <- try (sp *> bracketedToks <* sp)
630+ -- now parse the toks as inlines
631+ st <- getState
632+ parsed <- runParserT (mconcat <$> many inline) st "bracketed option" toks
633+ case parsed of
634+ Right result -> return result
635+ Left e -> throwError $ PandocParsecError (toSources toks) e
636+
1216-- block elements:
637-- block elements:
1217
638
1218preamble :: PandocMonad m => LP m Blocks
639preamble :: PandocMonad m => LP m Blocks
@@ -1306,10 +727,6 @@ insertIncluded defaultExtension f' = do
1306 getInput >>= setInput . (tokenize f contents ++)
727 getInput >>= setInput . (tokenize f contents ++)
1307 updateState dropLatestIncludeFile
728 updateState dropLatestIncludeFile
1308
729
1309−addMeta :: PandocMonad m => ToMetaValue a => Text -> a -> LP m ()
1310−addMeta field val = updateState $ \st ->
1311− st{ sMeta = addMetaField field val $ sMeta st }
1312−
1313authors :: PandocMonad m => LP m ()
730authors :: PandocMonad m => LP m ()
1314authors = try $ do
731authors = try $ do
1315 bgroup
732 bgroup
@@ -1318,150 +735,6 @@ authors = try $ do
1318 egroup
735 egroup
1319 addMeta "author" (map trimInlines auths)
736 addMeta "author" (map trimInlines auths)
1320
737
1321−macroDef :: (PandocMonad m, Monoid a) => (Text -> a) -> LP m a
1322−macroDef constructor = do
1323− (_, s) <- withRaw (commandDef <|> environmentDef)
1324− (constructor (untokenize s) <$
1325− guardDisabled Ext_latex_macros)
1326− <|> return mempty
1327− where commandDef = do
1328− (name, macro') <- newcommand <|> letmacro <|> defmacro
1329− guardDisabled Ext_latex_macros <|>
1330− updateState (\s -> s{ sMacros = M.insert name macro' (sMacros s) })
1331− environmentDef = do
1332− mbenv <- newenvironment
1333− case mbenv of
1334− Nothing -> return ()
1335− Just (name, macro1, macro2) ->
1336− guardDisabled Ext_latex_macros <|>
1337− do updateState $ \s -> s{ sMacros =
1338− M.insert name macro1 (sMacros s) }
1339− updateState $ \s -> s{ sMacros =
1340− M.insert ("end" <> name) macro2 (sMacros s) }
1341− -- @\newenvironment{envname}[n-args][default]{begin}{end}@
1342− -- is equivalent to
1343− -- @\newcommand{\envname}[n-args][default]{begin}@
1344− -- @\newcommand{\endenvname}@
1345−
1346−letmacro :: PandocMonad m => LP m (Text, Macro)
1347−letmacro = do
1348− controlSeq "let"
1349− (name, contents) <- withVerbatimMode $ do
1350− Tok _ (CtrlSeq name) _ <- anyControlSeq
1351− optional $ symbol '='
1352− spaces
1353− -- we first parse in verbatim mode, and then expand macros,
1354− -- because we don't want \let\foo\bar to turn into
1355− -- \let\foo hello if we have previously \def\bar{hello}
1356− contents <- bracedOrToken
1357− return (name, contents)
1358− contents' <- doMacros' 0 contents
1359− return (name, Macro ExpandWhenDefined [] Nothing contents')
1360−
1361−defmacro :: PandocMonad m => LP m (Text, Macro)
1362−defmacro = try $
1363− -- we use withVerbatimMode, because macros are to be expanded
1364− -- at point of use, not point of definition
1365− withVerbatimMode $ do
1366− controlSeq "def"
1367− Tok _ (CtrlSeq name) _ <- anyControlSeq
1368− argspecs <- many (argspecArg <|> argspecPattern)
1369− contents <- bracedOrToken
1370− return (name, Macro ExpandWhenUsed argspecs Nothing contents)
1371−
1372−argspecArg :: PandocMonad m => LP m ArgSpec
1373−argspecArg = do
1374− Tok _ (Arg i) _ <- satisfyTok isArgTok
1375− return $ ArgNum i
1376−
1377−argspecPattern :: PandocMonad m => LP m ArgSpec
1378−argspecPattern =
1379− Pattern <$> many1 (satisfyTok (\(Tok _ toktype' txt) ->
1380− (toktype' == Symbol || toktype' == Word) &&
1381− (txt /= "{" && txt /= "\\" && txt /= "}")))
1382−
1383−newcommand :: PandocMonad m => LP m (Text, Macro)
1384−newcommand = do
1385− pos <- getPosition
1386− Tok _ (CtrlSeq mtype) _ <- controlSeq "newcommand" <|>
1387− controlSeq "renewcommand" <|>
1388− controlSeq "providecommand" <|>
1389− controlSeq "DeclareMathOperator" <|>
1390− controlSeq "DeclareRobustCommand"
1391− withVerbatimMode $ do
1392− Tok _ (CtrlSeq name) txt <- do
1393− optional (symbol '*')
1394− anyControlSeq <|>
1395− (symbol '{' *> spaces *> anyControlSeq <* spaces <* symbol '}')
1396− spaces
1397− numargs <- option 0 $ try bracketedNum
1398− let argspecs = map ArgNum [1..numargs]
1399− spaces
1400− optarg <- option Nothing $ Just <$> try bracketedToks
1401− spaces
1402− contents' <- bracedOrToken
1403− let contents =
1404− case mtype of
1405− "DeclareMathOperator" ->
1406− Tok pos (CtrlSeq "mathop") "\\mathop"
1407− : Tok pos Symbol "{"
1408− : Tok pos (CtrlSeq "mathrm") "\\mathrm"
1409− : Tok pos Symbol "{"
1410− : (contents' ++
1411− [ Tok pos Symbol "}", Tok pos Symbol "}" ])
1412− _ -> contents'
1413− macros <- sMacros <$> getState
1414− case M.lookup name macros of
1415− Just macro
1416− | mtype == "newcommand" -> do
1417− report $ MacroAlreadyDefined txt pos
1418− return (name, macro)
1419− | mtype == "providecommand" -> return (name, macro)
1420− _ -> return (name, Macro ExpandWhenUsed argspecs optarg contents)
1421−
1422−newenvironment :: PandocMonad m => LP m (Maybe (Text, Macro, Macro))
1423−newenvironment = do
1424− pos <- getPosition
1425− Tok _ (CtrlSeq mtype) _ <- controlSeq "newenvironment" <|>
1426− controlSeq "renewenvironment" <|>
1427− controlSeq "provideenvironment"
1428− withVerbatimMode $ do
1429− optional $ symbol '*'
1430− spaces
1431− name <- untokenize <$> braced
1432− spaces
1433− numargs <- option 0 $ try bracketedNum
1434− spaces
1435− optarg <- option Nothing $ Just <$> try bracketedToks
1436− let argspecs = map (\i -> ArgNum i) [1..numargs]
1437− startcontents <- spaces >> bracedOrToken
1438− endcontents <- spaces >> bracedOrToken
1439− macros <- sMacros <$> getState
1440− case M.lookup name macros of
1441− Just _
1442− | mtype == "newenvironment" -> do
1443− report $ MacroAlreadyDefined name pos
1444− return Nothing
1445− | mtype == "provideenvironment" ->
1446− return Nothing
1447− _ -> return $ Just (name,
1448− Macro ExpandWhenUsed argspecs optarg startcontents,
1449− Macro ExpandWhenUsed [] Nothing endcontents)
1450−
1451−bracketedNum :: PandocMonad m => LP m Int
1452−bracketedNum = do
1453− ds <- untokenize <$> bracketedToks
1454− case safeRead ds of
1455− Just i -> return i
1456− _ -> return 0
1457−
1458−setCaption :: PandocMonad m => LP m ()
1459−setCaption = try $ do
1460− skipopts
1461− ils <- tok
1462− optional $ try $ spaces *> label
1463− updateState $ \st -> st{ sCaption = Just ils }
1464−
1465looseItem :: PandocMonad m => LP m Blocks
738looseItem :: PandocMonad m => LP m Blocks
1466looseItem = do
739looseItem = do
1467 inListItem <- sInListItem <$> getState
740 inListItem <- sInListItem <$> getState
@@ -1475,10 +748,6 @@ epigraph = do
1475 p2 <- grouped block
748 p2 <- grouped block
1476 return $ divWith ("", ["epigraph"], []) (p1 <> p2)
749 return $ divWith ("", ["epigraph"], []) (p1 <> p2)
1477
750
1478−resetCaption :: PandocMonad m => LP m ()
1479−resetCaption = updateState $ \st -> st{ sCaption = Nothing
1480− , sLastLabel = Nothing }
1481−
1482section :: PandocMonad m => Attr -> Int -> LP m Blocks
751section :: PandocMonad m => Attr -> Int -> LP m Blocks
1483section (ident, classes, kvs) lvl = do
752section (ident, classes, kvs) lvl = do
1484 skipopts
753 skipopts
@@ -1571,7 +840,7 @@ blockCommands = M.fromList
1571 , ("address", mempty <$ (skipopts *> tok >>= addMeta "address"))
840 , ("address", mempty <$ (skipopts *> tok >>= addMeta "address"))
1572 , ("signature", mempty <$ (skipopts *> authors))
841 , ("signature", mempty <$ (skipopts *> authors))
1573 , ("date", mempty <$ (skipopts *> tok >>= addMeta "date"))
842 , ("date", mempty <$ (skipopts *> tok >>= addMeta "date"))
1574− , ("newtheorem", newtheorem)
843+ , ("newtheorem", newtheorem inline)
1575 , ("theoremstyle", theoremstyle)
844 , ("theoremstyle", theoremstyle)
1576 -- KOMA-Script metadata commands
845 -- KOMA-Script metadata commands
1577 , ("extratitle", mempty <$ (skipopts *> tok >>= addMeta "extratitle"))
846 , ("extratitle", mempty <$ (skipopts *> tok >>= addMeta "extratitle"))
@@ -1619,7 +888,7 @@ blockCommands = M.fromList
1619 , ("item", looseItem)
888 , ("item", looseItem)
1620 , ("documentclass", skipopts *> braced *> preamble)
889 , ("documentclass", skipopts *> braced *> preamble)
1621 , ("centerline", para . trimInlines <$> (skipopts *> tok))
890 , ("centerline", para . trimInlines <$> (skipopts *> tok))
1622− , ("caption", mempty <$ setCaption)
891+ , ("caption", mempty <$ setCaption inline)
1623 , ("bibliography", mempty <$ (skipopts *> braced >>=
892 , ("bibliography", mempty <$ (skipopts *> braced >>=
1624 addMeta "bibliography" . splitBibs . untokenize))
893 addMeta "bibliography" . splitBibs . untokenize))
1625 , ("addbibresource", mempty <$ (skipopts *> braced >>=
894 , ("addbibresource", mempty <$ (skipopts *> braced >>=
@@ -1657,7 +926,8 @@ blockCommands = M.fromList
1657
926
1658
927
1659environments :: PandocMonad m => M.Map Text (LP m Blocks)
928environments :: PandocMonad m => M.Map Text (LP m Blocks)
929+environments = M.union (tableEnvironments blocks inline) $
1660−environments = M.fromList
930+ M.fromList
1661 [ ("document", env "document" blocks <* skipMany anyTok)
931 [ ("document", env "document" blocks <* skipMany anyTok)
1662 , ("abstract", mempty <$ (env "abstract" blocks >>= addMeta "abstract"))
932 , ("abstract", mempty <$ (env "abstract" blocks >>= addMeta "abstract"))
1663 , ("sloppypar", env "sloppypar" blocks)
933 , ("sloppypar", env "sloppypar" blocks)
@@ -1667,13 +937,6 @@ environments = M.fromList
1667 , ("figure", env "figure" $ skipopts *> figure)
937 , ("figure", env "figure" $ skipopts *> figure)
1668 , ("subfigure", env "subfigure" $ skipopts *> tok *> figure)
938 , ("subfigure", env "subfigure" $ skipopts *> tok *> figure)
1669 , ("center", divWith ("", ["center"], []) <$> env "center" blocks)
939 , ("center", divWith ("", ["center"], []) <$> env "center" blocks)
1670− , ("longtable", env "longtable" $
1671− resetCaption *> simpTable "longtable" False >>= addTableCaption)
1672− , ("table", env "table" $
1673− skipopts *> resetCaption *> blocks >>= addTableCaption)
1674− , ("tabular*", env "tabular*" $ simpTable "tabular*" True)
1675− , ("tabularx", env "tabularx" $ simpTable "tabularx" True)
1676− , ("tabular", env "tabular" $ simpTable "tabular" False)
1677 , ("quote", blockQuote <$> env "quote" blocks)
940 , ("quote", blockQuote <$> env "quote" blocks)
1678 , ("quotation", blockQuote <$> env "quotation" blocks)
941 , ("quotation", blockQuote <$> env "quotation" blocks)
1679 , ("verse", blockQuote <$> env "verse" blocks)
942 , ("verse", blockQuote <$> env "verse" blocks)
@@ -1696,7 +959,7 @@ environments = M.fromList
1696 , ("lilypond", rawVerbEnv "lilypond")
959 , ("lilypond", rawVerbEnv "lilypond")
1697 , ("ly", rawVerbEnv "ly")
960 , ("ly", rawVerbEnv "ly")
1698 -- amsthm
961 -- amsthm
1699− , ("proof", proof)
962+ , ("proof", proof blocks opt)
1700 -- etoolbox
963 -- etoolbox
1701 , ("ifstrequal", ifstrequal)
964 , ("ifstrequal", ifstrequal)
1702 , ("newtoggle", braced >>= newToggle)
965 , ("newtoggle", braced >>= newToggle)
@@ -1717,131 +980,17 @@ filecontents = try $ do
1717 st{ sFileContents = M.insert fp txt (sFileContents st) }
980 st{ sFileContents = M.insert fp txt (sFileContents st) }
1718 return mempty
981 return mempty
1719
982
1720−theoremstyle :: PandocMonad m => LP m Blocks
1721−theoremstyle = do
1722− stylename <- untokenize <$> braced
1723− let mbstyle = case stylename of
1724− "plain" -> Just PlainStyle
1725− "definition" -> Just DefinitionStyle
1726− "remark" -> Just RemarkStyle
1727− _ -> Nothing
1728− case mbstyle of
1729− Nothing -> return ()
1730− Just sty -> updateState $ \s -> s{ sLastTheoremStyle = sty }
1731− return mempty
1732−
1733−newtheorem :: PandocMonad m => LP m Blocks
1734−newtheorem = do
1735− number <- option True (False <$ symbol '*' <* sp)
1736− name <- untokenize <$> braced
1737− sp
1738− series <- option Nothing $ Just . untokenize <$> bracketedToks
1739− sp
1740− showName <- tok
1741− sp
1742− syncTo <- option Nothing $ Just . untokenize <$> bracketedToks
1743− sty <- sLastTheoremStyle <$> getState
1744− let spec = TheoremSpec { theoremName = showName
1745− , theoremStyle = sty
1746− , theoremSeries = series
1747− , theoremSyncTo = syncTo
1748− , theoremNumber = number
1749− , theoremLastNum = DottedNum [0] }
1750− tmap <- sTheoremMap <$> getState
1751− updateState $ \s -> s{ sTheoremMap =
1752− M.insert name spec tmap }
1753− return mempty
1754−
1755−proof :: PandocMonad m => LP m Blocks
1756−proof = do
1757− title <- option (B.text "Proof") opt
1758− bs <- env "proof" blocks
1759− return $
1760− B.divWith ("", ["proof"], []) $
1761− addQed $ addTitle (B.emph (title <> ".")) bs
1762−
1763−addTitle :: Inlines -> Blocks -> Blocks
1764−addTitle ils bs =
1765− case B.toList bs of
1766− (Para xs : rest)
1767− -> B.fromList (Para (B.toList ils ++ (Space : xs)) : rest)
1768− _ -> B.para ils <> bs
1769−
1770−addQed :: Blocks -> Blocks
1771−addQed bs =
1772− case Seq.viewr (B.unMany bs) of
1773− s Seq.:> Para ils
1774− -> B.Many (s Seq.|> Para (ils ++ B.toList qedSign))
1775− _ -> bs <> B.para qedSign
1776− where
1777− qedSign = B.str "\xa0\x25FB"
1778−
1779environment :: PandocMonad m => LP m Blocks
983environment :: PandocMonad m => LP m Blocks
1780environment = try $ do
984environment = try $ do
1781 controlSeq "begin"
985 controlSeq "begin"
1782 name <- untokenize <$> braced
986 name <- untokenize <$> braced
1783 M.findWithDefault mzero name environments <|>
987 M.findWithDefault mzero name environments <|>
1784− theoremEnvironment name <|>
988+ theoremEnvironment blocks opt name <|>
1785 if M.member name (inlineEnvironments
989 if M.member name (inlineEnvironments
1786 :: M.Map Text (LP PandocPure Inlines))
990 :: M.Map Text (LP PandocPure Inlines))
1787 then mzero
991 then mzero
1788 else try (rawEnv name) <|> rawVerbEnv name
992 else try (rawEnv name) <|> rawVerbEnv name
1789
993
1790−theoremEnvironment :: PandocMonad m => Text -> LP m Blocks
1791−theoremEnvironment name = do
1792− tmap <- sTheoremMap <$> getState
1793− case M.lookup name tmap of
1794− Nothing -> mzero
1795− Just tspec -> do
1796− optTitle <- option mempty $ (\x -> space <> "(" <> x <> ")") <$> opt
1797− mblabel <- option Nothing $ Just . untokenize <$>
1798− try (spaces >> controlSeq "label" >> spaces >> braced)
1799− bs <- env name blocks
1800− number <-
1801− if theoremNumber tspec
1802− then do
1803− let name' = fromMaybe name $ theoremSeries tspec
1804− num <- getNextNumber
1805− (maybe (DottedNum [0]) theoremLastNum .
1806− M.lookup name' . sTheoremMap)
1807− updateState $ \s ->
1808− s{ sTheoremMap =
1809− M.adjust
1810− (\spec -> spec{ theoremLastNum = num })
1811− name'
1812− (sTheoremMap s)
1813− }
1814−
1815− case mblabel of
1816− Just ident ->
1817− updateState $ \s ->
1818− s{ sLabels = M.insert ident
1819− (B.toList $
1820− theoremName tspec <> "\160" <>
1821− str (renderDottedNum num)) (sLabels s) }
1822− Nothing -> return ()
1823− return $ space <> B.text (renderDottedNum num)
1824− else return mempty
1825− let titleEmph = case theoremStyle tspec of
1826− PlainStyle -> B.strong
1827− DefinitionStyle -> B.strong
1828− RemarkStyle -> B.emph
1829− let title = titleEmph (theoremName tspec <> number)
1830− <> optTitle <> "." <> space
1831− return $ divWith (fromMaybe "" mblabel, [name], []) $ addTitle title
1832− $ case theoremStyle tspec of
1833− PlainStyle -> walk italicize bs
1834− _ -> bs
1835−
1836−italicize :: Block -> Block
1837−italicize x@(Para [Image{}]) = x -- see #6925
1838−italicize (Para ils) = Para [Emph ils]
1839−italicize (Plain ils) = Plain [Emph ils]
1840−italicize x = x
1841−
1842−env :: PandocMonad m => Text -> LP m a -> LP m a
1843−env name p = p <* end_ name
1844−
1845rawEnv :: PandocMonad m => Text -> LP m Blocks
994rawEnv :: PandocMonad m => Text -> LP m Blocks
1846rawEnv name = do
995rawEnv name = do
1847 exts <- getOption readerExtensions
996 exts <- getOption readerExtensions
@@ -1849,11 +998,13 @@ rawEnv name = do
1849 rawOptions <- mconcat <$> many rawopt
998 rawOptions <- mconcat <$> many rawopt
1850 let beginCommand = "\\begin{" <> name <> "}" <> rawOptions
999 let beginCommand = "\\begin{" <> name <> "}" <> rawOptions
1851 pos1 <- getPosition
1000 pos1 <- getPosition
1852− (bs, raw) <- withRaw $ env name blocks
1853 if parseRaw
1001 if parseRaw
1002+ then do
1003+ (_, raw) <- withRaw $ env name blocks
1854− then return $ rawBlock "latex"
1004+ return $ rawBlock "latex"
1855 $ beginCommand <> untokenize raw
1005 $ beginCommand <> untokenize raw
1856 else do
1006 else do
1007+ bs <- env name blocks
1857 report $ SkippedContent beginCommand pos1
1008 report $ SkippedContent beginCommand pos1
1858 pos2 <- getPosition
1009 pos2 <- getPosition
1859 report $ SkippedContent ("\\end{" <> name <> "}") pos2
1010 report $ SkippedContent ("\\end{" <> name <> "}") pos2
@@ -2011,7 +1162,8 @@ inputListing = do
2011 let (ident,classes,kvs) = parseListingsOptions options
1162 let (ident,classes,kvs) = parseListingsOptions options
2012 let classes' =
1163 let classes' =
2013 (case listingsLanguage options of
1164 (case listingsLanguage options of
1165+ Nothing -> (take 1 (languagesByExtension defaultSyntaxMap
2014− Nothing -> (take 1 (languagesByExtension (T.pack $ takeExtension $ T.unpack f)) <>)
1166+ (T.pack $ takeExtension $ T.unpack f)) <>)
2015 Just _ -> id) classes
1167 Just _ -> id) classes
2016 let firstline = fromMaybe 1 $ lookup "firstline" options >>= safeRead
1168 let firstline = fromMaybe 1 $ lookup "firstline" options >>= safeRead
2017 let lastline = fromMaybe (length codeLines) $
1169 let lastline = fromMaybe (length codeLines) $
@@ -2077,358 +1229,23 @@ orderedList' = try $ do
2077 bs <- listenv "enumerate" (many item)
1229 bs <- listenv "enumerate" (many item)
2078 return $ orderedListWith (start, style, delim) bs
1230 return $ orderedListWith (start, style, delim) bs
2079
1231
2080−-- tables
2081−
2082−hline :: PandocMonad m => LP m ()
2083−hline = try $ do
2084− spaces
2085− controlSeq "hline" <|>
2086− -- booktabs rules:
2087− controlSeq "toprule" <|>
2088− controlSeq "bottomrule" <|>
2089− controlSeq "midrule" <|>
2090− controlSeq "endhead" <|>
2091− controlSeq "endfirsthead"
2092− spaces
2093− optional opt
2094− return ()
2095−
2096−lbreak :: PandocMonad m => LP m Tok
2097−lbreak = (controlSeq "\\" <|> controlSeq "tabularnewline")
2098− <* skipopts <* spaces
2099−
2100−amp :: PandocMonad m => LP m Tok
2101−amp = symbol '&'
2102−
2103−-- Split a Word into individual Symbols (for parseAligns)
2104−splitWordTok :: PandocMonad m => LP m ()
2105−splitWordTok = do
2106− inp <- getInput
2107− case inp of
2108− (Tok spos Word t : rest) ->
2109− setInput $ map (Tok spos Symbol . T.singleton) (T.unpack t) <> rest
2110− _ -> return ()
2111−
2112−parseAligns :: PandocMonad m => LP m [(Alignment, ColWidth, ([Tok], [Tok]))]
2113−parseAligns = try $ do
2114− let maybeBar = skipMany
2115− (try $ sp *> (() <$ symbol '|' <|> () <$ (symbol '@' >> braced)))
2116− let cAlign = AlignCenter <$ symbol 'c'
2117− let lAlign = AlignLeft <$ symbol 'l'
2118− let rAlign = AlignRight <$ symbol 'r'
2119− let parAlign = AlignLeft <$ symbol 'p'
2120− -- aligns from tabularx
2121− let xAlign = AlignLeft <$ symbol 'X'
2122− let mAlign = AlignLeft <$ symbol 'm'
2123− let bAlign = AlignLeft <$ symbol 'b'
2124− let alignChar = splitWordTok *> ( cAlign <|> lAlign <|> rAlign <|> parAlign
2125− <|> xAlign <|> mAlign <|> bAlign )
2126− let alignPrefix = symbol '>' >> braced
2127− let alignSuffix = symbol '<' >> braced
2128− let colWidth = try $ do
2129− symbol '{'
2130− ds <- trim . untokenize <$> manyTill anyTok (controlSeq "linewidth")
2131− spaces
2132− symbol '}'
2133− return $ safeRead ds
2134− let alignSpec = do
2135− pref <- option [] alignPrefix
2136− spaces
2137− al <- alignChar
2138− width <- colWidth <|> option Nothing (do s <- untokenize <$> braced
2139− pos <- getPosition
2140− report $ SkippedContent s pos
2141− return Nothing)
2142− spaces
2143− suff <- option [] alignSuffix
2144− return (al, width, (pref, suff))
2145− let starAlign = do -- '*{2}{r}' == 'rr', we just expand like a macro
2146− symbol '*'
2147− spaces
2148− ds <- trim . untokenize <$> braced
2149− spaces
2150− spec <- braced
2151− case safeRead ds of
2152− Just n ->
2153− getInput >>= setInput . (mconcat (replicate n spec) ++)
2154− Nothing -> Prelude.fail $ "Could not parse " <> T.unpack ds <> " as number"
2155− bgroup
2156− spaces
2157− maybeBar
2158− aligns' <- many $ try $ spaces >> optional starAlign >>
2159− (alignSpec <* maybeBar)
2160− spaces
2161− egroup
2162− spaces
2163− return $ map toSpec aligns'
2164− where
2165− toColWidth (Just w) | w > 0 = ColWidth w
2166− toColWidth _ = ColWidthDefault
2167− toSpec (x, y, z) = (x, toColWidth y, z)
2168−
2169−-- N.B. this parser returns a Row that may have erroneous empty cells
2170−-- in it. See the note above fixTableHead for details.
2171−parseTableRow :: PandocMonad m
2172− => Text -- ^ table environment name
2173− -> [([Tok], [Tok])] -- ^ pref/suffixes
2174− -> LP m Row
2175−parseTableRow envname prefsufs = do
2176− notFollowedBy (spaces *> end_ envname)
2177− -- add prefixes and suffixes in token stream:
2178− let celltoks (pref, suff) = do
2179− prefpos <- getPosition
2180− contents <- mconcat <$>
2181− many ( snd <$> withRaw (controlSeq "parbox" >> parbox) -- #5711
2182− <|>
2183− snd <$> withRaw (inlineEnvironment <|> dollarsMath)
2184− <|>
2185− (do notFollowedBy
2186− (() <$ amp <|> () <$ lbreak <|> end_ envname)
2187− count 1 anyTok) )
2188−
2189− suffpos <- getPosition
2190− option [] (count 1 amp)
2191− return $ map (setpos prefpos) pref ++ contents ++ map (setpos suffpos) suff
2192− rawcells <- mapM celltoks prefsufs
2193− oldInput <- getInput
2194− cells <- mapM (\ts -> setInput ts >> parseTableCell) rawcells
2195− setInput oldInput
2196− spaces
2197− return $ Row nullAttr cells
2198−
2199−parseTableCell :: PandocMonad m => LP m Cell
2200−parseTableCell = do
2201− spaces
2202− updateState $ \st -> st{ sInTableCell = True }
2203− cell' <- multicolumnCell
2204− <|> multirowCell
2205− <|> parseSimpleCell
2206− <|> parseEmptyCell
2207− updateState $ \st -> st{ sInTableCell = False }
2208− spaces
2209− return cell'
2210− where
2211− -- The parsing of empty cells is important in LaTeX, especially when dealing
2212− -- with multirow/multicolumn. See #6603.
2213− parseEmptyCell = spaces $> emptyCell
2214−
2215−cellAlignment :: PandocMonad m => LP m Alignment
2216−cellAlignment = skipMany (symbol '|') *> alignment <* skipMany (symbol '|')
2217− where
2218− alignment = do
2219− c <- untoken <$> singleChar
2220− return $ case c of
2221− "l" -> AlignLeft
2222− "r" -> AlignRight
2223− "c" -> AlignCenter
2224− "*" -> AlignDefault
2225− _ -> AlignDefault
2226−
2227−plainify :: Blocks -> Blocks
2228−plainify bs = case toList bs of
2229− [Para ils] -> plain (fromList ils)
2230− _ -> bs
2231−
2232−multirowCell :: PandocMonad m => LP m Cell
2233−multirowCell = controlSeq "multirow" >> do
2234− -- Full prototype for \multirow macro is:
2235− -- \multirow[vpos]{nrows}[bigstruts]{width}[vmove]{text}
2236− -- However, everything except `nrows` and `text` make
2237− -- sense in the context of the Pandoc AST
2238− _ <- optional $ symbol '[' *> cellAlignment <* symbol ']' -- vertical position
2239− nrows <- fmap (fromMaybe 1 . safeRead . untokenize) braced
2240− _ <- optional $ symbol '[' *> manyTill anyTok (symbol ']') -- bigstrut-related
2241− _ <- symbol '{' *> manyTill anyTok (symbol '}') -- Cell width
2242− _ <- optional $ symbol '[' *> manyTill anyTok (symbol ']') -- Length used for fine-tuning
2243− content <- symbol '{' *> (plainify <$> blocks) <* symbol '}'
2244− return $ cell AlignDefault (RowSpan nrows) (ColSpan 1) content
2245−
2246−multicolumnCell :: PandocMonad m => LP m Cell
2247−multicolumnCell = controlSeq "multicolumn" >> do
2248− span' <- fmap (fromMaybe 1 . safeRead . untokenize) braced
2249− alignment <- symbol '{' *> cellAlignment <* symbol '}'
2250−
2251− let singleCell = do
2252− content <- plainify <$> blocks
2253− return $ cell alignment (RowSpan 1) (ColSpan span') content
2254−
2255− -- Two possible contents: either a \multirow cell, or content.
2256− -- E.g. \multicol{1}{c}{\multirow{2}{1em}{content}}
2257− -- Note that a \multirow cell can be nested in a \multicolumn,
2258− -- but not the other way around. See #6603
2259− let nestedCell = do
2260− (Cell _ _ (RowSpan rs) _ bs) <- multirowCell
2261− return $ cell
2262− alignment
2263− (RowSpan rs)
2264− (ColSpan span')
2265− (fromList bs)
2266−
2267− symbol '{' *> (nestedCell <|> singleCell) <* symbol '}'
2268−
2269−-- Parse a simple cell, i.e. not multirow/multicol
2270−parseSimpleCell :: PandocMonad m => LP m Cell
2271−parseSimpleCell = simpleCell <$> (plainify <$> blocks)
2272−
2273−-- LaTeX tables are stored with empty cells underneath multirow cells
2274−-- denoting the grid spaces taken up by them. More specifically, if a
2275−-- cell spans m rows, then it will overwrite all the cells in the
2276−-- columns it spans for (m-1) rows underneath it, requiring padding
2277−-- cells in these places. These padding cells need to be removed for
2278−-- proper table reading. See #6603.
2279−--
2280−-- These fixTable functions do not otherwise fix up malformed
2281−-- input tables: that is left to the table builder.
2282−fixTableHead :: TableHead -> TableHead
2283−fixTableHead (TableHead attr rows) = TableHead attr rows'
2284− where
2285− rows' = fixTableRows rows
2286−
2287−fixTableBody :: TableBody -> TableBody
2288−fixTableBody (TableBody attr rhc th tb)
2289− = TableBody attr rhc th' tb'
2290− where
2291− th' = fixTableRows th
2292− tb' = fixTableRows tb
2293−
2294−fixTableRows :: [Row] -> [Row]
2295−fixTableRows = fixTableRows' $ repeat Nothing
2296− where
2297− fixTableRows' oldHang (Row attr cells : rs)
2298− = let (newHang, cells') = fixTableRow oldHang cells
2299− rs' = fixTableRows' newHang rs
2300− in Row attr cells' : rs'
2301− fixTableRows' _ [] = []
2302−
2303−-- The overhang is represented as Just (relative cell dimensions) or
2304−-- Nothing for an empty grid space.
2305−fixTableRow :: [Maybe (ColSpan, RowSpan)] -> [Cell] -> ([Maybe (ColSpan, RowSpan)], [Cell])
2306−fixTableRow oldHang cells
2307− -- If there's overhang, drop cells until their total width meets the
2308− -- width of the occupied grid spaces (or we run out)
2309− | (n, prefHang, restHang) <- splitHang oldHang
2310− , n > 0
2311− = let cells' = dropToWidth getCellW n cells
2312− (restHang', cells'') = fixTableRow restHang cells'
2313− in (prefHang restHang', cells'')
2314− -- Otherwise record the overhang of a pending cell and fix the rest
2315− -- of the row
2316− | c@(Cell _ _ h w _):cells' <- cells
2317− = let h' = max 1 h
2318− w' = max 1 w
2319− oldHang' = dropToWidth getHangW w' oldHang
2320− (newHang, cells'') = fixTableRow oldHang' cells'
2321− in (toHang w' h' <> newHang, c : cells'')
2322− | otherwise
2323− = (oldHang, [])
2324− where
2325− getCellW (Cell _ _ _ w _) = w
2326− getHangW = maybe 1 fst
2327− getCS (ColSpan n) = n
2328−
2329− toHang c r
2330− | r > 1 = [Just (c, r)]
2331− | otherwise = replicate (getCS c) Nothing
2332−
2333− -- Take the prefix of the overhang list representing filled grid
2334− -- spaces. Also return the remainder and the length of this prefix.
2335− splitHang = splitHang' 0 id
2336−
2337− splitHang' !n l (Just (c, r):xs)
2338− = splitHang' (n + c) (l . (toHang c (r-1) ++)) xs
2339− splitHang' n l xs = (n, l, xs)
2340−
2341− -- Drop list items until the total width of the dropped items
2342− -- exceeds the passed width.
2343− dropToWidth _ n l | n < 1 = l
2344− dropToWidth wproj n (c:cs) = dropToWidth wproj (n - wproj c) cs
2345− dropToWidth _ _ [] = []
2346−
2347−simpTable :: PandocMonad m => Text -> Bool -> LP m Blocks
2348−simpTable envname hasWidthParameter = try $ do
2349− when hasWidthParameter $ () <$ (spaces >> tok)
2350− skipopts
2351− colspecs <- parseAligns
2352− let (aligns, widths, prefsufs) = unzip3 colspecs
2353− optional $ controlSeq "caption" *> setCaption
2354− spaces
2355− optional label
2356− spaces
2357− optional lbreak
2358− spaces
2359− skipMany hline
2360− spaces
2361− header' <- option [] . try . fmap (:[]) $
2362− parseTableRow envname prefsufs <* lbreak <* many1 hline
2363− spaces
2364− rows <- sepEndBy (parseTableRow envname prefsufs)
2365− (lbreak <* optional (skipMany hline))
2366− spaces
2367− optional $ controlSeq "caption" *> setCaption
2368− spaces
2369− optional label
2370− spaces
2371− optional lbreak
2372− spaces
2373− lookAhead $ controlSeq "end" -- make sure we're at end
2374− let th = fixTableHead $ TableHead nullAttr header'
2375− let tbs = [fixTableBody $ TableBody nullAttr 0 [] rows]
2376− let tf = TableFoot nullAttr []
2377− return $ table emptyCaption (zip aligns widths) th tbs tf
2378−
2379−addTableCaption :: PandocMonad m => Blocks -> LP m Blocks
2380−addTableCaption = walkM go
2381− where go (Table attr c spec th tb tf) = do
2382− st <- getState
2383− let mblabel = sLastLabel st
2384− capt <- case (sCaption st, mblabel) of
2385− (Just ils, Nothing) -> return $ caption Nothing (plain ils)
2386− (Just ils, Just lab) -> do
2387− num <- getNextNumber sLastTableNum
2388− setState
2389− st{ sLastTableNum = num
2390− , sLabels = M.insert lab
2391− [Str (renderDottedNum num)]
2392− (sLabels st) }
2393− return $ caption Nothing (plain ils) -- add number??
2394− (Nothing, _) -> return c
2395− let attr' = case (attr, mblabel) of
2396− ((_,classes,kvs), Just ident) ->
2397− (ident,classes,kvs)
2398− _ -> attr
2399− return $ addAttrDiv attr' $ Table nullAttr capt spec th tb tf
2400− go x = return x
2401−
2402−-- TODO: For now we add a Div to contain table attributes, since
2403−-- most writers don't do anything yet with attributes on Table.
2404−-- This can be removed when that changes.
2405−addAttrDiv :: Attr -> Block -> Block
2406−addAttrDiv ("",[],[]) b = b
2407−addAttrDiv attr b = Div attr [b]
2408−
2409block :: PandocMonad m => LP m Blocks
1232block :: PandocMonad m => LP m Blocks
2410block = do
1233block = do
1234+ Tok _ toktype _ <- lookAhead anyTok
1235+ res <- (case toktype of
2411− res <- (mempty <$ spaces1)
1236+ Newline -> mempty <$ spaces1
1237+ Spaces -> mempty <$ spaces1
1238+ Comment -> mempty <$ spaces1
1239+ Word -> paragraph
2412− <|> environment
1240+ CtrlSeq "begin" -> environment
2413− <|> macroDef (rawBlock "latex")
1241+ CtrlSeq _ -> macroDef (rawBlock "latex")
2414− <|> blockCommand
1242+ <|> blockCommand
1243+ _ -> mzero)
2415− <|> paragraph
1244+ <|> paragraph
2416− <|> grouped block
1245+ <|> grouped block
2417 trace (T.take 60 $ tshow $ B.toList res)
1246 trace (T.take 60 $ tshow $ B.toList res)
2418 return res
1247 return res
2419
1248
2420blocks :: PandocMonad m => LP m Blocks
1249blocks :: PandocMonad m => LP m Blocks
2421blocks = mconcat <$> many block
1250blocks = mconcat <$> many block
2422
1251
2423−setDefaultLanguage :: PandocMonad m => LP m Blocks
2424−setDefaultLanguage = do
2425− o <- option "" $ T.filter (\c -> c /= '[' && c /= ']')
2426− <$> rawopt
2427− polylang <- untokenize <$> braced
2428− case M.lookup polylang polyglossiaLangToBCP47 of
2429− Nothing -> return mempty -- TODO mzero? warning?
2430− Just langFunc -> do
2431− let l = langFunc o
2432− setTranslations l
2433− updateState $ setMeta "lang" $ str (renderLang l)
2434− return mempty
src/Text/Pandoc/Readers/LaTeX/Citation.hsnew file+210
@@ -0,0 +1,210 @@
1+{-# LANGUAGE ScopedTypeVariables #-}
2+{-# LANGUAGE OverloadedStrings #-}
3+module Text.Pandoc.Readers.LaTeX.Citation
4+ ( citationCommands
5+ , cites
6+ )
7+where
8+
9+import Text.Pandoc.Class
10+import Text.Pandoc.Readers.LaTeX.Parsing
11+import Text.Pandoc.Builder as B
12+import qualified Data.Map as M
13+import Data.Text (Text)
14+import Control.Applicative ((<|>), optional, many)
15+import Control.Monad (mzero)
16+import Control.Monad.Trans (lift)
17+import Control.Monad.Except (throwError)
18+import Text.Pandoc.Error (PandocError(PandocParsecError))
19+import Text.Pandoc.Parsing hiding (blankline, many, mathDisplay, mathInline,
20+ optional, space, spaces, withRaw, (<|>))
21+
22+citationCommands :: PandocMonad m => LP m Inlines -> M.Map Text (LP m Inlines)
23+citationCommands inline =
24+ let citation = citationWith inline
25+ tok = spaces *> grouped inline
26+ in M.fromList
27+ [ ("cite", citation "cite" NormalCitation False)
28+ , ("Cite", citation "Cite" NormalCitation False)
29+ , ("citep", citation "citep" NormalCitation False)
30+ , ("citep*", citation "citep*" NormalCitation False)
31+ , ("citeal", citation "citeal" NormalCitation False)
32+ , ("citealp", citation "citealp" NormalCitation False)
33+ , ("citealp*", citation "citealp*" NormalCitation False)
34+ , ("autocite", citation "autocite" NormalCitation False)
35+ , ("smartcite", citation "smartcite" NormalCitation False)
36+ , ("footcite", inNote <$> citation "footcite" NormalCitation False)
37+ , ("parencite", citation "parencite" NormalCitation False)
38+ , ("supercite", citation "supercite" NormalCitation False)
39+ , ("footcitetext", inNote <$> citation "footcitetext" NormalCitation False)
40+ , ("citeyearpar", citation "citeyearpar" SuppressAuthor False)
41+ , ("citeyear", citation "citeyear" SuppressAuthor False)
42+ , ("autocite*", citation "autocite*" SuppressAuthor False)
43+ , ("cite*", citation "cite*" SuppressAuthor False)
44+ , ("parencite*", citation "parencite*" SuppressAuthor False)
45+ , ("textcite", citation "textcite" AuthorInText False)
46+ , ("citet", citation "citet" AuthorInText False)
47+ , ("citet*", citation "citet*" AuthorInText False)
48+ , ("citealt", citation "citealt" AuthorInText False)
49+ , ("citealt*", citation "citealt*" AuthorInText False)
50+ , ("textcites", citation "textcites" AuthorInText True)
51+ , ("cites", citation "cites" NormalCitation True)
52+ , ("autocites", citation "autocites" NormalCitation True)
53+ , ("footcites", inNote <$> citation "footcites" NormalCitation True)
54+ , ("parencites", citation "parencites" NormalCitation True)
55+ , ("supercites", citation "supercites" NormalCitation True)
56+ , ("footcitetexts", inNote <$> citation "footcitetexts" NormalCitation True)
57+ , ("Autocite", citation "Autocite" NormalCitation False)
58+ , ("Smartcite", citation "Smartcite" NormalCitation False)
59+ , ("Footcite", inNote <$> citation "Footcite" NormalCitation False)
60+ , ("Parencite", citation "Parencite" NormalCitation False)
61+ , ("Supercite", citation "Supercite" NormalCitation False)
62+ , ("Footcitetext", inNote <$> citation "Footcitetext" NormalCitation False)
63+ , ("Citeyearpar", citation "Citeyearpar" SuppressAuthor False)
64+ , ("Citeyear", citation "Citeyear" SuppressAuthor False)
65+ , ("Autocite*", citation "Autocite*" SuppressAuthor False)
66+ , ("Cite*", citation "Cite*" SuppressAuthor False)
67+ , ("Parencite*", citation "Parencite*" SuppressAuthor False)
68+ , ("Textcite", citation "Textcite" AuthorInText False)
69+ , ("Textcites", citation "Textcites" AuthorInText True)
70+ , ("Cites", citation "Cites" NormalCitation True)
71+ , ("Autocites", citation "Autocites" NormalCitation True)
72+ , ("Footcites", inNote <$> citation "Footcites" NormalCitation True)
73+ , ("Parencites", citation "Parencites" NormalCitation True)
74+ , ("Supercites", citation "Supercites" NormalCitation True)
75+ , ("Footcitetexts", inNote <$> citation "Footcitetexts" NormalCitation True)
76+ , ("citetext", complexNatbibCitation inline NormalCitation)
77+ , ("citeauthor", (try (tok *> sp *> controlSeq "citetext") *>
78+ complexNatbibCitation inline AuthorInText)
79+ <|> citation "citeauthor" AuthorInText False)
80+ , ("nocite", mempty <$ (citation "nocite" NormalCitation False >>=
81+ addMeta "nocite"))
82+ ]
83+
84+-- citations
85+
86+addPrefix :: [Inline] -> [Citation] -> [Citation]
87+addPrefix p (k:ks) = k {citationPrefix = p ++ citationPrefix k} : ks
88+addPrefix _ _ = []
89+
90+addSuffix :: [Inline] -> [Citation] -> [Citation]
91+addSuffix s ks@(_:_) =
92+ let k = last ks
93+ in init ks ++ [k {citationSuffix = citationSuffix k ++ s}]
94+addSuffix _ _ = []
95+
96+simpleCiteArgs :: forall m . PandocMonad m => LP m Inlines -> LP m [Citation]
97+simpleCiteArgs inline = try $ do
98+ first <- optionMaybe $ toList <$> opt
99+ second <- optionMaybe $ toList <$> opt
100+ keys <- try $ bgroup *> manyTill citationLabel egroup
101+ let (pre, suf) = case (first , second ) of
102+ (Just s , Nothing) -> (mempty, s )
103+ (Just s , Just t ) -> (s , t )
104+ _ -> (mempty, mempty)
105+ conv k = Citation { citationId = k
106+ , citationPrefix = []
107+ , citationSuffix = []
108+ , citationMode = NormalCitation
109+ , citationHash = 0
110+ , citationNoteNum = 0
111+ }
112+ return $ addPrefix pre $ addSuffix suf $ map conv keys
113+ where
114+ opt :: PandocMonad m => LP m Inlines
115+ opt = do
116+ toks <- try (sp *> bracketedToks <* sp)
117+ -- now parse the toks as inlines
118+ st <- getState
119+ parsed <- lift $
120+ runParserT (mconcat <$> many inline) st "bracketed option" toks
121+ case parsed of
122+ Right result -> return result
123+ Left e -> throwError $ PandocParsecError (toSources toks) e
124+
125+
126+
127+citationLabel :: PandocMonad m => LP m Text
128+citationLabel = do
129+ sp
130+ untokenize <$>
131+ (many1 (satisfyTok isWordTok <|> symbolIn bibtexKeyChar)
132+ <* sp
133+ <* optional (symbol ',')
134+ <* sp)
135+ where bibtexKeyChar = ".:;?!`'()/*@_+=-&[]" :: [Char]
136+
137+cites :: PandocMonad m
138+ => LP m Inlines -> CitationMode -> Bool -> LP m [Citation]
139+cites inline mode multi = try $ do
140+ let paropt = parenWrapped inline
141+ cits <- if multi
142+ then do
143+ multiprenote <- optionMaybe $ toList <$> paropt
144+ multipostnote <- optionMaybe $ toList <$> paropt
145+ let (pre, suf) = case (multiprenote, multipostnote) of
146+ (Just s , Nothing) -> (mempty, s)
147+ (Nothing , Just t) -> (mempty, t)
148+ (Just s , Just t ) -> (s, t)
149+ _ -> (mempty, mempty)
150+ tempCits <- many1 $ simpleCiteArgs inline
151+ case tempCits of
152+ (k:ks) -> case ks of
153+ (_:_) -> return $ (addMprenote pre k : init ks) ++
154+ [addMpostnote suf (last ks)]
155+ _ -> return [addMprenote pre (addMpostnote suf k)]
156+ _ -> return [[]]
157+ else count 1 $ simpleCiteArgs inline
158+ let cs = concat cits
159+ return $ case mode of
160+ AuthorInText -> case cs of
161+ (c:rest) -> c {citationMode = mode} : rest
162+ [] -> []
163+ _ -> map (\a -> a {citationMode = mode}) cs
164+ where mprenote (k:ks) = (k:ks) ++ [Space]
165+ mprenote _ = mempty
166+ mpostnote (k:ks) = [Str ",", Space] ++ (k:ks)
167+ mpostnote _ = mempty
168+ addMprenote mpn (k:ks) =
169+ let mpnfinal = case citationPrefix k of
170+ (_:_) -> mprenote mpn
171+ _ -> mpn
172+ in addPrefix mpnfinal (k:ks)
173+ addMprenote _ _ = []
174+ addMpostnote = addSuffix . mpostnote
175+
176+citationWith :: PandocMonad m
177+ => LP m Inlines -> Text -> CitationMode -> Bool -> LP m Inlines
178+citationWith inline name mode multi = do
179+ (c,raw) <- withRaw $ cites inline mode multi
180+ return $ cite c (rawInline "latex" $ "\\" <> name <> untokenize raw)
181+
182+handleCitationPart :: Inlines -> [Citation]
183+handleCitationPart ils =
184+ let isCite Cite{} = True
185+ isCite _ = False
186+ (pref, rest) = break isCite (toList ils)
187+ in case rest of
188+ (Cite cs _:suff) -> addPrefix pref $ addSuffix suff cs
189+ _ -> []
190+
191+complexNatbibCitation :: PandocMonad m
192+ => LP m Inlines -> CitationMode -> LP m Inlines
193+complexNatbibCitation inline mode = try $ do
194+ (cs, raw) <-
195+ withRaw $ concat <$> do
196+ bgroup
197+ items <- mconcat <$>
198+ many1 (notFollowedBy (symbol ';') >> inline)
199+ `sepBy1` symbol ';'
200+ egroup
201+ return $ map handleCitationPart items
202+ case cs of
203+ [] -> mzero
204+ (c:cits) -> return $ cite (c{ citationMode = mode }:cits)
205+ (rawInline "latex" $ "\\citetext" <> untokenize raw)
206+
207+inNote :: Inlines -> Inlines
208+inNote ils =
209+ note $ para $ ils <> str "."
210+
src/Text/Pandoc/Readers/LaTeX/Inline.hsnew file+397
@@ -0,0 +1,397 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+{-# LANGUAGE ViewPatterns #-}
3+{- |
4+ Module : Text.Pandoc.Readers.LaTeX.Inline
5+ Copyright : Copyright (C) 2006-2021 John MacFarlane
6+ License : GNU GPL, version 2 or above
7+
8+ Maintainer : John MacFarlane <jgm@berkeley.edu>
9+ Stability : alpha
10+ Portability : portable
11+-}
12+module Text.Pandoc.Readers.LaTeX.Inline
13+ ( acronymCommands
14+ , verbCommands
15+ , charCommands
16+ , accentCommands
17+ , nameCommands
18+ , biblatexInlineCommands
19+ , refCommands
20+ , rawInlineOr
21+ , listingsLanguage
22+ )
23+where
24+
25+import qualified Data.Map as M
26+import Data.Text (Text)
27+import qualified Data.Text as T
28+import Text.Pandoc.Builder
29+import Text.Pandoc.Shared (toRomanNumeral, safeRead)
30+import Text.Pandoc.Readers.LaTeX.Types (Tok (..), TokType (..))
31+import Control.Applicative (optional, (<|>))
32+import Control.Monad (guard, mzero, mplus, unless)
33+import Text.Pandoc.Class.PandocMonad (PandocMonad (..), translateTerm)
34+import Text.Pandoc.Readers.LaTeX.Parsing
35+import Text.Pandoc.Extensions (extensionEnabled, Extension(..))
36+import Text.Pandoc.Parsing (getOption, updateState, getState, notFollowedBy,
37+ manyTill, getInput, setInput, incSourceColumn,
38+ option, many1, try)
39+import Data.Char (isDigit)
40+import Text.Pandoc.Highlighting (fromListingsLanguage,)
41+import Data.Maybe (maybeToList, fromMaybe)
42+import Text.Pandoc.Options (ReaderOptions(..))
43+import qualified Data.Text.Normalize as Normalize
44+import qualified Text.Pandoc.Translations as Translations
45+
46+rawInlineOr :: PandocMonad m => Text -> LP m Inlines -> LP m Inlines
47+rawInlineOr name' fallback = do
48+ parseRaw <- extensionEnabled Ext_raw_tex <$> getOption readerExtensions
49+ if parseRaw
50+ then rawInline "latex" <$> getRawCommand name' ("\\" <> name')
51+ else fallback
52+
53+dolabel :: PandocMonad m => LP m Inlines
54+dolabel = do
55+ v <- braced
56+ let refstr = untokenize v
57+ updateState $ \st ->
58+ st{ sLastLabel = Just refstr }
59+ return $ spanWith (refstr,[],[("label", refstr)])
60+ $ inBrackets $ str $ untokenize v
61+
62+doref :: PandocMonad m => Text -> LP m Inlines
63+doref cls = do
64+ v <- braced
65+ let refstr = untokenize v
66+ return $ linkWith ("",[],[ ("reference-type", cls)
67+ , ("reference", refstr)])
68+ ("#" <> refstr)
69+ ""
70+ (inBrackets $ str refstr)
71+
72+inBrackets :: Inlines -> Inlines
73+inBrackets x = str "[" <> x <> str "]"
74+
75+doTerm :: PandocMonad m => Translations.Term -> LP m Inlines
76+doTerm term = str <$> translateTerm term
77+
78+lit :: Text -> LP m Inlines
79+lit = pure . str
80+
81+doverb :: PandocMonad m => LP m Inlines
82+doverb = do
83+ Tok _ Symbol t <- anySymbol
84+ marker <- case T.uncons t of
85+ Just (c, ts) | T.null ts -> return c
86+ _ -> mzero
87+ withVerbatimMode $
88+ code . untokenize <$>
89+ manyTill (notFollowedBy newlineTok >> verbTok marker) (symbol marker)
90+
91+verbTok :: PandocMonad m => Char -> LP m Tok
92+verbTok stopchar = do
93+ t@(Tok pos toktype txt) <- anyTok
94+ case T.findIndex (== stopchar) txt of
95+ Nothing -> return t
96+ Just i -> do
97+ let (t1, t2) = T.splitAt i txt
98+ inp <- getInput
99+ setInput $ Tok (incSourceColumn pos i) Symbol (T.singleton stopchar)
100+ : totoks (incSourceColumn pos (i + 1)) (T.drop 1 t2) ++ inp
101+ return $ Tok pos toktype t1
102+
103+listingsLanguage :: [(Text, Text)] -> Maybe Text
104+listingsLanguage opts =
105+ case lookup "language" opts of
106+ Nothing -> Nothing
107+ Just l -> fromListingsLanguage l `mplus` Just l
108+
109+dolstinline :: PandocMonad m => LP m Inlines
110+dolstinline = do
111+ options <- option [] keyvals
112+ let classes = maybeToList $ listingsLanguage options
113+ doinlinecode classes
114+
115+domintinline :: PandocMonad m => LP m Inlines
116+domintinline = do
117+ skipopts
118+ cls <- untokenize <$> braced
119+ doinlinecode [cls]
120+
121+doinlinecode :: PandocMonad m => [Text] -> LP m Inlines
122+doinlinecode classes = do
123+ Tok _ Symbol t <- anySymbol
124+ marker <- case T.uncons t of
125+ Just (c, ts) | T.null ts -> return c
126+ _ -> mzero
127+ let stopchar = if marker == '{' then '}' else marker
128+ withVerbatimMode $
129+ codeWith ("",classes,[]) . T.map nlToSpace . untokenize <$>
130+ manyTill (verbTok stopchar) (symbol stopchar)
131+
132+nlToSpace :: Char -> Char
133+nlToSpace '\n' = ' '
134+nlToSpace x = x
135+
136+romanNumeralUpper :: (PandocMonad m) => LP m Inlines
137+romanNumeralUpper =
138+ str . toRomanNumeral <$> romanNumeralArg
139+
140+romanNumeralLower :: (PandocMonad m) => LP m Inlines
141+romanNumeralLower =
142+ str . T.toLower . toRomanNumeral <$> romanNumeralArg
143+
144+romanNumeralArg :: (PandocMonad m) => LP m Int
145+romanNumeralArg = spaces *> (parser <|> inBraces)
146+ where
147+ inBraces = do
148+ symbol '{'
149+ spaces
150+ res <- parser
151+ spaces
152+ symbol '}'
153+ return res
154+ parser = do
155+ s <- untokenize <$> many1 (satisfyTok isWordTok)
156+ let (digits, rest) = T.span isDigit s
157+ unless (T.null rest) $
158+ Prelude.fail "Non-digits in argument to \\Rn or \\RN"
159+ safeRead digits
160+
161+accentWith :: PandocMonad m
162+ => LP m Inlines -> Char -> Maybe Char -> LP m Inlines
163+accentWith tok combiningAccent fallBack = try $ do
164+ ils <- tok
165+ case toList ils of
166+ (Str (T.uncons -> Just (x, xs)) : ys) -> return $ fromList $
167+ -- try to normalize to the combined character:
168+ Str (Normalize.normalize Normalize.NFC
169+ (T.pack [x, combiningAccent]) <> xs) : ys
170+ [Space] -> return $ str $ T.singleton
171+ $ fromMaybe combiningAccent fallBack
172+ [] -> return $ str $ T.singleton
173+ $ fromMaybe combiningAccent fallBack
174+ _ -> return ils
175+
176+
177+verbCommands :: PandocMonad m => M.Map Text (LP m Inlines)
178+verbCommands = M.fromList
179+ [ ("verb", doverb)
180+ , ("lstinline", dolstinline)
181+ , ("mintinline", domintinline)
182+ , ("Verb", doverb)
183+ ]
184+
185+accentCommands :: PandocMonad m => LP m Inlines -> M.Map Text (LP m Inlines)
186+accentCommands tok =
187+ let accent = accentWith tok
188+ in M.fromList
189+ [ ("aa", lit "å")
190+ , ("AA", lit "Å")
191+ , ("ss", lit "ß")
192+ , ("o", lit "ø")
193+ , ("O", lit "Ø")
194+ , ("L", lit "Ł")
195+ , ("l", lit "ł")
196+ , ("ae", lit "æ")
197+ , ("AE", lit "Æ")
198+ , ("oe", lit "œ")
199+ , ("OE", lit "Œ")
200+ , ("pounds", lit "£")
201+ , ("euro", lit "€")
202+ , ("copyright", lit "©")
203+ , ("textasciicircum", lit "^")
204+ , ("textasciitilde", lit "~")
205+ , ("H", accent '\779' Nothing) -- hungarumlaut
206+ , ("`", accent '\768' (Just '`')) -- grave
207+ , ("'", accent '\769' (Just '\'')) -- acute
208+ , ("^", accent '\770' (Just '^')) -- circ
209+ , ("~", accent '\771' (Just '~')) -- tilde
210+ , ("\"", accent '\776' Nothing) -- umlaut
211+ , (".", accent '\775' Nothing) -- dot
212+ , ("=", accent '\772' Nothing) -- macron
213+ , ("|", accent '\781' Nothing) -- vertical line above
214+ , ("b", accent '\817' Nothing) -- macron below
215+ , ("c", accent '\807' Nothing) -- cedilla
216+ , ("G", accent '\783' Nothing) -- doublegrave
217+ , ("h", accent '\777' Nothing) -- hookabove
218+ , ("d", accent '\803' Nothing) -- dotbelow
219+ , ("f", accent '\785' Nothing) -- inverted breve
220+ , ("r", accent '\778' Nothing) -- ringabove
221+ , ("t", accent '\865' Nothing) -- double inverted breve
222+ , ("U", accent '\782' Nothing) -- double vertical line above
223+ , ("v", accent '\780' Nothing) -- hacek
224+ , ("u", accent '\774' Nothing) -- breve
225+ , ("k", accent '\808' Nothing) -- ogonek
226+ , ("textogonekcentered", accent '\808' Nothing) -- ogonek
227+ , ("i", lit "ı") -- dotless i
228+ , ("j", lit "ȷ") -- dotless j
229+ , ("newtie", accent '\785' Nothing) -- inverted breve
230+ , ("textcircled", accent '\8413' Nothing) -- combining circle
231+ ]
232+
233+charCommands :: PandocMonad m => M.Map Text (LP m Inlines)
234+charCommands = M.fromList
235+ [ ("ldots", lit "…")
236+ , ("vdots", lit "\8942")
237+ , ("dots", lit "…")
238+ , ("mdots", lit "…")
239+ , ("sim", lit "~")
240+ , ("sep", lit ",")
241+ , ("P", lit "¶")
242+ , ("S", lit "§")
243+ , ("$", lit "$")
244+ , ("%", lit "%")
245+ , ("&", lit "&")
246+ , ("#", lit "#")
247+ , ("_", lit "_")
248+ , ("{", lit "{")
249+ , ("}", lit "}")
250+ , ("qed", lit "\a0\x25FB")
251+ , ("lq", return (str "‘"))
252+ , ("rq", return (str "’"))
253+ , ("textquoteleft", return (str "‘"))
254+ , ("textquoteright", return (str "’"))
255+ , ("textquotedblleft", return (str "“"))
256+ , ("textquotedblright", return (str "”"))
257+ , ("/", pure mempty) -- italic correction
258+ , ("\\", linebreak <$ (do inTableCell <- sInTableCell <$> getState
259+ guard $ not inTableCell
260+ optional rawopt
261+ spaces))
262+ , (",", lit "\8198")
263+ , ("@", pure mempty)
264+ , (" ", lit "\160")
265+ , ("ps", pure $ str "PS." <> space)
266+ , ("TeX", lit "TeX")
267+ , ("LaTeX", lit "LaTeX")
268+ , ("bar", lit "|")
269+ , ("textless", lit "<")
270+ , ("textgreater", lit ">")
271+ , ("textbackslash", lit "\\")
272+ , ("backslash", lit "\\")
273+ , ("slash", lit "/")
274+ -- fontawesome
275+ , ("faCheck", lit "\10003")
276+ , ("faClose", lit "\10007")
277+ -- hyphenat
278+ , ("bshyp", lit "\\\173")
279+ , ("fshyp", lit "/\173")
280+ , ("dothyp", lit ".\173")
281+ , ("colonhyp", lit ":\173")
282+ , ("hyp", lit "-")
283+ ]
284+
285+biblatexInlineCommands :: PandocMonad m
286+ => LP m Inlines -> M.Map Text (LP m Inlines)
287+biblatexInlineCommands tok = M.fromList
288+ -- biblatex misc
289+ [ ("RN", romanNumeralUpper)
290+ , ("Rn", romanNumeralLower)
291+ , ("mkbibquote", spanWith nullAttr . doubleQuoted <$> tok)
292+ , ("mkbibemph", spanWith nullAttr . emph <$> tok)
293+ , ("mkbibitalic", spanWith nullAttr . emph <$> tok)
294+ , ("mkbibbold", spanWith nullAttr . strong <$> tok)
295+ , ("mkbibparens",
296+ spanWith nullAttr . (\x -> str "(" <> x <> str ")") <$> tok)
297+ , ("mkbibbrackets",
298+ spanWith nullAttr . (\x -> str "[" <> x <> str "]") <$> tok)
299+ , ("autocap", spanWith nullAttr <$> tok)
300+ , ("textnormal", spanWith ("",["nodecor"],[]) <$> tok)
301+ , ("bibstring",
302+ (\x -> spanWith ("",[],[("bibstring",x)]) (str x)) . untokenize
303+ <$> braced)
304+ , ("adddot", pure (str "."))
305+ , ("adddotspace", pure (spanWith nullAttr (str "." <> space)))
306+ , ("addabbrvspace", pure space)
307+ , ("hyphen", pure (str "-"))
308+ ]
309+
310+nameCommands :: PandocMonad m => M.Map Text (LP m Inlines)
311+nameCommands = M.fromList
312+ [ ("figurename", doTerm Translations.Figure)
313+ , ("prefacename", doTerm Translations.Preface)
314+ , ("refname", doTerm Translations.References)
315+ , ("bibname", doTerm Translations.Bibliography)
316+ , ("chaptername", doTerm Translations.Chapter)
317+ , ("partname", doTerm Translations.Part)
318+ , ("contentsname", doTerm Translations.Contents)
319+ , ("listfigurename", doTerm Translations.ListOfFigures)
320+ , ("listtablename", doTerm Translations.ListOfTables)
321+ , ("indexname", doTerm Translations.Index)
322+ , ("abstractname", doTerm Translations.Abstract)
323+ , ("tablename", doTerm Translations.Table)
324+ , ("enclname", doTerm Translations.Encl)
325+ , ("ccname", doTerm Translations.Cc)
326+ , ("headtoname", doTerm Translations.To)
327+ , ("pagename", doTerm Translations.Page)
328+ , ("seename", doTerm Translations.See)
329+ , ("seealsoname", doTerm Translations.SeeAlso)
330+ , ("proofname", doTerm Translations.Proof)
331+ , ("glossaryname", doTerm Translations.Glossary)
332+ , ("lstlistingname", doTerm Translations.Listing)
333+ ]
334+
335+refCommands :: PandocMonad m => M.Map Text (LP m Inlines)
336+refCommands = M.fromList
337+ [ ("label", rawInlineOr "label" dolabel)
338+ , ("ref", rawInlineOr "ref" $ doref "ref")
339+ , ("cref", rawInlineOr "cref" $ doref "ref") -- from cleveref.sty
340+ , ("vref", rawInlineOr "vref" $ doref "ref+page") -- from varioref.sty
341+ , ("eqref", rawInlineOr "eqref" $ doref "eqref") -- from amsmath.sty
342+ ]
343+
344+acronymCommands :: PandocMonad m => M.Map Text (LP m Inlines)
345+acronymCommands = M.fromList
346+ -- glossaries package
347+ [ ("gls", doAcronym "short")
348+ , ("Gls", doAcronym "short")
349+ , ("glsdesc", doAcronym "long")
350+ , ("Glsdesc", doAcronym "long")
351+ , ("GLSdesc", doAcronym "long")
352+ , ("acrlong", doAcronym "long")
353+ , ("Acrlong", doAcronym "long")
354+ , ("acrfull", doAcronym "full")
355+ , ("Acrfull", doAcronym "full")
356+ , ("acrshort", doAcronym "abbrv")
357+ , ("Acrshort", doAcronym "abbrv")
358+ , ("glspl", doAcronymPlural "short")
359+ , ("Glspl", doAcronymPlural "short")
360+ , ("glsdescplural", doAcronymPlural "long")
361+ , ("Glsdescplural", doAcronymPlural "long")
362+ , ("GLSdescplural", doAcronymPlural "long")
363+ -- acronyms package
364+ , ("ac", doAcronym "short")
365+ , ("acf", doAcronym "full")
366+ , ("acs", doAcronym "abbrv")
367+ , ("acl", doAcronym "long")
368+ , ("acp", doAcronymPlural "short")
369+ , ("acfp", doAcronymPlural "full")
370+ , ("acsp", doAcronymPlural "abbrv")
371+ , ("aclp", doAcronymPlural "long")
372+ , ("Ac", doAcronym "short")
373+ , ("Acf", doAcronym "full")
374+ , ("Acs", doAcronym "abbrv")
375+ , ("Acl", doAcronym "long")
376+ , ("Acp", doAcronymPlural "short")
377+ , ("Acfp", doAcronymPlural "full")
378+ , ("Acsp", doAcronymPlural "abbrv")
379+ , ("Aclp", doAcronymPlural "long")
380+ ]
381+
382+doAcronym :: PandocMonad m => Text -> LP m Inlines
383+doAcronym form = do
384+ acro <- braced
385+ return . mconcat $ [spanWith ("",[],[("acronym-label", untokenize acro),
386+ ("acronym-form", "singular+" <> form)])
387+ $ str $ untokenize acro]
388+
389+doAcronymPlural :: PandocMonad m => Text -> LP m Inlines
390+doAcronymPlural form = do
391+ acro <- braced
392+ let plural = str "s"
393+ return . mconcat $ [spanWith ("",[],[("acronym-label", untokenize acro),
394+ ("acronym-form", "plural+" <> form)]) $
395+ mconcat [str $ untokenize acro, plural]]
396+
397+
src/Text/Pandoc/Readers/LaTeX/Lang.hs+199−120
@@ -12,144 +12,223 @@ Functions for parsing polyglossia and babel language specifiers to
12BCP47 'Lang'.
12BCP47 'Lang'.
13-}
13-}
14module Text.Pandoc.Readers.LaTeX.Lang
14module Text.Pandoc.Readers.LaTeX.Lang
15+ ( setDefaultLanguage
15− ( polyglossiaLangToBCP47
16+ , polyglossiaLangToBCP47
16 , babelLangToBCP47
17 , babelLangToBCP47
18+ , enquoteCommands
19+ , inlineLanguageCommands
17 )
20 )
18where
21where
19import qualified Data.Map as M
22import qualified Data.Map as M
23+import Data.Text (Text)
20import qualified Data.Text as T
24import qualified Data.Text as T
21−import Text.Pandoc.BCP47 (Lang(..))
25+import Text.Pandoc.Shared (extractSpaces)
26+import Text.Collate.Lang (Lang(..), renderLang)
27+import Text.Pandoc.Class (PandocMonad(..), setTranslations)
28+import Text.Pandoc.Readers.LaTeX.Parsing
29+import Text.Pandoc.Parsing (updateState, option, getState, QuoteContext(..),
30+ withQuoteContext)
31+import Text.Pandoc.Builder (Blocks, Inlines, setMeta, str, spanWith,
32+ singleQuoted, doubleQuoted)
33+
34+enquote :: PandocMonad m
35+ => LP m Inlines
36+ -> Bool -> Maybe Text -> LP m Inlines
37+enquote tok starred mblang = do
38+ skipopts
39+ let lang = mblang >>= babelLangToBCP47
40+ let langspan = case lang of
41+ Nothing -> id
42+ Just l -> spanWith ("",[],[("lang", renderLang l)])
43+ quoteContext <- sQuoteContext <$> getState
44+ if starred || quoteContext == InDoubleQuote
45+ then singleQuoted . langspan <$> withQuoteContext InSingleQuote tok
46+ else doubleQuoted . langspan <$> withQuoteContext InDoubleQuote tok
47+
48+enquoteCommands :: PandocMonad m
49+ => LP m Inlines -> M.Map Text (LP m Inlines)
50+enquoteCommands tok = M.fromList
51+ [ ("enquote*", enquote tok True Nothing)
52+ , ("enquote", enquote tok False Nothing)
53+ -- foreignquote is supposed to use native quote marks
54+ , ("foreignquote*", braced >>= enquote tok True . Just . untokenize)
55+ , ("foreignquote", braced >>= enquote tok False . Just . untokenize)
56+ -- hypehnquote uses regular quotes
57+ , ("hyphenquote*", braced >>= enquote tok True . Just . untokenize)
58+ , ("hyphenquote", braced >>= enquote tok False . Just . untokenize)
59+ ]
60+
61+foreignlanguage :: PandocMonad m => LP m Inlines -> LP m Inlines
62+foreignlanguage tok = do
63+ babelLang <- untokenize <$> braced
64+ case babelLangToBCP47 babelLang of
65+ Just lang -> spanWith ("", [], [("lang", renderLang lang)]) <$> tok
66+ _ -> tok
67+
68+inlineLanguageCommands :: PandocMonad m
69+ => LP m Inlines -> M.Map Text (LP m Inlines)
70+inlineLanguageCommands tok =
71+ M.fromList $
72+ ("foreignlanguage", foreignlanguage tok) :
73+ (mk <$> M.toList polyglossiaLangToBCP47)
74+ where
75+ mk (polyglossia, bcp47Func) =
76+ ("text" <> polyglossia, inlineLanguage tok bcp47Func)
77+
78+inlineLanguage :: PandocMonad m
79+ => LP m Inlines -> (Text -> Lang) -> LP m Inlines
80+inlineLanguage tok bcp47Func = do
81+ o <- option "" $ T.filter (\c -> c /= '[' && c /= ']')
82+ <$> rawopt
83+ let lang = renderLang $ bcp47Func o
84+ extractSpaces (spanWith ("", [], [("lang", lang)])) <$> tok
85+
86+setDefaultLanguage :: PandocMonad m => LP m Blocks
87+setDefaultLanguage = do
88+ o <- option "" $ T.filter (\c -> c /= '[' && c /= ']')
89+ <$> rawopt
90+ polylang <- untokenize <$> braced
91+ case M.lookup polylang polyglossiaLangToBCP47 of
92+ Nothing -> return mempty -- TODO mzero? warning?
93+ Just langFunc -> do
94+ let l = langFunc o
95+ setTranslations l
96+ updateState $ setMeta "lang" $ str (renderLang l)
97+ return mempty
22
98
23polyglossiaLangToBCP47 :: M.Map T.Text (T.Text -> Lang)
99polyglossiaLangToBCP47 :: M.Map T.Text (T.Text -> Lang)
24polyglossiaLangToBCP47 = M.fromList
100polyglossiaLangToBCP47 = M.fromList
25 [ ("arabic", \o -> case T.filter (/=' ') o of
101 [ ("arabic", \o -> case T.filter (/=' ') o of
26− "locale=algeria" -> Lang "ar" "" "DZ" []
102+ "locale=algeria" -> Lang "ar" Nothing (Just "DZ") [] [] []
27− "locale=mashriq" -> Lang "ar" "" "SY" []
103+ "locale=mashriq" -> Lang "ar" Nothing (Just "SY") [] [] []
28− "locale=libya" -> Lang "ar" "" "LY" []
104+ "locale=libya" -> Lang "ar" Nothing (Just "LY") [] [] []
29− "locale=morocco" -> Lang "ar" "" "MA" []
105+ "locale=morocco" -> Lang "ar" Nothing (Just "MA") [] [] []
30− "locale=mauritania" -> Lang "ar" "" "MR" []
106+ "locale=mauritania" -> Lang "ar" Nothing (Just "MR") [] [] []
31− "locale=tunisia" -> Lang "ar" "" "TN" []
107+ "locale=tunisia" -> Lang "ar" Nothing (Just "TN") [] [] []
32− _ -> Lang "ar" "" "" [])
108+ _ -> Lang "ar" Nothing (Just "") [] [] [])
33 , ("german", \o -> case T.filter (/=' ') o of
109 , ("german", \o -> case T.filter (/=' ') o of
34− "spelling=old" -> Lang "de" "" "DE" ["1901"]
110+ "spelling=old" -> Lang "de" Nothing (Just "DE") ["1901"] [] []
35 "variant=austrian,spelling=old"
111 "variant=austrian,spelling=old"
36− -> Lang "de" "" "AT" ["1901"]
112+ -> Lang "de" Nothing (Just "AT") ["1901"] [] []
37− "variant=austrian" -> Lang "de" "" "AT" []
113+ "variant=austrian" -> Lang "de" Nothing (Just "AT") [] [] []
38 "variant=swiss,spelling=old"
114 "variant=swiss,spelling=old"
39− -> Lang "de" "" "CH" ["1901"]
115+ -> Lang "de" Nothing (Just "CH") ["1901"] [] []
40− "variant=swiss" -> Lang "de" "" "CH" []
116+ "variant=swiss" -> Lang "de" Nothing (Just "CH") [] [] []
41− _ -> Lang "de" "" "" [])
117+ _ -> Lang "de" Nothing Nothing [] [] [])
42− , ("lsorbian", \_ -> Lang "dsb" "" "" [])
118+ , ("lsorbian", \_ -> Lang "dsb" Nothing Nothing [] [] [])
43 , ("greek", \o -> case T.filter (/=' ') o of
119 , ("greek", \o -> case T.filter (/=' ') o of
44− "variant=poly" -> Lang "el" "" "polyton" []
120+ "variant=poly" -> Lang "el" Nothing (Just "polyton") [] [] []
45− "variant=ancient" -> Lang "grc" "" "" []
121+ "variant=ancient" -> Lang "grc" Nothing Nothing [] [] []
46− _ -> Lang "el" "" "" [])
122+ _ -> Lang "el" Nothing Nothing [] [] [])
47 , ("english", \o -> case T.filter (/=' ') o of
123 , ("english", \o -> case T.filter (/=' ') o of
48− "variant=australian" -> Lang "en" "" "AU" []
124+ "variant=australian" -> Lang "en" Nothing (Just "AU") [] [] []
49− "variant=canadian" -> Lang "en" "" "CA" []
125+ "variant=canadian" -> Lang "en" Nothing (Just "CA") [] [] []
50− "variant=british" -> Lang "en" "" "GB" []
126+ "variant=british" -> Lang "en" Nothing (Just "GB") [] [] []
51− "variant=newzealand" -> Lang "en" "" "NZ" []
127+ "variant=newzealand" -> Lang "en" Nothing (Just "NZ") [] [] []
52− "variant=american" -> Lang "en" "" "US" []
128+ "variant=american" -> Lang "en" Nothing (Just "US") [] [] []
53− _ -> Lang "en" "" "" [])
129+ _ -> Lang "en" Nothing (Just "") [] [] [])
54− , ("usorbian", \_ -> Lang "hsb" "" "" [])
130+ , ("usorbian", \_ -> Lang "hsb" Nothing Nothing [] [] [])
55 , ("latin", \o -> case T.filter (/=' ') o of
131 , ("latin", \o -> case T.filter (/=' ') o of
56− "variant=classic" -> Lang "la" "" "" ["x-classic"]
132+ "variant=classic" -> Lang "la" Nothing Nothing ["x-classic"] [] []
57− _ -> Lang "la" "" "" [])
133+ _ -> Lang "la" Nothing Nothing [] [] [])
58− , ("slovenian", \_ -> Lang "sl" "" "" [])
134+ , ("slovenian", \_ -> Lang "sl" Nothing Nothing [] [] [])
59− , ("serbianc", \_ -> Lang "sr" "cyrl" "" [])
135+ , ("serbianc", \_ -> Lang "sr" (Just "Cyrl") Nothing [] [] [])
60− , ("pinyin", \_ -> Lang "zh" "Latn" "" ["pinyin"])
136+ , ("pinyin", \_ -> Lang "zh" (Just "Latn") Nothing ["pinyin"] [] [])
61− , ("afrikaans", \_ -> Lang "af" "" "" [])
137+ , ("afrikaans", \_ -> simpleLang "af")
62− , ("amharic", \_ -> Lang "am" "" "" [])
138+ , ("amharic", \_ -> simpleLang "am")
63− , ("assamese", \_ -> Lang "as" "" "" [])
139+ , ("assamese", \_ -> simpleLang "as")
64− , ("asturian", \_ -> Lang "ast" "" "" [])
140+ , ("asturian", \_ -> simpleLang "ast")
65− , ("bulgarian", \_ -> Lang "bg" "" "" [])
141+ , ("bulgarian", \_ -> simpleLang "bg")
66− , ("bengali", \_ -> Lang "bn" "" "" [])
142+ , ("bengali", \_ -> simpleLang "bn")
67− , ("tibetan", \_ -> Lang "bo" "" "" [])
143+ , ("tibetan", \_ -> simpleLang "bo")
68− , ("breton", \_ -> Lang "br" "" "" [])
144+ , ("breton", \_ -> simpleLang "br")
69− , ("catalan", \_ -> Lang "ca" "" "" [])
145+ , ("catalan", \_ -> simpleLang "ca")
70− , ("welsh", \_ -> Lang "cy" "" "" [])
146+ , ("welsh", \_ -> simpleLang "cy")
71− , ("czech", \_ -> Lang "cs" "" "" [])
147+ , ("czech", \_ -> simpleLang "cs")
72− , ("coptic", \_ -> Lang "cop" "" "" [])
148+ , ("coptic", \_ -> simpleLang "cop")
73− , ("danish", \_ -> Lang "da" "" "" [])
149+ , ("danish", \_ -> simpleLang "da")
74− , ("divehi", \_ -> Lang "dv" "" "" [])
150+ , ("divehi", \_ -> simpleLang "dv")
75− , ("esperanto", \_ -> Lang "eo" "" "" [])
151+ , ("esperanto", \_ -> simpleLang "eo")
76− , ("spanish", \_ -> Lang "es" "" "" [])
152+ , ("spanish", \_ -> simpleLang "es")
77− , ("estonian", \_ -> Lang "et" "" "" [])
153+ , ("estonian", \_ -> simpleLang "et")
78− , ("basque", \_ -> Lang "eu" "" "" [])
154+ , ("basque", \_ -> simpleLang "eu")
79− , ("farsi", \_ -> Lang "fa" "" "" [])
155+ , ("farsi", \_ -> simpleLang "fa")
80− , ("finnish", \_ -> Lang "fi" "" "" [])
156+ , ("finnish", \_ -> simpleLang "fi")
81− , ("french", \_ -> Lang "fr" "" "" [])
157+ , ("french", \_ -> simpleLang "fr")
82− , ("friulan", \_ -> Lang "fur" "" "" [])
158+ , ("friulan", \_ -> simpleLang "fur")
83− , ("irish", \_ -> Lang "ga" "" "" [])
159+ , ("irish", \_ -> simpleLang "ga")
84− , ("scottish", \_ -> Lang "gd" "" "" [])
160+ , ("scottish", \_ -> simpleLang "gd")
85− , ("ethiopic", \_ -> Lang "gez" "" "" [])
161+ , ("ethiopic", \_ -> simpleLang "gez")
86− , ("galician", \_ -> Lang "gl" "" "" [])
162+ , ("galician", \_ -> simpleLang "gl")
87− , ("hebrew", \_ -> Lang "he" "" "" [])
163+ , ("hebrew", \_ -> simpleLang "he")
88− , ("hindi", \_ -> Lang "hi" "" "" [])
164+ , ("hindi", \_ -> simpleLang "hi")
89− , ("croatian", \_ -> Lang "hr" "" "" [])
165+ , ("croatian", \_ -> simpleLang "hr")
90− , ("magyar", \_ -> Lang "hu" "" "" [])
166+ , ("magyar", \_ -> simpleLang "hu")
91− , ("armenian", \_ -> Lang "hy" "" "" [])
167+ , ("armenian", \_ -> simpleLang "hy")
92− , ("interlingua", \_ -> Lang "ia" "" "" [])
168+ , ("interlingua", \_ -> simpleLang "ia")
93− , ("indonesian", \_ -> Lang "id" "" "" [])
169+ , ("indonesian", \_ -> simpleLang "id")
94− , ("icelandic", \_ -> Lang "is" "" "" [])
170+ , ("icelandic", \_ -> simpleLang "is")
95− , ("italian", \_ -> Lang "it" "" "" [])
171+ , ("italian", \_ -> simpleLang "it")
96− , ("japanese", \_ -> Lang "jp" "" "" [])
172+ , ("japanese", \_ -> simpleLang "jp")
97− , ("khmer", \_ -> Lang "km" "" "" [])
173+ , ("khmer", \_ -> simpleLang "km")
98− , ("kurmanji", \_ -> Lang "kmr" "" "" [])
174+ , ("kurmanji", \_ -> simpleLang "kmr")
99− , ("kannada", \_ -> Lang "kn" "" "" [])
175+ , ("kannada", \_ -> simpleLang "kn")
100− , ("korean", \_ -> Lang "ko" "" "" [])
176+ , ("korean", \_ -> simpleLang "ko")
101− , ("lao", \_ -> Lang "lo" "" "" [])
177+ , ("lao", \_ -> simpleLang "lo")
102− , ("lithuanian", \_ -> Lang "lt" "" "" [])
178+ , ("lithuanian", \_ -> simpleLang "lt")
103− , ("latvian", \_ -> Lang "lv" "" "" [])
179+ , ("latvian", \_ -> simpleLang "lv")
104− , ("malayalam", \_ -> Lang "ml" "" "" [])
180+ , ("malayalam", \_ -> simpleLang "ml")
105− , ("mongolian", \_ -> Lang "mn" "" "" [])
181+ , ("mongolian", \_ -> simpleLang "mn")
106− , ("marathi", \_ -> Lang "mr" "" "" [])
182+ , ("marathi", \_ -> simpleLang "mr")
107− , ("dutch", \_ -> Lang "nl" "" "" [])
183+ , ("dutch", \_ -> simpleLang "nl")
108− , ("nynorsk", \_ -> Lang "nn" "" "" [])
184+ , ("nynorsk", \_ -> simpleLang "nn")
109− , ("norsk", \_ -> Lang "no" "" "" [])
185+ , ("norsk", \_ -> simpleLang "no")
110− , ("nko", \_ -> Lang "nqo" "" "" [])
186+ , ("nko", \_ -> simpleLang "nqo")
111− , ("occitan", \_ -> Lang "oc" "" "" [])
187+ , ("occitan", \_ -> simpleLang "oc")
112− , ("panjabi", \_ -> Lang "pa" "" "" [])
188+ , ("panjabi", \_ -> simpleLang "pa")
113− , ("polish", \_ -> Lang "pl" "" "" [])
189+ , ("polish", \_ -> simpleLang "pl")
114− , ("piedmontese", \_ -> Lang "pms" "" "" [])
190+ , ("piedmontese", \_ -> simpleLang "pms")
115− , ("portuguese", \_ -> Lang "pt" "" "" [])
191+ , ("portuguese", \_ -> simpleLang "pt")
116− , ("romansh", \_ -> Lang "rm" "" "" [])
192+ , ("romansh", \_ -> simpleLang "rm")
117− , ("romanian", \_ -> Lang "ro" "" "" [])
193+ , ("romanian", \_ -> simpleLang "ro")
118− , ("russian", \_ -> Lang "ru" "" "" [])
194+ , ("russian", \_ -> simpleLang "ru")
119− , ("sanskrit", \_ -> Lang "sa" "" "" [])
195+ , ("sanskrit", \_ -> simpleLang "sa")
120− , ("samin", \_ -> Lang "se" "" "" [])
196+ , ("samin", \_ -> simpleLang "se")
121− , ("slovak", \_ -> Lang "sk" "" "" [])
197+ , ("slovak", \_ -> simpleLang "sk")
122− , ("albanian", \_ -> Lang "sq" "" "" [])
198+ , ("albanian", \_ -> simpleLang "sq")
123− , ("serbian", \_ -> Lang "sr" "" "" [])
199+ , ("serbian", \_ -> simpleLang "sr")
124− , ("swedish", \_ -> Lang "sv" "" "" [])
200+ , ("swedish", \_ -> simpleLang "sv")
125− , ("syriac", \_ -> Lang "syr" "" "" [])
201+ , ("syriac", \_ -> simpleLang "syr")
126− , ("tamil", \_ -> Lang "ta" "" "" [])
202+ , ("tamil", \_ -> simpleLang "ta")
127− , ("telugu", \_ -> Lang "te" "" "" [])
203+ , ("telugu", \_ -> simpleLang "te")
128− , ("thai", \_ -> Lang "th" "" "" [])
204+ , ("thai", \_ -> simpleLang "th")
129− , ("turkmen", \_ -> Lang "tk" "" "" [])
205+ , ("turkmen", \_ -> simpleLang "tk")
130− , ("turkish", \_ -> Lang "tr" "" "" [])
206+ , ("turkish", \_ -> simpleLang "tr")
131− , ("ukrainian", \_ -> Lang "uk" "" "" [])
207+ , ("ukrainian", \_ -> simpleLang "uk")
132− , ("urdu", \_ -> Lang "ur" "" "" [])
208+ , ("urdu", \_ -> simpleLang "ur")
133− , ("vietnamese", \_ -> Lang "vi" "" "" [])
209+ , ("vietnamese", \_ -> simpleLang "vi")
134 ]
210 ]
135
211
212+simpleLang :: Text -> Lang
213+simpleLang l = Lang l Nothing Nothing [] [] []
214+
136babelLangToBCP47 :: T.Text -> Maybe Lang
215babelLangToBCP47 :: T.Text -> Maybe Lang
137babelLangToBCP47 s =
216babelLangToBCP47 s =
138 case s of
217 case s of
139− "austrian" -> Just $ Lang "de" "" "AT" ["1901"]
218+ "austrian" -> Just $ Lang "de" Nothing (Just "AT") ["1901"] [] []
140− "naustrian" -> Just $ Lang "de" "" "AT" []
219+ "naustrian" -> Just $ Lang "de" Nothing (Just "AT") [] [] []
141− "swissgerman" -> Just $ Lang "de" "" "CH" ["1901"]
220+ "swissgerman" -> Just $ Lang "de" Nothing (Just "CH") ["1901"] [] []
142− "nswissgerman" -> Just $ Lang "de" "" "CH" []
221+ "nswissgerman" -> Just $ Lang "de" Nothing (Just "CH") [] [] []
143− "german" -> Just $ Lang "de" "" "DE" ["1901"]
222+ "german" -> Just $ Lang "de" Nothing (Just "DE") ["1901"] [] []
144− "ngerman" -> Just $ Lang "de" "" "DE" []
223+ "ngerman" -> Just $ Lang "de" Nothing (Just "DE") [] [] []
145− "lowersorbian" -> Just $ Lang "dsb" "" "" []
224+ "lowersorbian" -> Just $ Lang "dsb" Nothing Nothing [] [] []
146− "uppersorbian" -> Just $ Lang "hsb" "" "" []
225+ "uppersorbian" -> Just $ Lang "hsb" Nothing Nothing [] [] []
147− "polutonikogreek" -> Just $ Lang "el" "" "" ["polyton"]
226+ "polutonikogreek" -> Just $ Lang "el" Nothing Nothing ["polyton"] [] []
148− "slovene" -> Just $ Lang "sl" "" "" []
227+ "slovene" -> Just $ simpleLang "sl"
149− "australian" -> Just $ Lang "en" "" "AU" []
228+ "australian" -> Just $ Lang "en" Nothing (Just "AU") [] [] []
150− "canadian" -> Just $ Lang "en" "" "CA" []
229+ "canadian" -> Just $ Lang "en" Nothing (Just "CA") [] [] []
151− "british" -> Just $ Lang "en" "" "GB" []
230+ "british" -> Just $ Lang "en" Nothing (Just "GB") [] [] []
152− "newzealand" -> Just $ Lang "en" "" "NZ" []
231+ "newzealand" -> Just $ Lang "en" Nothing (Just "NZ") [] [] []
153− "american" -> Just $ Lang "en" "" "US" []
232+ "american" -> Just $ Lang "en" Nothing (Just "US") [] [] []
154− "classiclatin" -> Just $ Lang "la" "" "" ["x-classic"]
233+ "classiclatin" -> Just $ Lang "la" Nothing Nothing ["x-classic"] [] []
155 _ -> ($ "") <$> M.lookup s polyglossiaLangToBCP47
234 _ -> ($ "") <$> M.lookup s polyglossiaLangToBCP47
src/Text/Pandoc/Readers/LaTeX/Macro.hsnew file+184
@@ -0,0 +1,184 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+module Text.Pandoc.Readers.LaTeX.Macro
3+ ( macroDef
4+ )
5+where
6+import Text.Pandoc.Extensions (Extension(..))
7+import Text.Pandoc.Logging (LogMessage(MacroAlreadyDefined))
8+import Text.Pandoc.Readers.LaTeX.Parsing
9+import Text.Pandoc.Readers.LaTeX.Types
10+import Text.Pandoc.Class
11+import Text.Pandoc.Shared (safeRead)
12+import Text.Pandoc.Parsing hiding (blankline, mathDisplay, mathInline,
13+ optional, space, spaces, withRaw, (<|>))
14+import Control.Applicative ((<|>), optional)
15+import qualified Data.Map as M
16+import Data.Text (Text)
17+import qualified Data.Text as T
18+
19+macroDef :: (PandocMonad m, Monoid a) => (Text -> a) -> LP m a
20+macroDef constructor = do
21+ (_, s) <- withRaw (commandDef <|> environmentDef)
22+ (constructor (untokenize s) <$
23+ guardDisabled Ext_latex_macros)
24+ <|> return mempty
25+ where commandDef = do
26+ nameMacroPairs <- newcommand <|> letmacro <|> defmacro <|> newif
27+ guardDisabled Ext_latex_macros <|>
28+ mapM_ (\(name, macro') ->
29+ updateState (\s -> s{ sMacros = M.insert name macro'
30+ (sMacros s) })) nameMacroPairs
31+ environmentDef = do
32+ mbenv <- newenvironment
33+ case mbenv of
34+ Nothing -> return ()
35+ Just (name, macro1, macro2) ->
36+ guardDisabled Ext_latex_macros <|>
37+ do updateState $ \s -> s{ sMacros =
38+ M.insert name macro1 (sMacros s) }
39+ updateState $ \s -> s{ sMacros =
40+ M.insert ("end" <> name) macro2 (sMacros s) }
41+ -- @\newenvironment{envname}[n-args][default]{begin}{end}@
42+ -- is equivalent to
43+ -- @\newcommand{\envname}[n-args][default]{begin}@
44+ -- @\newcommand{\endenvname}@
45+
46+letmacro :: PandocMonad m => LP m [(Text, Macro)]
47+letmacro = do
48+ controlSeq "let"
49+ (name, contents) <- withVerbatimMode $ do
50+ Tok _ (CtrlSeq name) _ <- anyControlSeq
51+ optional $ symbol '='
52+ spaces
53+ -- we first parse in verbatim mode, and then expand macros,
54+ -- because we don't want \let\foo\bar to turn into
55+ -- \let\foo hello if we have previously \def\bar{hello}
56+ contents <- bracedOrToken
57+ return (name, contents)
58+ contents' <- doMacros' 0 contents
59+ return [(name, Macro ExpandWhenDefined [] Nothing contents')]
60+
61+defmacro :: PandocMonad m => LP m [(Text, Macro)]
62+defmacro = do
63+ -- we use withVerbatimMode, because macros are to be expanded
64+ -- at point of use, not point of definition
65+ controlSeq "def"
66+ withVerbatimMode $ do
67+ Tok _ (CtrlSeq name) _ <- anyControlSeq
68+ argspecs <- many (argspecArg <|> argspecPattern)
69+ contents <- bracedOrToken
70+ return [(name, Macro ExpandWhenUsed argspecs Nothing contents)]
71+
72+-- \newif\iffoo' defines:
73+-- \iffoo to be \iffalse
74+-- \footrue to be a command that defines \iffoo to be \iftrue
75+-- \foofalse to be a command that defines \iffoo to be \iffalse
76+newif :: PandocMonad m => LP m [(Text, Macro)]
77+newif = do
78+ controlSeq "newif"
79+ withVerbatimMode $ do
80+ Tok pos (CtrlSeq name) _ <- anyControlSeq
81+ -- \def\iffoo\iffalse
82+ -- \def\footrue{\def\iffoo\iftrue}
83+ -- \def\foofalse{\def\iffoo\iffalse}
84+ let base = T.drop 2 name
85+ return [ (name, Macro ExpandWhenUsed [] Nothing
86+ [Tok pos (CtrlSeq "iffalse") "\\iffalse"])
87+ , (base <> "true",
88+ Macro ExpandWhenUsed [] Nothing
89+ [ Tok pos (CtrlSeq "def") "\\def"
90+ , Tok pos (CtrlSeq name) ("\\" <> name)
91+ , Tok pos (CtrlSeq "iftrue") "\\iftrue"
92+ ])
93+ , (base <> "false",
94+ Macro ExpandWhenUsed [] Nothing
95+ [ Tok pos (CtrlSeq "def") "\\def"
96+ , Tok pos (CtrlSeq name) ("\\" <> name)
97+ , Tok pos (CtrlSeq "iffalse") "\\iffalse"
98+ ])
99+ ]
100+
101+argspecArg :: PandocMonad m => LP m ArgSpec
102+argspecArg = do
103+ Tok _ (Arg i) _ <- satisfyTok isArgTok
104+ return $ ArgNum i
105+
106+argspecPattern :: PandocMonad m => LP m ArgSpec
107+argspecPattern =
108+ Pattern <$> many1 (satisfyTok (\(Tok _ toktype' txt) ->
109+ (toktype' == Symbol || toktype' == Word) &&
110+ (txt /= "{" && txt /= "\\" && txt /= "}")))
111+
112+newcommand :: PandocMonad m => LP m [(Text, Macro)]
113+newcommand = do
114+ Tok pos (CtrlSeq mtype) _ <- controlSeq "newcommand" <|>
115+ controlSeq "renewcommand" <|>
116+ controlSeq "providecommand" <|>
117+ controlSeq "DeclareMathOperator" <|>
118+ controlSeq "DeclareRobustCommand"
119+ withVerbatimMode $ do
120+ Tok _ (CtrlSeq name) txt <- do
121+ optional (symbol '*')
122+ anyControlSeq <|>
123+ (symbol '{' *> spaces *> anyControlSeq <* spaces <* symbol '}')
124+ spaces
125+ numargs <- option 0 $ try bracketedNum
126+ let argspecs = map ArgNum [1..numargs]
127+ spaces
128+ optarg <- option Nothing $ Just <$> try bracketedToks
129+ spaces
130+ contents' <- bracedOrToken
131+ let contents =
132+ case mtype of
133+ "DeclareMathOperator" ->
134+ Tok pos (CtrlSeq "mathop") "\\mathop"
135+ : Tok pos Symbol "{"
136+ : Tok pos (CtrlSeq "mathrm") "\\mathrm"
137+ : Tok pos Symbol "{"
138+ : (contents' ++
139+ [ Tok pos Symbol "}", Tok pos Symbol "}" ])
140+ _ -> contents'
141+ macros <- sMacros <$> getState
142+ case M.lookup name macros of
143+ Just macro
144+ | mtype == "newcommand" -> do
145+ report $ MacroAlreadyDefined txt pos
146+ return [(name, macro)]
147+ | mtype == "providecommand" -> return [(name, macro)]
148+ _ -> return [(name, Macro ExpandWhenUsed argspecs optarg contents)]
149+
150+newenvironment :: PandocMonad m => LP m (Maybe (Text, Macro, Macro))
151+newenvironment = do
152+ pos <- getPosition
153+ Tok _ (CtrlSeq mtype) _ <- controlSeq "newenvironment" <|>
154+ controlSeq "renewenvironment" <|>
155+ controlSeq "provideenvironment"
156+ withVerbatimMode $ do
157+ optional $ symbol '*'
158+ spaces
159+ name <- untokenize <$> braced
160+ spaces
161+ numargs <- option 0 $ try bracketedNum
162+ spaces
163+ optarg <- option Nothing $ Just <$> try bracketedToks
164+ let argspecs = map (\i -> ArgNum i) [1..numargs]
165+ startcontents <- spaces >> bracedOrToken
166+ endcontents <- spaces >> bracedOrToken
167+ macros <- sMacros <$> getState
168+ case M.lookup name macros of
169+ Just _
170+ | mtype == "newenvironment" -> do
171+ report $ MacroAlreadyDefined name pos
172+ return Nothing
173+ | mtype == "provideenvironment" ->
174+ return Nothing
175+ _ -> return $ Just (name,
176+ Macro ExpandWhenUsed argspecs optarg startcontents,
177+ Macro ExpandWhenUsed [] Nothing endcontents)
178+
179+bracketedNum :: PandocMonad m => LP m Int
180+bracketedNum = do
181+ ds <- untokenize <$> bracketedToks
182+ case safeRead ds of
183+ Just i -> return i
184+ _ -> return 0
src/Text/Pandoc/Readers/LaTeX/Math.hsnew file+221
@@ -0,0 +1,221 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+module Text.Pandoc.Readers.LaTeX.Math
3+ ( dollarsMath
4+ , inlineEnvironments
5+ , inlineEnvironment
6+ , mathInline
7+ , mathDisplay
8+ , theoremstyle
9+ , theoremEnvironment
10+ , newtheorem
11+ , proof
12+ )
13+where
14+import Data.Maybe (fromMaybe)
15+import Text.Pandoc.Walk (walk)
16+import Text.Pandoc.Builder as B
17+import qualified Data.Sequence as Seq
18+import Text.Pandoc.Readers.LaTeX.Parsing
19+import Text.Pandoc.Readers.LaTeX.Types
20+import Text.Pandoc.Class
21+import Text.Pandoc.Shared (trimMath, stripTrailingNewlines)
22+import Text.Pandoc.Parsing hiding (blankline, mathDisplay, mathInline,
23+ optional, space, spaces, withRaw, (<|>))
24+import Control.Applicative ((<|>), optional)
25+import Control.Monad (guard, mzero)
26+import qualified Data.Map as M
27+import Data.Text (Text)
28+
29+dollarsMath :: PandocMonad m => LP m Inlines
30+dollarsMath = do
31+ symbol '$'
32+ display <- option False (True <$ symbol '$')
33+ (do contents <- try $ untokenize <$> pDollarsMath 0
34+ if display
35+ then mathDisplay contents <$ symbol '$'
36+ else return $ mathInline contents)
37+ <|> (guard display >> return (mathInline ""))
38+
39+-- Int is number of embedded groupings
40+pDollarsMath :: PandocMonad m => Int -> LP m [Tok]
41+pDollarsMath n = do
42+ tk@(Tok _ toktype t) <- anyTok
43+ case toktype of
44+ Symbol | t == "$"
45+ , n == 0 -> return []
46+ | t == "\\" -> do
47+ tk' <- anyTok
48+ (tk :) . (tk' :) <$> pDollarsMath n
49+ | t == "{" -> (tk :) <$> pDollarsMath (n+1)
50+ | t == "}" ->
51+ if n > 0
52+ then (tk :) <$> pDollarsMath (n-1)
53+ else mzero
54+ _ -> (tk :) <$> pDollarsMath n
55+
56+mathDisplay :: Text -> Inlines
57+mathDisplay = displayMath . trimMath
58+
59+mathInline :: Text -> Inlines
60+mathInline = math . trimMath
61+
62+mathEnvWith :: PandocMonad m
63+ => (Inlines -> a) -> Maybe Text -> Text -> LP m a
64+mathEnvWith f innerEnv name = f . mathDisplay . inner <$> mathEnv name
65+ where inner x = case innerEnv of
66+ Nothing -> x
67+ Just y -> "\\begin{" <> y <> "}\n" <> x <>
68+ "\\end{" <> y <> "}"
69+
70+mathEnv :: PandocMonad m => Text -> LP m Text
71+mathEnv name = do
72+ skipopts
73+ optional blankline
74+ res <- manyTill anyTok (end_ name)
75+ return $ stripTrailingNewlines $ untokenize res
76+
77+inlineEnvironment :: PandocMonad m => LP m Inlines
78+inlineEnvironment = try $ do
79+ controlSeq "begin"
80+ name <- untokenize <$> braced
81+ M.findWithDefault mzero name inlineEnvironments
82+
83+inlineEnvironments :: PandocMonad m => M.Map Text (LP m Inlines)
84+inlineEnvironments = M.fromList [
85+ ("displaymath", mathEnvWith id Nothing "displaymath")
86+ , ("math", math <$> mathEnv "math")
87+ , ("equation", mathEnvWith id Nothing "equation")
88+ , ("equation*", mathEnvWith id Nothing "equation*")
89+ , ("gather", mathEnvWith id (Just "gathered") "gather")
90+ , ("gather*", mathEnvWith id (Just "gathered") "gather*")
91+ , ("multline", mathEnvWith id (Just "gathered") "multline")
92+ , ("multline*", mathEnvWith id (Just "gathered") "multline*")
93+ , ("eqnarray", mathEnvWith id (Just "aligned") "eqnarray")
94+ , ("eqnarray*", mathEnvWith id (Just "aligned") "eqnarray*")
95+ , ("align", mathEnvWith id (Just "aligned") "align")
96+ , ("align*", mathEnvWith id (Just "aligned") "align*")
97+ , ("alignat", mathEnvWith id (Just "aligned") "alignat")
98+ , ("alignat*", mathEnvWith id (Just "aligned") "alignat*")
99+ , ("dmath", mathEnvWith id Nothing "dmath")
100+ , ("dmath*", mathEnvWith id Nothing "dmath*")
101+ , ("dgroup", mathEnvWith id (Just "aligned") "dgroup")
102+ , ("dgroup*", mathEnvWith id (Just "aligned") "dgroup*")
103+ , ("darray", mathEnvWith id (Just "aligned") "darray")
104+ , ("darray*", mathEnvWith id (Just "aligned") "darray*")
105+ ]
106+
107+theoremstyle :: PandocMonad m => LP m Blocks
108+theoremstyle = do
109+ stylename <- untokenize <$> braced
110+ let mbstyle = case stylename of
111+ "plain" -> Just PlainStyle
112+ "definition" -> Just DefinitionStyle
113+ "remark" -> Just RemarkStyle
114+ _ -> Nothing
115+ case mbstyle of
116+ Nothing -> return ()
117+ Just sty -> updateState $ \s -> s{ sLastTheoremStyle = sty }
118+ return mempty
119+
120+newtheorem :: PandocMonad m => LP m Inlines -> LP m Blocks
121+newtheorem inline = do
122+ number <- option True (False <$ symbol '*' <* sp)
123+ name <- untokenize <$> braced
124+ sp
125+ series <- option Nothing $ Just . untokenize <$> bracketedToks
126+ sp
127+ showName <- tokWith inline
128+ sp
129+ syncTo <- option Nothing $ Just . untokenize <$> bracketedToks
130+ sty <- sLastTheoremStyle <$> getState
131+ let spec = TheoremSpec { theoremName = showName
132+ , theoremStyle = sty
133+ , theoremSeries = series
134+ , theoremSyncTo = syncTo
135+ , theoremNumber = number
136+ , theoremLastNum = DottedNum [0] }
137+ tmap <- sTheoremMap <$> getState
138+ updateState $ \s -> s{ sTheoremMap =
139+ M.insert name spec tmap }
140+ return mempty
141+
142+theoremEnvironment :: PandocMonad m
143+ => LP m Blocks -> LP m Inlines -> Text -> LP m Blocks
144+theoremEnvironment blocks opt name = do
145+ tmap <- sTheoremMap <$> getState
146+ case M.lookup name tmap of
147+ Nothing -> mzero
148+ Just tspec -> do
149+ optTitle <- option mempty $ (\x -> space <> "(" <> x <> ")") <$> opt
150+ mblabel <- option Nothing $ Just . untokenize <$>
151+ try (spaces >> controlSeq "label" >> spaces >> braced)
152+ bs <- env name blocks
153+ number <-
154+ if theoremNumber tspec
155+ then do
156+ let name' = fromMaybe name $ theoremSeries tspec
157+ num <- getNextNumber
158+ (maybe (DottedNum [0]) theoremLastNum .
159+ M.lookup name' . sTheoremMap)
160+ updateState $ \s ->
161+ s{ sTheoremMap =
162+ M.adjust
163+ (\spec -> spec{ theoremLastNum = num })
164+ name'
165+ (sTheoremMap s)
166+ }
167+
168+ case mblabel of
169+ Just ident ->
170+ updateState $ \s ->
171+ s{ sLabels = M.insert ident
172+ (B.toList $
173+ theoremName tspec <> "\160" <>
174+ str (renderDottedNum num)) (sLabels s) }
175+ Nothing -> return ()
176+ return $ space <> B.text (renderDottedNum num)
177+ else return mempty
178+ let titleEmph = case theoremStyle tspec of
179+ PlainStyle -> B.strong
180+ DefinitionStyle -> B.strong
181+ RemarkStyle -> B.emph
182+ let title = titleEmph (theoremName tspec <> number)
183+ <> optTitle <> "." <> space
184+ return $ divWith (fromMaybe "" mblabel, [name], []) $ addTitle title
185+ $ case theoremStyle tspec of
186+ PlainStyle -> walk italicize bs
187+ _ -> bs
188+
189+
190+
191+proof :: PandocMonad m => LP m Blocks -> LP m Inlines -> LP m Blocks
192+proof blocks opt = do
193+ title <- option (B.text "Proof") opt
194+ bs <- env "proof" blocks
195+ return $
196+ B.divWith ("", ["proof"], []) $
197+ addQed $ addTitle (B.emph (title <> ".")) bs
198+
199+addTitle :: Inlines -> Blocks -> Blocks
200+addTitle ils bs =
201+ case B.toList bs of
202+ (Para xs : rest)
203+ -> B.fromList (Para (B.toList ils ++ (Space : xs)) : rest)
204+ _ -> B.para ils <> bs
205+
206+addQed :: Blocks -> Blocks
207+addQed bs =
208+ case Seq.viewr (B.unMany bs) of
209+ s Seq.:> Para ils
210+ -> B.Many (s Seq.|> Para (ils ++ B.toList qedSign))
211+ _ -> bs <> B.para qedSign
212+ where
213+ qedSign = B.str "\xa0\x25FB"
214+
215+italicize :: Block -> Block
216+italicize x@(Para [Image{}]) = x -- see #6925
217+italicize (Para ils) = Para [Emph ils]
218+italicize (Plain ils) = Plain [Emph ils]
219+italicize x = x
220+
221+
src/Text/Pandoc/Readers/LaTeX/Parsing.hs+179−53
@@ -27,11 +27,14 @@ module Text.Pandoc.Readers.LaTeX.Parsing
27 , rawLaTeXParser
27 , rawLaTeXParser
28 , applyMacros
28 , applyMacros
29 , tokenize
29 , tokenize
30+ , tokenizeSources
30 , untokenize
31 , untokenize
31 , untoken
32 , untoken
32 , totoks
33 , totoks
33 , toksToString
34 , toksToString
34 , satisfyTok
35 , satisfyTok
36+ , parseFromToks
37+ , disablingWithRaw
35 , doMacros
38 , doMacros
36 , doMacros'
39 , doMacros'
37 , setpos
40 , setpos
@@ -52,6 +55,7 @@ module Text.Pandoc.Readers.LaTeX.Parsing
52 , comment
55 , comment
53 , anyTok
56 , anyTok
54 , singleChar
57 , singleChar
58+ , tokWith
55 , specialChars
59 , specialChars
56 , endline
60 , endline
57 , blankline
61 , blankline
@@ -78,6 +82,11 @@ module Text.Pandoc.Readers.LaTeX.Parsing
78 , rawopt
82 , rawopt
79 , overlaySpecification
83 , overlaySpecification
80 , getNextNumber
84 , getNextNumber
85+ , label
86+ , setCaption
87+ , resetCaption
88+ , env
89+ , addMeta
81 ) where
90 ) where
82
91
83import Control.Applicative (many, (<|>))
92import Control.Applicative (many, (<|>))
@@ -87,13 +96,15 @@ import Control.Monad.Trans (lift)
87import Data.Char (chr, isAlphaNum, isDigit, isLetter, ord)
96import Data.Char (chr, isAlphaNum, isDigit, isLetter, ord)
88import Data.Default
97import Data.Default
89import Data.List (intercalate)
98import Data.List (intercalate)
99+import qualified Data.IntMap as IntMap
90import qualified Data.Map as M
100import qualified Data.Map as M
91import qualified Data.Set as Set
101import qualified Data.Set as Set
92import Data.Text (Text)
102import Data.Text (Text)
93import qualified Data.Text as T
103import qualified Data.Text as T
94import Text.Pandoc.Builder
104import Text.Pandoc.Builder
95import Text.Pandoc.Class.PandocMonad (PandocMonad, report)
105import Text.Pandoc.Class.PandocMonad (PandocMonad, report)
96−import Text.Pandoc.Error (PandocError (PandocMacroLoop))
106+import Text.Pandoc.Error
107+ (PandocError (PandocMacroLoop,PandocShouldNeverHappenError))
97import Text.Pandoc.Logging
108import Text.Pandoc.Logging
98import Text.Pandoc.Options
109import Text.Pandoc.Options
99import Text.Pandoc.Parsing hiding (blankline, many, mathDisplay, mathInline,
110import Text.Pandoc.Parsing hiding (blankline, many, mathDisplay, mathInline,
@@ -102,7 +113,6 @@ import Text.Pandoc.Readers.LaTeX.Types (ExpansionPoint (..), Macro (..),
102 ArgSpec (..), Tok (..), TokType (..))
113 ArgSpec (..), Tok (..), TokType (..))
103import Text.Pandoc.Shared
114import Text.Pandoc.Shared
104import Text.Parsec.Pos
115import Text.Parsec.Pos
105−-- import Debug.Trace
106
116
107newtype DottedNum = DottedNum [Int]
117newtype DottedNum = DottedNum [Int]
108 deriving (Show, Eq)
118 deriving (Show, Eq)
@@ -151,8 +161,9 @@ data LaTeXState = LaTeXState{ sOptions :: ReaderOptions
151 , sLabels :: M.Map Text [Inline]
161 , sLabels :: M.Map Text [Inline]
152 , sHasChapters :: Bool
162 , sHasChapters :: Bool
153 , sToggles :: M.Map Text Bool
163 , sToggles :: M.Map Text Bool
154− , sExpanded :: Bool
155 , sFileContents :: M.Map Text Text
164 , sFileContents :: M.Map Text Text
165+ , sEnableWithRaw :: Bool
166+ , sRawTokens :: IntMap.IntMap [Tok]
156 }
167 }
157 deriving Show
168 deriving Show
158
169
@@ -177,8 +188,9 @@ defaultLaTeXState = LaTeXState{ sOptions = def
177 , sLabels = M.empty
188 , sLabels = M.empty
178 , sHasChapters = False
189 , sHasChapters = False
179 , sToggles = M.empty
190 , sToggles = M.empty
180− , sExpanded = False
181 , sFileContents = M.empty
191 , sFileContents = M.empty
192+ , sEnableWithRaw = True
193+ , sRawTokens = IntMap.empty
182 }
194 }
183
195
184instance PandocMonad m => HasQuoteContext LaTeXState m where
196instance PandocMonad m => HasQuoteContext LaTeXState m where
@@ -236,7 +248,7 @@ withVerbatimMode parser = do
236
248
237rawLaTeXParser :: (PandocMonad m, HasMacros s, HasReaderOptions s)
249rawLaTeXParser :: (PandocMonad m, HasMacros s, HasReaderOptions s)
238 => [Tok] -> Bool -> LP m a -> LP m a
250 => [Tok] -> Bool -> LP m a -> LP m a
239− -> ParserT Text s m (a, Text)
251+ -> ParserT Sources s m (a, Text)
240rawLaTeXParser toks retokenize parser valParser = do
252rawLaTeXParser toks retokenize parser valParser = do
241 pstate <- getState
253 pstate <- getState
242 let lstate = def{ sOptions = extractReaderOptions pstate }
254 let lstate = def{ sOptions = extractReaderOptions pstate }
@@ -248,7 +260,6 @@ rawLaTeXParser toks retokenize parser valParser = do
248 Right toks' -> do
260 Right toks' -> do
249 res <- lift $ runParserT (do when retokenize $ do
261 res <- lift $ runParserT (do when retokenize $ do
250 -- retokenize, applying macros
262 -- retokenize, applying macros
251− doMacros
252 ts <- many (satisfyTok (const True))
263 ts <- many (satisfyTok (const True))
253 setInput ts
264 setInput ts
254 rawparser)
265 rawparser)
@@ -257,7 +268,7 @@ rawLaTeXParser toks retokenize parser valParser = do
257 Left _ -> mzero
268 Left _ -> mzero
258 Right ((val, raw), st) -> do
269 Right ((val, raw), st) -> do
259 updateState (updateMacros (sMacros st <>))
270 updateState (updateMacros (sMacros st <>))
260− _ <- takeP (T.length (untokenize toks'))
271+ void $ count (T.length (untokenize toks')) anyChar
261 let result = untokenize raw
272 let result = untokenize raw
262 -- ensure we end with space if input did, see #4442
273 -- ensure we end with space if input did, see #4442
263 let result' =
274 let result' =
@@ -270,7 +281,7 @@ rawLaTeXParser toks retokenize parser valParser = do
270 return (val, result')
281 return (val, result')
271
282
272applyMacros :: (PandocMonad m, HasMacros s, HasReaderOptions s)
283applyMacros :: (PandocMonad m, HasMacros s, HasReaderOptions s)
273− => Text -> ParserT Text s m Text
284+ => Text -> ParserT Sources s m Text
274applyMacros s = (guardDisabled Ext_latex_macros >> return s) <|>
285applyMacros s = (guardDisabled Ext_latex_macros >> return s) <|>
275 do let retokenize = untokenize <$> many (satisfyTok (const True))
286 do let retokenize = untokenize <$> many (satisfyTok (const True))
276 pstate <- getState
287 pstate <- getState
@@ -281,6 +292,20 @@ applyMacros s = (guardDisabled Ext_latex_macros >> return s) <|>
281 Left e -> Prelude.fail (show e)
292 Left e -> Prelude.fail (show e)
282 Right s' -> return s'
293 Right s' -> return s'
283
294
295+{-
296+When tokenize or untokenize change, test with this
297+QuickCheck property:
298+
299+> tokUntokRoundtrip :: String -> Bool
300+> tokUntokRoundtrip s =
301+> let t = T.pack s in untokenize (tokenize "random" t) == t
302+-}
303+
304+tokenizeSources :: Sources -> [Tok]
305+tokenizeSources = concatMap tokenizeSource . unSources
306+ where
307+ tokenizeSource (pos, t) = totoks pos t
308+
284tokenize :: SourceName -> Text -> [Tok]
309tokenize :: SourceName -> Text -> [Tok]
285tokenize sourcename = totoks (initialPos sourcename)
310tokenize sourcename = totoks (initialPos sourcename)
286
311
@@ -404,12 +429,31 @@ untoken t = untokenAccum t mempty
404toksToString :: [Tok] -> String
429toksToString :: [Tok] -> String
405toksToString = T.unpack . untokenize
430toksToString = T.unpack . untokenize
406
431
432+parseFromToks :: PandocMonad m => LP m a -> [Tok] -> LP m a
433+parseFromToks parser toks = do
434+ oldInput <- getInput
435+ setInput toks
436+ result <- disablingWithRaw parser
437+ setInput oldInput
438+ return result
439+
440+disablingWithRaw :: PandocMonad m => LP m a -> LP m a
441+disablingWithRaw parser = do
442+ oldEnableWithRaw <- sEnableWithRaw <$> getState
443+ updateState $ \st -> st{ sEnableWithRaw = False }
444+ result <- parser
445+ updateState $ \st -> st{ sEnableWithRaw = oldEnableWithRaw }
446+ return result
447+
407satisfyTok :: PandocMonad m => (Tok -> Bool) -> LP m Tok
448satisfyTok :: PandocMonad m => (Tok -> Bool) -> LP m Tok
408satisfyTok f = do
449satisfyTok f = do
409 doMacros -- apply macros on remaining input stream
450 doMacros -- apply macros on remaining input stream
410 res <- tokenPrim (T.unpack . untoken) updatePos matcher
451 res <- tokenPrim (T.unpack . untoken) updatePos matcher
411− updateState $ \st -> st{ sExpanded = False }
452+ updateState $ \st ->
453+ if sEnableWithRaw st
454+ then st{ sRawTokens = IntMap.map (res:) $ sRawTokens st }
455+ else st
412− return res
456+ return $! res
413 where matcher t | f t = Just t
457 where matcher t | f t = Just t
414 | otherwise = Nothing
458 | otherwise = Nothing
415 updatePos :: SourcePos -> Tok -> [Tok] -> SourcePos
459 updatePos :: SourcePos -> Tok -> [Tok] -> SourcePos
@@ -418,27 +462,24 @@ satisfyTok f = do
418
462
419doMacros :: PandocMonad m => LP m ()
463doMacros :: PandocMonad m => LP m ()
420doMacros = do
464doMacros = do
421− expanded <- sExpanded <$> getState
465+ st <- getState
422− verbatimMode <- sVerbatimMode <$> getState
466+ unless (sVerbatimMode st) $
423− unless (expanded || verbatimMode) $ do
424− getInput >>= doMacros' 1 >>= setInput
467+ getInput >>= doMacros' 1 >>= setInput
425− updateState $ \st -> st{ sExpanded = True }
426
468
427doMacros' :: PandocMonad m => Int -> [Tok] -> LP m [Tok]
469doMacros' :: PandocMonad m => Int -> [Tok] -> LP m [Tok]
428doMacros' n inp =
470doMacros' n inp =
429 case inp of
471 case inp of
430 Tok spos (CtrlSeq "begin") _ : Tok _ Symbol "{" :
472 Tok spos (CtrlSeq "begin") _ : Tok _ Symbol "{" :
431 Tok _ Word name : Tok _ Symbol "}" : ts
473 Tok _ Word name : Tok _ Symbol "}" : ts
432− -> handleMacros n spos name ts
474+ -> handleMacros n spos name ts <|> return inp
433 Tok spos (CtrlSeq "end") _ : Tok _ Symbol "{" :
475 Tok spos (CtrlSeq "end") _ : Tok _ Symbol "{" :
434 Tok _ Word name : Tok _ Symbol "}" : ts
476 Tok _ Word name : Tok _ Symbol "}" : ts
435− -> handleMacros n spos ("end" <> name) ts
477+ -> handleMacros n spos ("end" <> name) ts <|> return inp
436 Tok _ (CtrlSeq "expandafter") _ : t : ts
478 Tok _ (CtrlSeq "expandafter") _ : t : ts
437 -> combineTok t <$> doMacros' n ts
479 -> combineTok t <$> doMacros' n ts
438 Tok spos (CtrlSeq name) _ : ts
480 Tok spos (CtrlSeq name) _ : ts
439− -> handleMacros n spos name ts
481+ -> handleMacros n spos name ts <|> return inp
440 _ -> return inp
482 _ -> return inp
441− <|> return inp
442
483
443 where
484 where
444 combineTok (Tok spos (CtrlSeq name) x) (Tok _ Word w : ts)
485 combineTok (Tok spos (CtrlSeq name) x) (Tok _ Word w : ts)
@@ -484,7 +525,7 @@ doMacros' n inp =
484 $ throwError $ PandocMacroLoop name
525 $ throwError $ PandocMacroLoop name
485 macros <- sMacros <$> getState
526 macros <- sMacros <$> getState
486 case M.lookup name macros of
527 case M.lookup name macros of
487− Nothing -> mzero
528+ Nothing -> trySpecialMacro name ts
488 Just (Macro expansionPoint argspecs optarg newtoks) -> do
529 Just (Macro expansionPoint argspecs optarg newtoks) -> do
489 let getargs' = do
530 let getargs' = do
490 args <-
531 args <-
@@ -512,6 +553,41 @@ doMacros' n inp =
512 ExpandWhenUsed -> doMacros' (n' + 1) result
553 ExpandWhenUsed -> doMacros' (n' + 1) result
513 ExpandWhenDefined -> return result
554 ExpandWhenDefined -> return result
514
555
556+-- | Certain macros do low-level tex manipulations that can't
557+-- be represented in our Macro type, so we handle them here.
558+trySpecialMacro :: PandocMonad m => Text -> [Tok] -> LP m [Tok]
559+trySpecialMacro "xspace" ts = do
560+ ts' <- doMacros' 1 ts
561+ case ts' of
562+ Tok pos Word t : _
563+ | startsWithAlphaNum t -> return $ Tok pos Spaces " " : ts'
564+ _ -> return ts'
565+trySpecialMacro "iftrue" ts = handleIf True ts
566+trySpecialMacro "iffalse" ts = handleIf False ts
567+trySpecialMacro _ _ = mzero
568+
569+handleIf :: PandocMonad m => Bool -> [Tok] -> LP m [Tok]
570+handleIf b ts = do
571+ res' <- lift $ runParserT (ifParser b) defaultLaTeXState "tokens" ts
572+ case res' of
573+ Left _ -> Prelude.fail "Could not parse conditional"
574+ Right ts' -> return ts'
575+
576+ifParser :: PandocMonad m => Bool -> LP m [Tok]
577+ifParser b = do
578+ ifToks <- many (notFollowedBy (controlSeq "else" <|> controlSeq "fi")
579+ *> anyTok)
580+ elseToks <- (controlSeq "else" >> manyTill anyTok (controlSeq "fi"))
581+ <|> ([] <$ controlSeq "fi")
582+ rest <- getInput
583+ return $ (if b then ifToks else elseToks) ++ rest
584+
585+startsWithAlphaNum :: Text -> Bool
586+startsWithAlphaNum t =
587+ case T.uncons t of
588+ Just (c, _) | isAlphaNum c -> True
589+ _ -> False
590+
515setpos :: SourcePos -> Tok -> Tok
591setpos :: SourcePos -> Tok -> Tok
516setpos spos (Tok _ tt txt) = Tok spos tt txt
592setpos spos (Tok _ tt txt) = Tok spos tt txt
517
593
@@ -594,18 +670,22 @@ isCommentTok _ = False
594anyTok :: PandocMonad m => LP m Tok
670anyTok :: PandocMonad m => LP m Tok
595anyTok = satisfyTok (const True)
671anyTok = satisfyTok (const True)
596
672
673+singleCharTok :: PandocMonad m => LP m Tok
674+singleCharTok =
675+ satisfyTok $ \case
676+ Tok _ Word t -> T.length t == 1
677+ Tok _ Symbol t -> not (T.any (`Set.member` specialChars) t)
678+ _ -> False
679+
597singleChar :: PandocMonad m => LP m Tok
680singleChar :: PandocMonad m => LP m Tok
598−singleChar = try $ do
681+singleChar = singleCharTok <|> singleCharFromWord
682+ where
683+ singleCharFromWord = do
599− Tok pos toktype t <- satisfyTok (tokTypeIn [Word, Symbol])
684+ Tok pos toktype t <- disablingWithRaw $ satisfyTok isWordTok
600− guard $ not $ toktype == Symbol &&
601− T.any (`Set.member` specialChars) t
602− if T.length t > 1
603− then do
604− let (t1, t2) = (T.take 1 t, T.drop 1 t)
685+ let (t1, t2) = (T.take 1 t, T.drop 1 t)
605− inp <- getInput
686+ inp <- getInput
606− setInput $ Tok (incSourceColumn pos 1) toktype t2 : inp
687+ setInput $ Tok pos toktype t1 : Tok (incSourceColumn pos 1) toktype t2 : inp
607− return $ Tok pos toktype t1
688+ anyTok
608− else return $ Tok pos toktype t
609
689
610specialChars :: Set.Set Char
690specialChars :: Set.Set Char
611specialChars = Set.fromList "#$%&~_^\\{}"
691specialChars = Set.fromList "#$%&~_^\\{}"
@@ -648,28 +728,25 @@ grouped parser = try $ do
648 -- {{a,b}} should be parsed the same as {a,b}
728 -- {{a,b}} should be parsed the same as {a,b}
649 try (grouped parser <* egroup) <|> (mconcat <$> manyTill parser egroup)
729 try (grouped parser <* egroup) <|> (mconcat <$> manyTill parser egroup)
650
730
651−braced' :: PandocMonad m => LP m Tok -> Int -> LP m [Tok]
731+braced' :: PandocMonad m => LP m Tok -> LP m [Tok]
652−braced' getTok n =
732+braced' getTok = symbol '{' *> go (1 :: Int)
653− handleEgroup <|> handleBgroup <|> handleOther
654− where handleEgroup = do
733+ where
655− t <- symbol '}'
734+ go n = do
656− if n == 1
735+ t <- getTok
736+ case t of
737+ Tok _ Symbol "}"
738+ | n > 1 -> (t:) <$> go (n - 1)
657− then return []
739+ | otherwise -> return []
658− else (t:) <$> braced' getTok (n - 1)
740+ Tok _ Symbol "{" -> (t:) <$> go (n + 1)
659− handleBgroup = do
741+ _ -> (t:) <$> go n
660− t <- symbol '{'
661− (t:) <$> braced' getTok (n + 1)
662− handleOther = do
663− t <- getTok
664− (t:) <$> braced' getTok n
665
742
666braced :: PandocMonad m => LP m [Tok]
743braced :: PandocMonad m => LP m [Tok]
667−braced = symbol '{' *> braced' anyTok 1
744+braced = braced' anyTok
668
745
669-- URLs require special handling, because they can contain %
746-- URLs require special handling, because they can contain %
670-- characters. So we retonenize comments as we go...
747-- characters. So we retonenize comments as we go...
671bracedUrl :: PandocMonad m => LP m [Tok]
748bracedUrl :: PandocMonad m => LP m [Tok]
672−bracedUrl = bgroup *> braced' (retokenizeComment >> anyTok) 1
749+bracedUrl = braced' (retokenizeComment >> anyTok)
673
750
674-- For handling URLs, which allow literal % characters...
751-- For handling URLs, which allow literal % characters...
675retokenizeComment :: PandocMonad m => LP m ()
752retokenizeComment :: PandocMonad m => LP m ()
@@ -725,16 +802,29 @@ ignore raw = do
725
802
726withRaw :: PandocMonad m => LP m a -> LP m (a, [Tok])
803withRaw :: PandocMonad m => LP m a -> LP m (a, [Tok])
727withRaw parser = do
804withRaw parser = do
728− inp <- getInput
805+ rawTokensMap <- sRawTokens <$> getState
806+ let key = case IntMap.lookupMax rawTokensMap of
807+ Nothing -> 0
808+ Just (n,_) -> n + 1
809+ -- insert empty list at key
810+ updateState $ \st -> st{ sRawTokens =
811+ IntMap.insert key [] $ sRawTokens st }
729 result <- parser
812 result <- parser
730− nxtpos <- option Nothing ((\(Tok pos' _ _) -> Just pos') <$> lookAhead anyTok)
813+ mbRevToks <- IntMap.lookup key . sRawTokens <$> getState
731− let raw = takeWhile (\(Tok pos _ _) -> maybe True
814+ raw <- case mbRevToks of
732− (\p -> sourceName p /= sourceName pos || pos < p) nxtpos) inp
815+ Just revtoks -> do
816+ updateState $ \st -> st{ sRawTokens =
817+ IntMap.delete key $ sRawTokens st}
818+ return $ reverse revtoks
819+ Nothing ->
820+ throwError $ PandocShouldNeverHappenError $
821+ "sRawTokens has nothing at key " <> T.pack (show key)
733 return (result, raw)
822 return (result, raw)
734
823
735keyval :: PandocMonad m => LP m (Text, Text)
824keyval :: PandocMonad m => LP m (Text, Text)
736keyval = try $ do
825keyval = try $ do
826+ key <- untokenize <$> many1 (notFollowedBy (symbol '=') >>
737− Tok _ Word key <- satisfyTok isWordTok
827+ (symbol '-' <|> symbol '_' <|> satisfyTok isWordTok))
738 sp
828 sp
739 val <- option mempty $ do
829 val <- option mempty $ do
740 symbol '='
830 symbol '='
@@ -794,7 +884,7 @@ getRawCommand name txt = do
794 (_, rawargs) <- withRaw $
884 (_, rawargs) <- withRaw $
795 case name of
885 case name of
796 "write" -> do
886 "write" -> do
797− void $ satisfyTok isWordTok -- digits
887+ void $ many $ satisfyTok isDigitTok -- digits
798 void braced
888 void braced
799 "titleformat" -> do
889 "titleformat" -> do
800 void braced
890 void braced
@@ -809,6 +899,10 @@ getRawCommand name txt = do
809 void $ many braced
899 void $ many braced
810 return $ txt <> untokenize rawargs
900 return $ txt <> untokenize rawargs
811
901
902+isDigitTok :: Tok -> Bool
903+isDigitTok (Tok _ Word t) = T.all isDigit t
904+isDigitTok _ = False
905+
812skipopts :: PandocMonad m => LP m ()
906skipopts :: PandocMonad m => LP m ()
813skipopts = skipMany (void overlaySpecification <|> void rawopt)
907skipopts = skipMany (void overlaySpecification <|> void rawopt)
814
908
@@ -876,3 +970,35 @@ getNextNumber getCurrentNum = do
876 Just n -> [n, 1]
970 Just n -> [n, 1]
877 Nothing -> [1]
971 Nothing -> [1]
878
972
973+label :: PandocMonad m => LP m ()
974+label = do
975+ controlSeq "label"
976+ t <- braced
977+ updateState $ \st -> st{ sLastLabel = Just $ untokenize t }
978+
979+setCaption :: PandocMonad m => LP m Inlines -> LP m ()
980+setCaption inline = try $ do
981+ skipopts
982+ ils <- tokWith inline
983+ optional $ try $ spaces *> label
984+ updateState $ \st -> st{ sCaption = Just ils }
985+
986+resetCaption :: PandocMonad m => LP m ()
987+resetCaption = updateState $ \st -> st{ sCaption = Nothing
988+ , sLastLabel = Nothing }
989+
990+env :: PandocMonad m => Text -> LP m a -> LP m a
991+env name p = p <* end_ name
992+
993+tokWith :: PandocMonad m => LP m Inlines -> LP m Inlines
994+tokWith inlineParser = try $ spaces >>
995+ grouped inlineParser
996+ <|> (lookAhead anyControlSeq >> inlineParser)
997+ <|> singleChar'
998+ where singleChar' = do
999+ Tok _ _ t <- singleChar
1000+ return $ str t
1001+
1002+addMeta :: PandocMonad m => ToMetaValue a => Text -> a -> LP m ()
1003+addMeta field val = updateState $ \st ->
1004+ st{ sMeta = addMetaField field val $ sMeta st }
src/Text/Pandoc/Readers/LaTeX/SIunitx.hs+152−71
@@ -1,12 +1,7 @@
1+{-# LANGUAGE ScopedTypeVariables #-}
1{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE OverloadedStrings #-}
2module Text.Pandoc.Readers.LaTeX.SIunitx
3module Text.Pandoc.Readers.LaTeX.SIunitx
3− ( dosi
4+ ( siunitxCommands )
4− , doSI
5− , doSIrange
6− , doSInum
7− , doSInumlist
8− , doSIang
9− )
10where
5where
11import Text.Pandoc.Builder
6import Text.Pandoc.Builder
12import Text.Pandoc.Readers.LaTeX.Parsing
7import Text.Pandoc.Readers.LaTeX.Parsing
@@ -15,14 +10,32 @@ import Text.Pandoc.Class
15import Text.Pandoc.Parsing hiding (blankline, mathDisplay, mathInline,
10import Text.Pandoc.Parsing hiding (blankline, mathDisplay, mathInline,
16 optional, space, spaces, withRaw, (<|>))
11 optional, space, spaces, withRaw, (<|>))
17import Control.Applicative ((<|>))
12import Control.Applicative ((<|>))
13+import Control.Monad (void)
18import qualified Data.Map as M
14import qualified Data.Map as M
19import Data.Char (isDigit)
15import Data.Char (isDigit)
20import Data.Text (Text)
16import Data.Text (Text)
21import qualified Data.Text as T
17import qualified Data.Text as T
22import Data.List (intersperse)
18import Data.List (intersperse)
19+import qualified Data.Sequence as Seq
20+import Text.Pandoc.Walk (walk)
21+
22+siunitxCommands :: PandocMonad m
23+ => LP m Inlines -> M.Map Text (LP m Inlines)
24+siunitxCommands tok = M.fromList
25+ [ ("si", dosi tok)
26+ , ("SI", doSI tok)
27+ , ("SIrange", doSIrange True tok)
28+ , ("numrange", doSIrange False tok)
29+ , ("numlist", doSInumlist)
30+ , ("SIlist", doSIlist tok)
31+ , ("num", doSInum)
32+ , ("ang", doSIang)
33+ ]
23
34
24dosi :: PandocMonad m => LP m Inlines -> LP m Inlines
35dosi :: PandocMonad m => LP m Inlines -> LP m Inlines
36+dosi tok = do
37+ options <- option [] keyvals
25−dosi tok = grouped (siUnit tok) <|> siUnit tok
38+ grouped (siUnit options tok) <|> siUnit options tok
26
39
27-- converts e.g. \SI{1}[\$]{} to "$ 1" or \SI{1}{\euro} to "1 €"
40-- converts e.g. \SI{1}[\$]{} to "$ 1" or \SI{1}{\euro} to "1 €"
28doSI :: PandocMonad m => LP m Inlines -> LP m Inlines
41doSI :: PandocMonad m => LP m Inlines -> LP m Inlines
@@ -57,23 +70,50 @@ doSInumlist = do
57 mconcat (intersperse (str "," <> space) (init xs)) <>
70 mconcat (intersperse (str "," <> space) (init xs)) <>
58 text ", & " <> last xs
71 text ", & " <> last xs
59
72
73+doSIlist :: PandocMonad m => LP m Inlines -> LP m Inlines
74+doSIlist tok = do
75+ options <- option [] keyvals
76+ nums <- map tonum . T.splitOn ";" . untokenize <$> braced
77+ unit <- grouped (siUnit options tok) <|> siUnit options tok
78+ let xs = map (<> (str "\xa0" <> unit)) nums
79+ case xs of
80+ [] -> return mempty
81+ [x] -> return x
82+ _ -> return $
83+ mconcat (intersperse (str "," <> space) (init xs)) <>
84+ text ", & " <> last xs
85+
60parseNum :: Parser Text () Inlines
86parseNum :: Parser Text () Inlines
61parseNum = (mconcat <$> many parseNumPart) <* eof
87parseNum = (mconcat <$> many parseNumPart) <* eof
62
88
89+minus :: Text
90+minus = "\x2212"
91+
92+hyphenToMinus :: Inline -> Inline
93+hyphenToMinus (Str t) = Str (T.replace "-" minus t)
94+hyphenToMinus x = x
95+
63parseNumPart :: Parser Text () Inlines
96parseNumPart :: Parser Text () Inlines
64parseNumPart =
97parseNumPart =
65 parseDecimalNum <|>
98 parseDecimalNum <|>
66 parseComma <|>
99 parseComma <|>
67 parsePlusMinus <|>
100 parsePlusMinus <|>
101+ parsePM <|>
68 parseI <|>
102 parseI <|>
69 parseExp <|>
103 parseExp <|>
70 parseX <|>
104 parseX <|>
71 parseSpace
105 parseSpace
72 where
106 where
73− parseDecimalNum = do
107+ parseDecimalNum, parsePlusMinus, parsePM,
108+ parseComma, parseI, parseX,
109+ parseExp, parseSpace :: Parser Text () Inlines
110+ parseDecimalNum = try $ do
74− pref <- option mempty $ (mempty <$ char '+') <|> ("\x2212" <$ char '-')
111+ pref <- option mempty $ (mempty <$ char '+') <|> (minus <$ char '-')
75− basenum <- (pref <>) . T.pack
76− <$> many1 (satisfy (\c -> isDigit c || c == '.'))
112+ basenum' <- many1 (satisfy (\c -> isDigit c || c == '.'))
113+ let basenum = pref <> T.pack
114+ (case basenum' of
115+ '.':_ -> '0':basenum'
116+ _ -> basenum')
77 uncertainty <- option mempty $ T.pack <$> parseParens
117 uncertainty <- option mempty $ T.pack <$> parseParens
78 if T.null uncertainty
118 if T.null uncertainty
79 then return $ str basenum
119 then return $ str basenum
@@ -91,6 +131,7 @@ parseNumPart =
91 | otherwise -> "." <> t
131 | otherwise -> "." <> t
92 parseComma = str "." <$ char ','
132 parseComma = str "." <$ char ','
93 parsePlusMinus = str "\xa0\xb1\xa0" <$ try (string "+-")
133 parsePlusMinus = str "\xa0\xb1\xa0" <$ try (string "+-")
134+ parsePM = str "\xa0\xb1\xa0" <$ try (string "\\pm")
94 parseParens =
135 parseParens =
95 char '(' *> many1 (satisfy (\c -> isDigit c || c == '.')) <* char ')'
136 char '(' *> many1 (satisfy (\c -> isDigit c || c == '.')) <* char ')'
96 parseI = str "i" <$ char 'i'
137 parseI = str "i" <$ char 'i'
@@ -103,11 +144,14 @@ doSIang :: PandocMonad m => LP m Inlines
103doSIang = do
144doSIang = do
104 skipopts
145 skipopts
105 ps <- T.splitOn ";" . untokenize <$> braced
146 ps <- T.splitOn ";" . untokenize <$> braced
147+ let dropPlus t = case T.uncons t of
148+ Just ('+',t') -> t'
149+ _ -> t
106 case ps ++ repeat "" of
150 case ps ++ repeat "" of
107 (d:m:s:_) -> return $
151 (d:m:s:_) -> return $
108− (if T.null d then mempty else str d <> str "\xb0") <>
152+ (if T.null d then mempty else str (dropPlus d) <> str "\xb0") <>
109− (if T.null m then mempty else str m <> str "\x2032") <>
153+ (if T.null m then mempty else str (dropPlus m) <> str "\x2032") <>
110− (if T.null s then mempty else str s <> str "\x2033")
154+ (if T.null s then mempty else str (dropPlus s) <> str "\x2033")
111 _ -> return mempty
155 _ -> return mempty
112
156
113-- converts e.g. \SIrange{100}{200}{\ms} to "100 ms--200 ms"
157-- converts e.g. \SIrange{100}{200}{\ms} to "100 ms--200 ms"
@@ -136,40 +180,99 @@ doSIrange includeUnits tok = do
136emptyOr160 :: Inlines -> Inlines
180emptyOr160 :: Inlines -> Inlines
137emptyOr160 x = if x == mempty then x else str "\160"
181emptyOr160 x = if x == mempty then x else str "\160"
138
182
139−siUnit :: PandocMonad m => LP m Inlines -> LP m Inlines
183+siUnit :: forall m. PandocMonad m => [(Text,Text)] -> LP m Inlines -> LP m Inlines
140−siUnit tok = try (do
184+siUnit options tok = mconcat . intersperse (str "\xa0") <$> many1 siUnitPart
141− Tok _ (CtrlSeq name) _ <- anyControlSeq
185+ where
142− case name of
186+ siUnitPart :: LP m Inlines
187+ siUnitPart = try $ do
188+ skipMany (void (symbol '.') <|> void (symbol '~') <|> spaces1)
189+ x <- ((siPrefix <*> siBase)
190+ <|> (do u <- siBase <|> tok
191+ option u $ siSuffix <*> pure u))
192+ option x (siInfix x)
193+ siInfix :: Inlines -> LP m Inlines
194+ siInfix u1 = try $
195+ (do _ <- controlSeq "per"
196+ u2 <- siUnitPart
197+ let useSlash = lookup "per-mode" options == Just "symbol"
198+ if useSlash
199+ then return (u1 <> str "/" <> u2)
200+ else return (u1 <> str "\xa0" <> negateExponent u2))
201+ <|> (do _ <- symbol '/'
202+ u2 <- siUnitPart
203+ return (u1 <> str "/" <> u2))
204+ siPrefix :: LP m (Inlines -> Inlines)
205+ siPrefix =
143− "square" -> do
206+ (do _ <- controlSeq "square"
144− unit <- siUnit tok
207+ skipopts
145− return $ unit <> superscript "2"
208+ return (<> superscript "2"))
146− "cubic" -> do
209+ <|> (do _ <- controlSeq "cubic"
147− unit <- siUnit tok
210+ skipopts
148− return $ unit <> superscript "3"
211+ return (<> superscript "3"))
149− "raisetothe" -> do
212+ <|> (do _ <- controlSeq "raisetothe"
150− n <- tok
213+ skipopts
151− unit <- siUnit tok
214+ n <- walk hyphenToMinus <$> tok
152− return $ unit <> superscript n
215+ return (<> superscript n))
153− _ ->
216+ siSuffix :: LP m (Inlines -> Inlines)
154− case M.lookup name siUnitMap of
217+ siSuffix =
155− Just il ->
156− option il $
157− choice
158− [ (il <> superscript "2") <$ controlSeq "squared"
218+ (do _ <- controlSeq "squared"
219+ skipopts
220+ return (<> superscript "2"))
159− , (il <> superscript "3") <$ controlSeq "cubed"
221+ <|> (do _ <- controlSeq "cubed"
222+ skipopts
223+ return (<> superscript "3"))
160− , (\n -> il <> superscript n) <$> (controlSeq "tothe" *> tok)
224+ <|> (do _ <- controlSeq "tothe"
161− ]
225+ skipopts
226+ n <- walk hyphenToMinus <$> tok
227+ return (<> superscript n))
228+ <|> (symbol '^' *> (do n <- walk hyphenToMinus <$> tok
229+ return (<> superscript n)))
230+ <|> (symbol '_' *> (do n <- walk hyphenToMinus <$> tok
231+ return (<> subscript n)))
232+ negateExponent :: Inlines -> Inlines
233+ negateExponent ils =
234+ case Seq.viewr (unMany ils) of
235+ xs Seq.:> Superscript ss -> (Many xs) <>
236+ superscript (str minus <> fromList ss)
237+ _ -> ils <> superscript (str (minus <> "1"))
238+ siBase :: LP m Inlines
239+ siBase =
240+ ((try
241+ (do Tok _ (CtrlSeq name) _ <- anyControlSeq
242+ case M.lookup name siUnitModifierMap of
243+ Just il -> (il <>) <$> siBase
244+ Nothing ->
245+ case M.lookup name siUnitMap of
246+ Just il -> pure il
162− Nothing -> fail "not an siunit unit command")
247+ Nothing -> fail "not a unit command"))
163− <|> (lookAhead anyControlSeq >> tok)
164− <|> (do Tok _ Word t <- satisfyTok isWordTok
248+ <|> (do Tok _ Word t <- satisfyTok isWordTok
165− return $ str t)
249+ return $ str t)
166− <|> (symbol '^' *> (superscript <$> tok))
250+ )
167− <|> (symbol '_' *> (subscript <$> tok))
251+
168− <|> ("\xa0" <$ symbol '.')
252+siUnitModifierMap :: M.Map Text Inlines
169− <|> ("\xa0" <$ symbol '~')
253+siUnitModifierMap = M.fromList
170− <|> tok
254+ [ ("atto", str "a")
171− <|> (do Tok _ _ t <- anyTok
255+ , ("centi", str "c")
172− return (str t))
256+ , ("deca", str "d")
257+ , ("deci", str "d")
258+ , ("deka", str "d")
259+ , ("exa", str "E")
260+ , ("femto", str "f")
261+ , ("giga", str "G")
262+ , ("hecto", str "h")
263+ , ("kilo", str "k")
264+ , ("mega", str "M")
265+ , ("micro", str "μ")
266+ , ("milli", str "m")
267+ , ("nano", str "n")
268+ , ("peta", str "P")
269+ , ("pico", str "p")
270+ , ("tera", str "T")
271+ , ("yocto", str "y")
272+ , ("yotta", str "Y")
273+ , ("zepto", str "z")
274+ , ("zetta", str "Z")
275+ ]
173
276
174siUnitMap :: M.Map Text Inlines
277siUnitMap :: M.Map Text Inlines
175siUnitMap = M.fromList
278siUnitMap = M.fromList
@@ -269,7 +372,6 @@ siUnitMap = M.fromList
269 , ("arcsecond", str "″")
372 , ("arcsecond", str "″")
270 , ("astronomicalunit", str "ua")
373 , ("astronomicalunit", str "ua")
271 , ("atomicmassunit", str "u")
374 , ("atomicmassunit", str "u")
272− , ("atto", str "a")
273 , ("bar", str "bar")
375 , ("bar", str "bar")
274 , ("barn", str "b")
376 , ("barn", str "b")
275 , ("becquerel", str "Bq")
377 , ("becquerel", str "Bq")
@@ -277,51 +379,38 @@ siUnitMap = M.fromList
277 , ("bohr", emph (str "a") <> subscript (str "0"))
379 , ("bohr", emph (str "a") <> subscript (str "0"))
278 , ("candela", str "cd")
380 , ("candela", str "cd")
279 , ("celsius", str "°C")
381 , ("celsius", str "°C")
280− , ("centi", str "c")
281 , ("clight", emph (str "c") <> subscript (str "0"))
382 , ("clight", emph (str "c") <> subscript (str "0"))
282 , ("coulomb", str "C")
383 , ("coulomb", str "C")
283 , ("dalton", str "Da")
384 , ("dalton", str "Da")
284 , ("day", str "d")
385 , ("day", str "d")
285− , ("deca", str "d")
286− , ("deci", str "d")
287 , ("decibel", str "db")
386 , ("decibel", str "db")
288 , ("degreeCelsius",str "°C")
387 , ("degreeCelsius",str "°C")
289 , ("degree", str "°")
388 , ("degree", str "°")
290− , ("deka", str "d")
291 , ("electronmass", emph (str "m") <> subscript (str "e"))
389 , ("electronmass", emph (str "m") <> subscript (str "e"))
292 , ("electronvolt", str "eV")
390 , ("electronvolt", str "eV")
293 , ("elementarycharge", emph (str "e"))
391 , ("elementarycharge", emph (str "e"))
294− , ("exa", str "E")
295 , ("farad", str "F")
392 , ("farad", str "F")
296− , ("femto", str "f")
297− , ("giga", str "G")
298 , ("gram", str "g")
393 , ("gram", str "g")
299 , ("gray", str "Gy")
394 , ("gray", str "Gy")
300 , ("hartree", emph (str "E") <> subscript (str "h"))
395 , ("hartree", emph (str "E") <> subscript (str "h"))
301 , ("hectare", str "ha")
396 , ("hectare", str "ha")
302− , ("hecto", str "h")
303 , ("henry", str "H")
397 , ("henry", str "H")
304 , ("hertz", str "Hz")
398 , ("hertz", str "Hz")
305 , ("hour", str "h")
399 , ("hour", str "h")
306 , ("joule", str "J")
400 , ("joule", str "J")
307 , ("katal", str "kat")
401 , ("katal", str "kat")
308 , ("kelvin", str "K")
402 , ("kelvin", str "K")
309− , ("kilo", str "k")
310 , ("kilogram", str "kg")
403 , ("kilogram", str "kg")
311 , ("knot", str "kn")
404 , ("knot", str "kn")
312 , ("liter", str "L")
405 , ("liter", str "L")
313 , ("litre", str "l")
406 , ("litre", str "l")
314 , ("lumen", str "lm")
407 , ("lumen", str "lm")
315 , ("lux", str "lx")
408 , ("lux", str "lx")
316− , ("mega", str "M")
317 , ("meter", str "m")
409 , ("meter", str "m")
318 , ("metre", str "m")
410 , ("metre", str "m")
319− , ("micro", str "μ")
320− , ("milli", str "m")
321 , ("minute", str "min")
411 , ("minute", str "min")
322 , ("mmHg", str "mmHg")
412 , ("mmHg", str "mmHg")
323 , ("mole", str "mol")
413 , ("mole", str "mol")
324− , ("nano", str "n")
325 , ("nauticalmile", str "M")
414 , ("nauticalmile", str "M")
326 , ("neper", str "Np")
415 , ("neper", str "Np")
327 , ("newton", str "N")
416 , ("newton", str "N")
@@ -329,25 +418,17 @@ siUnitMap = M.fromList
329 , ("Pa", str "Pa")
418 , ("Pa", str "Pa")
330 , ("pascal", str "Pa")
419 , ("pascal", str "Pa")
331 , ("percent", str "%")
420 , ("percent", str "%")
332− , ("per", str "/")
333− , ("peta", str "P")
334− , ("pico", str "p")
335 , ("planckbar", emph (str "\x210f"))
421 , ("planckbar", emph (str "\x210f"))
336 , ("radian", str "rad")
422 , ("radian", str "rad")
337 , ("second", str "s")
423 , ("second", str "s")
338 , ("siemens", str "S")
424 , ("siemens", str "S")
339 , ("sievert", str "Sv")
425 , ("sievert", str "Sv")
340 , ("steradian", str "sr")
426 , ("steradian", str "sr")
341− , ("tera", str "T")
342 , ("tesla", str "T")
427 , ("tesla", str "T")
343 , ("tonne", str "t")
428 , ("tonne", str "t")
344 , ("volt", str "V")
429 , ("volt", str "V")
345 , ("watt", str "W")
430 , ("watt", str "W")
346 , ("weber", str "Wb")
431 , ("weber", str "Wb")
347− , ("yocto", str "y")
348− , ("yotta", str "Y")
349− , ("zepto", str "z")
350− , ("zetta", str "Z")
351 ]
432 ]
352
433
353
434
src/Text/Pandoc/Readers/LaTeX/Table.hsnew file+378
@@ -0,0 +1,378 @@
1+{-# LANGUAGE BangPatterns #-}
2+{-# LANGUAGE OverloadedStrings #-}
3+module Text.Pandoc.Readers.LaTeX.Table
4+ ( tableEnvironments )
5+where
6+
7+import Data.Functor (($>))
8+import Text.Pandoc.Class
9+import Text.Pandoc.Readers.LaTeX.Parsing
10+import Text.Pandoc.Readers.LaTeX.Types
11+import Text.Pandoc.Builder as B
12+import qualified Data.Map as M
13+import Data.Text (Text)
14+import Data.Maybe (fromMaybe)
15+import qualified Data.Text as T
16+import Control.Applicative ((<|>), optional, many)
17+import Control.Monad (when, void)
18+import Text.Pandoc.Shared (safeRead, trim)
19+import Text.Pandoc.Logging (LogMessage(SkippedContent))
20+import Text.Pandoc.Walk (walkM)
21+import Text.Pandoc.Parsing hiding (blankline, many, mathDisplay, mathInline,
22+ optional, space, spaces, withRaw, (<|>))
23+
24+tableEnvironments :: PandocMonad m
25+ => LP m Blocks
26+ -> LP m Inlines
27+ -> M.Map Text (LP m Blocks)
28+tableEnvironments blocks inline =
29+ M.fromList
30+ [ ("longtable", env "longtable" $
31+ resetCaption *>
32+ simpTable blocks inline "longtable" False >>= addTableCaption)
33+ , ("table", env "table" $
34+ skipopts *> resetCaption *> blocks >>= addTableCaption)
35+ , ("tabular*", env "tabular*" $ simpTable blocks inline "tabular*" True)
36+ , ("tabularx", env "tabularx" $ simpTable blocks inline "tabularx" True)
37+ , ("tabular", env "tabular" $ simpTable blocks inline "tabular" False)
38+ ]
39+
40+hline :: PandocMonad m => LP m ()
41+hline = try $ do
42+ spaces
43+ controlSeq "hline" <|>
44+ -- booktabs rules:
45+ controlSeq "toprule" <|>
46+ controlSeq "bottomrule" <|>
47+ controlSeq "midrule" <|>
48+ controlSeq "endhead" <|>
49+ controlSeq "endfirsthead"
50+ spaces
51+ optional rawopt
52+ return ()
53+
54+lbreak :: PandocMonad m => LP m Tok
55+lbreak = (controlSeq "\\" <|> controlSeq "tabularnewline")
56+ <* skipopts <* spaces
57+
58+amp :: PandocMonad m => LP m Tok
59+amp = symbol '&'
60+
61+-- Split a Word into individual Symbols (for parseAligns)
62+splitWordTok :: PandocMonad m => LP m ()
63+splitWordTok = do
64+ inp <- getInput
65+ case inp of
66+ (Tok spos Word t : rest) ->
67+ setInput $ map (Tok spos Symbol . T.singleton) (T.unpack t) <> rest
68+ _ -> return ()
69+
70+parseAligns :: PandocMonad m => LP m [(Alignment, ColWidth, ([Tok], [Tok]))]
71+parseAligns = try $ do
72+ let maybeBar = skipMany
73+ (try $ sp *> (() <$ symbol '|' <|> () <$ (symbol '@' >> braced)))
74+ let cAlign = AlignCenter <$ symbol 'c'
75+ let lAlign = AlignLeft <$ symbol 'l'
76+ let rAlign = AlignRight <$ symbol 'r'
77+ let parAlign = AlignLeft <$ symbol 'p'
78+ -- aligns from tabularx
79+ let xAlign = AlignLeft <$ symbol 'X'
80+ let mAlign = AlignLeft <$ symbol 'm'
81+ let bAlign = AlignLeft <$ symbol 'b'
82+ let alignChar = splitWordTok *> ( cAlign <|> lAlign <|> rAlign <|> parAlign
83+ <|> xAlign <|> mAlign <|> bAlign )
84+ let alignPrefix = symbol '>' >> braced
85+ let alignSuffix = symbol '<' >> braced
86+ let colWidth = try $ do
87+ symbol '{'
88+ ds <- trim . untokenize <$> manyTill anyTok (controlSeq "linewidth")
89+ spaces
90+ symbol '}'
91+ return $ safeRead ds
92+ let alignSpec = do
93+ pref <- option [] alignPrefix
94+ spaces
95+ al <- alignChar
96+ width <- colWidth <|> option Nothing (do s <- untokenize <$> braced
97+ pos <- getPosition
98+ report $ SkippedContent s pos
99+ return Nothing)
100+ spaces
101+ suff <- option [] alignSuffix
102+ return (al, width, (pref, suff))
103+ let starAlign = do -- '*{2}{r}' == 'rr', we just expand like a macro
104+ symbol '*'
105+ spaces
106+ ds <- trim . untokenize <$> braced
107+ spaces
108+ spec <- braced
109+ case safeRead ds of
110+ Just n ->
111+ getInput >>= setInput . (mconcat (replicate n spec) ++)
112+ Nothing -> Prelude.fail $ "Could not parse " <> T.unpack ds <> " as number"
113+ bgroup
114+ spaces
115+ maybeBar
116+ aligns' <- many $ try $ spaces >> optional starAlign >>
117+ (alignSpec <* maybeBar)
118+ spaces
119+ egroup
120+ spaces
121+ return $ map toSpec aligns'
122+ where
123+ toColWidth (Just w) | w > 0 = ColWidth w
124+ toColWidth _ = ColWidthDefault
125+ toSpec (x, y, z) = (x, toColWidth y, z)
126+
127+-- N.B. this parser returns a Row that may have erroneous empty cells
128+-- in it. See the note above fixTableHead for details.
129+parseTableRow :: PandocMonad m
130+ => LP m Blocks -- ^ block parser
131+ -> LP m Inlines -- ^ inline parser
132+ -> Text -- ^ table environment name
133+ -> [([Tok], [Tok])] -- ^ pref/suffixes
134+ -> LP m Row
135+parseTableRow blocks inline envname prefsufs = do
136+ notFollowedBy (spaces *> end_ envname)
137+ -- contexts that can contain & that is not colsep:
138+ let canContainAmp (Tok _ (CtrlSeq "begin") _) = True
139+ canContainAmp (Tok _ (CtrlSeq "verb") _) = True
140+ canContainAmp (Tok _ (CtrlSeq "Verb") _) = True
141+ canContainAmp _ = False
142+ -- add prefixes and suffixes in token stream:
143+ let celltoks (pref, suff) = do
144+ prefpos <- getPosition
145+ contents <- mconcat <$>
146+ many ( snd <$> withRaw
147+ ((lookAhead (controlSeq "parbox") >>
148+ void blocks) -- #5711
149+ <|>
150+ (lookAhead (satisfyTok canContainAmp) >> void inline)
151+ <|>
152+ (lookAhead (symbol '$') >> void inline))
153+ <|>
154+ (do notFollowedBy
155+ (() <$ amp <|> () <$ lbreak <|> end_ envname)
156+ count 1 anyTok) )
157+
158+ suffpos <- getPosition
159+ option [] (count 1 amp)
160+ return $ map (setpos prefpos) pref ++ contents ++ map (setpos suffpos) suff
161+ rawcells <- mapM celltoks prefsufs
162+ cells <- mapM (parseFromToks (parseTableCell blocks)) rawcells
163+ spaces
164+ return $ Row nullAttr cells
165+
166+parseTableCell :: PandocMonad m => LP m Blocks -> LP m Cell
167+parseTableCell blocks = do
168+ spaces
169+ updateState $ \st -> st{ sInTableCell = True }
170+ cell' <- multicolumnCell blocks
171+ <|> multirowCell blocks
172+ <|> parseSimpleCell
173+ <|> parseEmptyCell
174+ updateState $ \st -> st{ sInTableCell = False }
175+ spaces
176+ return cell'
177+ where
178+ -- The parsing of empty cells is important in LaTeX, especially when dealing
179+ -- with multirow/multicolumn. See #6603.
180+ parseEmptyCell = spaces $> emptyCell
181+ parseSimpleCell = simpleCell <$> (plainify <$> blocks)
182+
183+
184+cellAlignment :: PandocMonad m => LP m Alignment
185+cellAlignment = skipMany (symbol '|') *> alignment <* skipMany (symbol '|')
186+ where
187+ alignment = do
188+ c <- untoken <$> singleChar
189+ return $ case c of
190+ "l" -> AlignLeft
191+ "r" -> AlignRight
192+ "c" -> AlignCenter
193+ "*" -> AlignDefault
194+ _ -> AlignDefault
195+
196+plainify :: Blocks -> Blocks
197+plainify bs = case toList bs of
198+ [Para ils] -> plain (fromList ils)
199+ _ -> bs
200+
201+multirowCell :: PandocMonad m => LP m Blocks -> LP m Cell
202+multirowCell blocks = controlSeq "multirow" >> do
203+ -- Full prototype for \multirow macro is:
204+ -- \multirow[vpos]{nrows}[bigstruts]{width}[vmove]{text}
205+ -- However, everything except `nrows` and `text` make
206+ -- sense in the context of the Pandoc AST
207+ _ <- optional $ symbol '[' *> cellAlignment <* symbol ']' -- vertical position
208+ nrows <- fmap (fromMaybe 1 . safeRead . untokenize) braced
209+ _ <- optional $ symbol '[' *> manyTill anyTok (symbol ']') -- bigstrut-related
210+ _ <- symbol '{' *> manyTill anyTok (symbol '}') -- Cell width
211+ _ <- optional $ symbol '[' *> manyTill anyTok (symbol ']') -- Length used for fine-tuning
212+ content <- symbol '{' *> (plainify <$> blocks) <* symbol '}'
213+ return $ cell AlignDefault (RowSpan nrows) (ColSpan 1) content
214+
215+multicolumnCell :: PandocMonad m => LP m Blocks -> LP m Cell
216+multicolumnCell blocks = controlSeq "multicolumn" >> do
217+ span' <- fmap (fromMaybe 1 . safeRead . untokenize) braced
218+ alignment <- symbol '{' *> cellAlignment <* symbol '}'
219+
220+ let singleCell = do
221+ content <- plainify <$> blocks
222+ return $ cell alignment (RowSpan 1) (ColSpan span') content
223+
224+ -- Two possible contents: either a \multirow cell, or content.
225+ -- E.g. \multicol{1}{c}{\multirow{2}{1em}{content}}
226+ -- Note that a \multirow cell can be nested in a \multicolumn,
227+ -- but not the other way around. See #6603
228+ let nestedCell = do
229+ (Cell _ _ (RowSpan rs) _ bs) <- multirowCell blocks
230+ return $ cell
231+ alignment
232+ (RowSpan rs)
233+ (ColSpan span')
234+ (fromList bs)
235+
236+ symbol '{' *> (nestedCell <|> singleCell) <* symbol '}'
237+
238+-- LaTeX tables are stored with empty cells underneath multirow cells
239+-- denoting the grid spaces taken up by them. More specifically, if a
240+-- cell spans m rows, then it will overwrite all the cells in the
241+-- columns it spans for (m-1) rows underneath it, requiring padding
242+-- cells in these places. These padding cells need to be removed for
243+-- proper table reading. See #6603.
244+--
245+-- These fixTable functions do not otherwise fix up malformed
246+-- input tables: that is left to the table builder.
247+fixTableHead :: TableHead -> TableHead
248+fixTableHead (TableHead attr rows) = TableHead attr rows'
249+ where
250+ rows' = fixTableRows rows
251+
252+fixTableBody :: TableBody -> TableBody
253+fixTableBody (TableBody attr rhc th tb)
254+ = TableBody attr rhc th' tb'
255+ where
256+ th' = fixTableRows th
257+ tb' = fixTableRows tb
258+
259+fixTableRows :: [Row] -> [Row]
260+fixTableRows = fixTableRows' $ repeat Nothing
261+ where
262+ fixTableRows' oldHang (Row attr cells : rs)
263+ = let (newHang, cells') = fixTableRow oldHang cells
264+ rs' = fixTableRows' newHang rs
265+ in Row attr cells' : rs'
266+ fixTableRows' _ [] = []
267+
268+-- The overhang is represented as Just (relative cell dimensions) or
269+-- Nothing for an empty grid space.
270+fixTableRow :: [Maybe (ColSpan, RowSpan)] -> [Cell] -> ([Maybe (ColSpan, RowSpan)], [Cell])
271+fixTableRow oldHang cells
272+ -- If there's overhang, drop cells until their total width meets the
273+ -- width of the occupied grid spaces (or we run out)
274+ | (n, prefHang, restHang) <- splitHang oldHang
275+ , n > 0
276+ = let cells' = dropToWidth getCellW n cells
277+ (restHang', cells'') = fixTableRow restHang cells'
278+ in (prefHang restHang', cells'')
279+ -- Otherwise record the overhang of a pending cell and fix the rest
280+ -- of the row
281+ | c@(Cell _ _ h w _):cells' <- cells
282+ = let h' = max 1 h
283+ w' = max 1 w
284+ oldHang' = dropToWidth getHangW w' oldHang
285+ (newHang, cells'') = fixTableRow oldHang' cells'
286+ in (toHang w' h' <> newHang, c : cells'')
287+ | otherwise
288+ = (oldHang, [])
289+ where
290+ getCellW (Cell _ _ _ w _) = w
291+ getHangW = maybe 1 fst
292+ getCS (ColSpan n) = n
293+
294+ toHang c r
295+ | r > 1 = [Just (c, r)]
296+ | otherwise = replicate (getCS c) Nothing
297+
298+ -- Take the prefix of the overhang list representing filled grid
299+ -- spaces. Also return the remainder and the length of this prefix.
300+ splitHang = splitHang' 0 id
301+
302+ splitHang' !n l (Just (c, r):xs)
303+ = splitHang' (n + c) (l . (toHang c (r-1) ++)) xs
304+ splitHang' n l xs = (n, l, xs)
305+
306+ -- Drop list items until the total width of the dropped items
307+ -- exceeds the passed width.
308+ dropToWidth _ n l | n < 1 = l
309+ dropToWidth wproj n (c:cs) = dropToWidth wproj (n - wproj c) cs
310+ dropToWidth _ _ [] = []
311+
312+simpTable :: PandocMonad m
313+ => LP m Blocks
314+ -> LP m Inlines
315+ -> Text
316+ -> Bool
317+ -> LP m Blocks
318+simpTable blocks inline envname hasWidthParameter = try $ do
319+ when hasWidthParameter $ () <$ tokWith inline
320+ skipopts
321+ colspecs <- parseAligns
322+ let (aligns, widths, prefsufs) = unzip3 colspecs
323+ optional $ controlSeq "caption" *> setCaption inline
324+ spaces
325+ optional label
326+ spaces
327+ optional lbreak
328+ spaces
329+ skipMany hline
330+ spaces
331+ header' <- option [] . try . fmap (:[]) $
332+ parseTableRow blocks inline envname prefsufs <*
333+ lbreak <* many1 hline
334+ spaces
335+ rows <- sepEndBy (parseTableRow blocks inline envname prefsufs)
336+ (lbreak <* optional (skipMany hline))
337+ spaces
338+ optional $ controlSeq "caption" *> setCaption inline
339+ spaces
340+ optional label
341+ spaces
342+ optional lbreak
343+ spaces
344+ lookAhead $ controlSeq "end" -- make sure we're at end
345+ let th = fixTableHead $ TableHead nullAttr header'
346+ let tbs = [fixTableBody $ TableBody nullAttr 0 [] rows]
347+ let tf = TableFoot nullAttr []
348+ return $ table emptyCaption (zip aligns widths) th tbs tf
349+
350+addTableCaption :: PandocMonad m => Blocks -> LP m Blocks
351+addTableCaption = walkM go
352+ where go (Table attr c spec th tb tf) = do
353+ st <- getState
354+ let mblabel = sLastLabel st
355+ capt <- case (sCaption st, mblabel) of
356+ (Just ils, Nothing) -> return $ caption Nothing (plain ils)
357+ (Just ils, Just lab) -> do
358+ num <- getNextNumber sLastTableNum
359+ setState
360+ st{ sLastTableNum = num
361+ , sLabels = M.insert lab
362+ [Str (renderDottedNum num)]
363+ (sLabels st) }
364+ return $ caption Nothing (plain ils) -- add number??
365+ (Nothing, _) -> return c
366+ let attr' = case (attr, mblabel) of
367+ ((_,classes,kvs), Just ident) ->
368+ (ident,classes,kvs)
369+ _ -> attr
370+ return $ addAttrDiv attr' $ Table nullAttr capt spec th tb tf
371+ go x = return x
372+
373+-- TODO: For now we add a Div to contain table attributes, since
374+-- most writers don't do anything yet with attributes on Table.
375+-- This can be removed when that changes.
376+addAttrDiv :: Attr -> Block -> Block
377+addAttrDiv ("",[],[]) b = b
378+addAttrDiv attr b = Div attr [b]
src/Text/Pandoc/Readers/LaTeX/Types.hs+14−1
@@ -1,3 +1,4 @@
1+{-# LANGUAGE FlexibleInstances #-}
1{- |
2{- |
2 Module : Text.Pandoc.Readers.LaTeX.Types
3 Module : Text.Pandoc.Readers.LaTeX.Types
3 Copyright : Copyright (C) 2017-2021 John MacFarlane
4 Copyright : Copyright (C) 2017-2021 John MacFarlane
@@ -18,7 +19,9 @@ module Text.Pandoc.Readers.LaTeX.Types ( Tok(..)
18 )
19 )
19where
20where
20import Data.Text (Text)
21import Data.Text (Text)
21−import Text.Parsec.Pos (SourcePos)
22+import Text.Parsec.Pos (SourcePos, sourceName)
23+import Text.Pandoc.Sources
24+import Data.List (groupBy)
22
25
23data TokType = CtrlSeq Text | Spaces | Newline | Symbol | Word | Comment |
26data TokType = CtrlSeq Text | Spaces | Newline | Symbol | Word | Comment |
24 Esc1 | Esc2 | Arg Int
27 Esc1 | Esc2 | Arg Int
@@ -27,6 +30,16 @@ data TokType = CtrlSeq Text | Spaces | Newline | Symbol | Word | Comment |
27data Tok = Tok SourcePos TokType Text
30data Tok = Tok SourcePos TokType Text
28 deriving (Eq, Ord, Show)
31 deriving (Eq, Ord, Show)
29
32
33+instance ToSources [Tok] where
34+ toSources = Sources
35+ . map (\ts -> case ts of
36+ Tok p _ _ : _ -> (p, mconcat $ map tokToText ts)
37+ _ -> error "toSources [Tok] encountered empty group")
38+ . groupBy (\(Tok p1 _ _) (Tok p2 _ _) -> sourceName p1 == sourceName p2)
39+
40+tokToText :: Tok -> Text
41+tokToText (Tok _ _ t) = t
42+
30data ExpansionPoint = ExpandWhenDefined | ExpandWhenUsed
43data ExpansionPoint = ExpandWhenDefined | ExpandWhenUsed
31 deriving (Eq, Ord, Show)
44 deriving (Eq, Ord, Show)
32
45
src/Text/Pandoc/Readers/Man.hs+18−11
@@ -20,7 +20,7 @@ import Control.Monad (liftM, mzero, guard, void)
20import Control.Monad.Trans (lift)
20import Control.Monad.Trans (lift)
21import Control.Monad.Except (throwError)
21import Control.Monad.Except (throwError)
22import Data.Maybe (catMaybes, isJust)
22import Data.Maybe (catMaybes, isJust)
23−import Data.List (intersperse, intercalate)
23+import Data.List (intersperse)
24import qualified Data.Text as T
24import qualified Data.Text as T
25import Text.Pandoc.Builder as B
25import Text.Pandoc.Builder as B
26import Text.Pandoc.Class.PandocMonad (PandocMonad(..), report)
26import Text.Pandoc.Class.PandocMonad (PandocMonad(..), report)
@@ -29,9 +29,8 @@ import Text.Pandoc.Logging (LogMessage(..))
29import Text.Pandoc.Options
29import Text.Pandoc.Options
30import Text.Pandoc.Parsing
30import Text.Pandoc.Parsing
31import Text.Pandoc.Walk (query)
31import Text.Pandoc.Walk (query)
32−import Text.Pandoc.Shared (crFilter, mapLeft)
32+import Text.Pandoc.Shared (mapLeft)
33import Text.Pandoc.Readers.Roff -- TODO explicit imports
33import Text.Pandoc.Readers.Roff -- TODO explicit imports
34−import Text.Parsec hiding (tokenPrim)
35import qualified Text.Parsec as Parsec
34import qualified Text.Parsec as Parsec
36import Text.Parsec.Pos (updatePosString)
35import Text.Parsec.Pos (updatePosString)
37import qualified Data.Foldable as Foldable
36import qualified Data.Foldable as Foldable
@@ -50,13 +49,20 @@ type ManParser m = ParserT [RoffToken] ManState m
50
49
51
50
52-- | Read man (troff) from an input string and return a Pandoc document.
51-- | Read man (troff) from an input string and return a Pandoc document.
53−readMan :: PandocMonad m => ReaderOptions -> T.Text -> m Pandoc
52+readMan :: (PandocMonad m, ToSources a)
53+ => ReaderOptions
54+ -> a
55+ -> m Pandoc
54−readMan opts txt = do
56+readMan opts s = do
55− tokenz <- lexRoff (initialPos "input") (crFilter txt)
57+ let Sources inps = toSources s
58+ tokenz <- mconcat <$> mapM (uncurry lexRoff) inps
56 let state = def {readerOptions = opts} :: ManState
59 let state = def {readerOptions = opts} :: ManState
60+ let fixError (PandocParsecError _ e) = PandocParsecError (Sources inps) e
61+ fixError e = e
57 eitherdoc <- readWithMTokens parseMan state
62 eitherdoc <- readWithMTokens parseMan state
58 (Foldable.toList . unRoffTokens $ tokenz)
63 (Foldable.toList . unRoffTokens $ tokenz)
59− either throwError return eitherdoc
64+ either (throwError . fixError) return eitherdoc
65+
60
66
61readWithMTokens :: PandocMonad m
67readWithMTokens :: PandocMonad m
62 => ParserT [RoffToken] ManState m a -- ^ parser
68 => ParserT [RoffToken] ManState m a -- ^ parser
@@ -64,9 +70,10 @@ readWithMTokens :: PandocMonad m
64 -> [RoffToken] -- ^ input
70 -> [RoffToken] -- ^ input
65 -> m (Either PandocError a)
71 -> m (Either PandocError a)
66readWithMTokens parser state input =
72readWithMTokens parser state input =
67− let leftF = PandocParsecError . T.pack . intercalate "\n" $ show <$> input
73+ let leftF = PandocParsecError mempty
68 in mapLeft leftF `liftM` runParserT parser state "source" input
74 in mapLeft leftF `liftM` runParserT parser state "source" input
69
75
76+
70parseMan :: PandocMonad m => ManParser m Pandoc
77parseMan :: PandocMonad m => ManParser m Pandoc
71parseMan = do
78parseMan = do
72 bs <- many parseBlock <* eof
79 bs <- many parseBlock <* eof
@@ -89,7 +96,7 @@ parseBlock = choice [ parseList
89
96
90parseTable :: PandocMonad m => ManParser m Blocks
97parseTable :: PandocMonad m => ManParser m Blocks
91parseTable = do
98parseTable = do
92− modifyState $ \st -> st { tableCellsPlain = True }
99+ updateState $ \st -> st { tableCellsPlain = True }
93 let isTbl Tbl{} = True
100 let isTbl Tbl{} = True
94 isTbl _ = False
101 isTbl _ = False
95 Tbl _opts rows pos <- msatisfy isTbl
102 Tbl _opts rows pos <- msatisfy isTbl
@@ -135,7 +142,7 @@ parseTable = do
135 case res' of
142 case res' of
136 Left _ -> Prelude.fail "Could not parse table cell"
143 Left _ -> Prelude.fail "Could not parse table cell"
137 Right x -> do
144 Right x -> do
138− modifyState $ \s -> s{ tableCellsPlain = False }
145+ updateState $ \s -> s{ tableCellsPlain = False }
139 return x
146 return x
140 Right x -> return x
147 Right x -> return x
141
148
@@ -222,7 +229,7 @@ parseTitle = do
222 setMeta "section" (linePartsToInlines y)
229 setMeta "section" (linePartsToInlines y)
223 [x] -> setMeta "title" (linePartsToInlines x)
230 [x] -> setMeta "title" (linePartsToInlines x)
224 [] -> id
231 [] -> id
225− modifyState $ \st -> st{ metadata = adjustMeta $ metadata st }
232+ updateState $ \st -> st{ metadata = adjustMeta $ metadata st }
226 return mempty
233 return mempty
227
234
228linePartsToInlines :: [LinePart] -> Inlines
235linePartsToInlines :: [LinePart] -> Inlines
src/Text/Pandoc/Readers/Markdown.hs+286−238
@@ -21,15 +21,17 @@ module Text.Pandoc.Readers.Markdown (
21import Control.Monad
21import Control.Monad
22import Control.Monad.Except (throwError)
22import Control.Monad.Except (throwError)
23import Data.Char (isAlphaNum, isPunctuation, isSpace)
23import Data.Char (isAlphaNum, isPunctuation, isSpace)
24−import Data.List (transpose, elemIndex, sortOn)
24+import Data.List (transpose, elemIndex, sortOn, foldl')
25+import Data.List.NonEmpty (NonEmpty(..))
25import qualified Data.Map as M
26import qualified Data.Map as M
26import Data.Maybe
27import Data.Maybe
27import qualified Data.Set as Set
28import qualified Data.Set as Set
28import Data.Text (Text)
29import Data.Text (Text)
29import qualified Data.Text as T
30import qualified Data.Text as T
30−import qualified Data.Text.Lazy as TL
31import qualified Data.ByteString.Lazy as BL
31import qualified Data.ByteString.Lazy as BL
32−import System.FilePath (addExtension, takeExtension)
32+import System.FilePath (addExtension, takeExtension, takeDirectory)
33+import qualified System.FilePath.Windows as Windows
34+import qualified System.FilePath.Posix as Posix
33import Text.HTML.TagSoup hiding (Row)
35import Text.HTML.TagSoup hiding (Row)
34import Text.Pandoc.Builder (Blocks, Inlines)
36import Text.Pandoc.Builder (Blocks, Inlines)
35import qualified Text.Pandoc.Builder as B
37import qualified Text.Pandoc.Builder as B
@@ -45,20 +47,22 @@ import Text.Pandoc.Readers.HTML (htmlInBalanced, htmlTag, isBlockTag,
45 isCommentTag, isInlineTag, isTextTag)
47 isCommentTag, isInlineTag, isTextTag)
46import Text.Pandoc.Readers.LaTeX (applyMacros, rawLaTeXBlock, rawLaTeXInline)
48import Text.Pandoc.Readers.LaTeX (applyMacros, rawLaTeXBlock, rawLaTeXInline)
47import Text.Pandoc.Shared
49import Text.Pandoc.Shared
48−import qualified Text.Pandoc.UTF8 as UTF8
49import Text.Pandoc.XML (fromEntities)
50import Text.Pandoc.XML (fromEntities)
50−import Text.Pandoc.Readers.Metadata (yamlBsToMeta, yamlBsToRefs)
51+import Text.Pandoc.Readers.Metadata (yamlBsToMeta, yamlBsToRefs, yamlMetaBlock)
52+-- import Debug.Trace (traceShowId)
51
53
52−type MarkdownParser m = ParserT Text ParserState m
54+type MarkdownParser m = ParserT Sources ParserState m
55+
56+type F = Future ParserState
53
57
54-- | Read markdown from an input string and return a Pandoc document.
58-- | Read markdown from an input string and return a Pandoc document.
55−readMarkdown :: PandocMonad m
59+readMarkdown :: (PandocMonad m, ToSources a)
56 => ReaderOptions -- ^ Reader options
60 => ReaderOptions -- ^ Reader options
57− -> Text -- ^ String to parse (assuming @'\n'@ line endings)
61+ -> a -- ^ Input
58 -> m Pandoc
62 -> m Pandoc
59readMarkdown opts s = do
63readMarkdown opts s = do
60 parsed <- readWithM parseMarkdown def{ stateOptions = opts }
64 parsed <- readWithM parseMarkdown def{ stateOptions = opts }
61− (crFilter s <> "\n\n")
65+ (ensureFinalNewlines 3 (toSources s))
62 case parsed of
66 case parsed of
63 Right result -> return result
67 Right result -> return result
64 Left e -> throwError e
68 Left e -> throwError e
@@ -79,7 +83,7 @@ yamlToMeta opts mbfp bstr = do
79 meta <- yamlBsToMeta (fmap B.toMetaValue <$> parseBlocks) bstr
83 meta <- yamlBsToMeta (fmap B.toMetaValue <$> parseBlocks) bstr
80 setPosition oldPos
84 setPosition oldPos
81 return $ runF meta defaultParserState
85 return $ runF meta defaultParserState
82− parsed <- readWithM parser def{ stateOptions = opts } ""
86+ parsed <- readWithM parser def{ stateOptions = opts } ("" :: Text)
83 case parsed of
87 case parsed of
84 Right result -> return result
88 Right result -> return result
85 Left e -> throwError e
89 Left e -> throwError e
@@ -95,14 +99,12 @@ yamlToRefs :: PandocMonad m
95 -> m [MetaValue]
99 -> m [MetaValue]
96yamlToRefs idpred opts mbfp bstr = do
100yamlToRefs idpred opts mbfp bstr = do
97 let parser = do
101 let parser = do
98− oldPos <- getPosition
99 case mbfp of
102 case mbfp of
100 Nothing -> return ()
103 Nothing -> return ()
101 Just fp -> setPosition $ initialPos fp
104 Just fp -> setPosition $ initialPos fp
102 refs <- yamlBsToRefs (fmap B.toMetaValue <$> parseBlocks) idpred bstr
105 refs <- yamlBsToRefs (fmap B.toMetaValue <$> parseBlocks) idpred bstr
103− setPosition oldPos
104 return $ runF refs defaultParserState
106 return $ runF refs defaultParserState
105− parsed <- readWithM parser def{ stateOptions = opts } ""
107+ parsed <- readWithM parser def{ stateOptions = opts } ("" :: Text)
106 case parsed of
108 case parsed of
107 Right result -> return result
109 Right result -> return result
108 Left e -> throwError e
110 Left e -> throwError e
@@ -145,14 +147,14 @@ inList = do
145 ctx <- stateParserContext <$> getState
147 ctx <- stateParserContext <$> getState
146 guard (ctx == ListItemState)
148 guard (ctx == ListItemState)
147
149
148−spnl :: PandocMonad m => ParserT Text st m ()
150+spnl :: PandocMonad m => ParserT Sources st m ()
149spnl = try $ do
151spnl = try $ do
150 skipSpaces
152 skipSpaces
151 optional newline
153 optional newline
152 skipSpaces
154 skipSpaces
153 notFollowedBy (char '\n')
155 notFollowedBy (char '\n')
154
156
155−spnl' :: PandocMonad m => ParserT Text st m Text
157+spnl' :: PandocMonad m => ParserT Sources st m Text
156spnl' = try $ do
158spnl' = try $ do
157 xs <- many spaceChar
159 xs <- many spaceChar
158 ys <- option "" $ try $ (:) <$> newline
160 ys <- option "" $ try $ (:) <$> newline
@@ -247,51 +249,48 @@ titleBlock :: PandocMonad m => MarkdownParser m ()
247titleBlock = pandocTitleBlock <|> mmdTitleBlock
249titleBlock = pandocTitleBlock <|> mmdTitleBlock
248
250
249pandocTitleBlock :: PandocMonad m => MarkdownParser m ()
251pandocTitleBlock :: PandocMonad m => MarkdownParser m ()
250−pandocTitleBlock = try $ do
252+pandocTitleBlock = do
251 guardEnabled Ext_pandoc_title_block
253 guardEnabled Ext_pandoc_title_block
252 lookAhead (char '%')
254 lookAhead (char '%')
255+ try $ do
253− title <- option mempty titleLine
256+ title <- option mempty titleLine
254− author <- option (return []) authorsLine
257+ author <- option (return []) authorsLine
255− date <- option mempty dateLine
258+ date <- option mempty dateLine
256− optional blanklines
259+ optional blanklines
257− let meta' = do title' <- title
260+ let meta' = do title' <- title
258− author' <- author
261+ author' <- author
259− date' <- date
262+ date' <- date
260− return $
263+ return $
264+ (if null title'
265+ then id
261− (if null title' then id else B.setMeta "title" title')
266+ else B.setMeta "title" title')
267+ . (if null author'
268+ then id
262− . (if null author' then id else B.setMeta "author" author')
269+ else B.setMeta "author" author')
270+ . (if null date'
271+ then id
263− . (if null date' then id else B.setMeta "date" date')
272+ else B.setMeta "date" date')
264− $ nullMeta
273+ $ nullMeta
265− updateState $ \st -> st{ stateMeta' = stateMeta' st <> meta' }
274+ updateState $ \st -> st{ stateMeta' = stateMeta' st <> meta' }
266−
275+
267−yamlMetaBlock :: PandocMonad m => MarkdownParser m (F Blocks)
276+yamlMetaBlock' :: PandocMonad m => MarkdownParser m (F Blocks)
268−yamlMetaBlock = try $ do
277+yamlMetaBlock' = do
269 guardEnabled Ext_yaml_metadata_block
278 guardEnabled Ext_yaml_metadata_block
270− string "---"
271− blankline
272− notFollowedBy blankline -- if --- is followed by a blank it's an HRULE
273− rawYamlLines <- manyTill anyLine stopLine
274− -- by including --- and ..., we allow yaml blocks with just comments:
275− let rawYaml = T.unlines ("---" : (rawYamlLines ++ ["..."]))
276− optional blanklines
277− newMetaF <- yamlBsToMeta (fmap B.toMetaValue <$> parseBlocks)
279+ newMetaF <- yamlMetaBlock (fmap B.toMetaValue <$> parseBlocks)
278− $ UTF8.fromTextLazy $ TL.fromStrict rawYaml
279 -- Since `<>` is left-biased, existing values are not touched:
280 -- Since `<>` is left-biased, existing values are not touched:
280 updateState $ \st -> st{ stateMeta' = stateMeta' st <> newMetaF }
281 updateState $ \st -> st{ stateMeta' = stateMeta' st <> newMetaF }
281 return mempty
282 return mempty
282
283
283−stopLine :: PandocMonad m => MarkdownParser m ()
284−stopLine = try $ (string "---" <|> string "...") >> blankline >> return ()
285−
286mmdTitleBlock :: PandocMonad m => MarkdownParser m ()
284mmdTitleBlock :: PandocMonad m => MarkdownParser m ()
287−mmdTitleBlock = try $ do
285+mmdTitleBlock = do
288 guardEnabled Ext_mmd_title_block
286 guardEnabled Ext_mmd_title_block
287+ try $ do
289− firstPair <- kvPair False
288+ firstPair <- kvPair False
290− restPairs <- many (kvPair True)
289+ restPairs <- many (kvPair True)
291− let kvPairs = firstPair : restPairs
290+ let kvPairs = firstPair : restPairs
292− blanklines
291+ blanklines
293− updateState $ \st -> st{ stateMeta' = stateMeta' st <>
292+ updateState $ \st -> st{ stateMeta' = stateMeta' st <>
294− return (Meta $ M.fromList kvPairs) }
293+ return (Meta $ M.fromList kvPairs) }
295
294
296kvPair :: PandocMonad m => Bool -> MarkdownParser m (Text, MetaValue)
295kvPair :: PandocMonad m => Bool -> MarkdownParser m (Text, MetaValue)
297kvPair allowEmpty = try $ do
296kvPair allowEmpty = try $ do
@@ -300,7 +299,7 @@ kvPair allowEmpty = try $ do
300 (try $ newline >> lookAhead (blankline <|> nonspaceChar))
299 (try $ newline >> lookAhead (blankline <|> nonspaceChar))
301 guard $ allowEmpty || not (T.null val)
300 guard $ allowEmpty || not (T.null val)
302 let key' = T.concat $ T.words $ T.toLower key
301 let key' = T.concat $ T.words $ T.toLower key
303− let val' = MetaBlocks $ B.toList $ B.plain $ B.text val
302+ let val' = MetaInlines $ B.toList $ B.text val
304 return (key',val')
303 return (key',val')
305
304
306parseMarkdown :: PandocMonad m => MarkdownParser m Pandoc
305parseMarkdown :: PandocMonad m => MarkdownParser m Pandoc
@@ -334,10 +333,14 @@ referenceKey = try $ do
334 skipMany spaceChar
333 skipMany spaceChar
335 notFollowedBy' referenceTitle
334 notFollowedBy' referenceTitle
336 notFollowedBy' $ guardEnabled Ext_link_attributes >> attributes
335 notFollowedBy' $ guardEnabled Ext_link_attributes >> attributes
336+ notFollowedBy' $ guardEnabled Ext_mmd_link_attributes >>
337+ try (spnl <* keyValAttr)
337 notFollowedBy' (() <$ reference)
338 notFollowedBy' (() <$ reference)
338 many1Char $ notFollowedBy space >> litChar
339 many1Char $ notFollowedBy space >> litChar
339 let betweenAngles = try $ char '<' >> manyTillChar litChar (char '>')
340 let betweenAngles = try $ char '<' >> manyTillChar litChar (char '>')
341+ rebase <- option False (True <$ guardEnabled Ext_rebase_relative_paths)
342+ src <- (if rebase then rebasePath pos else id) <$>
340− src <- try betweenAngles <|> sourceURL
343+ (try betweenAngles <|> sourceURL)
341 tit <- option "" referenceTitle
344 tit <- option "" referenceTitle
342 attr <- option nullAttr $ try $
345 attr <- option nullAttr $ try $
343 do guardEnabled Ext_link_attributes
346 do guardEnabled Ext_link_attributes
@@ -346,7 +349,7 @@ referenceKey = try $ do
346 addKvs <- option [] $ guardEnabled Ext_mmd_link_attributes
349 addKvs <- option [] $ guardEnabled Ext_mmd_link_attributes
347 >> many (try $ spnl >> keyValAttr)
350 >> many (try $ spnl >> keyValAttr)
348 blanklines
351 blanklines
349− let attr' = extractIdClass $ foldl (\x f -> f x) attr addKvs
352+ let attr' = extractIdClass $ foldl' (\x f -> f x) attr addKvs
350 target = (escapeURI $ trimr src, tit)
353 target = (escapeURI $ trimr src, tit)
351 st <- getState
354 st <- getState
352 let oldkeys = stateKeys st
355 let oldkeys = stateKeys st
@@ -442,7 +445,7 @@ block :: PandocMonad m => MarkdownParser m (F Blocks)
442block = do
445block = do
443 res <- choice [ mempty <$ blanklines
446 res <- choice [ mempty <$ blanklines
444 , codeBlockFenced
447 , codeBlockFenced
445− , yamlMetaBlock
448+ , yamlMetaBlock'
446 -- note: bulletList needs to be before header because of
449 -- note: bulletList needs to be before header because of
447 -- the possibility of empty list items: -
450 -- the possibility of empty list items: -
448 , bulletList
451 , bulletList
@@ -568,7 +571,7 @@ registerImplicitHeader raw attr@(ident, _, _)
568-- hrule block
571-- hrule block
569--
572--
570
573
571−hrule :: PandocMonad m => ParserT Text st m (F Blocks)
574+hrule :: PandocMonad m => ParserT Sources st m (F Blocks)
572hrule = try $ do
575hrule = try $ do
573 skipSpaces
576 skipSpaces
574 start <- satisfy isHruleChar
577 start <- satisfy isHruleChar
@@ -588,7 +591,7 @@ indentedLine = indentSpaces >> anyLineNewline
588blockDelimiter :: PandocMonad m
591blockDelimiter :: PandocMonad m
589 => (Char -> Bool)
592 => (Char -> Bool)
590 -> Maybe Int
593 -> Maybe Int
591− -> ParserT Text ParserState m Int
594+ -> ParserT Sources ParserState m Int
592blockDelimiter f len = try $ do
595blockDelimiter f len = try $ do
593 skipNonindentSpaces
596 skipNonindentSpaces
594 c <- lookAhead (satisfy f)
597 c <- lookAhead (satisfy f)
@@ -602,7 +605,7 @@ attributes = try $ do
602 spnl
605 spnl
603 attrs <- many (attribute <* spnl)
606 attrs <- many (attribute <* spnl)
604 char '}'
607 char '}'
605− return $ foldl (\x f -> f x) nullAttr attrs
608+ return $ foldl' (\x f -> f x) nullAttr attrs
606
609
607attribute :: PandocMonad m => MarkdownParser m (Attr -> Attr)
610attribute :: PandocMonad m => MarkdownParser m (Attr -> Attr)
608attribute = identifierAttr <|> classAttr <|> keyValAttr <|> specialAttr
611attribute = identifierAttr <|> classAttr <|> keyValAttr <|> specialAttr
@@ -659,15 +662,15 @@ codeBlockFenced :: PandocMonad m => MarkdownParser m (F Blocks)
659codeBlockFenced = try $ do
662codeBlockFenced = try $ do
660 indentchars <- nonindentSpaces
663 indentchars <- nonindentSpaces
661 let indentLevel = T.length indentchars
664 let indentLevel = T.length indentchars
662− c <- try (guardEnabled Ext_fenced_code_blocks >> lookAhead (char '~'))
665+ c <- (guardEnabled Ext_fenced_code_blocks >> lookAhead (char '~'))
663 <|> (guardEnabled Ext_backtick_code_blocks >> lookAhead (char '`'))
666 <|> (guardEnabled Ext_backtick_code_blocks >> lookAhead (char '`'))
664 size <- blockDelimiter (== c) Nothing
667 size <- blockDelimiter (== c) Nothing
665 skipMany spaceChar
668 skipMany spaceChar
666 rawattr <-
669 rawattr <-
667− (Left <$> try (guardEnabled Ext_raw_attribute >> rawAttribute))
670+ (Left <$> (guardEnabled Ext_raw_attribute >> try rawAttribute))
668 <|>
671 <|>
669 (Right <$> option ("",[],[])
672 (Right <$> option ("",[],[])
670− (try (guardEnabled Ext_fenced_code_attributes >> attributes)
673+ ((guardEnabled Ext_fenced_code_attributes >> try attributes)
671 <|> ((\x -> ("",[toLanguageId x],[])) <$> many1Char nonspaceChar)))
674 <|> ((\x -> ("",[toLanguageId x],[])) <$> many1Char nonspaceChar)))
672 blankline
675 blankline
673 contents <- T.intercalate "\n" <$>
676 contents <- T.intercalate "\n" <$>
@@ -732,7 +735,7 @@ lhsCodeBlockBirdWith c = try $ do
732 blanklines
735 blanklines
733 return $ T.intercalate "\n" lns'
736 return $ T.intercalate "\n" lns'
734
737
735−birdTrackLine :: PandocMonad m => Char -> ParserT Text st m Text
738+birdTrackLine :: PandocMonad m => Char -> ParserT Sources st m Text
736birdTrackLine c = try $ do
739birdTrackLine c = try $ do
737 char c
740 char c
738 -- allow html tags on left margin:
741 -- allow html tags on left margin:
@@ -1025,7 +1028,7 @@ para = try $ do
1025 option (B.plain <$> result)
1028 option (B.plain <$> result)
1026 $ try $ do
1029 $ try $ do
1027 newline
1030 newline
1028− (blanklines >> return mempty)
1031+ (mempty <$ blanklines)
1029 <|> (guardDisabled Ext_blank_before_blockquote >> () <$ lookAhead blockQuote)
1032 <|> (guardDisabled Ext_blank_before_blockquote >> () <$ lookAhead blockQuote)
1030 <|> (guardEnabled Ext_backtick_code_blocks >> () <$ lookAhead codeBlockFenced)
1033 <|> (guardEnabled Ext_backtick_code_blocks >> () <$ lookAhead codeBlockFenced)
1031 <|> (guardDisabled Ext_blank_before_header >> () <$ lookAhead header)
1034 <|> (guardDisabled Ext_blank_before_header >> () <$ lookAhead header)
@@ -1155,11 +1158,12 @@ stripMarkdownAttribute s = renderTags' $ map filterAttrib $ parseTags s
1155--
1158--
1156
1159
1157lineBlock :: PandocMonad m => MarkdownParser m (F Blocks)
1160lineBlock :: PandocMonad m => MarkdownParser m (F Blocks)
1158−lineBlock = try $ do
1161+lineBlock = do
1159 guardEnabled Ext_line_blocks
1162 guardEnabled Ext_line_blocks
1163+ try $ do
1160− lines' <- lineBlockLines >>=
1164+ lines' <- lineBlockLines >>=
1161− mapM (parseFromString' (trimInlinesF <$> inlines))
1165+ mapM (parseFromString' (trimInlinesF <$> inlines))
1162− return $ B.lineBlock <$> sequence lines'
1166+ return $ B.lineBlock <$> sequence lines'
1163
1167
1164--
1168--
1165-- Tables
1169-- Tables
@@ -1169,7 +1173,7 @@ lineBlock = try $ do
1169-- and the length including trailing space.
1173-- and the length including trailing space.
1170dashedLine :: PandocMonad m
1174dashedLine :: PandocMonad m
1171 => Char
1175 => Char
1172− -> ParserT Text st m (Int, Int)
1176+ -> ParserT Sources st m (Int, Int)
1173dashedLine ch = do
1177dashedLine ch = do
1174 dashes <- many1 (char ch)
1178 dashes <- many1 (char ch)
1175 sp <- many spaceChar
1179 sp <- many spaceChar
@@ -1238,7 +1242,7 @@ rawTableLine :: PandocMonad m
1238 -> MarkdownParser m [Text]
1242 -> MarkdownParser m [Text]
1239rawTableLine indices = do
1243rawTableLine indices = do
1240 notFollowedBy' (blanklines' <|> tableFooter)
1244 notFollowedBy' (blanklines' <|> tableFooter)
1241− line <- take1WhileP (/='\n') <* newline
1245+ line <- anyLine
1242 return $ map trim $ tail $
1246 return $ map trim $ tail $
1243 splitTextByIndices (init indices) line
1247 splitTextByIndices (init indices) line
1244
1248
@@ -1261,11 +1265,12 @@ multilineRow indices = do
1261-- Parses a table caption: inlines beginning with 'Table:'
1265-- Parses a table caption: inlines beginning with 'Table:'
1262-- and followed by blank lines.
1266-- and followed by blank lines.
1263tableCaption :: PandocMonad m => MarkdownParser m (F Inlines)
1267tableCaption :: PandocMonad m => MarkdownParser m (F Inlines)
1264−tableCaption = try $ do
1268+tableCaption = do
1265 guardEnabled Ext_table_captions
1269 guardEnabled Ext_table_captions
1270+ try $ do
1266− skipNonindentSpaces
1271+ skipNonindentSpaces
1267− (string ":" <* notFollowedBy (satisfy isPunctuation)) <|> string "Table:"
1272+ (string ":" <* notFollowedBy (satisfy isPunctuation)) <|> string "Table:"
1268− trimInlinesF <$> inlines1 <* blanklines
1273+ trimInlinesF <$> inlines1 <* blanklines
1269
1274
1270-- Parse a simple table with '---' header and one line per row.
1275-- Parse a simple table with '---' header and one line per row.
1271simpleTable :: PandocMonad m
1276simpleTable :: PandocMonad m
@@ -1351,7 +1356,7 @@ pipeTable = try $ do
1351 lines' <- many pipeTableRow
1356 lines' <- many pipeTableRow
1352 let lines'' = map (take (length aligns) <$>) lines'
1357 let lines'' = map (take (length aligns) <$>) lines'
1353 let maxlength = maximum $
1358 let maxlength = maximum $
1354− map (\x -> T.length . stringify $ runF x def) (heads' : lines'')
1359+ fmap (\x -> T.length . stringify $ runF x def) (heads' :| lines'')
1355 numColumns <- getOption readerColumns
1360 numColumns <- getOption readerColumns
1356 let widths = if maxlength > numColumns
1361 let widths = if maxlength > numColumns
1357 then map (\len ->
1362 then map (\len ->
@@ -1388,7 +1393,7 @@ pipeTableCell =
1388 return $ B.plain <$> result)
1393 return $ B.plain <$> result)
1389 <|> return mempty
1394 <|> return mempty
1390
1395
1391−pipeTableHeaderPart :: PandocMonad m => ParserT Text st m (Alignment, Int)
1396+pipeTableHeaderPart :: PandocMonad m => ParserT Sources st m (Alignment, Int)
1392pipeTableHeaderPart = try $ do
1397pipeTableHeaderPart = try $ do
1393 skipMany spaceChar
1398 skipMany spaceChar
1394 left <- optionMaybe (char ':')
1399 left <- optionMaybe (char ':')
@@ -1404,10 +1409,14 @@ pipeTableHeaderPart = try $ do
1404 (Just _,Just _) -> AlignCenter, len)
1409 (Just _,Just _) -> AlignCenter, len)
1405
1410
1406-- Succeed only if current line contains a pipe.
1411-- Succeed only if current line contains a pipe.
1407−scanForPipe :: PandocMonad m => ParserT Text st m ()
1412+scanForPipe :: PandocMonad m => ParserT Sources st m ()
1408scanForPipe = do
1413scanForPipe = do
1409− inp <- getInput
1414+ Sources inps <- getInput
1415+ let ln = case inps of
1416+ [] -> ""
1417+ ((_,t):(_,t'):_) | T.null t -> t'
1418+ ((_,t):_) -> t
1410− case T.break (\c -> c == '\n' || c == '|') inp of
1419+ case T.break (\c -> c == '\n' || c == '|') ln of
1411 (_, T.uncons -> Just ('|', _)) -> return ()
1420 (_, T.uncons -> Just ('|', _)) -> return ()
1412 _ -> mzero
1421 _ -> mzero
1413
1422
@@ -1434,15 +1443,14 @@ table :: PandocMonad m => MarkdownParser m (F Blocks)
1434table = try $ do
1443table = try $ do
1435 frontCaption <- option Nothing (Just <$> tableCaption)
1444 frontCaption <- option Nothing (Just <$> tableCaption)
1436 (aligns, widths, heads, lns) <-
1445 (aligns, widths, heads, lns) <-
1437− try (guardEnabled Ext_pipe_tables >> scanForPipe >> pipeTable) <|>
1446+ (guardEnabled Ext_pipe_tables >> try (scanForPipe >> pipeTable)) <|>
1438− try (guardEnabled Ext_multiline_tables >>
1447+ (guardEnabled Ext_multiline_tables >> try (multilineTable False)) <|>
1439− multilineTable False) <|>
1440− try (guardEnabled Ext_simple_tables >>
1448+ (guardEnabled Ext_simple_tables >>
1441− (simpleTable True <|> simpleTable False)) <|>
1449+ try (simpleTable True <|> simpleTable False)) <|>
1442− try (guardEnabled Ext_multiline_tables >>
1450+ (guardEnabled Ext_multiline_tables >>
1443− multilineTable True) <|>
1451+ try (multilineTable True)) <|>
1444− try (guardEnabled Ext_grid_tables >>
1452+ (guardEnabled Ext_grid_tables >>
1445− (gridTable False <|> gridTable True)) <?> "table"
1453+ try (gridTable False <|> gridTable True)) <?> "table"
1446 optional blanklines
1454 optional blanklines
1447 caption <- case frontCaption of
1455 caption <- case frontCaption of
1448 Nothing -> option (return mempty) tableCaption
1456 Nothing -> option (return mempty) tableCaption
@@ -1476,35 +1484,37 @@ inlines1 :: PandocMonad m => MarkdownParser m (F Inlines)
1476inlines1 = mconcat <$> many1 inline
1484inlines1 = mconcat <$> many1 inline
1477
1485
1478inline :: PandocMonad m => MarkdownParser m (F Inlines)
1486inline :: PandocMonad m => MarkdownParser m (F Inlines)
1479−inline = choice [ whitespace
1487+inline = do
1480− , bareURL
1488+ c <- lookAhead anyChar
1481− , str
1489+ ((case c of
1490+ ' ' -> whitespace
1491+ '\t' -> whitespace
1482− , endline
1492+ '\n' -> endline
1483− , code
1493+ '`' -> code
1484− , strongOrEmph
1494+ '_' -> strongOrEmph
1485− , note
1495+ '*' -> strongOrEmph
1486− , cite
1496+ '^' -> superscript <|> inlineNote -- in this order bc ^[link](/foo)^
1487− , bracketedSpan
1497+ '[' -> note <|> cite <|> bracketedSpan <|> link
1488− , link
1489− , image
1498+ '!' -> image
1490− , math
1499+ '$' -> math
1491− , strikeout
1500+ '~' -> strikeout <|> subscript
1492− , subscript
1493− , superscript
1494− , inlineNote -- after superscript because of ^[link](/foo)^
1495− , autoLink
1496− , spanHtml
1497− , rawHtmlInline
1501+ '<' -> autoLink <|> spanHtml <|> rawHtmlInline <|> ltSign
1498− , escapedNewline
1502+ '\\' -> math <|> escapedNewline <|> escapedChar <|> rawLaTeXInline'
1499− , escapedChar
1500− , rawLaTeXInline'
1501− , exampleRef
1503+ '@' -> cite <|> exampleRef
1504+ '"' -> smart
1505+ '\'' -> smart
1506+ '\8216' -> smart
1507+ '\145' -> smart
1508+ '\8220' -> smart
1509+ '\147' -> smart
1502− , smart
1510+ '-' -> smart
1511+ '.' -> smart
1503− , return . B.singleton <$> charRef
1512+ '&' -> return . B.singleton <$> charRef
1504− , emoji
1513+ ':' -> emoji
1514+ _ -> mzero)
1515+ <|> bareURL
1516+ <|> str
1505− , symbol
1517+ <|> symbol) <?> "inline"
1506− , ltSign
1507− ] <?> "inline"
1508
1518
1509escapedChar' :: PandocMonad m => MarkdownParser m Char
1519escapedChar' :: PandocMonad m => MarkdownParser m Char
1510escapedChar' = try $ do
1520escapedChar' = try $ do
@@ -1515,11 +1525,12 @@ escapedChar' = try $ do
1515 <|> oneOf "\\`*_{}[]()>#+-.!~\""
1525 <|> oneOf "\\`*_{}[]()>#+-.!~\""
1516
1526
1517escapedNewline :: PandocMonad m => MarkdownParser m (F Inlines)
1527escapedNewline :: PandocMonad m => MarkdownParser m (F Inlines)
1518−escapedNewline = try $ do
1528+escapedNewline = do
1519 guardEnabled Ext_escaped_line_breaks
1529 guardEnabled Ext_escaped_line_breaks
1530+ try $ do
1520− char '\\'
1531+ char '\\'
1521− lookAhead (char '\n') -- don't consume the newline (see #3730)
1532+ lookAhead (char '\n') -- don't consume the newline (see #3730)
1522− return $ return B.linebreak
1533+ return $ return B.linebreak
1523
1534
1524escapedChar :: PandocMonad m => MarkdownParser m (F Inlines)
1535escapedChar :: PandocMonad m => MarkdownParser m (F Inlines)
1525escapedChar = do
1536escapedChar = do
@@ -1541,19 +1552,20 @@ ltSign = do
1541-- whole document has been parsed. But we need this parser
1552-- whole document has been parsed. But we need this parser
1542-- here in case citations is disabled.
1553-- here in case citations is disabled.
1543exampleRef :: PandocMonad m => MarkdownParser m (F Inlines)
1554exampleRef :: PandocMonad m => MarkdownParser m (F Inlines)
1544−exampleRef = try $ do
1555+exampleRef = do
1545 guardEnabled Ext_example_lists
1556 guardEnabled Ext_example_lists
1557+ try $ do
1546− char '@'
1558+ char '@'
1547− lab <- mconcat . map T.pack <$>
1559+ lab <- mconcat . map T.pack <$>
1548− many (many1 alphaNum <|>
1560+ many (many1 alphaNum <|>
1549− try (do c <- char '_' <|> char '-'
1561+ try (do c <- char '_' <|> char '-'
1550− cs <- many1 alphaNum
1562+ cs <- many1 alphaNum
1551− return (c:cs)))
1563+ return (c:cs)))
1552− return $ do
1564+ return $ do
1553− st <- askF
1565+ st <- askF
1554− return $ case M.lookup lab (stateExamples st) of
1566+ return $ case M.lookup lab (stateExamples st) of
1555− Just n -> B.str $ tshow n
1567+ Just n -> B.str $ tshow n
1556− Nothing -> B.str $ "@" <> lab
1568+ Nothing -> B.str $ "@" <> lab
1557
1569
1558symbol :: PandocMonad m => MarkdownParser m (F Inlines)
1570symbol :: PandocMonad m => MarkdownParser m (F Inlines)
1559symbol = do
1571symbol = do
@@ -1580,10 +1592,10 @@ code = try $ do
1580 >> count (length starts) (char '`')
1592 >> count (length starts) (char '`')
1581 >> notFollowedBy (char '`'))
1593 >> notFollowedBy (char '`'))
1582 rawattr <-
1594 rawattr <-
1583− (Left <$> try (guardEnabled Ext_raw_attribute >> rawAttribute))
1595+ (Left <$> (guardEnabled Ext_raw_attribute >> try rawAttribute))
1584 <|>
1596 <|>
1585 (Right <$> option ("",[],[])
1597 (Right <$> option ("",[],[])
1586− (try (guardEnabled Ext_inline_code_attributes >> attributes)))
1598+ (guardEnabled Ext_inline_code_attributes >> try attributes))
1587 return $ return $
1599 return $ return $
1588 case rawattr of
1600 case rawattr of
1589 Left syn -> B.rawInline syn result
1601 Left syn -> B.rawInline syn result
@@ -1676,38 +1688,40 @@ strikeout = fmap B.strikeout <$>
1676 strikeEnd = try $ string "~~"
1688 strikeEnd = try $ string "~~"
1677
1689
1678superscript :: PandocMonad m => MarkdownParser m (F Inlines)
1690superscript :: PandocMonad m => MarkdownParser m (F Inlines)
1679−superscript = fmap B.superscript <$> try (do
1691+superscript = do
1680 guardEnabled Ext_superscript
1692 guardEnabled Ext_superscript
1693+ fmap B.superscript <$> try (do
1681− char '^'
1694+ char '^'
1682− mconcat <$> many1Till (do notFollowedBy spaceChar
1695+ mconcat <$> many1Till (do notFollowedBy spaceChar
1683− notFollowedBy newline
1696+ notFollowedBy newline
1684− inline) (char '^'))
1697+ inline) (char '^'))
1685
1698
1686subscript :: PandocMonad m => MarkdownParser m (F Inlines)
1699subscript :: PandocMonad m => MarkdownParser m (F Inlines)
1687−subscript = fmap B.subscript <$> try (do
1700+subscript = do
1688 guardEnabled Ext_subscript
1701 guardEnabled Ext_subscript
1702+ fmap B.subscript <$> try (do
1689− char '~'
1703+ char '~'
1690− mconcat <$> many1Till (do notFollowedBy spaceChar
1704+ mconcat <$> many1Till (do notFollowedBy spaceChar
1691− notFollowedBy newline
1705+ notFollowedBy newline
1692− inline) (char '~'))
1706+ inline) (char '~'))
1693
1707
1694whitespace :: PandocMonad m => MarkdownParser m (F Inlines)
1708whitespace :: PandocMonad m => MarkdownParser m (F Inlines)
1695whitespace = spaceChar >> return <$> (lb <|> regsp) <?> "whitespace"
1709whitespace = spaceChar >> return <$> (lb <|> regsp) <?> "whitespace"
1696 where lb = spaceChar >> skipMany spaceChar >> option B.space (endline >> return B.linebreak)
1710 where lb = spaceChar >> skipMany spaceChar >> option B.space (endline >> return B.linebreak)
1697 regsp = skipMany spaceChar >> return B.space
1711 regsp = skipMany spaceChar >> return B.space
1698
1712
1699−nonEndline :: PandocMonad m => ParserT Text st m Char
1713+nonEndline :: PandocMonad m => ParserT Sources st m Char
1700nonEndline = satisfy (/='\n')
1714nonEndline = satisfy (/='\n')
1701
1715
1702str :: PandocMonad m => MarkdownParser m (F Inlines)
1716str :: PandocMonad m => MarkdownParser m (F Inlines)
1703str = do
1717str = do
1704 result <- mconcat <$> many1
1718 result <- mconcat <$> many1
1705− ( take1WhileP isAlphaNum
1719+ ( T.pack <$> (many1 alphaNum)
1706 <|> "." <$ try (char '.' <* notFollowedBy (char '.')) )
1720 <|> "." <$ try (char '.' <* notFollowedBy (char '.')) )
1707 updateLastStrPos
1721 updateLastStrPos
1708 (do guardEnabled Ext_smart
1722 (do guardEnabled Ext_smart
1709 abbrevs <- getOption readerAbbreviations
1723 abbrevs <- getOption readerAbbreviations
1710− if not (T.null result) && T.last result == '.' && result `Set.member` abbrevs
1724+ if result `Set.member` abbrevs
1711 then try (do ils <- whitespace
1725 then try (do ils <- whitespace
1712 notFollowedBy (() <$ cite <|> () <$ note)
1726 notFollowedBy (() <$ cite <|> () <$ note)
1713 -- ?? lookAhead alphaNum
1727 -- ?? lookAhead alphaNum
@@ -1790,15 +1804,16 @@ link = try $ do
1790 regLink B.linkWith lab <|> referenceLink B.linkWith (lab,raw)
1804 regLink B.linkWith lab <|> referenceLink B.linkWith (lab,raw)
1791
1805
1792bracketedSpan :: PandocMonad m => MarkdownParser m (F Inlines)
1806bracketedSpan :: PandocMonad m => MarkdownParser m (F Inlines)
1793−bracketedSpan = try $ do
1807+bracketedSpan = do
1794 guardEnabled Ext_bracketed_spans
1808 guardEnabled Ext_bracketed_spans
1809+ try $ do
1795− (lab,_) <- reference
1810+ (lab,_) <- reference
1796− attr <- attributes
1811+ attr <- attributes
1797− return $ if isSmallCaps attr
1812+ return $ if isSmallCaps attr
1798− then B.smallcaps <$> lab
1813+ then B.smallcaps <$> lab
1799− else if isUnderline attr
1814+ else if isUnderline attr
1800− then B.underline <$> lab
1815+ then B.underline <$> lab
1801− else B.spanWith attr <$> lab
1816+ else B.spanWith attr <$> lab
1802
1817
1803-- | We treat a span as SmallCaps if class is "smallcaps" (and
1818-- | We treat a span as SmallCaps if class is "smallcaps" (and
1804-- no other attributes are set or if style is "font-variant:small-caps"
1819-- no other attributes are set or if style is "font-variant:small-caps"
@@ -1825,9 +1840,12 @@ regLink :: PandocMonad m
1825 -> MarkdownParser m (F Inlines)
1840 -> MarkdownParser m (F Inlines)
1826regLink constructor lab = try $ do
1841regLink constructor lab = try $ do
1827 (src, tit) <- source
1842 (src, tit) <- source
1843+ rebase <- option False (True <$ guardEnabled Ext_rebase_relative_paths)
1844+ pos <- getPosition
1845+ let src' = if rebase then rebasePath pos src else src
1828 attr <- option nullAttr $
1846 attr <- option nullAttr $
1829 guardEnabled Ext_link_attributes >> attributes
1847 guardEnabled Ext_link_attributes >> attributes
1830− return $ constructor attr src tit <$> lab
1848+ return $ constructor attr src' tit <$> lab
1831
1849
1832-- a link like [this][ref] or [this][] or [this]
1850-- a link like [this][ref] or [this][] or [this]
1833referenceLink :: PandocMonad m
1851referenceLink :: PandocMonad m
@@ -1867,7 +1885,8 @@ referenceLink constructor (lab, raw) = do
1867 Just ((src, tit), _) -> constructor nullAttr src tit <$> lab
1885 Just ((src, tit), _) -> constructor nullAttr src tit <$> lab
1868 Nothing -> makeFallback
1886 Nothing -> makeFallback
1869 else makeFallback
1887 else makeFallback
1888+ Just ((src,tit), attr) ->
1870− Just ((src,tit), attr) -> constructor attr src tit <$> lab
1889+ constructor attr src tit <$> lab
1871
1890
1872dropBrackets :: Text -> Text
1891dropBrackets :: Text -> Text
1873dropBrackets = dropRB . dropLB
1892dropBrackets = dropRB . dropLB
@@ -1877,12 +1896,13 @@ dropBrackets = dropRB . dropLB
1877 dropLB xs = xs
1896 dropLB xs = xs
1878
1897
1879bareURL :: PandocMonad m => MarkdownParser m (F Inlines)
1898bareURL :: PandocMonad m => MarkdownParser m (F Inlines)
1880−bareURL = try $ do
1899+bareURL = do
1881 guardEnabled Ext_autolink_bare_uris
1900 guardEnabled Ext_autolink_bare_uris
1882 getState >>= guard . stateAllowLinks
1901 getState >>= guard . stateAllowLinks
1902+ try $ do
1883− (cls, (orig, src)) <- (("uri",) <$> uri) <|> (("email",) <$> emailAddress)
1903+ (cls, (orig, src)) <- (("uri",) <$> uri) <|> (("email",) <$> emailAddress)
1884− notFollowedBy $ try $ spaces >> htmlTag (~== TagClose ("a" :: Text))
1904+ notFollowedBy $ try $ spaces >> htmlTag (~== TagClose ("a" :: Text))
1885− return $ return $ B.linkWith ("",[cls],[]) src "" (B.str orig)
1905+ return $ return $ B.linkWith ("",[cls],[]) src "" (B.str orig)
1886
1906
1887autoLink :: PandocMonad m => MarkdownParser m (F Inlines)
1907autoLink :: PandocMonad m => MarkdownParser m (F Inlines)
1888autoLink = try $ do
1908autoLink = try $ do
@@ -1899,15 +1919,33 @@ autoLink = try $ do
1899 return $ return $ B.linkWith attr (src <> escapeURI extra) ""
1919 return $ return $ B.linkWith attr (src <> escapeURI extra) ""
1900 (B.str $ orig <> extra)
1920 (B.str $ orig <> extra)
1901
1921
1922+-- | Rebase a relative path, by adding the (relative) directory
1923+-- of the containing source position. Absolute links and URLs
1924+-- are untouched.
1925+rebasePath :: SourcePos -> Text -> Text
1926+rebasePath pos path = do
1927+ let fp = sourceName pos
1928+ isFragment = T.take 1 path == "#"
1929+ path' = T.unpack path
1930+ isAbsolutePath = Posix.isAbsolute path' || Windows.isAbsolute path'
1931+ in if T.null path || isFragment || isAbsolutePath || isURI path
1932+ then path
1933+ else
1934+ case takeDirectory fp of
1935+ "" -> path
1936+ "." -> path
1937+ d -> T.pack d <> "/" <> path
1938+
1902image :: PandocMonad m => MarkdownParser m (F Inlines)
1939image :: PandocMonad m => MarkdownParser m (F Inlines)
1903image = try $ do
1940image = try $ do
1904 char '!'
1941 char '!'
1905 (lab,raw) <- reference
1942 (lab,raw) <- reference
1906 defaultExt <- getOption readerDefaultImageExtension
1943 defaultExt <- getOption readerDefaultImageExtension
1944+ let constructor attr' src =
1907− let constructor attr' src = case takeExtension (T.unpack src) of
1945+ case takeExtension (T.unpack src) of
1908− "" -> B.imageWith attr' (T.pack $ addExtension (T.unpack src)
1946+ "" -> B.imageWith attr' (T.pack $ addExtension (T.unpack src)
1909− $ T.unpack defaultExt)
1947+ $ T.unpack defaultExt)
1910− _ -> B.imageWith attr' src
1948+ _ -> B.imageWith attr' src
1911 regLink constructor lab <|> referenceLink constructor (lab,raw)
1949 regLink constructor lab <|> referenceLink constructor (lab,raw)
1912
1950
1913note :: PandocMonad m => MarkdownParser m (F Inlines)
1951note :: PandocMonad m => MarkdownParser m (F Inlines)
@@ -1935,23 +1973,25 @@ note = try $ do
1935 return $ B.note $ walk adjustCite contents'
1973 return $ B.note $ walk adjustCite contents'
1936
1974
1937inlineNote :: PandocMonad m => MarkdownParser m (F Inlines)
1975inlineNote :: PandocMonad m => MarkdownParser m (F Inlines)
1938−inlineNote = try $ do
1976+inlineNote = do
1939 guardEnabled Ext_inline_notes
1977 guardEnabled Ext_inline_notes
1978+ try $ do
1940− char '^'
1979+ char '^'
1941− updateState $ \st -> st{ stateInNote = True
1980+ updateState $ \st -> st{ stateInNote = True
1942− , stateNoteNumber = stateNoteNumber st + 1 }
1981+ , stateNoteNumber = stateNoteNumber st + 1 }
1943− contents <- inlinesInBalancedBrackets
1982+ contents <- inlinesInBalancedBrackets
1944− updateState $ \st -> st{ stateInNote = False }
1983+ updateState $ \st -> st{ stateInNote = False }
1945− return $ B.note . B.para <$> contents
1984+ return $ B.note . B.para <$> contents
1946
1985
1947rawLaTeXInline' :: PandocMonad m => MarkdownParser m (F Inlines)
1986rawLaTeXInline' :: PandocMonad m => MarkdownParser m (F Inlines)
1948−rawLaTeXInline' = try $ do
1987+rawLaTeXInline' = do
1949 guardEnabled Ext_raw_tex
1988 guardEnabled Ext_raw_tex
1950 notFollowedBy' rawConTeXtEnvironment
1989 notFollowedBy' rawConTeXtEnvironment
1990+ try $ do
1951− s <- rawLaTeXInline
1991+ s <- rawLaTeXInline
1952− return $ return $ B.rawInline "tex" s -- "tex" because it might be context
1992+ return $ return $ B.rawInline "tex" s -- "tex" because it might be context
1953
1993
1954−rawConTeXtEnvironment :: PandocMonad m => ParserT Text st m Text
1994+rawConTeXtEnvironment :: PandocMonad m => ParserT Sources st m Text
1955rawConTeXtEnvironment = try $ do
1995rawConTeXtEnvironment = try $ do
1956 string "\\start"
1996 string "\\start"
1957 completion <- inBrackets (letter <|> digit <|> spaceChar)
1997 completion <- inBrackets (letter <|> digit <|> spaceChar)
@@ -1960,7 +2000,7 @@ rawConTeXtEnvironment = try $ do
1960 (try $ string "\\stop" >> textStr completion)
2000 (try $ string "\\stop" >> textStr completion)
1961 return $ "\\start" <> completion <> T.concat contents <> "\\stop" <> completion
2001 return $ "\\start" <> completion <> T.concat contents <> "\\stop" <> completion
1962
2002
1963−inBrackets :: PandocMonad m => ParserT Text st m Char -> ParserT Text st m Text
2003+inBrackets :: PandocMonad m => ParserT Sources st m Char -> ParserT Sources st m Text
1964inBrackets parser = do
2004inBrackets parser = do
1965 char '['
2005 char '['
1966 contents <- manyChar parser
2006 contents <- manyChar parser
@@ -1968,55 +2008,60 @@ inBrackets parser = do
1968 return $ "[" <> contents <> "]"
2008 return $ "[" <> contents <> "]"
1969
2009
1970spanHtml :: PandocMonad m => MarkdownParser m (F Inlines)
2010spanHtml :: PandocMonad m => MarkdownParser m (F Inlines)
1971−spanHtml = try $ do
2011+spanHtml = do
1972 guardEnabled Ext_native_spans
2012 guardEnabled Ext_native_spans
2013+ try $ do
1973− (TagOpen _ attrs, _) <- htmlTag (~== TagOpen ("span" :: Text) [])
2014+ (TagOpen _ attrs, _) <- htmlTag (~== TagOpen ("span" :: Text) [])
1974− contents <- mconcat <$> manyTill inline (htmlTag (~== TagClose ("span" :: Text)))
2015+ contents <- mconcat <$> manyTill inline (htmlTag (~== TagClose ("span" :: Text)))
1975− let ident = fromMaybe "" $ lookup "id" attrs
2016+ let ident = fromMaybe "" $ lookup "id" attrs
1976− let classes = maybe [] T.words $ lookup "class" attrs
2017+ let classes = maybe [] T.words $ lookup "class" attrs
1977− let keyvals = [(k,v) | (k,v) <- attrs, k /= "id" && k /= "class"]
2018+ let keyvals = [(k,v) | (k,v) <- attrs, k /= "id" && k /= "class"]
1978− return $ if isSmallCaps (ident, classes, keyvals)
2019+ return $ if isSmallCaps (ident, classes, keyvals)
1979− then B.smallcaps <$> contents
2020+ then B.smallcaps <$> contents
1980− else if isUnderline (ident, classes, keyvals)
2021+ else if isUnderline (ident, classes, keyvals)
1981− then B.underline <$> contents
2022+ then B.underline <$> contents
1982− else B.spanWith (ident, classes, keyvals) <$> contents
2023+ else B.spanWith (ident, classes, keyvals) <$> contents
1983
2024
1984divHtml :: PandocMonad m => MarkdownParser m (F Blocks)
2025divHtml :: PandocMonad m => MarkdownParser m (F Blocks)
1985−divHtml = try $ do
2026+divHtml = do
1986 guardEnabled Ext_native_divs
2027 guardEnabled Ext_native_divs
2028+ try $ do
1987− (TagOpen _ attrs, rawtag) <- htmlTag (~== TagOpen ("div" :: Text) [])
2029+ (TagOpen _ attrs, rawtag) <- htmlTag (~== TagOpen ("div" :: Text) [])
1988− -- we set stateInHtmlBlock so that closing tags that can be either block or
2030+ -- we set stateInHtmlBlock so that closing tags that can be either block
1989− -- inline will not be parsed as inline tags
2031+ -- or inline will not be parsed as inline tags
1990− oldInHtmlBlock <- stateInHtmlBlock <$> getState
2032+ oldInHtmlBlock <- stateInHtmlBlock <$> getState
1991− updateState $ \st -> st{ stateInHtmlBlock = Just "div" }
2033+ updateState $ \st -> st{ stateInHtmlBlock = Just "div" }
1992− bls <- option "" (blankline >> option "" blanklines)
2034+ bls <- option "" (blankline >> option "" blanklines)
1993− contents <- mconcat <$>
2035+ contents <- mconcat <$>
1994− many (notFollowedBy' (htmlTag (~== TagClose ("div" :: Text))) >> block)
2036+ many (notFollowedBy' (htmlTag (~== TagClose ("div" :: Text))) >> block)
1995− closed <- option False (True <$ htmlTag (~== TagClose ("div" :: Text)))
2037+ closed <- option False (True <$ htmlTag (~== TagClose ("div" :: Text)))
1996− if closed
2038+ if closed
1997− then do
2039+ then do
1998− updateState $ \st -> st{ stateInHtmlBlock = oldInHtmlBlock }
2040+ updateState $ \st -> st{ stateInHtmlBlock = oldInHtmlBlock }
1999− let ident = fromMaybe "" $ lookup "id" attrs
2041+ let ident = fromMaybe "" $ lookup "id" attrs
2000− let classes = maybe [] T.words $ lookup "class" attrs
2042+ let classes = maybe [] T.words $ lookup "class" attrs
2001− let keyvals = [(k,v) | (k,v) <- attrs, k /= "id" && k /= "class"]
2043+ let keyvals = [(k,v) | (k,v) <- attrs, k /= "id" && k /= "class"]
2002− return $ B.divWith (ident, classes, keyvals) <$> contents
2044+ return $ B.divWith (ident, classes, keyvals) <$> contents
2003− else -- avoid backtracing
2045+ else -- avoid backtracing
2004− return $ return (B.rawBlock "html" (rawtag <> bls)) <> contents
2046+ return $ return (B.rawBlock "html" (rawtag <> bls)) <> contents
2005
2047
2006divFenced :: PandocMonad m => MarkdownParser m (F Blocks)
2048divFenced :: PandocMonad m => MarkdownParser m (F Blocks)
2007−divFenced = try $ do
2049+divFenced = do
2008 guardEnabled Ext_fenced_divs
2050 guardEnabled Ext_fenced_divs
2051+ try $ do
2009− string ":::"
2052+ string ":::"
2010− skipMany (char ':')
2053+ skipMany (char ':')
2011− skipMany spaceChar
2054+ skipMany spaceChar
2012− attribs <- attributes <|> ((\x -> ("",[x],[])) <$> many1Char nonspaceChar)
2055+ attribs <- attributes <|> ((\x -> ("",[x],[])) <$> many1Char nonspaceChar)
2013− skipMany spaceChar
2056+ skipMany spaceChar
2014− skipMany (char ':')
2057+ skipMany (char ':')
2015− blankline
2058+ blankline
2059+ updateState $ \st ->
2016− updateState $ \st -> st{ stateFencedDivLevel = stateFencedDivLevel st + 1 }
2060+ st{ stateFencedDivLevel = stateFencedDivLevel st + 1 }
2017− bs <- mconcat <$> manyTill block divFenceEnd
2061+ bs <- mconcat <$> manyTill block divFenceEnd
2062+ updateState $ \st ->
2018− updateState $ \st -> st{ stateFencedDivLevel = stateFencedDivLevel st - 1 }
2063+ st{ stateFencedDivLevel = stateFencedDivLevel st - 1 }
2019− return $ B.divWith attribs <$> bs
2064+ return $ B.divWith attribs <$> bs
2020
2065
2021divFenceEnd :: PandocMonad m => MarkdownParser m ()
2066divFenceEnd :: PandocMonad m => MarkdownParser m ()
2022divFenceEnd = try $ do
2067divFenceEnd = try $ do
@@ -2048,14 +2093,15 @@ emojiChars :: [Char]
2048emojiChars = ['a'..'z'] ++ ['0'..'9'] ++ ['_','+','-']
2093emojiChars = ['a'..'z'] ++ ['0'..'9'] ++ ['_','+','-']
2049
2094
2050emoji :: PandocMonad m => MarkdownParser m (F Inlines)
2095emoji :: PandocMonad m => MarkdownParser m (F Inlines)
2051−emoji = try $ do
2096+emoji = do
2052 guardEnabled Ext_emoji
2097 guardEnabled Ext_emoji
2098+ try $ do
2053− char ':'
2099+ char ':'
2054− emojikey <- many1Char (oneOf emojiChars)
2100+ emojikey <- many1Char (oneOf emojiChars)
2055− char ':'
2101+ char ':'
2056− case emojiToInline emojikey of
2102+ case emojiToInline emojikey of
2057− Just i -> return (return $ B.singleton i)
2103+ Just i -> return (return $ B.singleton i)
2058− Nothing -> mzero
2104+ Nothing -> mzero
2059
2105
2060-- Citations
2106-- Citations
2061
2107
@@ -2074,7 +2120,7 @@ cite = do
2074
2120
2075textualCite :: PandocMonad m => MarkdownParser m (F Inlines)
2121textualCite :: PandocMonad m => MarkdownParser m (F Inlines)
2076textualCite = try $ do
2122textualCite = try $ do
2077− (suppressAuthor, key) <- citeKey
2123+ (suppressAuthor, key) <- citeKey True
2078 -- If this is a reference to an earlier example list item,
2124 -- If this is a reference to an earlier example list item,
2079 -- then don't parse it as a citation. If the example list
2125 -- then don't parse it as a citation. If the example list
2080 -- item comes later, we'll parse it here and figure out in
2126 -- item comes later, we'll parse it here and figure out in
@@ -2154,7 +2200,7 @@ prefix = trimInlinesF . mconcat <$>
2154 manyTill inline (char ']'
2200 manyTill inline (char ']'
2155 <|> lookAhead
2201 <|> lookAhead
2156 (try $ do optional (try (char ';' >> spnl))
2202 (try $ do optional (try (char ';' >> spnl))
2157− citeKey
2203+ citeKey True
2158 return ']'))
2204 return ']'))
2159
2205
2160citeList :: PandocMonad m => MarkdownParser m (F [Citation])
2206citeList :: PandocMonad m => MarkdownParser m (F [Citation])
@@ -2163,7 +2209,7 @@ citeList = fmap sequence $ sepBy1 citation (try $ char ';' >> spnl)
2163citation :: PandocMonad m => MarkdownParser m (F Citation)
2209citation :: PandocMonad m => MarkdownParser m (F Citation)
2164citation = try $ do
2210citation = try $ do
2165 pref <- prefix
2211 pref <- prefix
2166− (suppress_author, key) <- citeKey
2212+ (suppress_author, key) <- citeKey True
2167 suff <- suffix
2213 suff <- suffix
2168 noteNum <- stateNoteNumber <$> getState
2214 noteNum <- stateNoteNumber <$> getState
2169 return $ do
2215 return $ do
@@ -2182,28 +2228,30 @@ citation = try $ do
2182smart :: PandocMonad m => MarkdownParser m (F Inlines)
2228smart :: PandocMonad m => MarkdownParser m (F Inlines)
2183smart = do
2229smart = do
2184 guardEnabled Ext_smart
2230 guardEnabled Ext_smart
2185− doubleQuoted <|> singleQuoted <|>
2231+ doubleQuoted <|> singleQuoted <|> (return <$> doubleCloseQuote) <|>
2186− choice (map (return <$>) [apostrophe, dash, ellipses])
2232+ (return <$> apostrophe) <|> (return <$> dash) <|> (return <$> ellipses)
2187
2233
2188singleQuoted :: PandocMonad m => MarkdownParser m (F Inlines)
2234singleQuoted :: PandocMonad m => MarkdownParser m (F Inlines)
2189−singleQuoted = try $ do
2235+singleQuoted = do
2190 singleQuoteStart
2236 singleQuoteStart
2191− withQuoteContext InSingleQuote $
2237+ (try (withQuoteContext InSingleQuote $
2192 fmap B.singleQuoted . trimInlinesF . mconcat <$>
2238 fmap B.singleQuoted . trimInlinesF . mconcat <$>
2193− many1Till inline singleQuoteEnd
2239+ many1Till inline singleQuoteEnd))
2240+ <|> (return (return (B.str "\8217")))
2194
2241
2195-- doubleQuoted will handle regular double-quoted sections, as well
2242-- doubleQuoted will handle regular double-quoted sections, as well
2196-- as dialogues with an open double-quote without a close double-quote
2243-- as dialogues with an open double-quote without a close double-quote
2197-- in the same paragraph.
2244-- in the same paragraph.
2198doubleQuoted :: PandocMonad m => MarkdownParser m (F Inlines)
2245doubleQuoted :: PandocMonad m => MarkdownParser m (F Inlines)
2199−doubleQuoted = try $ do
2246+doubleQuoted = do
2200 doubleQuoteStart
2247 doubleQuoteStart
2201− withQuoteContext InDoubleQuote $
2248+ (try (withQuoteContext InDoubleQuote $
2202 fmap B.doubleQuoted . trimInlinesF . mconcat <$>
2249 fmap B.doubleQuoted . trimInlinesF . mconcat <$>
2203− many1Till inline doubleQuoteEnd
2250+ many1Till inline doubleQuoteEnd))
2251+ <|> (return (return (B.str "\8220")))
2204
2252
2205toRow :: [Blocks] -> Row
2253toRow :: [Blocks] -> Row
2206toRow = Row nullAttr . map B.simpleCell
2254toRow = Row nullAttr . map B.simpleCell
2207
2255
2208toHeaderRow :: [Blocks] -> [Row]
2256toHeaderRow :: [Blocks] -> [Row]
2209−toHeaderRow l = [toRow l | not (null l)]
2257+toHeaderRow l = [toRow l | not (null l) && not (all null l)]
src/Text/Pandoc/Readers/MediaWiki.hs+16−7
@@ -36,17 +36,18 @@ import Text.Pandoc.Logging
36import Text.Pandoc.Options
36import Text.Pandoc.Options
37import Text.Pandoc.Parsing hiding (nested)
37import Text.Pandoc.Parsing hiding (nested)
38import Text.Pandoc.Readers.HTML (htmlTag, isBlockTag, isCommentTag)
38import Text.Pandoc.Readers.HTML (htmlTag, isBlockTag, isCommentTag)
39−import Text.Pandoc.Shared (crFilter, safeRead, stringify, stripTrailingNewlines,
39+import Text.Pandoc.Shared (safeRead, stringify, stripTrailingNewlines,
40 trim, splitTextBy, tshow)
40 trim, splitTextBy, tshow)
41import Text.Pandoc.Walk (walk)
41import Text.Pandoc.Walk (walk)
42import Text.Pandoc.XML (fromEntities)
42import Text.Pandoc.XML (fromEntities)
43
43
44-- | Read mediawiki from an input string and return a Pandoc document.
44-- | Read mediawiki from an input string and return a Pandoc document.
45−readMediaWiki :: PandocMonad m
45+readMediaWiki :: (PandocMonad m, ToSources a)
46− => ReaderOptions -- ^ Reader options
46+ => ReaderOptions
47− -> Text -- ^ String to parse (assuming @'\n'@ line endings)
47+ -> a
48 -> m Pandoc
48 -> m Pandoc
49readMediaWiki opts s = do
49readMediaWiki opts s = do
50+ let sources = toSources s
50 parsed <- readWithM parseMediaWiki MWState{ mwOptions = opts
51 parsed <- readWithM parseMediaWiki MWState{ mwOptions = opts
51 , mwMaxNestingLevel = 4
52 , mwMaxNestingLevel = 4
52 , mwNextLinkNumber = 1
53 , mwNextLinkNumber = 1
@@ -55,7 +56,7 @@ readMediaWiki opts s = do
55 , mwLogMessages = []
56 , mwLogMessages = []
56 , mwInTT = False
57 , mwInTT = False
57 }
58 }
58− (crFilter s <> "\n")
59+ sources
59 case parsed of
60 case parsed of
60 Right result -> return result
61 Right result -> return result
61 Left e -> throwError e
62 Left e -> throwError e
@@ -69,7 +70,7 @@ data MWState = MWState { mwOptions :: ReaderOptions
69 , mwInTT :: Bool
70 , mwInTT :: Bool
70 }
71 }
71
72
72−type MWParser m = ParserT Text MWState m
73+type MWParser m = ParserT Sources MWState m
73
74
74instance HasReaderOptions MWState where
75instance HasReaderOptions MWState where
75 extractReaderOptions = mwOptions
76 extractReaderOptions = mwOptions
@@ -112,12 +113,14 @@ newBlockTags = ["haskell","syntaxhighlight","source","gallery","references"]
112isBlockTag' :: Tag Text -> Bool
113isBlockTag' :: Tag Text -> Bool
113isBlockTag' tag@(TagOpen t _) = (isBlockTag tag || t `elem` newBlockTags) &&
114isBlockTag' tag@(TagOpen t _) = (isBlockTag tag || t `elem` newBlockTags) &&
114 t `notElem` eitherBlockOrInline
115 t `notElem` eitherBlockOrInline
116+isBlockTag' (TagClose "ref") = True -- needed so 'special' doesn't parse it
115isBlockTag' tag@(TagClose t) = (isBlockTag tag || t `elem` newBlockTags) &&
117isBlockTag' tag@(TagClose t) = (isBlockTag tag || t `elem` newBlockTags) &&
116 t `notElem` eitherBlockOrInline
118 t `notElem` eitherBlockOrInline
117isBlockTag' tag = isBlockTag tag
119isBlockTag' tag = isBlockTag tag
118
120
119isInlineTag' :: Tag Text -> Bool
121isInlineTag' :: Tag Text -> Bool
120isInlineTag' (TagComment _) = True
122isInlineTag' (TagComment _) = True
123+isInlineTag' (TagClose "ref") = False -- see below inlineTag
121isInlineTag' t = not (isBlockTag' t)
124isInlineTag' t = not (isBlockTag' t)
122
125
123eitherBlockOrInline :: [Text]
126eitherBlockOrInline :: [Text]
@@ -554,11 +557,17 @@ variable = try $ do
554 contents <- manyTillChar anyChar (try $ string "}}}")
557 contents <- manyTillChar anyChar (try $ string "}}}")
555 return $ "{{{" <> contents <> "}}}"
558 return $ "{{{" <> contents <> "}}}"
556
559
560+singleParaToPlain :: Blocks -> Blocks
561+singleParaToPlain bs =
562+ case B.toList bs of
563+ [Para ils] -> B.fromList [Plain ils]
564+ _ -> bs
565+
557inlineTag :: PandocMonad m => MWParser m Inlines
566inlineTag :: PandocMonad m => MWParser m Inlines
558inlineTag = do
567inlineTag = do
559 (tag, _) <- lookAhead $ htmlTag isInlineTag'
568 (tag, _) <- lookAhead $ htmlTag isInlineTag'
560 case tag of
569 case tag of
561− TagOpen "ref" _ -> B.note . B.plain <$> inlinesInTags "ref"
570+ TagOpen "ref" _ -> B.note . singleParaToPlain <$> blocksInTags "ref"
562 TagOpen "nowiki" _ -> try $ do
571 TagOpen "nowiki" _ -> try $ do
563 (_,raw) <- htmlTag (~== tag)
572 (_,raw) <- htmlTag (~== tag)
564 if T.any (== '/') raw
573 if T.any (== '/') raw
src/Text/Pandoc/Readers/Metadata.hs+44−17
@@ -14,6 +14,7 @@ Parse YAML/JSON metadata to 'Pandoc' 'Meta'.
14module Text.Pandoc.Readers.Metadata (
14module Text.Pandoc.Readers.Metadata (
15 yamlBsToMeta,
15 yamlBsToMeta,
16 yamlBsToRefs,
16 yamlBsToRefs,
17+ yamlMetaBlock,
17 yamlMap ) where
18 yamlMap ) where
18
19
19import Control.Monad
20import Control.Monad
@@ -30,11 +31,13 @@ import Text.Pandoc.Definition
30import Text.Pandoc.Error
31import Text.Pandoc.Error
31import Text.Pandoc.Parsing hiding (tableWith)
32import Text.Pandoc.Parsing hiding (tableWith)
32import Text.Pandoc.Shared
33import Text.Pandoc.Shared
34+import qualified Data.Text.Lazy as TL
35+import qualified Text.Pandoc.UTF8 as UTF8
33
36
34−yamlBsToMeta :: PandocMonad m
37+yamlBsToMeta :: (PandocMonad m, HasLastStrPosition st)
35− => ParserT Text ParserState m (F MetaValue)
38+ => ParserT Sources st m (Future st MetaValue)
36 -> BL.ByteString
39 -> BL.ByteString
37− -> ParserT Text ParserState m (F Meta)
40+ -> ParserT Sources st m (Future st Meta)
38yamlBsToMeta pMetaValue bstr = do
41yamlBsToMeta pMetaValue bstr = do
39 case YAML.decodeNode' YAML.failsafeSchemaResolver False False bstr of
42 case YAML.decodeNode' YAML.failsafeSchemaResolver False False bstr of
40 Right (YAML.Doc (YAML.Mapping _ _ o):_)
43 Right (YAML.Doc (YAML.Mapping _ _ o):_)
@@ -42,6 +45,9 @@ yamlBsToMeta pMetaValue bstr = do
42 Right [] -> return . return $ mempty
45 Right [] -> return . return $ mempty
43 Right [YAML.Doc (YAML.Scalar _ YAML.SNull)]
46 Right [YAML.Doc (YAML.Scalar _ YAML.SNull)]
44 -> return . return $ mempty
47 -> return . return $ mempty
48+ -- the following is what we get from a comment:
49+ Right [YAML.Doc (YAML.Scalar _ (YAML.SUnknown _ ""))]
50+ -> return . return $ mempty
45 Right _ -> Prelude.fail "expected YAML object"
51 Right _ -> Prelude.fail "expected YAML object"
46 Left (yamlpos, err')
52 Left (yamlpos, err')
47 -> do pos <- getPosition
53 -> do pos <- getPosition
@@ -63,11 +69,11 @@ lookupYAML t (YAML.Mapping _ _ m) =
63lookupYAML _ _ = Nothing
69lookupYAML _ _ = Nothing
64
70
65-- Returns filtered list of references.
71-- Returns filtered list of references.
66−yamlBsToRefs :: PandocMonad m
72+yamlBsToRefs :: (PandocMonad m, HasLastStrPosition st)
67− => ParserT Text ParserState m (F MetaValue)
73+ => ParserT Sources st m (Future st MetaValue)
68 -> (Text -> Bool) -- ^ Filter for id
74 -> (Text -> Bool) -- ^ Filter for id
69 -> BL.ByteString
75 -> BL.ByteString
70− -> ParserT Text ParserState m (F [MetaValue])
76+ -> ParserT Sources st m (Future st [MetaValue])
71yamlBsToRefs pMetaValue idpred bstr =
77yamlBsToRefs pMetaValue idpred bstr =
72 case YAML.decodeNode' YAML.failsafeSchemaResolver False False bstr of
78 case YAML.decodeNode' YAML.failsafeSchemaResolver False False bstr of
73 Right (YAML.Doc o@YAML.Mapping{}:_)
79 Right (YAML.Doc o@YAML.Mapping{}:_)
@@ -95,8 +101,12 @@ yamlBsToRefs pMetaValue idpred bstr =
95 Right [YAML.Doc (YAML.Scalar _ YAML.SNull)]
101 Right [YAML.Doc (YAML.Scalar _ YAML.SNull)]
96 -> return . return $ mempty
102 -> return . return $ mempty
97 Right _ -> Prelude.fail "expecting YAML object"
103 Right _ -> Prelude.fail "expecting YAML object"
98− Left (_pos, err')
104+ Left (yamlpos, err')
105+ -> do pos <- getPosition
106+ setPosition $ incSourceLine
107+ (setSourceColumn pos (YE.posColumn yamlpos))
108+ (YE.posLine yamlpos - 1)
99− -> Prelude.fail err'
109+ Prelude.fail err'
100
110
101
111
102nodeToKey :: YAML.Node YE.Pos -> Maybe Text
112nodeToKey :: YAML.Node YE.Pos -> Maybe Text
@@ -104,10 +114,10 @@ nodeToKey (YAML.Scalar _ (YAML.SStr t)) = Just t
104nodeToKey (YAML.Scalar _ (YAML.SUnknown _ t)) = Just t
114nodeToKey (YAML.Scalar _ (YAML.SUnknown _ t)) = Just t
105nodeToKey _ = Nothing
115nodeToKey _ = Nothing
106
116
107−normalizeMetaValue :: PandocMonad m
117+normalizeMetaValue :: (PandocMonad m, HasLastStrPosition st)
108− => ParserT Text ParserState m (F MetaValue)
118+ => ParserT Sources st m (Future st MetaValue)
109 -> Text
119 -> Text
110− -> ParserT Text ParserState m (F MetaValue)
120+ -> ParserT Sources st m (Future st MetaValue)
111normalizeMetaValue pMetaValue x =
121normalizeMetaValue pMetaValue x =
112 -- Note: a standard quoted or unquoted YAML value will
122 -- Note: a standard quoted or unquoted YAML value will
113 -- not end in a newline, but a "block" set off with
123 -- not end in a newline, but a "block" set off with
@@ -129,10 +139,10 @@ checkBoolean t
129 | t == T.pack "false" || t == T.pack "False" || t == T.pack "FALSE" = Just False
139 | t == T.pack "false" || t == T.pack "False" || t == T.pack "FALSE" = Just False
130 | otherwise = Nothing
140 | otherwise = Nothing
131
141
132−yamlToMetaValue :: PandocMonad m
142+yamlToMetaValue :: (PandocMonad m, HasLastStrPosition st)
133− => ParserT Text ParserState m (F MetaValue)
143+ => ParserT Sources st m (Future st MetaValue)
134 -> YAML.Node YE.Pos
144 -> YAML.Node YE.Pos
135− -> ParserT Text ParserState m (F MetaValue)
145+ -> ParserT Sources st m (Future st MetaValue)
136yamlToMetaValue pMetaValue (YAML.Scalar _ x) =
146yamlToMetaValue pMetaValue (YAML.Scalar _ x) =
137 case x of
147 case x of
138 YAML.SStr t -> normalizeMetaValue pMetaValue t
148 YAML.SStr t -> normalizeMetaValue pMetaValue t
@@ -152,10 +162,10 @@ yamlToMetaValue pMetaValue (YAML.Mapping _ _ o) =
152 fmap MetaMap <$> yamlMap pMetaValue o
162 fmap MetaMap <$> yamlMap pMetaValue o
153yamlToMetaValue _ _ = return $ return $ MetaString ""
163yamlToMetaValue _ _ = return $ return $ MetaString ""
154
164
155−yamlMap :: PandocMonad m
165+yamlMap :: (PandocMonad m, HasLastStrPosition st)
156− => ParserT Text ParserState m (F MetaValue)
166+ => ParserT Sources st m (Future st MetaValue)
157 -> M.Map (YAML.Node YE.Pos) (YAML.Node YE.Pos)
167 -> M.Map (YAML.Node YE.Pos) (YAML.Node YE.Pos)
158− -> ParserT Text ParserState m (F (M.Map Text MetaValue))
168+ -> ParserT Sources st m (Future st (M.Map Text MetaValue))
159yamlMap pMetaValue o = do
169yamlMap pMetaValue o = do
160 kvs <- forM (M.toList o) $ \(key, v) -> do
170 kvs <- forM (M.toList o) $ \(key, v) -> do
161 k <- maybe (throwError $ PandocParseError
171 k <- maybe (throwError $ PandocParseError
@@ -171,3 +181,20 @@ yamlMap pMetaValue o = do
171 return $ do
181 return $ do
172 v' <- fv
182 v' <- fv
173 return (k, v')
183 return (k, v')
184+
185+-- | Parse a YAML metadata block using the supplied 'MetaValue' parser.
186+yamlMetaBlock :: (HasLastStrPosition st, PandocMonad m)
187+ => ParserT Sources st m (Future st MetaValue)
188+ -> ParserT Sources st m (Future st Meta)
189+yamlMetaBlock parser = try $ do
190+ string "---"
191+ blankline
192+ notFollowedBy blankline -- if --- is followed by a blank it's an HRULE
193+ rawYamlLines <- manyTill anyLine stopLine
194+ -- by including --- and ..., we allow yaml blocks with just comments:
195+ let rawYaml = T.unlines ("---" : (rawYamlLines ++ ["..."]))
196+ optional blanklines
197+ yamlBsToMeta parser $ UTF8.fromTextLazy $ TL.fromStrict rawYaml
198+
199+stopLine :: Monad m => ParserT Sources st m ()
200+stopLine = try $ (string "---" <|> string "...") >> blankline >> return ()
src/Text/Pandoc/Readers/Muse.hs+10−9
@@ -36,19 +36,20 @@ import Text.Pandoc.Definition
36import Text.Pandoc.Error (PandocError (PandocParsecError))
36import Text.Pandoc.Error (PandocError (PandocParsecError))
37import Text.Pandoc.Logging
37import Text.Pandoc.Logging
38import Text.Pandoc.Options
38import Text.Pandoc.Options
39−import Text.Pandoc.Parsing hiding (F)
39+import Text.Pandoc.Parsing
40−import Text.Pandoc.Shared (crFilter, trimr, tshow)
40+import Text.Pandoc.Shared (trimr, tshow)
41
41
42-- | Read Muse from an input string and return a Pandoc document.
42-- | Read Muse from an input string and return a Pandoc document.
43−readMuse :: PandocMonad m
43+readMuse :: (PandocMonad m, ToSources a)
44 => ReaderOptions
44 => ReaderOptions
45− -> Text
45+ -> a
46 -> m Pandoc
46 -> m Pandoc
47readMuse opts s = do
47readMuse opts s = do
48− let input = crFilter s
48+ let sources = toSources s
49− res <- flip runReaderT def $ runParserT parseMuse def{ museOptions = opts } "source" input
49+ res <- flip runReaderT def $ runParserT parseMuse def{ museOptions = opts }
50+ (initialSourceName sources) sources
50 case res of
51 case res of
51− Left e -> throwError $ PandocParsecError input e
52+ Left e -> throwError $ PandocParsecError sources e
52 Right d -> return d
53 Right d -> return d
53
54
54type F = Future MuseState
55type F = Future MuseState
@@ -82,7 +83,7 @@ instance Default MuseEnv where
82 , museInPara = False
83 , museInPara = False
83 }
84 }
84
85
85−type MuseParser m = ParserT Text MuseState (ReaderT MuseEnv m)
86+type MuseParser m = ParserT Sources MuseState (ReaderT MuseEnv m)
86
87
87instance HasReaderOptions MuseState where
88instance HasReaderOptions MuseState where
88 extractReaderOptions = museOptions
89 extractReaderOptions = museOptions
@@ -155,7 +156,7 @@ firstColumn = getPosition >>= \pos -> guard (sourceColumn pos == 1)
155-- * Parsers
156-- * Parsers
156
157
157-- | Parse end-of-line, which can be either a newline or end-of-file.
158-- | Parse end-of-line, which can be either a newline or end-of-file.
158−eol :: Stream s m Char => ParserT s st m ()
159+eol :: (Stream s m Char, UpdateSourcePos s Char) => ParserT s st m ()
159eol = void newline <|> eof
160eol = void newline <|> eof
160
161
161getIndent :: PandocMonad m
162getIndent :: PandocMonad m
src/Text/Pandoc/Readers/Native.hs+7−5
@@ -21,6 +21,7 @@ import Control.Monad.Except (throwError)
21import Data.Text (Text)
21import Data.Text (Text)
22import Text.Pandoc.Class.PandocMonad (PandocMonad)
22import Text.Pandoc.Class.PandocMonad (PandocMonad)
23import Text.Pandoc.Error
23import Text.Pandoc.Error
24+import Text.Pandoc.Sources (ToSources(..), sourcesToText)
24
25
25-- | Read native formatted text and return a Pandoc document.
26-- | Read native formatted text and return a Pandoc document.
26-- The input may be a full pandoc document, a block list, a block,
27-- The input may be a full pandoc document, a block list, a block,
@@ -32,14 +33,15 @@ import Text.Pandoc.Error
32--
33--
33-- > Pandoc nullMeta [Plain [Str "hi"]]
34-- > Pandoc nullMeta [Plain [Str "hi"]]
34--
35--
35−readNative :: PandocMonad m
36+readNative :: (PandocMonad m, ToSources a)
36 => ReaderOptions
37 => ReaderOptions
37− -> Text -- ^ String to parse (assuming @'\n'@ line endings)
38+ -> a
38 -> m Pandoc
39 -> m Pandoc
39readNative _ s =
40readNative _ s =
41+ let t = sourcesToText . toSources $ s
40− case maybe (Pandoc nullMeta <$> readBlocks s) Right (safeRead s) of
42+ in case maybe (Pandoc nullMeta <$> readBlocks t) Right (safeRead t) of
41− Right doc -> return doc
43+ Right doc -> return doc
42− Left _ -> throwError $ PandocParseError "couldn't read native"
44+ Left _ -> throwError $ PandocParseError "couldn't read native"
43
45
44readBlocks :: Text -> Either PandocError [Block]
46readBlocks :: Text -> Either PandocError [Block]
45readBlocks s = maybe ((:[]) <$> readBlock s) Right (safeRead s)
47readBlocks s = maybe ((:[]) <$> readBlock s) Right (safeRead s)
src/Text/Pandoc/Readers/OPML.hs+21−35
@@ -13,20 +13,21 @@ Conversion of OPML to 'Pandoc' document.
13
13
14module Text.Pandoc.Readers.OPML ( readOPML ) where
14module Text.Pandoc.Readers.OPML ( readOPML ) where
15import Control.Monad.State.Strict
15import Control.Monad.State.Strict
16−import Data.Char (toUpper)
17import Data.Default
16import Data.Default
18−import Data.Generics
19import Data.Maybe (fromMaybe)
17import Data.Maybe (fromMaybe)
20import Data.Text (Text)
18import Data.Text (Text)
21import qualified Data.Text as T
19import qualified Data.Text as T
22−import Text.HTML.TagSoup.Entity (lookupEntity)
20+import qualified Data.Text.Lazy as TL
23import Text.Pandoc.Builder
21import Text.Pandoc.Builder
24import Text.Pandoc.Class.PandocMonad (PandocMonad)
22import Text.Pandoc.Class.PandocMonad (PandocMonad)
25import Text.Pandoc.Options
23import Text.Pandoc.Options
24+import Text.Pandoc.Error (PandocError(..))
26import Text.Pandoc.Readers.HTML (readHtml)
25import Text.Pandoc.Readers.HTML (readHtml)
27import Text.Pandoc.Readers.Markdown (readMarkdown)
26import Text.Pandoc.Readers.Markdown (readMarkdown)
28−import Text.Pandoc.Shared (crFilter, blocksToInlines')
27+import Text.Pandoc.Shared (blocksToInlines')
29−import Text.XML.Light
28+import Text.Pandoc.Sources (ToSources(..), sourcesToText)
29+import Text.Pandoc.XML.Light
30+import Control.Monad.Except (throwError)
30
31
31type OPML m = StateT OPMLState m
32type OPML m = StateT OPMLState m
32
33
@@ -46,42 +47,27 @@ instance Default OPMLState where
46 , opmlOptions = def
47 , opmlOptions = def
47 }
48 }
48
49
49−readOPML :: PandocMonad m => ReaderOptions -> Text -> m Pandoc
50+readOPML :: (PandocMonad m, ToSources a)
51+ => ReaderOptions
52+ -> a
53+ -> m Pandoc
50readOPML opts inp = do
54readOPML opts inp = do
55+ let sources = toSources inp
51− (bs, st') <- runStateT
56+ (bs, st') <-
57+ runStateT (case parseXMLContents (TL.fromStrict . sourcesToText $ sources) of
58+ Left msg -> throwError $ PandocXMLError "" msg
52− (mapM parseBlock $ normalizeTree $
59+ Right ns -> mapM parseBlock ns)
53− parseXML (T.unpack (crFilter inp))) def{ opmlOptions = opts }
60+ def{ opmlOptions = opts }
54 return $
61 return $
55 setTitle (opmlDocTitle st') $
62 setTitle (opmlDocTitle st') $
56 setAuthors (opmlDocAuthors st') $
63 setAuthors (opmlDocAuthors st') $
57 setDate (opmlDocDate st') $
64 setDate (opmlDocDate st') $
58 doc $ mconcat bs
65 doc $ mconcat bs
59
66
60−-- normalize input, consolidating adjacent Text and CRef elements
61−normalizeTree :: [Content] -> [Content]
62−normalizeTree = everywhere (mkT go)
63− where go :: [Content] -> [Content]
64− go (Text (CData CDataRaw _ _):xs) = xs
65− go (Text (CData CDataText s1 z):Text (CData CDataText s2 _):xs) =
66− Text (CData CDataText (s1 ++ s2) z):xs
67− go (Text (CData CDataText s1 z):CRef r:xs) =
68− Text (CData CDataText (s1 ++ convertEntity r) z):xs
69− go (CRef r:Text (CData CDataText s1 z):xs) =
70− Text (CData CDataText (convertEntity r ++ s1) z):xs
71− go (CRef r1:CRef r2:xs) =
72− Text (CData CDataText (convertEntity r1 ++ convertEntity r2) Nothing):xs
73− go xs = xs
74−
75−convertEntity :: String -> String
76−convertEntity e = Data.Maybe.fromMaybe (map toUpper e) (lookupEntity e)
77−
78-- convenience function to get an attribute value, defaulting to ""
67-- convenience function to get an attribute value, defaulting to ""
79−attrValue :: String -> Element -> Text
68+attrValue :: Text -> Element -> Text
80attrValue attr elt =
69attrValue attr elt =
81− maybe "" T.pack (lookupAttrBy (\x -> qName x == attr) (elAttribs elt))
70+ fromMaybe "" (lookupAttrBy (\x -> qName x == attr) (elAttribs elt))
82−
83−textContent :: Element -> Text
84−textContent = T.pack . strContent
85
71
86-- exceptT :: PandocMonad m => Either PandocError a -> OPML m a
72-- exceptT :: PandocMonad m => Either PandocError a -> OPML m a
87-- exceptT = either throwError return
73-- exceptT = either throwError return
@@ -105,11 +91,11 @@ parseBlock :: PandocMonad m => Content -> OPML m Blocks
105parseBlock (Elem e) =
91parseBlock (Elem e) =
106 case qName (elName e) of
92 case qName (elName e) of
107 "ownerName" -> mempty <$ modify (\st ->
93 "ownerName" -> mempty <$ modify (\st ->
108− st{opmlDocAuthors = [text $ textContent e]})
94+ st{opmlDocAuthors = [text $ strContent e]})
109 "dateModified" -> mempty <$ modify (\st ->
95 "dateModified" -> mempty <$ modify (\st ->
110− st{opmlDocDate = text $ textContent e})
96+ st{opmlDocDate = text $ strContent e})
111 "title" -> mempty <$ modify (\st ->
97 "title" -> mempty <$ modify (\st ->
112− st{opmlDocTitle = text $ textContent e})
98+ st{opmlDocTitle = text $ strContent e})
113 "outline" -> gets opmlSectionLevel >>= sect . (+1)
99 "outline" -> gets opmlSectionLevel >>= sect . (+1)
114 "?xml" -> return mempty
100 "?xml" -> return mempty
115 _ -> getBlocks e
101 _ -> getBlocks e
src/Text/Pandoc/Readers/Odt.hs+28−25
@@ -1,4 +1,3 @@
1−{-# LANGUAGE PatternGuards #-}
2{-# LANGUAGE OverloadedStrings #-}
1{-# LANGUAGE OverloadedStrings #-}
3{- |
2{- |
4 Module : Text.Pandoc.Reader.Odt
3 Module : Text.Pandoc.Reader.Odt
@@ -15,7 +14,7 @@ Entry point to the odt reader.
15module Text.Pandoc.Readers.Odt ( readOdt ) where
14module Text.Pandoc.Readers.Odt ( readOdt ) where
16
15
17import Codec.Archive.Zip
16import Codec.Archive.Zip
18−import qualified Text.XML.Light as XML
17+import Text.Pandoc.XML.Light
19
18
20import qualified Data.ByteString.Lazy as B
19import qualified Data.ByteString.Lazy as B
21
20
@@ -23,6 +22,8 @@ import System.FilePath
23
22
24import Control.Monad.Except (throwError)
23import Control.Monad.Except (throwError)
25
24
25+import qualified Data.Text as T
26+
26import Text.Pandoc.Class.PandocMonad (PandocMonad)
27import Text.Pandoc.Class.PandocMonad (PandocMonad)
27import qualified Text.Pandoc.Class.PandocMonad as P
28import qualified Text.Pandoc.Class.PandocMonad as P
28import Text.Pandoc.Definition
29import Text.Pandoc.Definition
@@ -60,35 +61,37 @@ readOdt' _ bytes = bytesToOdt bytes-- of
60bytesToOdt :: B.ByteString -> Either PandocError (Pandoc, MediaBag)
61bytesToOdt :: B.ByteString -> Either PandocError (Pandoc, MediaBag)
61bytesToOdt bytes = case toArchiveOrFail bytes of
62bytesToOdt bytes = case toArchiveOrFail bytes of
62 Right archive -> archiveToOdt archive
63 Right archive -> archiveToOdt archive
63− Left _ -> Left $ PandocParseError "Couldn't parse odt file."
64+ Left err -> Left $ PandocParseError
65+ $ "Could not unzip ODT: " <> T.pack err
64
66
65--
67--
66archiveToOdt :: Archive -> Either PandocError (Pandoc, MediaBag)
68archiveToOdt :: Archive -> Either PandocError (Pandoc, MediaBag)
67−archiveToOdt archive
69+archiveToOdt archive = do
70+ let onFailure msg Nothing = Left $ PandocParseError msg
71+ onFailure _ (Just x) = Right x
72+ contentEntry <- onFailure "Could not find content.xml"
68− | Just contentEntry <- findEntryByPath "content.xml" archive
73+ (findEntryByPath "content.xml" archive)
74+ stylesEntry <- onFailure "Could not find styles.xml"
69− , Just stylesEntry <- findEntryByPath "styles.xml" archive
75+ (findEntryByPath "styles.xml" archive)
70− , Just contentElem <- entryToXmlElem contentEntry
76+ contentElem <- entryToXmlElem contentEntry
71− , Just stylesElem <- entryToXmlElem stylesEntry
77+ stylesElem <- entryToXmlElem stylesEntry
78+ styles <- either
79+ (\_ -> Left $ PandocParseError "Could not read styles")
80+ Right
72− , Right styles <- chooseMax (readStylesAt stylesElem )
81+ (chooseMax (readStylesAt stylesElem ) (readStylesAt contentElem))
73− (readStylesAt contentElem)
74− , media <- filteredFilesFromArchive archive filePathIsOdtMedia
75− , startState <- readerState styles media
76− , Right pandocWithMedia <- runConverter' read_body
77− startState
78− contentElem
79−
80− = Right pandocWithMedia
81−
82− | otherwise
83− -- Not very detailed, but I don't think more information would be helpful
84− = Left $ PandocParseError "Couldn't parse odt file."
85− where
86− filePathIsOdtMedia :: FilePath -> Bool
82+ let filePathIsOdtMedia :: FilePath -> Bool
87 filePathIsOdtMedia fp =
83 filePathIsOdtMedia fp =
88 let (dir, name) = splitFileName fp
84 let (dir, name) = splitFileName fp
89 in (dir == "Pictures/") || (dir /= "./" && name == "content.xml")
85 in (dir == "Pictures/") || (dir /= "./" && name == "content.xml")
86+ let media = filteredFilesFromArchive archive filePathIsOdtMedia
87+ let startState = readerState styles media
88+ either (\_ -> Left $ PandocParseError "Could not convert opendocument") Right
89+ (runConverter' read_body startState contentElem)
90
90
91
91
92--
92--
93−entryToXmlElem :: Entry -> Maybe XML.Element
93+entryToXmlElem :: Entry -> Either PandocError Element
94−entryToXmlElem = XML.parseXMLDoc . UTF8.toStringLazy . fromEntry
94+entryToXmlElem entry =
95+ case parseXMLElement . UTF8.toTextLazy . fromEntry $ entry of
96+ Right x -> Right x
97+ Left msg -> Left $ PandocXMLError (T.pack $ eRelativePath entry) msg
src/Text/Pandoc/Readers/Odt/Arrows/State.hs+2−2
@@ -22,7 +22,7 @@ module Text.Pandoc.Readers.Odt.Arrows.State where
22import Control.Arrow
22import Control.Arrow
23import qualified Control.Category as Cat
23import qualified Control.Category as Cat
24import Control.Monad
24import Control.Monad
25−
25+import Data.List (foldl')
26import Text.Pandoc.Readers.Odt.Arrows.Utils
26import Text.Pandoc.Readers.Odt.Arrows.Utils
27import Text.Pandoc.Readers.Odt.Generic.Fallible
27import Text.Pandoc.Readers.Odt.Generic.Fallible
28
28
@@ -122,7 +122,7 @@ iterateS a = ArrowState $ \(s,f) -> foldr a' (s,mzero) f
122iterateSL :: (Foldable f, MonadPlus m)
122iterateSL :: (Foldable f, MonadPlus m)
123 => ArrowState s x y
123 => ArrowState s x y
124 -> ArrowState s (f x) (m y)
124 -> ArrowState s (f x) (m y)
125−iterateSL a = ArrowState $ \(s,f) -> foldl a' (s,mzero) f
125+iterateSL a = ArrowState $ \(s,f) -> foldl' a' (s,mzero) f
126 where a' (s',m) x = second (mplus m.return) $ runArrowState a (s',x)
126 where a' (s',m) x = second (mplus m.return) $ runArrowState a (s',x)
127
127
128
128
src/Text/Pandoc/Readers/Odt/ContentReader.hs+19−13
@@ -1,4 +1,5 @@
1{-# LANGUAGE Arrows #-}
1{-# LANGUAGE Arrows #-}
2+{-# LANGUAGE CPP #-}
2{-# LANGUAGE DeriveFoldable #-}
3{-# LANGUAGE DeriveFoldable #-}
3{-# LANGUAGE GeneralizedNewtypeDeriving #-}
4{-# LANGUAGE GeneralizedNewtypeDeriving #-}
4{-# LANGUAGE PatternGuards #-}
5{-# LANGUAGE PatternGuards #-}
@@ -29,14 +30,14 @@ import Control.Monad ((<=<))
29
30
30import qualified Data.ByteString.Lazy as B
31import qualified Data.ByteString.Lazy as B
31import Data.Foldable (fold)
32import Data.Foldable (fold)
32−import Data.List (find, stripPrefix)
33+import Data.List (find)
33import qualified Data.Map as M
34import qualified Data.Map as M
34import qualified Data.Text as T
35import qualified Data.Text as T
35import Data.Maybe
36import Data.Maybe
36−import Data.Semigroup (First(..), Option(..))
37+import Data.Monoid (Alt (..))
37
38
38import Text.TeXMath (readMathML, writeTeX)
39import Text.TeXMath (readMathML, writeTeX)
39−import qualified Text.XML.Light as XML
40+import qualified Text.Pandoc.XML.Light as XML
40
41
41import Text.Pandoc.Builder hiding (underline)
42import Text.Pandoc.Builder hiding (underline)
42import Text.Pandoc.MediaBag (MediaBag, insertMedia)
43import Text.Pandoc.MediaBag (MediaBag, insertMedia)
@@ -505,13 +506,11 @@ type InlineMatcher = ElementMatcher Inlines
505
506
506type BlockMatcher = ElementMatcher Blocks
507type BlockMatcher = ElementMatcher Blocks
507
508
508−
509−newtype FirstMatch a = FirstMatch (Option (First a))
509+newtype FirstMatch a = FirstMatch (Alt Maybe a)
510− deriving (Foldable, Monoid, Semigroup)
510+ deriving (Foldable, Monoid, Semigroup)
511
511
512firstMatch :: a -> FirstMatch a
512firstMatch :: a -> FirstMatch a
513−firstMatch = FirstMatch . Option . Just . First
513+firstMatch = FirstMatch . Alt . Just
514−
515
514
516--
515--
517matchingElement :: (Monoid e)
516matchingElement :: (Monoid e)
@@ -557,7 +556,7 @@ read_plain_text = fst ^&&& read_plain_text' >>% recover
557 >>?% mappend
556 >>?% mappend
558 --
557 --
559 extractText :: XML.Content -> Fallible T.Text
558 extractText :: XML.Content -> Fallible T.Text
560− extractText (XML.Text cData) = succeedWith (T.pack $ XML.cdData cData)
559+ extractText (XML.Text cData) = succeedWith (XML.cdData cData)
561 extractText _ = failEmpty
560 extractText _ = failEmpty
562
561
563read_text_seq :: InlineMatcher
562read_text_seq :: InlineMatcher
@@ -577,7 +576,10 @@ read_spaces = matchingElement NsText "s" (
577read_line_break :: InlineMatcher
576read_line_break :: InlineMatcher
578read_line_break = matchingElement NsText "line-break"
577read_line_break = matchingElement NsText "line-break"
579 $ returnV linebreak
578 $ returnV linebreak
580−
579+--
580+read_tab :: InlineMatcher
581+read_tab = matchingElement NsText "tab"
582+ $ returnV space
581--
583--
582read_span :: InlineMatcher
584read_span :: InlineMatcher
583read_span = matchingElement NsText "span"
585read_span = matchingElement NsText "span"
@@ -585,6 +587,7 @@ read_span = matchingElement NsText "span"
585 $ matchChildContent [ read_span
587 $ matchChildContent [ read_span
586 , read_spaces
588 , read_spaces
587 , read_line_break
589 , read_line_break
590+ , read_tab
588 , read_link
591 , read_link
589 , read_note
592 , read_note
590 , read_citation
593 , read_citation
@@ -604,6 +607,7 @@ read_paragraph = matchingElement NsText "p"
604 $ matchChildContent [ read_span
607 $ matchChildContent [ read_span
605 , read_spaces
608 , read_spaces
606 , read_line_break
609 , read_line_break
610+ , read_tab
607 , read_link
611 , read_link
608 , read_note
612 , read_note
609 , read_citation
613 , read_citation
@@ -630,6 +634,7 @@ read_header = matchingElement NsText "h"
630 children <- ( matchChildContent [ read_span
634 children <- ( matchChildContent [ read_span
631 , read_spaces
635 , read_spaces
632 , read_line_break
636 , read_line_break
637+ , read_tab
633 , read_link
638 , read_link
634 , read_note
639 , read_note
635 , read_citation
640 , read_citation
@@ -777,14 +782,14 @@ read_frame_img =
777 "" -> returnV mempty -< ()
782 "" -> returnV mempty -< ()
778 src' -> do
783 src' -> do
779 let exts = extensionsFromList [Ext_auto_identifiers]
784 let exts = extensionsFromList [Ext_auto_identifiers]
780− resource <- lookupResource -< src'
785+ resource <- lookupResource -< T.unpack src'
781 _ <- updateMediaWithResource -< resource
786 _ <- updateMediaWithResource -< resource
782 w <- findAttrText' NsSVG "width" -< ()
787 w <- findAttrText' NsSVG "width" -< ()
783 h <- findAttrText' NsSVG "height" -< ()
788 h <- findAttrText' NsSVG "height" -< ()
784 titleNodes <- matchChildContent' [ read_frame_title ] -< ()
789 titleNodes <- matchChildContent' [ read_frame_title ] -< ()
785 alt <- matchChildContent [] read_plain_text -< ()
790 alt <- matchChildContent [] read_plain_text -< ()
786 arr (firstMatch . uncurry4 imageWith) -<
791 arr (firstMatch . uncurry4 imageWith) -<
787− (image_attributes w h, T.pack src', inlineListToIdentifier exts (toList titleNodes), alt)
792+ (image_attributes w h, src', inlineListToIdentifier exts (toList titleNodes), alt)
788
793
789read_frame_title :: InlineMatcher
794read_frame_title :: InlineMatcher
790read_frame_title = matchingElement NsSVG "title" (matchChildContent [] read_plain_text)
795read_frame_title = matchingElement NsSVG "title" (matchChildContent [] read_plain_text)
@@ -804,7 +809,8 @@ read_frame_mathml =
804 case fold src of
809 case fold src of
805 "" -> returnV mempty -< ()
810 "" -> returnV mempty -< ()
806 src' -> do
811 src' -> do
812+ let path = T.unpack $
807− let path = fromMaybe src' (stripPrefix "./" src') ++ "/content.xml"
813+ fromMaybe src' (T.stripPrefix "./" src') <> "/content.xml"
808 (_, mathml) <- lookupResource -< path
814 (_, mathml) <- lookupResource -< path
809 case readMathML (UTF8.toText $ B.toStrict mathml) of
815 case readMathML (UTF8.toText $ B.toStrict mathml) of
810 Left _ -> returnV mempty -< ()
816 Left _ -> returnV mempty -< ()
src/Text/Pandoc/Readers/Odt/Generic/Namespaces.hs+2−1
@@ -14,9 +14,10 @@ typesafe Haskell namespace identifiers and unsafe "real world" namespaces.
14module Text.Pandoc.Readers.Odt.Generic.Namespaces where
14module Text.Pandoc.Readers.Odt.Generic.Namespaces where
15
15
16import qualified Data.Map as M
16import qualified Data.Map as M
17+import Data.Text (Text)
17
18
18--
19--
19−type NameSpaceIRI = String
20+type NameSpaceIRI = Text
20
21
21--
22--
22type NameSpaceIRIs nsID = M.Map nsID NameSpaceIRI
23type NameSpaceIRIs nsID = M.Map nsID NameSpaceIRI
src/Text/Pandoc/Readers/Odt/Generic/Utils.hs+9−24
@@ -20,7 +20,6 @@ module Text.Pandoc.Readers.Odt.Generic.Utils
20, reverseComposition
20, reverseComposition
21, tryToRead
21, tryToRead
22, Lookupable(..)
22, Lookupable(..)
23−, readLookupables
24, readLookupable
23, readLookupable
25, readPercent
24, readPercent
26, findBy
25, findBy
@@ -30,11 +29,11 @@ module Text.Pandoc.Readers.Odt.Generic.Utils
30
29
31import Control.Category (Category, (<<<), (>>>))
30import Control.Category (Category, (<<<), (>>>))
32import qualified Control.Category as Cat (id)
31import qualified Control.Category as Cat (id)
33−import Control.Monad (msum)
32+import Data.Char (isSpace)
34−
35import qualified Data.Foldable as F (Foldable, foldr)
33import qualified Data.Foldable as F (Foldable, foldr)
36import Data.Maybe
34import Data.Maybe
37−
35+import Data.Text (Text)
36+import qualified Data.Text as T
38
37
39-- | Equivalent to
38-- | Equivalent to
40-- > foldr (.) id
39-- > foldr (.) id
@@ -76,8 +75,8 @@ swing = flip.(.flip id)
76-- (nobody wants that) while the latter returns "to much" for simple purposes.
75-- (nobody wants that) while the latter returns "to much" for simple purposes.
77-- This function instead applies 'reads' and returns the first match (if any)
76-- This function instead applies 'reads' and returns the first match (if any)
78-- in a 'Maybe'.
77-- in a 'Maybe'.
79−tryToRead :: (Read r) => String -> Maybe r
78+tryToRead :: (Read r) => Text -> Maybe r
80−tryToRead = reads >>> listToMaybe >>> fmap fst
79+tryToRead = (reads . T.unpack) >>> listToMaybe >>> fmap fst
81
80
82-- | A version of 'reads' that requires a '%' sign after the number
81-- | A version of 'reads' that requires a '%' sign after the number
83readPercent :: ReadS Int
82readPercent :: ReadS Int
@@ -88,26 +87,12 @@ readPercent s = [ (i,s') | (i , r ) <- reads s
88-- | Data that can be looked up.
87-- | Data that can be looked up.
89-- This is mostly a utility to read data with kind *.
88-- This is mostly a utility to read data with kind *.
90class Lookupable a where
89class Lookupable a where
91− lookupTable :: [(String, a)]
90+ lookupTable :: [(Text, a)]
92−
93−-- | The idea is to use this function as if there was a declaration like
94−--
95−-- > instance (Lookupable a) => (Read a) where
96−-- > readsPrec _ = readLookupables
97−-- .
98−-- But including this code in this form would need UndecideableInstances.
99−-- That is a bad idea. Luckily 'readLookupable' (without the s at the end)
100−-- can be used directly in almost any case.
101−readLookupables :: (Lookupable a) => String -> [(a,String)]
102−readLookupables s = [ (a,rest) | (word,rest) <- lex s,
103− a <- maybeToList (lookup word lookupTable)
104− ]
105
91
106-- | Very similar to a simple 'lookup' in the 'lookupTable', but with a lexer.
92-- | Very similar to a simple 'lookup' in the 'lookupTable', but with a lexer.
107−readLookupable :: (Lookupable a) => String -> Maybe a
93+readLookupable :: (Lookupable a) => Text -> Maybe a
108−readLookupable s = msum
94+readLookupable s =
109− $ map ((`lookup` lookupTable).fst)
95+ lookup (T.takeWhile (not . isSpace) $ T.dropWhile isSpace s) lookupTable
110− $ lex s
111
96
112uncurry3 :: (a->b->c -> z) -> (a,b,c ) -> z
97uncurry3 :: (a->b->c -> z) -> (a,b,c ) -> z
113uncurry4 :: (a->b->c->d -> z) -> (a,b,c,d ) -> z
98uncurry4 :: (a->b->c->d -> z) -> (a,b,c,d ) -> z
src/Text/Pandoc/Readers/Odt/Generic/XMLConverter.hs+13−14
@@ -1,3 +1,4 @@
1+{-# LANGUAGE OverloadedStrings #-}
1{-# LANGUAGE TupleSections #-}
2{-# LANGUAGE TupleSections #-}
2{-# LANGUAGE GADTs #-}
3{-# LANGUAGE GADTs #-}
3{-# LANGUAGE LambdaCase #-}
4{-# LANGUAGE LambdaCase #-}
@@ -60,15 +61,15 @@ import Control.Arrow
60import Data.Bool ( bool )
61import Data.Bool ( bool )
61import Data.Either ( rights )
62import Data.Either ( rights )
62import qualified Data.Map as M
63import qualified Data.Map as M
63−import qualified Data.Text as T
64+import Data.Text (Text)
64import Data.Default
65import Data.Default
65import Data.Maybe
66import Data.Maybe
67+import Data.List (foldl')
66
68
67−import qualified Text.XML.Light as XML
69+import qualified Text.Pandoc.XML.Light as XML
68
70
69import Text.Pandoc.Readers.Odt.Arrows.State
71import Text.Pandoc.Readers.Odt.Arrows.State
70import Text.Pandoc.Readers.Odt.Arrows.Utils
72import Text.Pandoc.Readers.Odt.Arrows.Utils
71−
72import Text.Pandoc.Readers.Odt.Generic.Namespaces
73import Text.Pandoc.Readers.Odt.Generic.Namespaces
73import Text.Pandoc.Readers.Odt.Generic.Utils
74import Text.Pandoc.Readers.Odt.Generic.Utils
74import Text.Pandoc.Readers.Odt.Generic.Fallible
75import Text.Pandoc.Readers.Odt.Generic.Fallible
@@ -78,13 +79,13 @@ import Text.Pandoc.Readers.Odt.Generic.Fallible
78--------------------------------------------------------------------------------
79--------------------------------------------------------------------------------
79
80
80--
81--
81−type ElementName = String
82+type ElementName = Text
82−type AttributeName = String
83+type AttributeName = Text
83−type AttributeValue = String
84+type AttributeValue = Text
84−type TextAttributeValue = T.Text
85+type TextAttributeValue = Text
85
86
86--
87--
87−type NameSpacePrefix = String
88+type NameSpacePrefix = Text
88
89
89--
90--
90type NameSpacePrefixes nsID = M.Map nsID NameSpacePrefix
91type NameSpacePrefixes nsID = M.Map nsID NameSpacePrefix
@@ -292,7 +293,7 @@ readNSattributes = fromState $ \state -> maybe (state, failEmpty )
292 => XMLConverterState nsID extraState
293 => XMLConverterState nsID extraState
293 -> Maybe (XMLConverterState nsID extraState)
294 -> Maybe (XMLConverterState nsID extraState)
294 extractNSAttrs startState
295 extractNSAttrs startState
295− = foldl (\state d -> state >>= addNS d)
296+ = foldl' (\state d -> state >>= addNS d)
296 (Just startState)
297 (Just startState)
297 nsAttribs
298 nsAttribs
298 where nsAttribs = mapMaybe readNSattr (XML.elAttribs element)
299 where nsAttribs = mapMaybe readNSattr (XML.elAttribs element)
@@ -461,7 +462,7 @@ lookupDefaultingAttr :: (NameSpaceID nsID, Lookupable a, Default a)
461lookupDefaultingAttr nsID attrName
462lookupDefaultingAttr nsID attrName
462 = lookupAttrWithDefault nsID attrName def
463 = lookupAttrWithDefault nsID attrName def
463
464
464−-- | Return value as a (Maybe String)
465+-- | Return value as a (Maybe Text)
465findAttr' :: (NameSpaceID nsID)
466findAttr' :: (NameSpaceID nsID)
466 => nsID -> AttributeName
467 => nsID -> AttributeName
467 -> XMLConverter nsID extraState x (Maybe AttributeValue)
468 -> XMLConverter nsID extraState x (Maybe AttributeValue)
@@ -477,7 +478,6 @@ findAttrText' nsID attrName
477 = qualifyName nsID attrName
478 = qualifyName nsID attrName
478 &&& getCurrentElement
479 &&& getCurrentElement
479 >>% XML.findAttr
480 >>% XML.findAttr
480− >>^ fmap T.pack
481
481
482-- | Return value as string or fail
482-- | Return value as string or fail
483findAttr :: (NameSpaceID nsID)
483findAttr :: (NameSpaceID nsID)
@@ -492,7 +492,6 @@ findAttrText :: (NameSpaceID nsID)
492 -> FallibleXMLConverter nsID extraState x TextAttributeValue
492 -> FallibleXMLConverter nsID extraState x TextAttributeValue
493findAttrText nsID attrName
493findAttrText nsID attrName
494 = findAttr' nsID attrName
494 = findAttr' nsID attrName
495− >>^ fmap T.pack
496 >>> maybeToChoice
495 >>> maybeToChoice
497
496
498-- | Return value as string or return provided default value
497-- | Return value as string or return provided default value
@@ -511,7 +510,7 @@ findAttrTextWithDefault :: (NameSpaceID nsID)
511 -> XMLConverter nsID extraState x TextAttributeValue
510 -> XMLConverter nsID extraState x TextAttributeValue
512findAttrTextWithDefault nsID attrName deflt
511findAttrTextWithDefault nsID attrName deflt
513 = findAttr' nsID attrName
512 = findAttr' nsID attrName
514− >>^ maybe deflt T.pack
513+ >>^ fromMaybe deflt
515
514
516-- | Read and return value or fail
515-- | Read and return value or fail
517readAttr :: (NameSpaceID nsID, Read attrValue)
516readAttr :: (NameSpaceID nsID, Read attrValue)
@@ -748,7 +747,7 @@ matchContent lookups fallback
748-- Internals
747-- Internals
749--------------------------------------------------------------------------------
748--------------------------------------------------------------------------------
750
749
751−stringToBool' :: String -> Maybe Bool
750+stringToBool' :: Text -> Maybe Bool
752stringToBool' val | val `elem` trueValues = Just True
751stringToBool' val | val `elem` trueValues = Just True
753 | val `elem` falseValues = Just False
752 | val `elem` falseValues = Just False
754 | otherwise = Nothing
753 | otherwise = Nothing
src/Text/Pandoc/Readers/Odt/Namespaces.hs+6−5
@@ -1,3 +1,4 @@
1+{-# LANGUAGE OverloadedStrings #-}
1{- |
2{- |
2 Module : Text.Pandoc.Reader.Odt.Namespaces
3 Module : Text.Pandoc.Reader.Odt.Namespaces
3 Copyright : Copyright (C) 2015 Martin Linnemann
4 Copyright : Copyright (C) 2015 Martin Linnemann
@@ -13,10 +14,10 @@ Namespaces used in odt files.
13module Text.Pandoc.Readers.Odt.Namespaces ( Namespace (..)
14module Text.Pandoc.Readers.Odt.Namespaces ( Namespace (..)
14 ) where
15 ) where
15
16
16−import Data.List (isPrefixOf)
17import qualified Data.Map as M (empty, insert)
17import qualified Data.Map as M (empty, insert)
18import Data.Maybe (fromMaybe, listToMaybe)
18import Data.Maybe (fromMaybe, listToMaybe)
19−
19+import Data.Text (Text)
20+import qualified Data.Text as T
20import Text.Pandoc.Readers.Odt.Generic.Namespaces
21import Text.Pandoc.Readers.Odt.Generic.Namespaces
21
22
22
23
@@ -30,7 +31,7 @@ instance NameSpaceID Namespace where
30
31
31
32
32findID :: NameSpaceIRI -> Maybe Namespace
33findID :: NameSpaceIRI -> Maybe Namespace
33−findID iri = listToMaybe [nsID | (iri',nsID) <- nsIDs, iri' `isPrefixOf` iri]
34+findID iri = listToMaybe [nsID | (iri',nsID) <- nsIDs, iri' `T.isPrefixOf` iri]
34
35
35nsIDmap :: NameSpaceIRIs Namespace
36nsIDmap :: NameSpaceIRIs Namespace
36nsIDmap = foldr (uncurry $ flip M.insert) M.empty nsIDs
37nsIDmap = foldr (uncurry $ flip M.insert) M.empty nsIDs
@@ -54,12 +55,12 @@ data Namespace = -- Open Document core
54 -- Core XML (basically only for the 'id'-attribute)
55 -- Core XML (basically only for the 'id'-attribute)
55 | NsXML
56 | NsXML
56 -- Fallback
57 -- Fallback
57− | NsOther String
58+ | NsOther Text
58 deriving ( Eq, Ord, Show )
59 deriving ( Eq, Ord, Show )
59
60
60-- | Not the actual iri's, but large prefixes of them - this way there are
61-- | Not the actual iri's, but large prefixes of them - this way there are
61-- less versioning problems and the like.
62-- less versioning problems and the like.
62−nsIDs :: [(String,Namespace)]
63+nsIDs :: [(Text, Namespace)]
63nsIDs = [
64nsIDs = [
64 ("urn:oasis:names:tc:opendocument:xmlns:animation" , NsAnim ),
65 ("urn:oasis:names:tc:opendocument:xmlns:animation" , NsAnim ),
65 ("urn:oasis:names:tc:opendocument:xmlns:chart" , NsChart ),
66 ("urn:oasis:names:tc:opendocument:xmlns:chart" , NsChart ),
src/Text/Pandoc/Readers/Odt/StyleReader.hs+21−17
@@ -2,6 +2,7 @@
2{-# LANGUAGE Arrows #-}
2{-# LANGUAGE Arrows #-}
3{-# LANGUAGE RecordWildCards #-}
3{-# LANGUAGE RecordWildCards #-}
4{-# LANGUAGE TupleSections #-}
4{-# LANGUAGE TupleSections #-}
5+{-# LANGUAGE OverloadedStrings #-}
5{- |
6{- |
6 Module : Text.Pandoc.Readers.Odt.StyleReader
7 Module : Text.Pandoc.Readers.Odt.StyleReader
7 Copyright : Copyright (C) 2015 Martin Linnemann
8 Copyright : Copyright (C) 2015 Martin Linnemann
@@ -43,14 +44,16 @@ import Control.Arrow
43
44
44import Data.Default
45import Data.Default
45import qualified Data.Foldable as F
46import qualified Data.Foldable as F
46−import Data.List (unfoldr)
47+import Data.List (unfoldr, foldl')
47import qualified Data.Map as M
48import qualified Data.Map as M
48import Data.Maybe
49import Data.Maybe
50+import Data.Text (Text)
51+import qualified Data.Text as T
49import qualified Data.Set as S
52import qualified Data.Set as S
50
53
51−import qualified Text.XML.Light as XML
54+import qualified Text.Pandoc.XML.Light as XML
52
55
53−import Text.Pandoc.Shared (safeRead)
56+import Text.Pandoc.Shared (safeRead, tshow)
54
57
55import Text.Pandoc.Readers.Odt.Arrows.Utils
58import Text.Pandoc.Readers.Odt.Arrows.Utils
56
59
@@ -90,7 +93,7 @@ instance Default FontPitch where
90--
93--
91-- Thus, we want
94-- Thus, we want
92
95
93−type FontFaceName = String
96+type FontFaceName = Text
94
97
95type FontPitches = M.Map FontFaceName FontPitch
98type FontPitches = M.Map FontFaceName FontPitch
96
99
@@ -117,7 +120,7 @@ fontPitchReader = executeInSub NsOffice "font-face-decls" (
117 &&&
120 &&&
118 lookupDefaultingAttr NsStyle "font-pitch"
121 lookupDefaultingAttr NsStyle "font-pitch"
119 ))
122 ))
120− >>?^ ( M.fromList . foldl accumLegalPitches [] )
123+ >>?^ ( M.fromList . foldl' accumLegalPitches [] )
121 ) `ifFailedDo` returnV (Right M.empty)
124 ) `ifFailedDo` returnV (Right M.empty)
122 where accumLegalPitches ls (Nothing,_) = ls
125 where accumLegalPitches ls (Nothing,_) = ls
123 accumLegalPitches ls (Just n,p) = (n,p):ls
126 accumLegalPitches ls (Just n,p) = (n,p):ls
@@ -151,7 +154,7 @@ findPitch = ( lookupAttr NsStyle "font-pitch"
151-- Definitions of main data
154-- Definitions of main data
152--------------------------------------------------------------------------------
155--------------------------------------------------------------------------------
153
156
154−type StyleName = String
157+type StyleName = Text
155
158
156-- | There are two types of styles: named styles with a style family and an
159-- | There are two types of styles: named styles with a style family and an
157-- optional style parent, and default styles for each style family,
160-- optional style parent, and default styles for each style family,
@@ -355,8 +358,8 @@ getListLevelStyle level ListStyle{..} =
355 -- \^ simpler, but in general less efficient
358 -- \^ simpler, but in general less efficient
356
359
357data ListLevelStyle = ListLevelStyle { listLevelType :: ListLevelType
360data ListLevelStyle = ListLevelStyle { listLevelType :: ListLevelType
358− , listItemPrefix :: Maybe String
361+ , listItemPrefix :: Maybe Text
359− , listItemSuffix :: Maybe String
362+ , listItemSuffix :: Maybe Text
360 , listItemFormat :: ListItemNumberFormat
363 , listItemFormat :: ListItemNumberFormat
361 , listItemStart :: Int
364 , listItemStart :: Int
362 }
365 }
@@ -366,9 +369,9 @@ instance Show ListLevelStyle where
366 show ListLevelStyle{..} = "<LLS|"
369 show ListLevelStyle{..} = "<LLS|"
367 ++ show listLevelType
370 ++ show listLevelType
368 ++ "|"
371 ++ "|"
369− ++ maybeToString listItemPrefix
372+ ++ maybeToString (T.unpack <$> listItemPrefix)
370 ++ show listItemFormat
373 ++ show listItemFormat
371− ++ maybeToString listItemSuffix
374+ ++ maybeToString (T.unpack <$> listItemSuffix)
372 ++ ">"
375 ++ ">"
373 where maybeToString = fromMaybe ""
376 where maybeToString = fromMaybe ""
374
377
@@ -471,7 +474,7 @@ readTextProperties =
471 )
474 )
472 where isFontEmphasised = [("normal",False),("italic",True),("oblique",True)]
475 where isFontEmphasised = [("normal",False),("italic",True),("oblique",True)]
473 isFontBold = ("normal",False):("bold",True)
476 isFontBold = ("normal",False):("bold",True)
474− :map ((,True).show) ([100,200..900]::[Int])
477+ :map ((,True) . tshow) ([100,200..900]::[Int])
475
478
476readUnderlineMode :: StyleReaderSafe _x (Maybe UnderlineMode)
479readUnderlineMode :: StyleReaderSafe _x (Maybe UnderlineMode)
477readUnderlineMode = readLineMode "text-underline-mode"
480readUnderlineMode = readLineMode "text-underline-mode"
@@ -481,7 +484,7 @@ readStrikeThroughMode :: StyleReaderSafe _x (Maybe UnderlineMode)
481readStrikeThroughMode = readLineMode "text-line-through-mode"
484readStrikeThroughMode = readLineMode "text-line-through-mode"
482 "text-line-through-style"
485 "text-line-through-style"
483
486
484−readLineMode :: String -> String -> StyleReaderSafe _x (Maybe UnderlineMode)
487+readLineMode :: Text -> Text -> StyleReaderSafe _x (Maybe UnderlineMode)
485readLineMode modeAttr styleAttr = proc x -> do
488readLineMode modeAttr styleAttr = proc x -> do
486 isUL <- searchAttr NsStyle styleAttr False isLinePresent -< x
489 isUL <- searchAttr NsStyle styleAttr False isLinePresent -< x
487 mode <- lookupAttr' NsStyle modeAttr -< x
490 mode <- lookupAttr' NsStyle modeAttr -< x
@@ -560,12 +563,13 @@ readListLevelStyle levelType = readAttr NsText "level"
560
563
561--
564--
562chooseMostSpecificListLevelStyle :: S.Set ListLevelStyle -> Maybe ListLevelStyle
565chooseMostSpecificListLevelStyle :: S.Set ListLevelStyle -> Maybe ListLevelStyle
563−chooseMostSpecificListLevelStyle ls | ls == mempty = Nothing
566+chooseMostSpecificListLevelStyle ls = F.foldr select Nothing ls
564− | otherwise = Just ( F.foldr1 select ls )
565 where
567 where
568+ select l Nothing = Just l
566− select ( ListLevelStyle t1 p1 s1 f1 b1 )
569+ select ( ListLevelStyle t1 p1 s1 f1 b1 )
567− ( ListLevelStyle t2 p2 s2 f2 _ )
570+ ( Just ( ListLevelStyle t2 p2 s2 f2 _ ))
568− = ListLevelStyle (select' t1 t2) (p1 <|> p2) (s1 <|> s2) (selectLinf f1 f2) b1
571+ = Just $ ListLevelStyle (select' t1 t2) (p1 <|> p2) (s1 <|> s2)
572+ (selectLinf f1 f2) b1
569 select' LltNumbered _ = LltNumbered
573 select' LltNumbered _ = LltNumbered
570 select' _ LltNumbered = LltNumbered
574 select' _ LltNumbered = LltNumbered
571 select' _ _ = LltBullet
575 select' _ _ = LltBullet
src/Text/Pandoc/Readers/Org.hs+4−7
@@ -18,22 +18,19 @@ import Text.Pandoc.Class.PandocMonad (PandocMonad)
18import Text.Pandoc.Definition
18import Text.Pandoc.Definition
19import Text.Pandoc.Options
19import Text.Pandoc.Options
20import Text.Pandoc.Parsing (reportLogMessages)
20import Text.Pandoc.Parsing (reportLogMessages)
21−import Text.Pandoc.Shared (crFilter)
21+import Text.Pandoc.Sources (ToSources(..), ensureFinalNewlines)
22−
23import Control.Monad.Except (throwError)
22import Control.Monad.Except (throwError)
24import Control.Monad.Reader (runReaderT)
23import Control.Monad.Reader (runReaderT)
25
24
26−import Data.Text (Text)
27−
28-- | Parse org-mode string and return a Pandoc document.
25-- | Parse org-mode string and return a Pandoc document.
29−readOrg :: PandocMonad m
26+readOrg :: (PandocMonad m, ToSources a)
30 => ReaderOptions -- ^ Reader options
27 => ReaderOptions -- ^ Reader options
31− -> Text -- ^ String to parse (assuming @'\n'@ line endings)
28+ -> a
32 -> m Pandoc
29 -> m Pandoc
33readOrg opts s = do
30readOrg opts s = do
34 parsed <- flip runReaderT def $
31 parsed <- flip runReaderT def $
35 readWithM parseOrg (optionsToParserState opts)
32 readWithM parseOrg (optionsToParserState opts)
36− (crFilter s <> "\n\n")
33+ (ensureFinalNewlines 2 (toSources s))
37 case parsed of
34 case parsed of
38 Right result -> return result
35 Right result -> return result
39 Left e -> throwError e
36 Left e -> throwError e
src/Text/Pandoc/Readers/Org/Blocks.hs+46−6
@@ -1,6 +1,7 @@
1{-# LANGUAGE FlexibleContexts #-}
1{-# LANGUAGE FlexibleContexts #-}
2−{-# LANGUAGE RecordWildCards #-}
2+{-# LANGUAGE LambdaCase #-}
3{-# LANGUAGE OverloadedStrings #-}
3{-# LANGUAGE OverloadedStrings #-}
4+{-# LANGUAGE RecordWildCards #-}
4{- |
5{- |
5 Module : Text.Pandoc.Readers.Org.Blocks
6 Module : Text.Pandoc.Readers.Org.Blocks
6 Copyright : Copyright (C) 2014-2021 Albert Krewinkel
7 Copyright : Copyright (C) 2014-2021 Albert Krewinkel
@@ -38,10 +39,12 @@ import Data.Functor (($>))
38import Data.List (foldl', intersperse)
39import Data.List (foldl', intersperse)
39import Data.Maybe (fromMaybe, isJust, isNothing)
40import Data.Maybe (fromMaybe, isJust, isNothing)
40import Data.Text (Text)
41import Data.Text (Text)
41−
42+import Data.List.NonEmpty (nonEmpty)
43+import System.FilePath
42import qualified Data.Text as T
44import qualified Data.Text as T
43import qualified Text.Pandoc.Builder as B
45import qualified Text.Pandoc.Builder as B
44import qualified Text.Pandoc.Walk as Walk
46import qualified Text.Pandoc.Walk as Walk
47+import Text.Pandoc.Sources (ToSources(..))
45
48
46--
49--
47-- parsing blocks
50-- parsing blocks
@@ -525,7 +528,9 @@ include = try $ do
525 _ -> nullAttr
528 _ -> nullAttr
526 return $ pure . B.codeBlockWith attr <$> parseRaw
529 return $ pure . B.codeBlockWith attr <$> parseRaw
527 _ -> return $ return . B.fromList . blockFilter params <$> blockList
530 _ -> return $ return . B.fromList . blockFilter params <$> blockList
528− insertIncludedFileF blocksParser ["."] filename
531+ currentDir <- takeDirectory . sourceName <$> getPosition
532+ insertIncludedFile blocksParser toSources
533+ [currentDir] filename Nothing Nothing
529 where
534 where
530 includeTarget :: PandocMonad m => OrgParser m FilePath
535 includeTarget :: PandocMonad m => OrgParser m FilePath
531 includeTarget = do
536 includeTarget = do
@@ -541,8 +546,7 @@ include = try $ do
541 in case (minlvl >>= safeRead :: Maybe Int) of
546 in case (minlvl >>= safeRead :: Maybe Int) of
542 Nothing -> blks
547 Nothing -> blks
543 Just lvl -> let levels = Walk.query headerLevel blks
548 Just lvl -> let levels = Walk.query headerLevel blks
544− -- CAVE: partial function in else
545− curMin = if null levels then 0 else minimum levels
549+ curMin = maybe 0 minimum $ nonEmpty levels
546 in Walk.walk (shiftHeader (curMin - lvl)) blks
550 in Walk.walk (shiftHeader (curMin - lvl)) blks
547
551
548 headerLevel :: Block -> [Int]
552 headerLevel :: Block -> [Int]
@@ -850,16 +854,52 @@ definitionListItem parseIndentedMarker = try $ do
850 definitionMarker =
854 definitionMarker =
851 spaceChar *> string "::" <* (spaceChar <|> lookAhead newline)
855 spaceChar *> string "::" <* (spaceChar <|> lookAhead newline)
852
856
857+-- | Checkbox for tasks.
858+data Checkbox
859+ = UncheckedBox
860+ | CheckedBox
861+ | SemicheckedBox
862+
863+-- | Parses a checkbox in a plain list.
864+checkbox :: PandocMonad m
865+ => OrgParser m Checkbox
866+checkbox = do
867+ guardEnabled Ext_task_lists
868+ try (char '[' *> status <* char ']') <?> "checkbox"
869+ where
870+ status = choice
871+ [ UncheckedBox <$ char ' '
872+ , CheckedBox <$ char 'X'
873+ , SemicheckedBox <$ char '-'
874+ ]
875+
876+checkboxToInlines :: Checkbox -> Inline
877+checkboxToInlines = B.Str . \case
878+ UncheckedBox -> "☐"
879+ SemicheckedBox -> "☐"
880+ CheckedBox -> "☒"
881+
853-- | parse raw text for one list item
882-- | parse raw text for one list item
854listItem :: PandocMonad m
883listItem :: PandocMonad m
855 => OrgParser m Int
884 => OrgParser m Int
856 -> OrgParser m (F Blocks)
885 -> OrgParser m (F Blocks)
857listItem parseIndentedMarker = try . withContext ListItemState $ do
886listItem parseIndentedMarker = try . withContext ListItemState $ do
858 markerLength <- try parseIndentedMarker
887 markerLength <- try parseIndentedMarker
888+ box <- optionMaybe checkbox
859 firstLine <- anyLineNewline
889 firstLine <- anyLineNewline
860 blank <- option "" ("\n" <$ blankline)
890 blank <- option "" ("\n" <$ blankline)
861 rest <- T.concat <$> many (listContinuation markerLength)
891 rest <- T.concat <$> many (listContinuation markerLength)
862− parseFromString blocks $ firstLine <> blank <> rest
892+ contents <- parseFromString blocks $ firstLine <> blank <> rest
893+ return (maybe id (prependInlines . checkboxToInlines) box <$> contents)
894+
895+-- | Prepend inlines to blocks, adding them to the first paragraph or
896+-- creating a new Plain element if necessary.
897+prependInlines :: Inline -> Blocks -> Blocks
898+prependInlines inlns = B.fromList . prepend . B.toList
899+ where
900+ prepend (Plain is : bs) = Plain (inlns : Space : is) : bs
901+ prepend (Para is : bs) = Para (inlns : Space : is) : bs
902+ prepend bs = Plain [inlns, Space] : bs
863
903
864-- continuation of a list item - indented and separated by blankline or endline.
904-- continuation of a list item - indented and separated by blankline or endline.
865-- Note: nested lists are parsed as continuations.
905-- Note: nested lists are parsed as continuations.
src/Text/Pandoc/Readers/Org/Inlines.hs+7−6
@@ -29,6 +29,7 @@ import Text.Pandoc.Definition
29import Text.Pandoc.Options
29import Text.Pandoc.Options
30import Text.Pandoc.Readers.LaTeX (inlineCommand, rawLaTeXInline)
30import Text.Pandoc.Readers.LaTeX (inlineCommand, rawLaTeXInline)
31import Text.TeXMath (DisplayType (..), readTeX, writePandoc)
31import Text.TeXMath (DisplayType (..), readTeX, writePandoc)
32+import Text.Pandoc.Sources (ToSources(..))
32import qualified Text.TeXMath.Readers.MathML.EntityMap as MathMLEntityMap
33import qualified Text.TeXMath.Readers.MathML.EntityMap as MathMLEntityMap
33
34
34import Control.Monad (guard, mplus, mzero, unless, void, when)
35import Control.Monad (guard, mplus, mzero, unless, void, when)
@@ -262,7 +263,7 @@ berkeleyCitationList = try $ do
262 where
263 where
263 citationListPart :: PandocMonad m => OrgParser m (F Inlines)
264 citationListPart :: PandocMonad m => OrgParser m (F Inlines)
264 citationListPart = fmap (trimInlinesF . mconcat) . try . many1 $ do
265 citationListPart = fmap (trimInlinesF . mconcat) . try . many1 $ do
265− notFollowedBy' citeKey
266+ notFollowedBy' $ citeKey False
266 notFollowedBy (oneOf ";]")
267 notFollowedBy (oneOf ";]")
267 inline
268 inline
268
269
@@ -277,7 +278,7 @@ berkeleyBareTag' = try $ void (string "cite")
277
278
278berkeleyTextualCite :: PandocMonad m => OrgParser m (F [Citation])
279berkeleyTextualCite :: PandocMonad m => OrgParser m (F [Citation])
279berkeleyTextualCite = try $ do
280berkeleyTextualCite = try $ do
280− (suppressAuthor, key) <- citeKey
281+ (suppressAuthor, key) <- citeKey False
281 returnF . return $ Citation
282 returnF . return $ Citation
282 { citationId = key
283 { citationId = key
283 , citationPrefix = mempty
284 , citationPrefix = mempty
@@ -322,7 +323,7 @@ linkLikeOrgRefCite = try $ do
322-- from the `org-ref-cite-re` variable in `org-ref.el`.
323-- from the `org-ref-cite-re` variable in `org-ref.el`.
323orgRefCiteKey :: PandocMonad m => OrgParser m Text
324orgRefCiteKey :: PandocMonad m => OrgParser m Text
324orgRefCiteKey =
325orgRefCiteKey =
325− let citeKeySpecialChars = "-_:\\./," :: String
326+ let citeKeySpecialChars = "-_:\\./" :: String
326 isCiteKeySpecialChar c = c `elem` citeKeySpecialChars
327 isCiteKeySpecialChar c = c `elem` citeKeySpecialChars
327 isCiteKeyChar c = isAlphaNum c || isCiteKeySpecialChar c
328 isCiteKeyChar c = isAlphaNum c || isCiteKeySpecialChar c
328 endOfCitation = try $ do
329 endOfCitation = try $ do
@@ -350,7 +351,7 @@ citeList = sequence <$> sepEndBy1 citation (try $ char ';' *> skipSpaces)
350citation :: PandocMonad m => OrgParser m (F Citation)
351citation :: PandocMonad m => OrgParser m (F Citation)
351citation = try $ do
352citation = try $ do
352 pref <- prefix
353 pref <- prefix
353− (suppress_author, key) <- citeKey
354+ (suppress_author, key) <- citeKey False
354 suff <- suffix
355 suff <- suffix
355 return $ do
356 return $ do
356 x <- pref
357 x <- pref
@@ -367,7 +368,7 @@ citation = try $ do
367 }
368 }
368 where
369 where
369 prefix = trimInlinesF . mconcat <$>
370 prefix = trimInlinesF . mconcat <$>
370− manyTill inline (char ']' <|> (']' <$ lookAhead citeKey))
371+ manyTill inline (char ']' <|> (']' <$ lookAhead (citeKey False)))
371 suffix = try $ do
372 suffix = try $ do
372 hasSpace <- option False (notFollowedBy nonspaceChar >> return True)
373 hasSpace <- option False (notFollowedBy nonspaceChar >> return True)
373 skipSpaces
374 skipSpaces
@@ -802,7 +803,7 @@ inlineLaTeX = try $ do
802 parseAsInlineLaTeX :: PandocMonad m
803 parseAsInlineLaTeX :: PandocMonad m
803 => Text -> TeXExport -> OrgParser m (Maybe Inlines)
804 => Text -> TeXExport -> OrgParser m (Maybe Inlines)
804 parseAsInlineLaTeX cs = \case
805 parseAsInlineLaTeX cs = \case
805− TeXExport -> maybeRight <$> runParserT inlineCommand state "" cs
806+ TeXExport -> maybeRight <$> runParserT inlineCommand state "" (toSources cs)
806 TeXIgnore -> return (Just mempty)
807 TeXIgnore -> return (Just mempty)
807 TeXVerbatim -> return (Just $ B.str cs)
808 TeXVerbatim -> return (Just $ B.str cs)
808
809
src/Text/Pandoc/Readers/Org/Parsing.hs+5−5
@@ -63,8 +63,7 @@ module Text.Pandoc.Readers.Org.Parsing
63 , ellipses
63 , ellipses
64 , citeKey
64 , citeKey
65 , gridTableWith
65 , gridTableWith
66− , insertIncludedFileF
66+ , insertIncludedFile
67− -- * Re-exports from Text.Pandoc.Parsec
68 , runParser
67 , runParser
69 , runParserT
68 , runParserT
70 , getInput
69 , getInput
@@ -100,21 +99,22 @@ module Text.Pandoc.Readers.Org.Parsing
100 , getState
99 , getState
101 , updateState
100 , updateState
102 , SourcePos
101 , SourcePos
102+ , sourceName
103 , getPosition
103 , getPosition
104 ) where
104 ) where
105
105
106import Data.Text (Text)
106import Data.Text (Text)
107import Text.Pandoc.Readers.Org.ParserState
107import Text.Pandoc.Readers.Org.ParserState
108
108
109−import Text.Pandoc.Parsing hiding (F, anyLine, blanklines, newline,
109+import Text.Pandoc.Parsing hiding (anyLine, blanklines, newline,
110− parseFromString)
110+ parseFromString)
111import qualified Text.Pandoc.Parsing as P
111import qualified Text.Pandoc.Parsing as P
112
112
113import Control.Monad (guard)
113import Control.Monad (guard)
114import Control.Monad.Reader (ReaderT)
114import Control.Monad.Reader (ReaderT)
115
115
116-- | The parser used to read org files.
116-- | The parser used to read org files.
117−type OrgParser m = ParserT Text OrgParserState (ReaderT OrgParserLocal m)
117+type OrgParser m = ParserT Sources OrgParserState (ReaderT OrgParserLocal m)
118
118
119--
119--
120-- Adaptions and specializations of parsing utilities
120-- Adaptions and specializations of parsing utilities
src/Text/Pandoc/Readers/RST.hs+84−115
@@ -27,8 +27,7 @@ import Data.Text (Text)
27import qualified Data.Text as T
27import qualified Data.Text as T
28import Text.Pandoc.Builder (Blocks, Inlines, fromList, setMeta, trimInlines)
28import Text.Pandoc.Builder (Blocks, Inlines, fromList, setMeta, trimInlines)
29import qualified Text.Pandoc.Builder as B
29import qualified Text.Pandoc.Builder as B
30−import Text.Pandoc.Class.PandocMonad (PandocMonad, fetchItem,
30+import Text.Pandoc.Class.PandocMonad (PandocMonad, fetchItem, getTimestamp)
31− readFileFromDirs, getCurrentTime)
32import Text.Pandoc.CSV (CSVOptions (..), defaultCSVOptions, parseCSV)
31import Text.Pandoc.CSV (CSVOptions (..), defaultCSVOptions, parseCSV)
33import Text.Pandoc.Definition
32import Text.Pandoc.Definition
34import Text.Pandoc.Error
33import Text.Pandoc.Error
@@ -38,25 +37,25 @@ import Text.Pandoc.Options
38import Text.Pandoc.Parsing
37import Text.Pandoc.Parsing
39import Text.Pandoc.Shared
38import Text.Pandoc.Shared
40import qualified Text.Pandoc.UTF8 as UTF8
39import qualified Text.Pandoc.UTF8 as UTF8
41−import Text.Printf (printf)
42import Data.Time.Format
40import Data.Time.Format
41+import System.FilePath (takeDirectory)
43
42
44-- TODO:
43-- TODO:
45-- [ ] .. parsed-literal
44-- [ ] .. parsed-literal
46
45
47-- | Parse reStructuredText string and return Pandoc document.
46-- | Parse reStructuredText string and return Pandoc document.
48−readRST :: PandocMonad m
47+readRST :: (PandocMonad m, ToSources a)
49 => ReaderOptions -- ^ Reader options
48 => ReaderOptions -- ^ Reader options
50− -> Text -- ^ Text to parse (assuming @'\n'@ line endings)
49+ -> a
51 -> m Pandoc
50 -> m Pandoc
52readRST opts s = do
51readRST opts s = do
53 parsed <- readWithM parseRST def{ stateOptions = opts }
52 parsed <- readWithM parseRST def{ stateOptions = opts }
54− (crFilter s <> "\n\n")
53+ (ensureFinalNewlines 2 (toSources s))
55 case parsed of
54 case parsed of
56 Right result -> return result
55 Right result -> return result
57 Left e -> throwError e
56 Left e -> throwError e
58
57
59−type RSTParser m = ParserT Text ParserState m
58+type RSTParser m = ParserT Sources ParserState m
60
59
61--
60--
62-- Constants and data structure definitions
61-- Constants and data structure definitions
@@ -151,11 +150,19 @@ parseRST = do
151 startPos <- getPosition
150 startPos <- getPosition
152 -- go through once just to get list of reference keys and notes
151 -- go through once just to get list of reference keys and notes
153 -- docMinusKeys is the raw document with blanks where the keys were...
152 -- docMinusKeys is the raw document with blanks where the keys were...
154− docMinusKeys <- T.concat <$>
155− manyTill (referenceKey <|> anchorDef <|>
153+ let chunk = referenceKey
154+ <|> anchorDef
155+ <|> noteBlock
156− noteBlock <|> citationBlock <|>
156+ <|> citationBlock
157− (snd <$> withRaw comment) <|>
157+ <|> (snd <$> withRaw comment)
158− headerBlock <|> lineClump) eof
158+ <|> headerBlock
159+ <|> lineClump
160+ docMinusKeys <- Sources <$>
161+ manyTill (do pos <- getPosition
162+ t <- chunk
163+ return (pos, t)) eof
164+ -- UGLY: we collapse source position information.
165+ -- TODO: fix the parser to use the F monad instead of two passes
159 setInput docMinusKeys
166 setInput docMinusKeys
160 setPosition startPos
167 setPosition startPos
161 st' <- getState
168 st' <- getState
@@ -348,7 +355,7 @@ singleHeader' = try $ do
348-- hrule block
355-- hrule block
349--
356--
350
357
351−hrule :: Monad m => ParserT Text st m Blocks
358+hrule :: Monad m => ParserT Sources st m Blocks
352hrule = try $ do
359hrule = try $ do
353 chr <- oneOf underlineChars
360 chr <- oneOf underlineChars
354 count 3 (char chr)
361 count 3 (char chr)
@@ -363,7 +370,7 @@ hrule = try $ do
363
370
364-- read a line indented by a given string
371-- read a line indented by a given string
365indentedLine :: (HasReaderOptions st, Monad m)
372indentedLine :: (HasReaderOptions st, Monad m)
366− => Int -> ParserT Text st m Text
373+ => Int -> ParserT Sources st m Text
367indentedLine indents = try $ do
374indentedLine indents = try $ do
368 lookAhead spaceChar
375 lookAhead spaceChar
369 gobbleAtMostSpaces indents
376 gobbleAtMostSpaces indents
@@ -372,7 +379,7 @@ indentedLine indents = try $ do
372-- one or more indented lines, possibly separated by blank lines.
379-- one or more indented lines, possibly separated by blank lines.
373-- any amount of indentation will work.
380-- any amount of indentation will work.
374indentedBlock :: (HasReaderOptions st, Monad m)
381indentedBlock :: (HasReaderOptions st, Monad m)
375− => ParserT Text st m Text
382+ => ParserT Sources st m Text
376indentedBlock = try $ do
383indentedBlock = try $ do
377 indents <- length <$> lookAhead (many1 spaceChar)
384 indents <- length <$> lookAhead (many1 spaceChar)
378 lns <- many1 $ try $ do b <- option "" blanklines
385 lns <- many1 $ try $ do b <- option "" blanklines
@@ -381,20 +388,20 @@ indentedBlock = try $ do
381 optional blanklines
388 optional blanklines
382 return $ T.unlines lns
389 return $ T.unlines lns
383
390
384−quotedBlock :: Monad m => ParserT Text st m Text
391+quotedBlock :: Monad m => ParserT Sources st m Text
385quotedBlock = try $ do
392quotedBlock = try $ do
386 quote <- lookAhead $ oneOf "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
393 quote <- lookAhead $ oneOf "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
387 lns <- many1 $ lookAhead (char quote) >> anyLine
394 lns <- many1 $ lookAhead (char quote) >> anyLine
388 optional blanklines
395 optional blanklines
389 return $ T.unlines lns
396 return $ T.unlines lns
390
397
391−codeBlockStart :: Monad m => ParserT Text st m Char
398+codeBlockStart :: Monad m => ParserT Sources st m Char
392codeBlockStart = string "::" >> blankline >> blankline
399codeBlockStart = string "::" >> blankline >> blankline
393
400
394−codeBlock :: Monad m => ParserT Text ParserState m Blocks
401+codeBlock :: Monad m => ParserT Sources ParserState m Blocks
395codeBlock = try $ codeBlockStart >> codeBlockBody
402codeBlock = try $ codeBlockStart >> codeBlockBody
396
403
397−codeBlockBody :: Monad m => ParserT Text ParserState m Blocks
404+codeBlockBody :: Monad m => ParserT Sources ParserState m Blocks
398codeBlockBody = do
405codeBlockBody = do
399 lang <- stateRstHighlight <$> getState
406 lang <- stateRstHighlight <$> getState
400 try $ B.codeBlockWith ("", maybeToList lang, []) . stripTrailingNewlines <$>
407 try $ B.codeBlockWith ("", maybeToList lang, []) . stripTrailingNewlines <$>
@@ -410,14 +417,14 @@ lhsCodeBlock = try $ do
410 return $ B.codeBlockWith ("", ["haskell","literate"], [])
417 return $ B.codeBlockWith ("", ["haskell","literate"], [])
411 $ T.intercalate "\n" lns
418 $ T.intercalate "\n" lns
412
419
413−latexCodeBlock :: Monad m => ParserT Text st m [Text]
420+latexCodeBlock :: Monad m => ParserT Sources st m [Text]
414latexCodeBlock = try $ do
421latexCodeBlock = try $ do
415 try (latexBlockLine "\\begin{code}")
422 try (latexBlockLine "\\begin{code}")
416 many1Till anyLine (try $ latexBlockLine "\\end{code}")
423 many1Till anyLine (try $ latexBlockLine "\\end{code}")
417 where
424 where
418 latexBlockLine s = skipMany spaceChar >> string s >> blankline
425 latexBlockLine s = skipMany spaceChar >> string s >> blankline
419
426
420−birdCodeBlock :: Monad m => ParserT Text st m [Text]
427+birdCodeBlock :: Monad m => ParserT Sources st m [Text]
421birdCodeBlock = filterSpace <$> many1 birdTrackLine
428birdCodeBlock = filterSpace <$> many1 birdTrackLine
422 where filterSpace lns =
429 where filterSpace lns =
423 -- if (as is normal) there is always a space after >, drop it
430 -- if (as is normal) there is always a space after >, drop it
@@ -425,7 +432,7 @@ birdCodeBlock = filterSpace <$> many1 birdTrackLine
425 then map (T.drop 1) lns
432 then map (T.drop 1) lns
426 else lns
433 else lns
427
434
428−birdTrackLine :: Monad m => ParserT Text st m Text
435+birdTrackLine :: Monad m => ParserT Sources st m Text
429birdTrackLine = char '>' >> anyLine
436birdTrackLine = char '>' >> anyLine
430
437
431--
438--
@@ -446,64 +453,39 @@ encoding
446-}
453-}
447
454
448includeDirective :: PandocMonad m
455includeDirective :: PandocMonad m
456+ => Text
449− => Text -> [(Text, Text)] -> Text
457+ -> [(Text, Text)]
458+ -> Text
450 -> RSTParser m Blocks
459 -> RSTParser m Blocks
451includeDirective top fields body = do
460includeDirective top fields body = do
452− let f = trim top
461+ let f = T.unpack $ trim top
453− guard $ not (T.null f)
462+ guard $ not $ null f
454 guard $ T.null (trim body)
463 guard $ T.null (trim body)
455− -- options
456− let (startLine :: Maybe Int) = lookup "start-line" fields >>= safeRead
464+ let startLine = lookup "start-line" fields >>= safeRead
457− let (endLine :: Maybe Int) = lookup "end-line" fields >>= safeRead
465+ let endLine = lookup "end-line" fields >>= safeRead
458− oldPos <- getPosition
459− oldInput <- getInput
460− containers <- stateContainers <$> getState
461− when (f `elem` containers) $
462− throwError $ PandocParseError $ "Include file loop at " <> tshow oldPos
463− updateState $ \s -> s{ stateContainers = f : stateContainers s }
464− mbContents <- readFileFromDirs ["."] $ T.unpack f
465− contentLines <- case mbContents of
466− Just s -> return $ T.lines s
467− Nothing -> do
468− logMessage $ CouldNotLoadIncludeFile f oldPos
469− return []
470− let numLines = length contentLines
471− let startLine' = case startLine of
472− Nothing -> 1
473− Just x | x >= 0 -> x
474− | otherwise -> numLines + x -- negative from end
475− let endLine' = case endLine of
476− Nothing -> numLines + 1
477− Just x | x >= 0 -> x
478− | otherwise -> numLines + x -- negative from end
479− let contentLines' = drop (startLine' - 1)
480− $ take (endLine' - 1) contentLines
481− let contentLines'' = (case trim <$> lookup "end-before" fields of
482− Just patt -> takeWhile (not . (patt `T.isInfixOf`))
483− Nothing -> id) .
484− (case trim <$> lookup "start-after" fields of
485− Just patt -> drop 1 .
486− dropWhile (not . (patt `T.isInfixOf`))
487− Nothing -> id) $ contentLines'
488− let contents' = T.unlines contentLines''
489− case lookup "code" fields of
490− Just lang -> do
491− let classes = maybe [] T.words (lookup "class" fields)
466+ let classes = maybe [] T.words (lookup "class" fields)
492− let ident = maybe "" trimr $ lookup "name" fields
467+ let ident = maybe "" trimr $ lookup "name" fields
468+ let parser =
469+ case lookup "code" fields of
470+ Just lang ->
493− codeblock ident classes fields (trimr lang) contents' False
471+ (codeblock ident classes fields (trimr lang) False
472+ . sourcesToText) <$> getInput
473+ Nothing ->
494− Nothing -> case lookup "literal" fields of
474+ case lookup "literal" fields of
495− Just _ -> return $ B.rawBlock "rst" contents'
475+ Just _ -> B.rawBlock "rst" . sourcesToText <$> getInput
496− Nothing -> do
476+ Nothing -> parseBlocks
497− setPosition $ newPos (T.unpack f) 1 1
477+ let selectLines =
498− setInput $ contents' <> "\n"
478+ (case trim <$> lookup "end-before" fields of
499− bs <- optional blanklines >>
479+ Just patt -> takeWhile (not . (patt `T.isInfixOf`))
500− (mconcat <$> many block)
480+ Nothing -> id) .
501− setInput oldInput
481+ (case trim <$> lookup "start-after" fields of
502− setPosition oldPos
482+ Just patt -> drop 1 .
503− updateState $ \s -> s{ stateContainers =
483+ dropWhile (not . (patt `T.isInfixOf`))
504− tail $ stateContainers s }
484+ Nothing -> id)
505− return bs
485+ let toStream t =
506−
486+ toSources [(f, T.unlines . selectLines . T.lines $ t)]
487+ currentDir <- takeDirectory . sourceName <$> getPosition
488+ insertIncludedFile parser toStream [currentDir] f startLine endLine
507
489
508--
490--
509-- list blocks
491-- list blocks
@@ -526,7 +508,7 @@ definitionList :: PandocMonad m => RSTParser m Blocks
526definitionList = B.definitionList <$> many1 definitionListItem
508definitionList = B.definitionList <$> many1 definitionListItem
527
509
528-- parses bullet list start and returns its length (inc. following whitespace)
510-- parses bullet list start and returns its length (inc. following whitespace)
529−bulletListStart :: Monad m => ParserT Text st m Int
511+bulletListStart :: Monad m => ParserT Sources st m Int
530bulletListStart = try $ do
512bulletListStart = try $ do
531 notFollowedBy' hrule -- because hrules start out just like lists
513 notFollowedBy' hrule -- because hrules start out just like lists
532 marker <- oneOf bulletListMarkers
514 marker <- oneOf bulletListMarkers
@@ -613,8 +595,9 @@ comment = try $ do
613 string ".."
595 string ".."
614 skipMany1 spaceChar <|> (() <$ lookAhead newline)
596 skipMany1 spaceChar <|> (() <$ lookAhead newline)
615 -- notFollowedBy' directiveLabel -- comment comes after directive so unnec.
597 -- notFollowedBy' directiveLabel -- comment comes after directive so unnec.
616− manyTill anyChar blanklines
598+ _ <- anyLine
617 optional indentedBlock
599 optional indentedBlock
600+ optional blanklines
618 return mempty
601 return mempty
619
602
620directiveLabel :: Monad m => RSTParser m Text
603directiveLabel :: Monad m => RSTParser m Text
@@ -685,7 +668,7 @@ directive' = do
685 "replace" -> B.para <$> -- consumed by substKey
668 "replace" -> B.para <$> -- consumed by substKey
686 parseInlineFromText (trim top)
669 parseInlineFromText (trim top)
687 "date" -> B.para <$> do -- consumed by substKey
670 "date" -> B.para <$> do -- consumed by substKey
688− t <- getCurrentTime
671+ t <- getTimestamp
689 let format = case T.unpack (T.strip top) of
672 let format = case T.unpack (T.strip top) of
690 [] -> "%Y-%m-%d"
673 [] -> "%Y-%m-%d"
691 x -> x
674 x -> x
@@ -731,8 +714,8 @@ directive' = do
731 "" -> stateRstHighlight def
714 "" -> stateRstHighlight def
732 lang -> Just lang })
715 lang -> Just lang })
733 x | x == "code" || x == "code-block" || x == "sourcecode" ->
716 x | x == "code" || x == "code-block" || x == "sourcecode" ->
734− codeblock name classes (map (second trimr) fields)
717+ return $ codeblock name classes (map (second trimr) fields)
735− (trim top) body True
718+ (trim top) True body
736 "aafig" -> do
719 "aafig" -> do
737 let attribs = (name, ["aafig"], map (second trimr) fields)
720 let attribs = (name, ["aafig"], map (second trimr) fields)
738 return $ B.codeBlockWith attribs $ stripTrailingNewlines body
721 return $ B.codeBlockWith attribs $ stripTrailingNewlines body
@@ -877,10 +860,11 @@ csvTableDirective top fields rawcsv = do
877 (bs, _) <- fetchItem u
860 (bs, _) <- fetchItem u
878 return $ UTF8.toText bs
861 return $ UTF8.toText bs
879 Nothing -> return rawcsv
862 Nothing -> return rawcsv
880− let res = parseCSV opts (case explicitHeader of
863+ let header' = case explicitHeader of
881− Just h -> h <> "\n" <> rawcsv'
864+ Just h -> parseCSV defaultCSVOptions h
882− Nothing -> rawcsv')
865+ Nothing -> Right []
866+ let res = parseCSV opts rawcsv'
883− case res of
867+ case (<>) <$> header' <*> res of
884 Left e ->
868 Left e ->
885 throwError $ PandocParsecError "csv table" e
869 throwError $ PandocParsecError "csv table" e
886 Right rawrows -> do
870 Right rawrows -> do
@@ -1017,10 +1001,10 @@ toChunks = dropWhile T.null
1017 then "\\begin{aligned}\n" <> s <> "\n\\end{aligned}"
1001 then "\\begin{aligned}\n" <> s <> "\n\\end{aligned}"
1018 else s
1002 else s
1019
1003
1020−codeblock :: Text -> [Text] -> [(Text, Text)] -> Text -> Text -> Bool
1004+codeblock :: Text -> [Text] -> [(Text, Text)] -> Text -> Bool -> Text
1021− -> RSTParser m Blocks
1005+ -> Blocks
1022−codeblock ident classes fields lang body rmTrailingNewlines =
1006+codeblock ident classes fields lang rmTrailingNewlines body =
1023− return $ B.codeBlockWith attribs $ stripTrailingNewlines' body
1007+ B.codeBlockWith attribs $ stripTrailingNewlines' body
1024 where stripTrailingNewlines' = if rmTrailingNewlines
1008 where stripTrailingNewlines' = if rmTrailingNewlines
1025 then stripTrailingNewlines
1009 then stripTrailingNewlines
1026 else id
1010 else id
@@ -1101,7 +1085,7 @@ quotedReferenceName = try $ do
1101-- plus isolated (no two adjacent) internal hyphens, underscores,
1085-- plus isolated (no two adjacent) internal hyphens, underscores,
1102-- periods, colons and plus signs; no whitespace or other characters
1086-- periods, colons and plus signs; no whitespace or other characters
1103-- are allowed.
1087-- are allowed.
1104−simpleReferenceName :: Monad m => ParserT Text st m Text
1088+simpleReferenceName :: Monad m => ParserT Sources st m Text
1105simpleReferenceName = do
1089simpleReferenceName = do
1106 x <- alphaNum
1090 x <- alphaNum
1107 xs <- many $ alphaNum
1091 xs <- many $ alphaNum
@@ -1120,7 +1104,7 @@ referenceKey = do
1120 -- return enough blanks to replace key
1104 -- return enough blanks to replace key
1121 return $ T.replicate (sourceLine endPos - sourceLine startPos) "\n"
1105 return $ T.replicate (sourceLine endPos - sourceLine startPos) "\n"
1122
1106
1123−targetURI :: Monad m => ParserT Text st m Text
1107+targetURI :: Monad m => ParserT Sources st m Text
1124targetURI = do
1108targetURI = do
1125 skipSpaces
1109 skipSpaces
1126 optional $ try $ newline >> notFollowedBy blankline
1110 optional $ try $ newline >> notFollowedBy blankline
@@ -1158,8 +1142,10 @@ anonymousKey :: Monad m => RSTParser m ()
1158anonymousKey = try $ do
1142anonymousKey = try $ do
1159 oneOfStrings [".. __:", "__"]
1143 oneOfStrings [".. __:", "__"]
1160 src <- targetURI
1144 src <- targetURI
1161− pos <- getPosition
1145+ -- we need to ensure that the keys are ordered by occurrence in
1146+ -- the document.
1147+ numKeys <- M.size . stateKeys <$> getState
1162− let key = toKey $ "_" <> T.pack (printf "%09d" (sourceLine pos))
1148+ let key = toKey $ "_" <> T.pack (show numKeys)
1163 updateState $ \s -> s { stateKeys = M.insert key ((src,""), nullAttr) $
1149 updateState $ \s -> s { stateKeys = M.insert key ((src,""), nullAttr) $
1164 stateKeys s }
1150 stateKeys s }
1165
1151
@@ -1248,13 +1234,13 @@ headerBlock = do
1248-- Grid tables TODO:
1234-- Grid tables TODO:
1249-- - column spans
1235-- - column spans
1250
1236
1251−dashedLine :: Monad m => Char -> ParserT Text st m (Int, Int)
1237+dashedLine :: Monad m => Char -> ParserT Sources st m (Int, Int)
1252dashedLine ch = do
1238dashedLine ch = do
1253 dashes <- many1 (char ch)
1239 dashes <- many1 (char ch)
1254 sp <- many (char ' ')
1240 sp <- many (char ' ')
1255 return (length dashes, length $ dashes ++ sp)
1241 return (length dashes, length $ dashes ++ sp)
1256
1242
1257−simpleDashedLines :: Monad m => Char -> ParserT Text st m [(Int,Int)]
1243+simpleDashedLines :: Monad m => Char -> ParserT Sources st m [(Int,Int)]
1258simpleDashedLines ch = try $ many1 (dashedLine ch)
1244simpleDashedLines ch = try $ many1 (dashedLine ch)
1259
1245
1260-- Parse a table row separator
1246-- Parse a table row separator
@@ -1380,7 +1366,7 @@ hyphens = do
1380 -- don't want to treat endline after hyphen or dash as a space
1366 -- don't want to treat endline after hyphen or dash as a space
1381 return $ B.str result
1367 return $ B.str result
1382
1368
1383−escapedChar :: Monad m => ParserT Text st m Inlines
1369+escapedChar :: Monad m => ParserT Sources st m Inlines
1384escapedChar = do c <- escaped anyChar
1370escapedChar = do c <- escaped anyChar
1385 return $ if c == ' ' || c == '\n' || c == '\r'
1371 return $ if c == ' ' || c == '\n' || c == '\r'
1386 -- '\ ' is null in RST
1372 -- '\ ' is null in RST
@@ -1656,21 +1642,4 @@ note = try $ do
1656 return $ B.note contents
1642 return $ B.note contents
1657
1643
1658smart :: PandocMonad m => RSTParser m Inlines
1644smart :: PandocMonad m => RSTParser m Inlines
1659−smart = do
1645+smart = smartPunctuation inline
1660− guardEnabled Ext_smart
1661− doubleQuoted <|> singleQuoted <|>
1662− choice [apostrophe, dash, ellipses]
1663−
1664−singleQuoted :: PandocMonad m => RSTParser m Inlines
1665−singleQuoted = try $ do
1666− singleQuoteStart
1667− withQuoteContext InSingleQuote $
1668− B.singleQuoted . trimInlines . mconcat <$>
1669− many1Till inline singleQuoteEnd
1670−
1671−doubleQuoted :: PandocMonad m => RSTParser m Inlines
1672−doubleQuoted = try $ do
1673− doubleQuoteStart
1674− withQuoteContext InDoubleQuote $
1675− B.doubleQuoted . trimInlines . mconcat <$>
1676− many1Till inline doubleQuoteEnd
src/Text/Pandoc/Readers/Roff.hs+16−18

This diff is too big to show here.

src/Text/Pandoc/Readers/TWiki.hs+7−27

This diff is too big to show here.

src/Text/Pandoc/Readers/Textile.hs+95−89

This diff is too big to show here.

src/Text/Pandoc/Readers/TikiWiki.hs+6−6

This diff is too big to show here.

src/Text/Pandoc/Readers/Txt2Tags.hs+47−21

This diff is too big to show here.

src/Text/Pandoc/Readers/Vimwiki.hs+12−7

This diff is too big to show here.

src/Text/Pandoc/SelfContained.hs+4−7

This diff is too big to show here.

src/Text/Pandoc/Shared.hs+51−100

This diff is too big to show here.

src/Text/Pandoc/Sources.hsnew file+195

This diff is too big to show here.

src/Text/Pandoc/Templates.hs+4−2

This diff is too big to show here.

src/Text/Pandoc/UTF8.hs+20−23

This diff is too big to show here.

src/Text/Pandoc/Writers.hs+3−3

This diff is too big to show here.

src/Text/Pandoc/Writers/AsciiDoc.hs+10−4

This diff is too big to show here.

src/Text/Pandoc/Writers/BibTeX.hs+17−4

This diff is too big to show here.

src/Text/Pandoc/Writers/ConTeXt.hs+44−38

This diff is too big to show here.

src/Text/Pandoc/Writers/CslJson.hs+9−8

This diff is too big to show here.

src/Text/Pandoc/Writers/Docbook.hs+50−7

This diff is too big to show here.

src/Text/Pandoc/Writers/Docx.hs+176−363

This diff is too big to show here.

src/Text/Pandoc/Writers/Docx/Table.hsnew file+227

This diff is too big to show here.

src/Text/Pandoc/Writers/Docx/Types.hsnew file+185

This diff is too big to show here.

src/Text/Pandoc/Writers/DokuWiki.hs+3−1

This diff is too big to show here.

src/Text/Pandoc/Writers/EPUB.hs+223−209

This diff is too big to show here.

src/Text/Pandoc/Writers/FB2.hs+60−76

This diff is too big to show here.

src/Text/Pandoc/Writers/GridTable.hsnew file+157

This diff is too big to show here.

src/Text/Pandoc/Writers/HTML.hs+107−34

This diff is too big to show here.

src/Text/Pandoc/Writers/Haddock.hs+13−2

This diff is too big to show here.

src/Text/Pandoc/Writers/JATS.hs+67−44

This diff is too big to show here.

src/Text/Pandoc/Writers/JATS/References.hs+8−4

This diff is too big to show here.

src/Text/Pandoc/Writers/JATS/Table.hs+25−5

This diff is too big to show here.

src/Text/Pandoc/Writers/JATS/Types.hs+12−3

This diff is too big to show here.

src/Text/Pandoc/Writers/Jira.hs+41−16

This diff is too big to show here.

src/Text/Pandoc/Writers/LaTeX.hs+53−565

This diff is too big to show here.

src/Text/Pandoc/Writers/LaTeX/Caption.hs−1

This diff is too big to show here.

src/Text/Pandoc/Writers/LaTeX/Citation.hsnew file+181

This diff is too big to show here.

src/Text/Pandoc/Writers/LaTeX/Lang.hsnew file+192

This diff is too big to show here.

src/Text/Pandoc/Writers/LaTeX/Table.hs+21−8

This diff is too big to show here.

src/Text/Pandoc/Writers/LaTeX/Types.hs+2−1

This diff is too big to show here.

src/Text/Pandoc/Writers/LaTeX/Util.hsnew file+275

This diff is too big to show here.

src/Text/Pandoc/Writers/Man.hs+2−2

This diff is too big to show here.

src/Text/Pandoc/Writers/Markdown.hs+60−643

This diff is too big to show here.

src/Text/Pandoc/Writers/Markdown/Inline.hsnew file+616

This diff is too big to show here.

src/Text/Pandoc/Writers/Markdown/Types.hsnew file+81

This diff is too big to show here.

src/Text/Pandoc/Writers/Ms.hs+24−8

This diff is too big to show here.

src/Text/Pandoc/Writers/Muse.hs+6−3

This diff is too big to show here.

src/Text/Pandoc/Writers/ODT.hs+27−21

This diff is too big to show here.

src/Text/Pandoc/Writers/OOXML.hs+21−21

This diff is too big to show here.

src/Text/Pandoc/Writers/OpenDocument.hs+50−54

This diff is too big to show here.

src/Text/Pandoc/Writers/Org.hs+37−17

This diff is too big to show here.

src/Text/Pandoc/Writers/Powerpoint.hs+1−1

This diff is too big to show here.

src/Text/Pandoc/Writers/Powerpoint/Output.hs+148−129

This diff is too big to show here.

src/Text/Pandoc/Writers/Powerpoint/Presentation.hs+2−3

This diff is too big to show here.

src/Text/Pandoc/Writers/RST.hs+14−10

This diff is too big to show here.

src/Text/Pandoc/Writers/RTF.hs+16−10

This diff is too big to show here.

src/Text/Pandoc/Writers/Shared.hs+18−3

This diff is too big to show here.

src/Text/Pandoc/Writers/TEI.hs+4−3

This diff is too big to show here.

src/Text/Pandoc/Writers/Texinfo.hs+22−15

This diff is too big to show here.

src/Text/Pandoc/Writers/ZimWiki.hs+4−2

This diff is too big to show here.

src/Text/Pandoc/XML.hs+41−19

This diff is too big to show here.

src/Text/Pandoc/XML/Light.hsnew file+89

This diff is too big to show here.

src/Text/Pandoc/XML/Light/Output.hsnew file+234

This diff is too big to show here.

src/Text/Pandoc/XML/Light/Proc.hsnew file+139

This diff is too big to show here.

src/Text/Pandoc/XML/Light/Types.hsnew file+193

This diff is too big to show here.

stack.yaml+11−18

This diff is too big to show here.

test/Tests/Command.hs+33−38

This diff is too big to show here.

test/Tests/Helpers.hs+24−35

This diff is too big to show here.

test/Tests/Lua.hs−2

This diff is too big to show here.

test/Tests/Lua/Module.hs+2

This diff is too big to show here.

test/Tests/Old.hs+11−25

This diff is too big to show here.

test/Tests/Readers/Creole.hs−2

This diff is too big to show here.

test/Tests/Readers/Docx.hs+25−7

This diff is too big to show here.

test/Tests/Readers/DokuWiki.hs−2

This diff is too big to show here.

test/Tests/Readers/EPUB.hs+1−3

This diff is too big to show here.

test/Tests/Readers/FB2.hs−2

This diff is too big to show here.

test/Tests/Readers/HTML.hs+20−7

This diff is too big to show here.

test/Tests/Readers/JATS.hs+1−2

This diff is too big to show here.

test/Tests/Readers/Jira.hs+21−1

This diff is too big to show here.

test/Tests/Readers/LaTeX.hs+1−20

This diff is too big to show here.

test/Tests/Readers/Man.hs−1

This diff is too big to show here.

test/Tests/Readers/Markdown.hs+3−5

This diff is too big to show here.

test/Tests/Readers/Muse.hs−2

This diff is too big to show here.

test/Tests/Readers/Odt.hs+1−2

This diff is too big to show here.

test/Tests/Readers/Org/Block.hs−2

This diff is too big to show here.

test/Tests/Readers/Org/Block/CodeBlock.hs−2

This diff is too big to show here.

test/Tests/Readers/Org/Block/Figure.hs−2

This diff is too big to show here.

test/Tests/Readers/Org/Block/Header.hs−2

This diff is too big to show here.

test/Tests/Readers/Org/Block/List.hs+13−2

This diff is too big to show here.

test/Tests/Readers/Org/Block/Table.hs−2

This diff is too big to show here.

test/Tests/Readers/Org/Directive.hs−2

This diff is too big to show here.

test/Tests/Readers/Org/Inline.hs−2

This diff is too big to show here.

test/Tests/Readers/Org/Inline/Citation.hs+40−2

This diff is too big to show here.

test/Tests/Readers/Org/Inline/Note.hs−2

This diff is too big to show here.

test/Tests/Readers/Org/Inline/Smart.hs−2

This diff is too big to show here.

test/Tests/Readers/Org/Meta.hs−2

This diff is too big to show here.

test/Tests/Readers/Org/Shared.hs−2

This diff is too big to show here.

test/Tests/Readers/RST.hs−2

This diff is too big to show here.

test/Tests/Readers/Txt2Tags.hs−2

This diff is too big to show here.

test/Tests/Shared.hs−2

This diff is too big to show here.

test/Tests/Writers/AnnotatedTable.hs−1

This diff is too big to show here.

test/Tests/Writers/AsciiDoc.hs−2

This diff is too big to show here.

test/Tests/Writers/ConTeXt.hs+122−115

This diff is too big to show here.

test/Tests/Writers/Docbook.hs+43−8

This diff is too big to show here.

test/Tests/Writers/Docx.hs+5−2

This diff is too big to show here.

test/Tests/Writers/FB2.hs−2

This diff is too big to show here.

test/Tests/Writers/HTML.hs+57−54

This diff is too big to show here.

test/Tests/Writers/JATS.hs+135−93

This diff is too big to show here.

test/Tests/Writers/Jira.hs+60−1

This diff is too big to show here.

test/Tests/Writers/LaTeX.hs−2

This diff is too big to show here.

test/Tests/Writers/Markdown.hs−2

This diff is too big to show here.

test/Tests/Writers/Ms.hs−2

This diff is too big to show here.

test/Tests/Writers/Muse.hs−1

This diff is too big to show here.

test/Tests/Writers/Native.hs−2

This diff is too big to show here.

test/Tests/Writers/OOXML.hs+5−4

This diff is too big to show here.

test/Tests/Writers/Org.hs+46−13

This diff is too big to show here.

test/Tests/Writers/Plain.hs−2

This diff is too big to show here.

test/Tests/Writers/Powerpoint.hs−2

This diff is too big to show here.

test/Tests/Writers/RST.hs−2

This diff is too big to show here.

test/Tests/Writers/TEI.hs−2

This diff is too big to show here.

test/command/1279.md+1−1

This diff is too big to show here.

test/command/2103.mdnew file+8

This diff is too big to show here.

test/command/2874.md+2−2

This diff is too big to show here.

test/command/3752.mdnew file+60

This diff is too big to show here.

test/command/3792.md+1−1

This diff is too big to show here.

test/command/4063.mdnew file+29

This diff is too big to show here.

test/command/4164.md+1−1

This diff is too big to show here.

test/command/4442.md+1−1

This diff is too big to show here.

test/command/4465.mdnew file+15

This diff is too big to show here.

test/command/5304.md+1−1

This diff is too big to show here.

test/command/5321.md+2−2

This diff is too big to show here.

test/command/5367.md+6−2

This diff is too big to show here.

test/command/5519.md+1−1

This diff is too big to show here.

test/command/5686.md+1−1

This diff is too big to show here.

test/command/6026.mdnew file+19

This diff is too big to show here.

test/command/6541.mdnew file+27

This diff is too big to show here.

test/command/6620.md+3

This diff is too big to show here.

test/command/6658.mdnew file+75

This diff is too big to show here.

test/command/6709.mdnew file+11

This diff is too big to show here.

test/command/6890.md+2−2

This diff is too big to show here.

test/command/6925.md+2−2

This diff is too big to show here.

test/command/6958.mdnew file+10

This diff is too big to show here.

test/command/7041.mdnew file+23

This diff is too big to show here.

test/command/7058.mdnew file+6

This diff is too big to show here.

test/command/7064.mdnew file+32

This diff is too big to show here.

test/command/7067.mdnew file+90

This diff is too big to show here.

test/command/7080.mdnew file+8

This diff is too big to show here.

test/command/7092.mdnew file+8

This diff is too big to show here.

test/command/7099.mdnew file+18

This diff is too big to show here.

test/command/7112.mdnew file+15

This diff is too big to show here.

test/command/7129.mdnew file+27

This diff is too big to show here.

test/command/7132.mdnew file+10

This diff is too big to show here.

test/command/7134.mdnew file+16

This diff is too big to show here.

test/command/7145.mdnew file+12

This diff is too big to show here.

test/command/7155.mdnew file+15

This diff is too big to show here.

test/command/7173.mdnew file+137

This diff is too big to show here.

test/command/7208.mdnew file+6

This diff is too big to show here.

test/command/7214.mdnew file+28

This diff is too big to show here.

test/command/7216.mdnew file+19

This diff is too big to show here.

test/command/7266.mdnew file+10

This diff is too big to show here.

test/command/7272.mdnew file+27

This diff is too big to show here.

test/command/7278.mdnew file+38

This diff is too big to show here.

test/command/7282.mdnew file+7

This diff is too big to show here.

test/command/7288.mdnew file+40

This diff is too big to show here.

test/command/7299.mdnew file+23

This diff is too big to show here.

test/command/7321.mdnew file+24

This diff is too big to show here.

test/command/7323.mdnew file+29

This diff is too big to show here.

test/command/7324.mdnew file+25

This diff is too big to show here.

test/command/7339.mdnew file+11

This diff is too big to show here.

test/command/7340.mdnew file+6

This diff is too big to show here.

test/command/7376.mdnew file+16

This diff is too big to show here.

test/command/7394.mdnew file+85

This diff is too big to show here.

test/command/7397.mdnew file+14

This diff is too big to show here.

test/command/7400.mdnew file+9

This diff is too big to show here.

test/command/7416.mdnew file+19

This diff is too big to show here.

test/command/averroes.bibnew file+95

This diff is too big to show here.

test/command/biblatex-baez-article.md+1−1

This diff is too big to show here.

test/command/biblatex-baez-online.md+1−1

This diff is too big to show here.

test/command/biblatex-basic.md+5−3

This diff is too big to show here.

test/command/biblatex-cotton.md+3−3

This diff is too big to show here.

test/command/biblatex-examples.bibdeleted−1674

This diff is too big to show here.

test/command/biblatex-inproceedings.md+1−1

This diff is too big to show here.

test/command/biblatex-itzhaki.md+1−1

This diff is too big to show here.

test/command/biblatex-jaffe.md+2−2

This diff is too big to show here.

test/command/biblatex-moraux.md+1−1

This diff is too big to show here.

test/command/biblatex-murray.md+3−2

This diff is too big to show here.

test/command/biblatex-nussbaum.mddeleted−63

This diff is too big to show here.

test/command/biblatex-quotes.md+1−1

This diff is too big to show here.

test/command/biblatex-sigfridsson.md+1−1

This diff is too big to show here.

test/command/biblatex-spiegelberg.md+2−2

This diff is too big to show here.

test/command/biblatex-test-case-conversion.md+2−2

This diff is too big to show here.

test/command/biblatex-textnormal.md+1−1

This diff is too big to show here.

test/command/biblatex-wassenberg.md+1−1

This diff is too big to show here.

test/command/biblatex-wilde.md+1−1

This diff is too big to show here.

test/command/chap1/spider.pngnew file

Binary file not shown.

test/command/chap1/text.mdnew file+17

This diff is too big to show here.

test/command/chap2/spider.pngnew file

Binary file not shown.

test/command/chap2/text.mdnew file+5

This diff is too big to show here.

test/command/defaults-inheritance-2.md+1−1

This diff is too big to show here.

test/command/duplicate_attributes.mdnew file+7

This diff is too big to show here.

test/command/mmd-metadata.mdnew file+20

This diff is too big to show here.

test/command/newif.mdnew file+55

This diff is too big to show here.

test/command/pandoc-citeproc-119.md+1−1

This diff is too big to show here.

test/command/pandoc-citeproc-320a.md+3−3

This diff is too big to show here.

test/command/pandoc-citeproc-322.md+1−1

This diff is too big to show here.

test/command/pandoc-citeproc-356.md+2−7

This diff is too big to show here.

test/command/pandoc-citeproc-65.md+1−1

This diff is too big to show here.

test/command/pandoc-citeproc-70.md+2−2

This diff is too big to show here.

test/command/pandoc-citeproc-chicago-author-date.md+7−7

This diff is too big to show here.

test/command/pandoc-citeproc-chicago-fullnote-bibliography.md+6−7

This diff is too big to show here.

test/command/svg.md+1−1

This diff is too big to show here.

test/command/toc.md+2−2

This diff is too big to show here.

test/command/unicode-collation.mdnew file+130

This diff is too big to show here.

test/docbook-reader.docbook+9−2

This diff is too big to show here.

test/docx/0_level_headers.native+2−2

This diff is too big to show here.

test/docx/golden/block_quotes.docx

Binary file not shown.

test/docx/golden/codeblock.docx

Binary file not shown.

test/docx/golden/comments.docx

Binary file not shown.

test/docx/golden/custom_style_no_reference.docx

Binary file not shown.

test/docx/golden/custom_style_preserve.docx

Binary file not shown.

test/docx/golden/custom_style_reference.docx

Binary file not shown.

test/docx/golden/definition_list.docx

Binary file not shown.

test/docx/golden/document-properties-short-desc.docx

Binary file not shown.

test/docx/golden/document-properties.docx

Binary file not shown.

test/docx/golden/headers.docx

Binary file not shown.

test/docx/golden/image.docx

Binary file not shown.

test/docx/golden/inline_code.docx

Binary file not shown.

test/docx/golden/inline_formatting.docx

Binary file not shown.

test/docx/golden/inline_images.docx

Binary file not shown.

test/docx/golden/link_in_notes.docx

Binary file not shown.

test/docx/golden/links.docx

Binary file not shown.

test/docx/golden/lists.docx

Binary file not shown.

test/docx/golden/lists_continuing.docx

Binary file not shown.

test/docx/golden/lists_multiple_initial.docx

Binary file not shown.

test/docx/golden/lists_restarting.docx

Binary file not shown.

test/docx/golden/nested_anchors_in_header.docx

Binary file not shown.

test/docx/golden/notes.docx

Binary file not shown.

test/docx/golden/raw-blocks.docx

Binary file not shown.

test/docx/golden/raw-bookmarks.docx

Binary file not shown.

test/docx/golden/table_one_row.docx

Binary file not shown.

test/docx/golden/table_with_list_cell.docx

Binary file not shown.

test/docx/golden/tables-default-widths.docxnew file

Binary file not shown.

test/docx/golden/tables.docx

Binary file not shown.

test/docx/golden/track_changes_deletion.docx

Binary file not shown.

test/docx/golden/track_changes_insertion.docx

Binary file not shown.

test/docx/golden/track_changes_move.docx

Binary file not shown.

test/docx/golden/track_changes_scrubbed_metadata.docx

Binary file not shown.

test/docx/golden/unicode.docx

Binary file not shown.

test/docx/golden/verbatim_subsuper.docx

Binary file not shown.

test/docx/image_vml_as_object.docxnew file

Binary file not shown.

test/docx/image_vml_as_object.nativenew file+2

This diff is too big to show here.

test/docx/sdt_elements.native+9−10

This diff is too big to show here.

test/docx/table_captions_no_field.docxnew file

Binary file not shown.

test/docx/table_captions_no_field.nativenew file+34

This diff is too big to show here.

test/docx/table_captions_with_field.docxnew file

Binary file not shown.

test/docx/table_captions_with_field.nativenew file+54

This diff is too big to show here.

test/docx/table_header_rowspan.docxnew file

Binary file not shown.

test/docx/table_header_rowspan.nativenew file+189

This diff is too big to show here.

test/docx/table_one_header_row.docxnew file

Binary file not shown.

test/docx/table_one_header_row.nativenew file+18

This diff is too big to show here.

test/docx/table_one_row.docx

Binary file not shown.

test/docx/table_one_row.native+4−4

This diff is too big to show here.

test/docx/table_variable_width.native+12−17

This diff is too big to show here.

test/docx/table_with_list_cell.native+3−3

This diff is too big to show here.

test/docx/tables-default-widths.nativenew file+92

This diff is too big to show here.

test/docx/tables.native+9−9

This diff is too big to show here.

test/epub/features.native+2−2

This diff is too big to show here.

test/epub/wasteland.epub

Binary file not shown.

test/fb2/images.fb2+1−1

This diff is too big to show here.

test/fb2/test.jpg

Binary file not shown.

test/html-reader.native+2−1

This diff is too big to show here.

test/jats-reader.native+1−1

This diff is too big to show here.

test/jats-reader.xml+1

This diff is too big to show here.

test/jira-reader.native+1−1

This diff is too big to show here.

test/lhs-test.html+2−1

This diff is too big to show here.

test/lhs-test.html+lhs+2−1

This diff is too big to show here.

test/lhs-test.latex+3−3

This diff is too big to show here.

test/lhs-test.latex+lhs+3−3

This diff is too big to show here.

test/lua/module/pandoc-path.luanew file+44

This diff is too big to show here.

test/odt/native/tab.nativenew file+1

This diff is too big to show here.

test/odt/odt/tab.odtnew file

Binary file not shown.

test/pipe-tables.native+2−10

This diff is too big to show here.

test/pptx/code-custom.pptx

Binary file not shown.

test/pptx/code-custom_templated.pptx

Binary file not shown.

test/pptx/code.pptx

Binary file not shown.

test/pptx/code_templated.pptx

Binary file not shown.

test/pptx/document-properties-short-desc.pptx

Binary file not shown.

test/pptx/document-properties-short-desc_templated.pptx

Binary file not shown.

test/pptx/document-properties.pptx

Binary file not shown.

test/pptx/document-properties_templated.pptx

Binary file not shown.

test/pptx/endnotes.pptx

Binary file not shown.

test/pptx/endnotes_templated.pptx

Binary file not shown.

test/pptx/endnotes_toc.pptx

Binary file not shown.

test/pptx/endnotes_toc_templated.pptx

Binary file not shown.

test/pptx/images.pptx

Binary file not shown.

test/pptx/images_templated.pptx

Binary file not shown.

test/pptx/inline_formatting.pptx

Binary file not shown.

test/pptx/inline_formatting_templated.pptx

Binary file not shown.

test/pptx/lists.pptx

Binary file not shown.

test/pptx/lists_templated.pptx

Binary file not shown.

test/pptx/raw_ooxml.pptx

Binary file not shown.

test/pptx/raw_ooxml_templated.pptx

Binary file not shown.

test/pptx/reference_depth.pptx

Binary file not shown.

test/pptx/remove_empty_slides.pptx

Binary file not shown.

test/pptx/remove_empty_slides_templated.pptx

Binary file not shown.

test/pptx/slide_breaks.pptx

Binary file not shown.

test/pptx/slide_breaks_slide_level_1.pptx

Binary file not shown.

test/pptx/slide_breaks_slide_level_1_templated.pptx

Binary file not shown.

test/pptx/slide_breaks_templated.pptx

Binary file not shown.

test/pptx/slide_breaks_toc.pptx

Binary file not shown.

test/pptx/slide_breaks_toc_templated.pptx

Binary file not shown.

test/pptx/speaker_notes.pptx

Binary file not shown.

test/pptx/speaker_notes_after_metadata.pptx

Binary file not shown.

test/pptx/speaker_notes_after_metadata_templated.pptx

Binary file not shown.

test/pptx/speaker_notes_afterheader.pptx

Binary file not shown.

test/pptx/speaker_notes_afterheader_templated.pptx

Binary file not shown.

test/pptx/speaker_notes_afterseps.pptx

Binary file not shown.

test/pptx/speaker_notes_afterseps_templated.pptx

Binary file not shown.

test/pptx/speaker_notes_templated.pptx

Binary file not shown.

test/pptx/start_numbering_at.pptx

Binary file not shown.

test/pptx/start_numbering_at_templated.pptx

Binary file not shown.

test/pptx/tables.pptx

Binary file not shown.

test/pptx/tables_templated.pptx

Binary file not shown.

test/pptx/two_column.pptx

Binary file not shown.

test/pptx/two_column_templated.pptx

Binary file not shown.

test/tables.latex+27−3

This diff is too big to show here.

test/tables.ms+30

This diff is too big to show here.

test/tables/nordics.latex+22−4

This diff is too big to show here.

test/tables/planets.latex+4−4

This diff is too big to show here.

test/tables/students.latex+10−2

This diff is too big to show here.

test/test-pandoc.hs+18−8

This diff is too big to show here.

test/txt2tags.native+1−1

This diff is too big to show here.

test/txt2tags.t2t+1−1

This diff is too big to show here.

test/writer.context+42−38

This diff is too big to show here.

test/writer.html4+2−1

This diff is too big to show here.

test/writer.html5+2−1

This diff is too big to show here.

test/writer.jats_archiving+38−48

This diff is too big to show here.

test/writer.jats_articleauthoring+41−49

This diff is too big to show here.

test/writer.jats_publishing+38−48

This diff is too big to show here.

test/writer.jira+23−35

This diff is too big to show here.

test/writer.latex+3−3

This diff is too big to show here.

test/writer.ms+1−1

This diff is too big to show here.

test/writer.opml+1−1

This diff is too big to show here.

test/writer.org+7−7

This diff is too big to show here.

test/writer.zimwiki+3−3

This diff is too big to show here.

test/writers-lang-and-dir.context+4

This diff is too big to show here.

test/writers-lang-and-dir.latex+6−6

This diff is too big to show here.

tools/build-arm.shnew file+110

This diff is too big to show here.

tools/changelog-helper.sh+11−4

This diff is too big to show here.

tools/parseTimings.plnew file+33

This diff is too big to show here.

trypandoc/index.html+1−1

This diff is too big to show here.

trypandoc/trypandoc.hs+2−7

This diff is too big to show here.

windows/Makefile+1−1

This diff is too big to show here.

windows/pandoc.wxs+2−2

This diff is too big to show here.