diff options
-rw-r--r-- | src/Text/Pandoc/Writers/FB2.hs | 48 | ||||
-rw-r--r-- | test/fb2/basic.fb2 | 2 | ||||
-rw-r--r-- | test/writer.fb2 | 168 |
3 files changed, 119 insertions, 99 deletions
diff --git a/src/Text/Pandoc/Writers/FB2.hs b/src/Text/Pandoc/Writers/FB2.hs index b7dc43685..6d61ea62c 100644 --- a/src/Text/Pandoc/Writers/FB2.hs +++ b/src/Text/Pandoc/Writers/FB2.hs @@ -338,13 +338,13 @@ blockToXml (LineBlock lns) = blockToXml (OrderedList a bss) = do state <- get let pmrk = parentListMarker state - let markers = map ((pmrk ++ " ") ++) $ orderedListMarkers a + let markers = map (pmrk ++) $ orderedListMarkers a let mkitem mrk bs = do - modify (\s -> s { parentListMarker = mrk }) - itemtext <- cMapM blockToXml . paraToPlain $ bs + modify (\s -> s { parentListMarker = mrk ++ " "}) + item <- cMapM blockToXml $ plainToPara $ indentBlocks (mrk ++ " ") bs modify (\s -> s { parentListMarker = pmrk }) -- old parent marker - return . el "p" $ [ txt mrk, txt " " ] ++ itemtext - zipWithM mkitem markers bss + return item + concat <$> (zipWithM mkitem markers bss) blockToXml (BulletList bss) = do state <- get let level = parentBulletLevel state @@ -354,10 +354,10 @@ blockToXml (BulletList bss) = do let mrk = prefix ++ bullets !! (level `mod` length bullets) let mkitem bs = do modify (\s -> s { parentBulletLevel = level+1 }) - itemtext <- cMapM blockToXml . paraToPlain $ bs + item <- cMapM blockToXml $ plainToPara $ indentBlocks (mrk ++ " ") bs modify (\s -> s { parentBulletLevel = level }) -- restore bullet level - return $ el "p" $ txt (mrk ++ " ") : itemtext - mapM mkitem bss + return item + cMapM mkitem bss blockToXml (DefinitionList defs) = cMapM mkdef defs where @@ -408,19 +408,24 @@ blockToXml Null = return [] paraToPlain :: [Block] -> [Block] paraToPlain [] = [] paraToPlain (Para inlines : rest) = - let p = Plain (inlines ++ [LineBreak]) - in p : paraToPlain rest + Plain (inlines) : Plain ([LineBreak]) : paraToPlain rest paraToPlain (p:rest) = p : paraToPlain rest +-- Replace plain text with paragraphs and add line break after paragraphs. +-- It is used to convert plain text from tight list items to paragraphs. +plainToPara :: [Block] -> [Block] +plainToPara [] = [] +plainToPara (Plain inlines : rest) = + Para (inlines) : plainToPara rest +plainToPara (Para inlines : rest) = + Para (inlines) : Plain [LineBreak] : plainToPara rest +plainToPara (p:rest) = p : plainToPara rest + -- Simulate increased indentation level. Will not really work -- for multi-line paragraphs. -indent :: Block -> Block -indent = indentBlock +indentPrefix :: String -> Block -> Block +indentPrefix spacer = indentBlock where - -- indentation space - spacer :: String - spacer = replicate 4 ' ' - -- indentBlock (Plain ins) = Plain (Str spacer:ins) indentBlock (Para ins) = Para (Str spacer:ins) indentBlock (CodeBlock a s) = @@ -434,6 +439,17 @@ indent = indentBlock indentLines ins = let lns = split isLineBreak ins :: [[Inline]] in intercalate [LineBreak] $ map (Str spacer:) lns +indent :: Block -> Block +indent = indentPrefix spacer + where + -- indentation space + spacer :: String + spacer = replicate 4 ' ' + +indentBlocks :: String -> [Block] -> [Block] +indentBlocks _ [] = [] +indentBlocks prefix (x:xs) = indentPrefix prefix x : map (indentPrefix $ replicate (length prefix) ' ') xs + -- | Convert a Pandoc's Inline element to FictionBook XML representation. toXml :: PandocMonad m => Inline -> FBM m [Content] toXml (Str s) = return [txt s] diff --git a/test/fb2/basic.fb2 b/test/fb2/basic.fb2 index ffb2bfbdf..ff1c65c11 100644 --- a/test/fb2/basic.fb2 +++ b/test/fb2/basic.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 /><document-info><program-used>pandoc</program-used></document-info></description><body><title><p /></title><annotation><p></p></annotation><section><title><p>Top-level title</p></title><section><title><p>Section</p></title><section><title><p>Subsection</p></title><p>This <emphasis>emphasized</emphasis> <strong>strong</strong> <code>verbatim</code> markdown. See this link<a l:href="#l1" type="note"><sup>[1]</sup></a>.</p><p>Ordered list:</p><p> 1. one</p><p> 2. two</p><p> 3. three</p><cite><p>Blockquote is for citatons.</p></cite><empty-line /><p><code>Code</code></p><p><code>block</code></p><p><code>is</code></p><p><code>for</code></p><p><code>code.</code></p><empty-line /><p><strikethrough>Strikeout</strikethrough> is Pandoc’s extension. Superscript and subscripts too: H<sub>2</sub>O is a liquid<a l:href="#n2" type="note"><sup>[2]</sup></a>. 2<sup>10</sup> is 1024.</p><p>Math is another Pandoc extension: <code>E = m c^2</code>.</p></section></section></section></body><body name="notes"><section id="l1"><title><p>1</p></title><p><code>http://example.com/</code></p></section><section id="n2"><title><p>2</p></title><p>Sometimes.</p></section></body></FictionBook> +<FictionBook xmlns="http://www.gribuser.ru/xml/fictionbook/2.0" xmlns:l="http://www.w3.org/1999/xlink"><description><title-info /><document-info><program-used>pandoc</program-used></document-info></description><body><title><p /></title><annotation><p></p></annotation><section><title><p>Top-level title</p></title><section><title><p>Section</p></title><section><title><p>Subsection</p></title><p>This <emphasis>emphasized</emphasis> <strong>strong</strong> <code>verbatim</code> markdown. See this link<a l:href="#l1" type="note"><sup>[1]</sup></a>.</p><p>Ordered list:</p><p>1. one</p><p>2. two</p><p>3. three</p><cite><p>Blockquote is for citatons.</p></cite><empty-line /><p><code>Code</code></p><p><code>block</code></p><p><code>is</code></p><p><code>for</code></p><p><code>code.</code></p><empty-line /><p><strikethrough>Strikeout</strikethrough> is Pandoc’s extension. Superscript and subscripts too: H<sub>2</sub>O is a liquid<a l:href="#n2" type="note"><sup>[2]</sup></a>. 2<sup>10</sup> is 1024.</p><p>Math is another Pandoc extension: <code>E = m c^2</code>.</p></section></section></section></body><body name="notes"><section id="l1"><title><p>1</p></title><p><code>http://example.com/</code></p></section><section id="n2"><title><p>2</p></title><p>Sometimes.</p></section></body></FictionBook> diff --git a/test/writer.fb2 b/test/writer.fb2 index 63d0bdfbf..8a4986508 100644 --- a/test/writer.fb2 +++ b/test/writer.fb2 @@ -115,8 +115,8 @@ </p> <empty-line /> <p>A list:</p> -<p> 1. item one</p> -<p> 2. item two</p> +<p>1. item one</p> +<p>2. item two</p> <p>Nested block quotes:</p> <cite> <p>nested</p> @@ -191,127 +191,131 @@ <p>• asterisk 2</p> <p>• asterisk 3</p> <p>Asterisks loose:</p> -<p>• asterisk 1<empty-line /> -</p> -<p>• asterisk 2<empty-line /> -</p> -<p>• asterisk 3<empty-line /> -</p> +<p>• asterisk 1</p> +<empty-line /> +<p>• asterisk 2</p> +<empty-line /> +<p>• asterisk 3</p> +<empty-line /> <p>Pluses tight:</p> <p>• Plus 1</p> <p>• Plus 2</p> <p>• Plus 3</p> <p>Pluses loose:</p> -<p>• Plus 1<empty-line /> -</p> -<p>• Plus 2<empty-line /> -</p> -<p>• Plus 3<empty-line /> -</p> +<p>• Plus 1</p> +<empty-line /> +<p>• Plus 2</p> +<empty-line /> +<p>• Plus 3</p> +<empty-line /> <p>Minuses tight:</p> <p>• Minus 1</p> <p>• Minus 2</p> <p>• Minus 3</p> <p>Minuses loose:</p> -<p>• Minus 1<empty-line /> -</p> -<p>• Minus 2<empty-line /> -</p> -<p>• Minus 3<empty-line /> -</p> +<p>• Minus 1</p> +<empty-line /> +<p>• Minus 2</p> +<empty-line /> +<p>• Minus 3</p> +<empty-line /> </section> <section> <title> <p>Ordered</p> </title> <p>Tight:</p> -<p> 1. First</p> -<p> 2. Second</p> -<p> 3. Third</p> +<p>1. First</p> +<p>2. Second</p> +<p>3. Third</p> <p>and:</p> -<p> 1. One</p> -<p> 2. Two</p> -<p> 3. Three</p> +<p>1. One</p> +<p>2. Two</p> +<p>3. Three</p> <p>Loose using tabs:</p> -<p> 1. First<empty-line /> -</p> -<p> 2. Second<empty-line /> -</p> -<p> 3. Third<empty-line /> -</p> +<p>1. First</p> +<empty-line /> +<p>2. Second</p> +<empty-line /> +<p>3. Third</p> +<empty-line /> <p>and using spaces:</p> -<p> 1. One<empty-line /> -</p> -<p> 2. Two<empty-line /> -</p> -<p> 3. Three<empty-line /> -</p> +<p>1. One</p> +<empty-line /> +<p>2. Two</p> +<empty-line /> +<p>3. Three</p> +<empty-line /> <p>Multiple paragraphs:</p> -<p> 1. Item 1, graf one.<empty-line />Item 1. graf two. The quick brown fox jumped over the lazy dog’s back.<empty-line /> -</p> -<p> 2. Item 2.<empty-line /> -</p> -<p> 3. Item 3.<empty-line /> -</p> +<p>1. Item 1, graf one.</p> +<empty-line /> +<p> Item 1. graf two. The quick brown fox jumped over the lazy dog’s back.</p> +<empty-line /> +<p>2. Item 2.</p> +<empty-line /> +<p>3. Item 3.</p> +<empty-line /> </section> <section> <title> <p>Nested</p> </title> -<p>• Tab<p>◦ Tab<p>* Tab</p> -</p> -</p> +<p>• Tab</p> +<p>◦ Tab</p> +<p>* Tab</p> <p>Here’s another:</p> -<p> 1. First</p> -<p> 2. Second:<p> • Fee</p> +<p>1. First</p> +<p>2. Second:</p> +<p> • Fee</p> <p> • Fie</p> <p> • Foe</p> -</p> -<p> 3. Third</p> +<p>3. Third</p> <p>Same thing but with paragraphs:</p> -<p> 1. First<empty-line /> -</p> -<p> 2. Second:<empty-line /> +<p>1. First</p> +<empty-line /> +<p>2. Second:</p> +<empty-line /> <p> • Fee</p> <p> • Fie</p> <p> • Foe</p> -</p> -<p> 3. Third<empty-line /> -</p> +<p>3. Third</p> +<empty-line /> </section> <section> <title> <p>Tabs and spaces</p> </title> -<p>• this is a list item indented with tabs<empty-line /> -</p> -<p>• this is a list item indented with spaces<empty-line /> -<p>◦ this is an example list item indented with tabs<empty-line /> -</p> -<p>◦ this is an example list item indented with spaces<empty-line /> -</p> -</p> +<p>• this is a list item indented with tabs</p> +<empty-line /> +<p>• this is a list item indented with spaces</p> +<empty-line /> +<p>◦ this is an example list item indented with tabs</p> +<empty-line /> +<p>◦ this is an example list item indented with spaces</p> +<empty-line /> </section> <section> <title> <p>Fancy list markers</p> </title> -<p> (2) begins with 2</p> -<p> (3) and now 3<empty-line />with a continuation<empty-line /> -<p> (3) iv. sublist with roman numerals, starting with 4</p> -<p> (3) v. more items<p> (3) v. (A) a subsublist</p> -<p> (3) v. (B) a subsublist</p> -</p> -</p> +<p>(2) begins with 2</p> +<p>(3) and now 3</p> +<empty-line /> +<p> with a continuation</p> +<empty-line /> +<p>(3) iv. sublist with roman numerals, starting with 4</p> +<p>(3) v. more items</p> +<p>(3) v. (A) a subsublist</p> +<p>(3) v. (B) a subsublist</p> <p>Nesting:</p> -<p> A. Upper Alpha<p> A. I. Upper Roman.<p> A. I. (6) Decimal start with 6<p> A. I. (6) c) Lower alpha with paren</p> -</p> -</p> -</p> +<p>A. Upper Alpha</p> +<p>A. I. Upper Roman.</p> +<p>A. I. (6) Decimal start with 6</p> +<p>A. I. (6) c) Lower alpha with paren</p> <p>Autonumbering:</p> -<p> 1. Autonumber.</p> -<p> 2. More.<p> 2. 1. Nested.</p> -</p> +<p>1. Autonumber.</p> +<p>2. More.</p> +<p>2. 1. Nested.</p> <p>Should not be a list item:</p> <p>M.A. 2007</p> <p>B. Williams</p> @@ -427,8 +431,8 @@ <strong>orange</strong> </p> <p> orange fruit<empty-line /> -<p> 1. sublist</p> -<p> 2. sublist</p> +<p>1. sublist</p> +<p>2. sublist</p> </p> </section> <section> @@ -753,7 +757,7 @@ </a> </p> </cite> -<p> 1. And in list items.<a l:href="#n32" type="note"> +<p>1. And in list items.<a l:href="#n32" type="note"> <sup>[32]</sup> </a> </p> |