From ff3ed5c9f8ee985bb6026dd10b8c2549782c0296 Mon Sep 17 00:00:00 2001 From: Alexander Krotov Date: Wed, 4 Apr 2018 13:45:28 +0300 Subject: FB2 writer: allow emphasis and notes in titles Only

and are allowed in titles, but

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. --- src/Text/Pandoc/Writers/FB2.hs | 10 +-- test/fb2/titles.fb2 | 2 +- test/fb2/titles.markdown | 2 +- test/writer.fb2 | 188 ++++++++++++++++++++++------------------- 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

and are allowed within 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 />

<p>Simple title</p>

This example tests if Pandoc doesn’t insert forbidden elements in FictionBook titles.

<p>Emphasized Strong Title</p>
+unrecognisedpandoc<p />
<p>Simple title</p>

This example tests FictionBook titles.

<p><emphasis>Emphasized</emphasis> <strong>Strong</strong> Title</p>
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 @@
-<p>Level 2 with an embedded link </url></p> +<p>Level 2 with an embedded link<a l:href="#l1" type="note"> +<sup>[1]</sup> +</a> +</p>
-<p>Level 3 with emphasis</p> +<p>Level 3 with <emphasis>emphasis</emphasis> +</p>
@@ -58,7 +62,8 @@
-<p>Level 2 with emphasis</p> +<p>Level 2 with <emphasis>emphasis</emphasis> +</p>
@@ -485,8 +490,8 @@

This is emphasized, and so is this.

This is strong, and so is this.

-

An emphasized link -[1] +

An emphasized link +[2] .

@@ -526,8 +531,8 @@

‘A’, ‘B’, and ‘C’ are letters.

‘Oak,’ ‘elm,’ and ‘beech’ are names of trees. So is ‘pine.’

‘He said, “I want to go.”’ Were you alive in the 70’s?

-

Here is some quoted ‘code’ and a “quoted link -[2] +

Here is some quoted ‘code’ and a “quoted link +[3] ”.

Some dashes: one—two — three—four — five.

Dashes between numbers: 5–7, 255–66, 1987–1999.

@@ -606,10 +611,7 @@ <p>Explicit</p> -

Just a URL -[3] -.

-

URL and title +

Just a URL [4] .

URL and title @@ -620,97 +622,100 @@ .

URL and title [7] - -

+.

URL and title [8]

-

with_underscore +

URL and title [9]

-

Email link +

with_underscore [10]

-

Empty +

Email link [11] + +

+

Empty +[12] .

<p>Reference</p> -

Foo bar -[12] -.

-

With embedded [brackets] +

Foo bar [13] .

-

b +

With embedded [brackets] [14] - by itself should be a link.

-

Indented once -[15] .

-

Indented twice +

b +[15] + by itself should be a link.

+

Indented once [16] .

-

Indented thrice +

Indented twice [17] .

+

Indented thrice +[18] +.

This should [not][] be a link.

[not]: /url

-

Foo bar -[18] -.

-

Foo biz +

Foo bar [19] .

+

Foo biz +[20] +.

<p>With ampersands</p> -

Here’s a link with an ampersand in the URL -[20] -.

-

Here’s a link with an amersand in the link text: AT&T +

Here’s a link with an ampersand in the URL [21] .

-

Here’s an inline link +

Here’s a link with an amersand in the link text: AT&T [22] .

-

Here’s an inline link in pointy braces +

Here’s an inline link [23] .

+

Here’s an inline link in pointy braces +[24] +.

<p>Autolinks</p> -

With an ampersand: http://example.com/?foo=1&bar=2 -[24] +

With an ampersand: http://example.com/?foo=1&bar=2 +[25]

• In a list?

-

• http://example.com/ -[25] +

• http://example.com/ +[26]

• It should.

-

An e-mail address: nobody@nowhere.net -[26] +

An e-mail address: nobody@nowhere.net +[27]

-

Blockquoted: http://example.com/ -[27] +

Blockquoted: http://example.com/ +[28]

@@ -741,22 +746,22 @@ <p>Footnotes</p> -

Here is a footnote reference, -[28] - and another. +

Here is a footnote reference, [29] - This should not be a footnote reference, because it contains a space.[^my note] Here is an inline note. + and another. [30] + This should not be a footnote reference, because it contains a space.[^my note] Here is an inline note. +[31]

-

Notes can go in quotes. -[31] +

Notes can go in quotes. +[32]

-

1. And in list items. -[32] +

1. And in list items. +[33]

This paragraph should not be part of the note, as it is not indented.

@@ -776,7 +781,7 @@

2

-http://example.com/?foo=1&bar=2 +/url

@@ -784,50 +789,50 @@

3

-/url/ +http://example.com/?foo=1&bar=2

<p>4</p> -

title: /url/ +

+/url/

<p>5</p> -

title preceded by two spaces: /url/ +

title: /url/

<p>6</p> -

title preceded by a tab: /url/ +

title preceded by two spaces: /url/

<p>7</p> -

title with "quotes" in it: /url/ +

title preceded by a tab: /url/

<p>8</p> -

title with single quotes: /url/ +

title with "quotes" in it: /url/

<p>9</p> -

-/url/with_underscore +

title with single quotes: /url/

@@ -835,7 +840,7 @@

10

-mailto:nobody@nowhere.net +/url/with_underscore

@@ -843,8 +848,7 @@

11

- - +mailto:nobody@nowhere.net

@@ -852,7 +856,8 @@

12

-/url/ + +

@@ -876,7 +881,7 @@

15

-/url +/url/

@@ -899,37 +904,37 @@ <p>18</p> -

Title with "quotes" inside: /url/ +

+/url

<p>19</p> -

Title with "quote" inside: /url/ +

Title with "quotes" inside: /url/

<p>20</p> -

-http://example.com/?foo=1&bar=2 +

Title with "quote" inside: /url/

<p>21</p> -

AT&T: http://att.com/ +

+http://example.com/?foo=1&bar=2

<p>22</p> -

-/script?foo=1&bar=2 +

AT&T: http://att.com/

@@ -945,7 +950,7 @@

24

-http://example.com/?foo=1&bar=2 +/script?foo=1&bar=2

@@ -953,7 +958,7 @@

25

-http://example.com/ +http://example.com/?foo=1&bar=2

@@ -961,7 +966,7 @@

26

-mailto:nobody@nowhere.net +http://example.com/

@@ -969,19 +974,27 @@

27

-http://example.com/ +mailto:nobody@nowhere.net

-
+
<p>28</p> -

Here is the footnote. It can go anywhere after the footnote reference. It need not be placed at the end of the document.

+

+http://example.com/ +

<p>29</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>30</p> +

Here’s the long note. This one contains multiple blocks.

Subsequent blocks are indented to show that they belong to the footnote (as with list items).

@@ -991,25 +1004,26 @@

If you want, you can indent every line, but you can also be lazy and just indent the first line of each block.

-
+
-<p>30</p> +<p>31</p> -

This is easier to type. Inline notes may contain links -[30] +

This is easier to type. Inline notes may contain links +[31] and ] verbatim characters, as well as [bracketed text].

-
+
-<p>31</p> +<p>32</p>

In quote.

-
+
-<p>32</p> +<p>33</p>

In list.

+ -- cgit v1.2.3