aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Krotov <ilabdsf@gmail.com>2018-04-04 13:45:28 +0300
committerAlexander Krotov <ilabdsf@gmail.com>2018-04-04 14:22:34 +0300
commitff3ed5c9f8ee985bb6026dd10b8c2549782c0296 (patch)
treee180708763a3c415b6c67a17e5e47acd16a4d697
parent2388be648227ca041b6d7d6a79ad56f9175dc813 (diff)
downloadpandoc-ff3ed5c9f8ee985bb6026dd10b8c2549782c0296.tar.gz
FB2 writer: allow emphasis and notes in titles
Only <p> and <empty-line /> are allowed in titles, but <p> has the same type as an ordinary paragraphs. Therefore, there is no need to remove emphasis from titles. Also, don't intersperse paragraph with empty lines.
-rw-r--r--src/Text/Pandoc/Writers/FB2.hs10
-rw-r--r--test/fb2/titles.fb22
-rw-r--r--test/fb2/titles.markdown2
-rw-r--r--test/writer.fb2188
4 files changed, 107 insertions, 95 deletions
diff --git a/src/Text/Pandoc/Writers/FB2.hs b/src/Text/Pandoc/Writers/FB2.hs
index 3f90f47b1..a62cf4524 100644
--- a/src/Text/Pandoc/Writers/FB2.hs
+++ b/src/Text/Pandoc/Writers/FB2.hs
@@ -46,7 +46,7 @@ import Data.ByteString.Base64 (encode)
import qualified Data.ByteString.Char8 as B8
import Data.Char (isAscii, isControl, isSpace, toLower)
import Data.Either (lefts, rights)
-import Data.List (intercalate, intersperse, isPrefixOf, stripPrefix)
+import Data.List (intercalate, isPrefixOf, stripPrefix)
import Data.Text (Text, pack)
import Network.HTTP (urlEncode)
import Text.XML.Light
@@ -180,7 +180,7 @@ renderSection :: PandocMonad m => Int -> ([Inline], [Block]) -> FBM m Content
renderSection level (ttl, body) = do
title <- if null ttl
then return []
- else return . list . el "title" . formatTitle $ ttl
+ else list . el "title" <$> formatTitle ttl
content <- if hasSubsections body
then renderSections (level + 1) body
else cMapM blockToXml body
@@ -189,11 +189,9 @@ renderSection level (ttl, body) = do
hasSubsections = any isHeaderBlock
-- | Only <p> and <empty-line> are allowed within <title> in FB2.
-formatTitle :: [Inline] -> [Content]
+formatTitle :: PandocMonad m => [Inline] -> FBM m [Content]
formatTitle inlines =
- let lns = split isLineBreak inlines
- lns' = map (el "p" . cMap plain) lns
- in intersperse (el "empty-line" ()) lns'
+ cMapM (blockToXml . Para) $ split (== LineBreak) inlines
split :: (a -> Bool) -> [a] -> [[a]]
split _ [] = []
diff --git a/test/fb2/titles.fb2 b/test/fb2/titles.fb2
index 49a741b57..4e56fb3e6 100644
--- a/test/fb2/titles.fb2
+++ b/test/fb2/titles.fb2
@@ -1,3 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
-<FictionBook xmlns="http://www.gribuser.ru/xml/fictionbook/2.0" xmlns:l="http://www.w3.org/1999/xlink"><description><title-info><genre>unrecognised</genre></title-info><document-info><program-used>pandoc</program-used></document-info></description><body><title><p /></title><section><title><p>Simple title</p></title><p>This example tests if Pandoc doesn’t insert forbidden elements in FictionBook titles.</p></section><section><title><p>Emphasized Strong Title</p></title></section></body></FictionBook>
+<FictionBook xmlns="http://www.gribuser.ru/xml/fictionbook/2.0" xmlns:l="http://www.w3.org/1999/xlink"><description><title-info><genre>unrecognised</genre></title-info><document-info><program-used>pandoc</program-used></document-info></description><body><title><p /></title><section><title><p>Simple title</p></title><p>This example tests FictionBook titles.</p></section><section><title><p><emphasis>Emphasized</emphasis> <strong>Strong</strong> Title</p></title></section></body></FictionBook>
diff --git a/test/fb2/titles.markdown b/test/fb2/titles.markdown
index 1eaf2ccd5..1d405a3c1 100644
--- a/test/fb2/titles.markdown
+++ b/test/fb2/titles.markdown
@@ -1,6 +1,6 @@
# Simple title
-This example tests if Pandoc doesn't insert forbidden elements in FictionBook titles.
+This example tests FictionBook titles.
# *Emphasized* **Strong** Title
diff --git a/test/writer.fb2 b/test/writer.fb2
index 20f566334..2136a6d8e 100644
--- a/test/writer.fb2
+++ b/test/writer.fb2
@@ -33,11 +33,15 @@
</title>
<section>
<title>
-<p>Level 2 with an embedded link &lt;/url&gt;</p>
+<p>Level 2 with an embedded link<a l:href="#l1" type="note">
+<sup>[1]</sup>
+</a>
+</p>
</title>
<section>
<title>
-<p>Level 3 with emphasis</p>
+<p>Level 3 with <emphasis>emphasis</emphasis>
+</p>
</title>
<section>
<title>
@@ -58,7 +62,8 @@
</title>
<section>
<title>
-<p>Level 2 with emphasis</p>
+<p>Level 2 with <emphasis>emphasis</emphasis>
+</p>
</title>
<section>
<title>
@@ -485,8 +490,8 @@
</title>
<p>This is <emphasis>emphasized</emphasis>, and so <emphasis>is this</emphasis>.</p>
<p>This is <strong>strong</strong>, and so <strong>is this</strong>.</p>
-<p>An <emphasis>emphasized link<a l:href="#l1" type="note">
-<sup>[1]</sup>
+<p>An <emphasis>emphasized link<a l:href="#l2" type="note">
+<sup>[2]</sup>
</a>
</emphasis>.</p>
<p>
@@ -526,8 +531,8 @@
<p>‘A’, ‘B’, and ‘C’ are letters.</p>
<p>‘Oak,’ ‘elm,’ and ‘beech’ are names of trees. So is ‘pine.’</p>
<p>‘He said, “I want to go.”’ Were you alive in the 70’s?</p>
-<p>Here is some quoted ‘<code>code</code>’ and a “quoted link<a l:href="#l2" type="note">
-<sup>[2]</sup>
+<p>Here is some quoted ‘<code>code</code>’ and a “quoted link<a l:href="#l3" type="note">
+<sup>[3]</sup>
</a>”.</p>
<p>Some dashes: one—two — three—four — five.</p>
<p>Dashes between numbers: 5–7, 255–66, 1987–1999.</p>
@@ -606,10 +611,7 @@
<title>
<p>Explicit</p>
</title>
-<p>Just a URL<a l:href="#l3" type="note">
-<sup>[3]</sup>
-</a>.</p>
-<p>URL and title<a l:href="#l4" type="note">
+<p>Just a URL<a l:href="#l4" type="note">
<sup>[4]</sup>
</a>.</p>
<p>URL and title<a l:href="#l5" type="note">
@@ -620,97 +622,100 @@
</a>.</p>
<p>URL and title<a l:href="#l7" type="note">
<sup>[7]</sup>
-</a>
-</p>
+</a>.</p>
<p>URL and title<a l:href="#l8" type="note">
<sup>[8]</sup>
</a>
</p>
-<p>with_underscore<a l:href="#l9" type="note">
+<p>URL and title<a l:href="#l9" type="note">
<sup>[9]</sup>
</a>
</p>
-<p>Email link<a l:href="#l10" type="note">
+<p>with_underscore<a l:href="#l10" type="note">
<sup>[10]</sup>
</a>
</p>
-<p>Empty<a l:href="#l11" type="note">
+<p>Email link<a l:href="#l11" type="note">
<sup>[11]</sup>
+</a>
+</p>
+<p>Empty<a l:href="#l12" type="note">
+<sup>[12]</sup>
</a>.</p>
</section>
<section>
<title>
<p>Reference</p>
</title>
-<p>Foo bar<a l:href="#l12" type="note">
-<sup>[12]</sup>
-</a>.</p>
-<p>With embedded [brackets]<a l:href="#l13" type="note">
+<p>Foo bar<a l:href="#l13" type="note">
<sup>[13]</sup>
</a>.</p>
-<p>b<a l:href="#l14" type="note">
+<p>With embedded [brackets]<a l:href="#l14" type="note">
<sup>[14]</sup>
-</a> by itself should be a link.</p>
-<p>Indented once<a l:href="#l15" type="note">
-<sup>[15]</sup>
</a>.</p>
-<p>Indented twice<a l:href="#l16" type="note">
+<p>b<a l:href="#l15" type="note">
+<sup>[15]</sup>
+</a> by itself should be a link.</p>
+<p>Indented once<a l:href="#l16" type="note">
<sup>[16]</sup>
</a>.</p>
-<p>Indented thrice<a l:href="#l17" type="note">
+<p>Indented twice<a l:href="#l17" type="note">
<sup>[17]</sup>
</a>.</p>
+<p>Indented thrice<a l:href="#l18" type="note">
+<sup>[18]</sup>
+</a>.</p>
<p>This should [not][] be a link.</p>
<empty-line />
<p>
<code>[not]: /url</code>
</p>
<empty-line />
-<p>Foo bar<a l:href="#l18" type="note">
-<sup>[18]</sup>
-</a>.</p>
-<p>Foo biz<a l:href="#l19" type="note">
+<p>Foo bar<a l:href="#l19" type="note">
<sup>[19]</sup>
</a>.</p>
+<p>Foo biz<a l:href="#l20" type="note">
+<sup>[20]</sup>
+</a>.</p>
</section>
<section>
<title>
<p>With ampersands</p>
</title>
-<p>Here’s a link with an ampersand in the URL<a l:href="#l20" type="note">
-<sup>[20]</sup>
-</a>.</p>
-<p>Here’s a link with an amersand in the link text: AT&amp;T<a l:href="#l21" type="note">
+<p>Here’s a link with an ampersand in the URL<a l:href="#l21" type="note">
<sup>[21]</sup>
</a>.</p>
-<p>Here’s an inline link<a l:href="#l22" type="note">
+<p>Here’s a link with an amersand in the link text: AT&amp;T<a l:href="#l22" type="note">
<sup>[22]</sup>
</a>.</p>
-<p>Here’s an inline link in pointy braces<a l:href="#l23" type="note">
+<p>Here’s an inline link<a l:href="#l23" type="note">
<sup>[23]</sup>
</a>.</p>
+<p>Here’s an inline link in pointy braces<a l:href="#l24" type="note">
+<sup>[24]</sup>
+</a>.</p>
</section>
<section>
<title>
<p>Autolinks</p>
</title>
-<p>With an ampersand: http://example.com/?foo=1&amp;bar=2<a l:href="#l24" type="note">
-<sup>[24]</sup>
+<p>With an ampersand: http://example.com/?foo=1&amp;bar=2<a l:href="#l25" type="note">
+<sup>[25]</sup>
</a>
</p>
<p>• In a list?</p>
-<p>• http://example.com/<a l:href="#l25" type="note">
-<sup>[25]</sup>
+<p>• http://example.com/<a l:href="#l26" type="note">
+<sup>[26]</sup>
</a>
</p>
<p>• It should.</p>
-<p>An e-mail address: nobody@nowhere.net<a l:href="#l26" type="note">
-<sup>[26]</sup>
+<p>An e-mail address: nobody@nowhere.net<a l:href="#l27" type="note">
+<sup>[27]</sup>
</a>
</p>
<cite>
-<p>Blockquoted: http://example.com/<a l:href="#l27" type="note">
-<sup>[27]</sup>
+<p>Blockquoted: http://example.com/<a l:href="#l28" type="note">
+<sup>[28]</sup>
</a>
</p>
</cite>
@@ -741,22 +746,22 @@
<title>
<p>Footnotes</p>
</title>
-<p>Here is a footnote reference,<a l:href="#n28" type="note">
-<sup>[28]</sup>
-</a> and another.<a l:href="#n29" type="note">
+<p>Here is a footnote reference,<a l:href="#n29" type="note">
<sup>[29]</sup>
-</a> This should <emphasis>not</emphasis> be a footnote reference, because it contains a space.[^my note] Here is an inline note.<a l:href="#n30" type="note">
+</a> and another.<a l:href="#n30" type="note">
<sup>[30]</sup>
+</a> This should <emphasis>not</emphasis> be a footnote reference, because it contains a space.[^my note] Here is an inline note.<a l:href="#n31" type="note">
+<sup>[31]</sup>
</a>
</p>
<cite>
-<p>Notes can go in quotes.<a l:href="#n31" type="note">
-<sup>[31]</sup>
+<p>Notes can go in quotes.<a l:href="#n32" type="note">
+<sup>[32]</sup>
</a>
</p>
</cite>
-<p>1. And in list items.<a l:href="#n32" type="note">
-<sup>[32]</sup>
+<p>1. And in list items.<a l:href="#n33" type="note">
+<sup>[33]</sup>
</a>
</p>
<p>This paragraph should not be part of the note, as it is not indented.</p>
@@ -776,7 +781,7 @@
<p>2</p>
</title>
<p>
-<code>http://example.com/?foo=1&amp;bar=2</code>
+<code>/url</code>
</p>
</section>
<section id="l3">
@@ -784,50 +789,50 @@
<p>3</p>
</title>
<p>
-<code>/url/</code>
+<code>http://example.com/?foo=1&amp;bar=2</code>
</p>
</section>
<section id="l4">
<title>
<p>4</p>
</title>
-<p>title: <code>/url/</code>
+<p>
+<code>/url/</code>
</p>
</section>
<section id="l5">
<title>
<p>5</p>
</title>
-<p>title preceded by two spaces: <code>/url/</code>
+<p>title: <code>/url/</code>
</p>
</section>
<section id="l6">
<title>
<p>6</p>
</title>
-<p>title preceded by a tab: <code>/url/</code>
+<p>title preceded by two spaces: <code>/url/</code>
</p>
</section>
<section id="l7">
<title>
<p>7</p>
</title>
-<p>title with &quot;quotes&quot; in it: <code>/url/</code>
+<p>title preceded by a tab: <code>/url/</code>
</p>
</section>
<section id="l8">
<title>
<p>8</p>
</title>
-<p>title with single quotes: <code>/url/</code>
+<p>title with &quot;quotes&quot; in it: <code>/url/</code>
</p>
</section>
<section id="l9">
<title>
<p>9</p>
</title>
-<p>
-<code>/url/with_underscore</code>
+<p>title with single quotes: <code>/url/</code>
</p>
</section>
<section id="l10">
@@ -835,7 +840,7 @@
<p>10</p>
</title>
<p>
-<code>mailto:nobody@nowhere.net</code>
+<code>/url/with_underscore</code>
</p>
</section>
<section id="l11">
@@ -843,8 +848,7 @@
<p>11</p>
</title>
<p>
-<code>
-</code>
+<code>mailto:nobody@nowhere.net</code>
</p>
</section>
<section id="l12">
@@ -852,7 +856,8 @@
<p>12</p>
</title>
<p>
-<code>/url/</code>
+<code>
+</code>
</p>
</section>
<section id="l13">
@@ -876,7 +881,7 @@
<p>15</p>
</title>
<p>
-<code>/url</code>
+<code>/url/</code>
</p>
</section>
<section id="l16">
@@ -899,37 +904,37 @@
<title>
<p>18</p>
</title>
-<p>Title with &quot;quotes&quot; inside: <code>/url/</code>
+<p>
+<code>/url</code>
</p>
</section>
<section id="l19">
<title>
<p>19</p>
</title>
-<p>Title with &quot;quote&quot; inside: <code>/url/</code>
+<p>Title with &quot;quotes&quot; inside: <code>/url/</code>
</p>
</section>
<section id="l20">
<title>
<p>20</p>
</title>
-<p>
-<code>http://example.com/?foo=1&amp;bar=2</code>
+<p>Title with &quot;quote&quot; inside: <code>/url/</code>
</p>
</section>
<section id="l21">
<title>
<p>21</p>
</title>
-<p>AT&amp;T: <code>http://att.com/</code>
+<p>
+<code>http://example.com/?foo=1&amp;bar=2</code>
</p>
</section>
<section id="l22">
<title>
<p>22</p>
</title>
-<p>
-<code>/script?foo=1&amp;bar=2</code>
+<p>AT&amp;T: <code>http://att.com/</code>
</p>
</section>
<section id="l23">
@@ -945,7 +950,7 @@
<p>24</p>
</title>
<p>
-<code>http://example.com/?foo=1&amp;bar=2</code>
+<code>/script?foo=1&amp;bar=2</code>
</p>
</section>
<section id="l25">
@@ -953,7 +958,7 @@
<p>25</p>
</title>
<p>
-<code>http://example.com/</code>
+<code>http://example.com/?foo=1&amp;bar=2</code>
</p>
</section>
<section id="l26">
@@ -961,7 +966,7 @@
<p>26</p>
</title>
<p>
-<code>mailto:nobody@nowhere.net</code>
+<code>http://example.com/</code>
</p>
</section>
<section id="l27">
@@ -969,19 +974,27 @@
<p>27</p>
</title>
<p>
-<code>http://example.com/</code>
+<code>mailto:nobody@nowhere.net</code>
</p>
</section>
-<section id="n28">
+<section id="l28">
<title>
<p>28</p>
</title>
-<p>Here is the footnote. It can go anywhere after the footnote reference. It need not be placed at the end of the document.</p>
+<p>
+<code>http://example.com/</code>
+</p>
</section>
<section id="n29">
<title>
<p>29</p>
</title>
+<p>Here is the footnote. It can go anywhere after the footnote reference. It need not be placed at the end of the document.</p>
+</section>
+<section id="n30">
+<title>
+<p>30</p>
+</title>
<p>Here’s the long note. This one contains multiple blocks.</p>
<p>Subsequent blocks are indented to show that they belong to the footnote (as with list items).</p>
<empty-line />
@@ -991,25 +1004,26 @@
<empty-line />
<p>If you want, you can indent every line, but you can also be lazy and just indent the first line of each block.</p>
</section>
-<section id="n30">
+<section id="n31">
<title>
-<p>30</p>
+<p>31</p>
</title>
-<p>This is <emphasis>easier</emphasis> to type. Inline notes may contain links<a l:href="#l30" type="note">
-<sup>[30]</sup>
+<p>This is <emphasis>easier</emphasis> to type. Inline notes may contain links<a l:href="#l31" type="note">
+<sup>[31]</sup>
</a> and <code>]</code> verbatim characters, as well as [bracketed text].</p>
</section>
-<section id="n31">
+<section id="n32">
<title>
-<p>31</p>
+<p>32</p>
</title>
<p>In quote.</p>
</section>
-<section id="n32">
+<section id="n33">
<title>
-<p>32</p>
+<p>33</p>
</title>
<p>In list.</p>
</section>
</body>
</FictionBook>
+