git.iko.soy/typeable/pandoc
on master help sign in
Correctly implement capitalisation.
ec88d47Artyom Kazak
src/Text/Pandoc/Shared.hs+13
@@ -59,6 +59,7 @@ module Text.Pandoc.Shared (
59 normalizeBlocks,
59 normalizeBlocks,
60 removeFormatting,
60 removeFormatting,
61 stringify,
61 stringify,
62+ capitalize,
62 compactify,
63 compactify,
63 compactify',
64 compactify',
64 compactify'DL,
65 compactify'DL,
@@ -122,6 +123,7 @@ import qualified Data.ByteString.Char8 as B8
122import Text.Pandoc.Compat.Monoid
123import Text.Pandoc.Compat.Monoid
123import Data.ByteString.Base64 (decodeLenient)
124import Data.ByteString.Base64 (decodeLenient)
124import Data.Sequence (ViewR(..), ViewL(..), viewl, viewr)
125import Data.Sequence (ViewR(..), ViewL(..), viewl, viewr)
126+import qualified Data.Text as T (toUpper, pack, unpack)
125
127
126#ifdef EMBED_DATA_FILES
128#ifdef EMBED_DATA_FILES
127import Text.Pandoc.Data (dataFiles)
129import Text.Pandoc.Data (dataFiles)
@@ -527,6 +529,17 @@ stringify = query go . walk deNote
527 deNote (Note _) = Str ""
529 deNote (Note _) = Str ""
528 deNote x = x
530 deNote x = x
529
531
532+-- | Bring all regular text in a pandoc structure to uppercase.
533+--
534+-- This function correctly handles cases where a lowercase character doesn't
535+-- match to a single uppercase character – e.g. “Straße” would be converted
536+-- to “STRASSE”, not “STRAßE”.
537+capitalize :: Walkable Inline a => a -> a
538+capitalize = walk go
539+ where go :: Inline -> Inline
540+ go (Str s) = Str (T.unpack $ T.toUpper $ T.pack s)
541+ go x = x
542+
530-- | Change final list item from @Para@ to @Plain@ if the list contains
543-- | Change final list item from @Para@ to @Plain@ if the list contains
531-- no other @Para@ blocks.
544-- no other @Para@ blocks.
532compactify :: [[Block]] -- ^ List of list items (each a list of blocks)
545compactify :: [[Block]] -- ^ List of list items (each a list of blocks)
src/Text/Pandoc/Writers/FB2.hs+3−8
@@ -28,7 +28,7 @@ module Text.Pandoc.Writers.FB2 (writeFB2) where
28import Control.Monad.State (StateT, evalStateT, get, modify)
28import Control.Monad.State (StateT, evalStateT, get, modify)
29import Control.Monad.State (liftM, liftM2, liftIO)
29import Control.Monad.State (liftM, liftM2, liftIO)
30import Data.ByteString.Base64 (encode)
30import Data.ByteString.Base64 (encode)
31−import Data.Char (toUpper, toLower, isSpace, isAscii, isControl)
31+import Data.Char (toLower, isSpace, isAscii, isControl)
32import Data.List (intersperse, intercalate, isPrefixOf)
32import Data.List (intersperse, intercalate, isPrefixOf)
33import Data.Either (lefts, rights)
33import Data.Either (lefts, rights)
34import Network.Browser (browse, request, setAllowRedirects, setOutHandler)
34import Network.Browser (browse, request, setAllowRedirects, setOutHandler)
@@ -44,8 +44,7 @@ import qualified Text.XML.Light.Cursor as XC
44
44
45import Text.Pandoc.Definition
45import Text.Pandoc.Definition
46import Text.Pandoc.Options (WriterOptions(..), HTMLMathMethod(..), def)
46import Text.Pandoc.Options (WriterOptions(..), HTMLMathMethod(..), def)
47−import Text.Pandoc.Shared (orderedListMarkers, isHeaderBlock)
47+import Text.Pandoc.Shared (orderedListMarkers, isHeaderBlock, capitalize)
48−import Text.Pandoc.Walk
49
48
50-- | Data to be written at the end of the document:
49-- | Data to be written at the end of the document:
51-- (foot)notes, URLs, references, images.
50-- (foot)notes, URLs, references, images.
@@ -421,10 +420,6 @@ indent = indentBlock
421 indentLines ins = let lns = split isLineBreak ins :: [[Inline]]
420 indentLines ins = let lns = split isLineBreak ins :: [[Inline]]
422 in intercalate [LineBreak] $ map ((Str spacer):) lns
421 in intercalate [LineBreak] $ map ((Str spacer):) lns
423
422
424−capitalize :: Inline -> Inline
425−capitalize (Str xs) = Str $ map toUpper xs
426−capitalize x = x
427−
428-- | Convert a Pandoc's Inline element to FictionBook XML representation.
423-- | Convert a Pandoc's Inline element to FictionBook XML representation.
429toXml :: Inline -> FBM [Content]
424toXml :: Inline -> FBM [Content]
430toXml (Str s) = return [txt s]
425toXml (Str s) = return [txt s]
@@ -434,7 +429,7 @@ toXml (Strong ss) = list `liftM` wrap "strong" ss
434toXml (Strikeout ss) = list `liftM` wrap "strikethrough" ss
429toXml (Strikeout ss) = list `liftM` wrap "strikethrough" ss
435toXml (Superscript ss) = list `liftM` wrap "sup" ss
430toXml (Superscript ss) = list `liftM` wrap "sup" ss
436toXml (Subscript ss) = list `liftM` wrap "sub" ss
431toXml (Subscript ss) = list `liftM` wrap "sub" ss
437−toXml (SmallCaps ss) = cMapM toXml $ walk capitalize ss
432+toXml (SmallCaps ss) = cMapM toXml $ capitalize ss
438toXml (Quoted SingleQuote ss) = do -- FIXME: should be language-specific
433toXml (Quoted SingleQuote ss) = do -- FIXME: should be language-specific
439 inner <- cMapM toXml ss
434 inner <- cMapM toXml ss
440 return $ [txt "‘"] ++ inner ++ [txt "’"]
435 return $ [txt "‘"] ++ inner ++ [txt "’"]
src/Text/Pandoc/Writers/Markdown.hs+3−7
@@ -39,7 +39,7 @@ import Text.Pandoc.Writers.Shared
39import Text.Pandoc.Options
39import Text.Pandoc.Options
40import Text.Pandoc.Parsing hiding (blankline, blanklines, char, space)
40import Text.Pandoc.Parsing hiding (blankline, blanklines, char, space)
41import Data.List ( group, isPrefixOf, find, intersperse, transpose, sortBy )
41import Data.List ( group, isPrefixOf, find, intersperse, transpose, sortBy )
42−import Data.Char ( isSpace, isPunctuation, toUpper )
42+import Data.Char ( isSpace, isPunctuation )
43import Data.Ord ( comparing )
43import Data.Ord ( comparing )
44import Text.Pandoc.Pretty
44import Text.Pandoc.Pretty
45import Control.Monad.State
45import Control.Monad.State
@@ -672,10 +672,6 @@ escapeSpaces (Str s) = Str $ substitute " " "\\ " s
672escapeSpaces Space = Str "\\ "
672escapeSpaces Space = Str "\\ "
673escapeSpaces x = x
673escapeSpaces x = x
674
674
675−toCaps :: Inline -> Inline
676−toCaps (Str s) = Str (map toUpper s)
677−toCaps x = x
678−
679-- | Convert Pandoc inline element to markdown.
675-- | Convert Pandoc inline element to markdown.
680inlineToMarkdown :: WriterOptions -> Inline -> State WriterState Doc
676inlineToMarkdown :: WriterOptions -> Inline -> State WriterState Doc
681inlineToMarkdown opts (Span attrs ils) = do
677inlineToMarkdown opts (Span attrs ils) = do
@@ -693,7 +689,7 @@ inlineToMarkdown opts (Emph lst) = do
693inlineToMarkdown opts (Strong lst) = do
689inlineToMarkdown opts (Strong lst) = do
694 plain <- gets stPlain
690 plain <- gets stPlain
695 if plain
691 if plain
696− then inlineListToMarkdown opts $ walk toCaps lst
692+ then inlineListToMarkdown opts $ capitalize lst
697 else do
693 else do
698 contents <- inlineListToMarkdown opts lst
694 contents <- inlineListToMarkdown opts lst
699 return $ "**" <> contents <> "**"
695 return $ "**" <> contents <> "**"
@@ -716,7 +712,7 @@ inlineToMarkdown opts (Subscript lst) = do
716inlineToMarkdown opts (SmallCaps lst) = do
712inlineToMarkdown opts (SmallCaps lst) = do
717 plain <- gets stPlain
713 plain <- gets stPlain
718 if plain
714 if plain
719− then inlineListToMarkdown opts $ walk toCaps lst
715+ then inlineListToMarkdown opts $ capitalize lst
720 else do
716 else do
721 contents <- inlineListToMarkdown opts lst
717 contents <- inlineListToMarkdown opts lst
722 return $ tagWithAttrs "span"
718 return $ tagWithAttrs "span"
tests/Tests/Writers/Plain.hsnew file+21
@@ -0,0 +1,21 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+module Tests.Writers.Plain (tests) where
3+
4+import Test.Framework
5+import Text.Pandoc.Builder
6+import Text.Pandoc
7+import Tests.Helpers
8+import Tests.Arbitrary()
9+
10+
11+infix 4 =:
12+(=:) :: (ToString a, ToPandoc a)
13+ => String -> (a, String) -> Test
14+(=:) = test (writePlain def . toPandoc)
15+
16+
17+tests :: [Test]
18+tests = [ "strongly emphasized text to uppercase"
19+ =: strong "Straße"
20+ =?> "STRASSE"
21+ ]
tests/test-pandoc.hs+2
@@ -17,6 +17,7 @@ import qualified Tests.Writers.HTML
17import qualified Tests.Writers.Docbook
17import qualified Tests.Writers.Docbook
18import qualified Tests.Writers.Native
18import qualified Tests.Writers.Native
19import qualified Tests.Writers.Markdown
19import qualified Tests.Writers.Markdown
20+import qualified Tests.Writers.Plain
20import qualified Tests.Writers.AsciiDoc
21import qualified Tests.Writers.AsciiDoc
21import qualified Tests.Shared
22import qualified Tests.Shared
22import qualified Tests.Walk
23import qualified Tests.Walk
@@ -33,6 +34,7 @@ tests = [ testGroup "Old" Tests.Old.tests
33 , testGroup "HTML" Tests.Writers.HTML.tests
34 , testGroup "HTML" Tests.Writers.HTML.tests
34 , testGroup "Docbook" Tests.Writers.Docbook.tests
35 , testGroup "Docbook" Tests.Writers.Docbook.tests
35 , testGroup "Markdown" Tests.Writers.Markdown.tests
36 , testGroup "Markdown" Tests.Writers.Markdown.tests
37+ , testGroup "Plain" Tests.Writers.Plain.tests
36 , testGroup "AsciiDoc" Tests.Writers.AsciiDoc.tests
38 , testGroup "AsciiDoc" Tests.Writers.AsciiDoc.tests
37 ]
39 ]
38 , testGroup "Readers"
40 , testGroup "Readers"