aboutsummaryrefslogtreecommitdiff
path: root/src/Text/Pandoc/Readers/Txt2Tags.hs
diff options
context:
space:
mode:
authordespresc <christian.j.j.despres@gmail.com>2019-11-04 16:12:37 -0500
committerJohn MacFarlane <jgm@berkeley.edu>2019-11-12 16:03:45 -0800
commit90e436d49604e3fd1ef9432fb23f6d7f6245c7fd (patch)
tree4e7f0692f989643189f1fc6786050d95e239a0ea /src/Text/Pandoc/Readers/Txt2Tags.hs
parentd3966372f5049eea56213b069fc4d70d8af9144c (diff)
downloadpandoc-90e436d49604e3fd1ef9432fb23f6d7f6245c7fd.tar.gz
Switch to new pandoc-types and use Text instead of String [API change].
PR #5884. + Use pandoc-types 1.20 and texmath 0.12. + Text is now used instead of String, with a few exceptions. + In the MediaBag module, some of the types using Strings were switched to use FilePath instead (not Text). + In the Parsing module, new parsers `manyChar`, `many1Char`, `manyTillChar`, `many1TillChar`, `many1Till`, `manyUntil`, `mantyUntilChar` have been added: these are like their unsuffixed counterparts but pack some or all of their output. + `glob` in Text.Pandoc.Class still takes String since it seems to be intended as an interface to Glob, which uses strings. It seems to be used only once in the package, in the EPUB writer, so that is not hard to change.
Diffstat (limited to 'src/Text/Pandoc/Readers/Txt2Tags.hs')
-rw-r--r--src/Text/Pandoc/Readers/Txt2Tags.hs132
1 files changed, 66 insertions, 66 deletions
diff --git a/src/Text/Pandoc/Readers/Txt2Tags.hs b/src/Text/Pandoc/Readers/Txt2Tags.hs
index 0af52e046..996a818fd 100644
--- a/src/Text/Pandoc/Readers/Txt2Tags.hs
+++ b/src/Text/Pandoc/Readers/Txt2Tags.hs
@@ -1,4 +1,5 @@
{-# LANGUAGE NoImplicitPrelude #-}
+{-# LANGUAGE OverloadedStrings #-}
{- |
Module : Text.Pandoc.Readers.Txt2Tags
Copyright : Copyright (C) 2014 Matthew Pickering
@@ -18,7 +19,6 @@ import Prelude
import Control.Monad (guard, void, when)
import Control.Monad.Except (catchError, throwError)
import Control.Monad.Reader (Reader, asks, runReader)
-import Data.Char (toLower)
import Data.Default
import Data.List (intercalate, transpose)
import Data.Maybe (fromMaybe)
@@ -36,13 +36,13 @@ import Text.Pandoc.Parsing hiding (space, spaces, uri)
import Text.Pandoc.Shared (compactify, compactifyDL, crFilter, escapeURI,
underlineSpan)
-type T2T = ParserT String ParserState (Reader T2TMeta)
+type T2T = ParserT Text ParserState (Reader T2TMeta)
-- | An object for the T2T macros meta information
-- the contents of each field is simply substituted verbatim into the file
data T2TMeta = T2TMeta {
- date :: String -- ^ Current date
- , mtime :: String -- ^ Last modification time of infile
+ date :: Text -- ^ Current date
+ , mtime :: Text -- ^ Last modification time of infile
, infile :: FilePath -- ^ Input file
, outfile :: FilePath -- ^ Output file
} deriving Show
@@ -63,7 +63,7 @@ getT2TMeta = do
_ -> catchError
(maximum <$> mapM getModTime inps)
(const (return ""))
- return $ T2TMeta curDate curMtime (intercalate ", " inps) outp
+ return $ T2TMeta (T.pack curDate) (T.pack curMtime) (intercalate ", " inps) outp
-- | Read Txt2Tags from an input string returning a Pandoc document
readTxt2Tags :: PandocMonad m
@@ -74,14 +74,14 @@ readTxt2Tags opts s = do
meta <- getT2TMeta
let parsed = flip runReader meta $
readWithM parseT2T (def {stateOptions = opts}) $
- T.unpack (crFilter s) ++ "\n\n"
+ crFilter s <> "\n\n"
case parsed of
Right result -> return result
Left e -> throwError e
-- | Read Txt2Tags (ignoring all macros) from an input string returning
-- a Pandoc document
--- readTxt2TagsNoMacros :: PandocMonad m => ReaderOptions -> String -> m Pandoc
+-- readTxt2TagsNoMacros :: PandocMonad m => ReaderOptions -> Text -> m Pandoc
-- readTxt2TagsNoMacros = readTxt2Tags
parseT2T :: T2T Pandoc
@@ -106,7 +106,7 @@ parseHeader = do
header :: T2T ()
header = titleline >> authorline >> dateline
-headerline :: B.ToMetaValue a => String -> T2T a -> T2T ()
+headerline :: B.ToMetaValue a => Text -> T2T a -> T2T ()
headerline field p = (() <$ try blankline)
<|> (p >>= updateState . B.setMeta field)
@@ -123,15 +123,15 @@ authorline =
dateline :: T2T ()
dateline = headerline "date" (trimInlines . mconcat <$> manyTill inline newline)
-type Keyword = String
-type Value = String
+type Keyword = Text
+type Value = Text
setting :: T2T (Keyword, Value)
setting = do
string "%!"
- keyword <- ignoreSpacesCap (many1 alphaNum)
+ keyword <- ignoreSpacesCap (many1Char alphaNum)
char ':'
- value <- ignoreSpacesCap (manyTill anyChar newline)
+ value <- ignoreSpacesCap (manyTillChar anyChar newline)
return (keyword, value)
-- Blocks
@@ -163,10 +163,10 @@ balancedTitle c = try $ do
spaces
level <- length <$> many1 (char c)
guard (level <= 5) -- Max header level 5
- heading <- manyTill (noneOf "\n\r") (count level (char c))
+ heading <- manyTillChar (noneOf "\n\r") (count level (char c))
label <- optionMaybe (enclosed (char '[') (char ']') (alphaNum <|> oneOf "_-"))
many spaceChar *> newline
- let attr = maybe nullAttr (\x -> (x, [], [])) label
+ let attr = maybe nullAttr (\x -> (T.pack x, [], [])) label
return $ B.headerWith attr level (trimInlines $ B.text heading)
para :: T2T Blocks
@@ -192,7 +192,7 @@ quote :: T2T Blocks
quote = try $ do
lookAhead tab
rawQuote <- many1 (tab *> optional spaces *> anyLine)
- contents <- parseFromString' parseBlocks (intercalate "\n" rawQuote ++ "\n\n")
+ contents <- parseFromString' parseBlocks (T.intercalate "\n" rawQuote <> "\n\n")
return $ B.blockQuote contents
commentLine :: T2T Inlines
@@ -243,17 +243,17 @@ listItem start end = try $ do
markerLength <- try start
firstLine <- anyLineNewline
blank <- option "" ("\n" <$ blankline)
- rest <- concat <$> many (listContinuation markerLength)
- parseFromString' end $ firstLine ++ blank ++ rest
+ rest <- T.concat <$> many (listContinuation markerLength)
+ parseFromString' end $ firstLine <> blank <> rest
-- continuation of a list item - indented and separated by blankline or endline.
-- Note: nested lists are parsed as continuations.
listContinuation :: Int
- -> T2T String
+ -> T2T Text
listContinuation markerLength = try $
notFollowedBy' (blankline >> blankline)
- *> (mappend <$> (concat <$> many1 listLine)
- <*> many blankline)
+ *> (mappend <$> (T.concat <$> many1 listLine)
+ <*> manyChar blankline)
where listLine = try $ indentWith markerLength *> anyLineNewline
-- Table
@@ -327,16 +327,16 @@ taggedBlock = do
-- Generic
-genericBlock :: Monoid a => T2T a -> (a -> Blocks) -> String -> T2T Blocks
+genericBlock :: Monoid a => T2T a -> (a -> Blocks) -> Text -> T2T Blocks
genericBlock p f s = blockMarkupArea p f s <|> blockMarkupLine p f s
-blockMarkupArea :: Monoid a => T2T a -> (a -> Blocks) -> String -> T2T Blocks
+blockMarkupArea :: Monoid a => T2T a -> (a -> Blocks) -> Text -> T2T Blocks
blockMarkupArea p f s = try (do
- string s *> blankline
- f . mconcat <$> manyTill p (eof <|> void (string s *> blankline)))
+ textStr s *> blankline
+ f . mconcat <$> manyTill p (eof <|> void (textStr s *> blankline)))
-blockMarkupLine :: T2T a -> (a -> Blocks) -> String -> T2T Blocks
-blockMarkupLine p f s = try (f <$> (string s *> space *> p))
+blockMarkupLine :: T2T a -> (a -> Blocks) -> Text -> T2T Blocks
+blockMarkupLine p f s = try (f <$> (textStr s *> space *> p))
-- Can be in either block or inline position
comment :: Monoid a => T2T a
@@ -385,15 +385,15 @@ italic :: T2T Inlines
italic = inlineMarkup inline B.emph '/' B.str
code :: T2T Inlines
-code = inlineMarkup ((:[]) <$> anyChar) B.code '`' id
+code = inlineMarkup (T.singleton <$> anyChar) B.code '`' id
raw :: T2T Inlines
-raw = inlineMarkup ((:[]) <$> anyChar) B.text '"' id
+raw = inlineMarkup (T.singleton <$> anyChar) B.text '"' id
tagged :: T2T Inlines
tagged = do
target <- getTarget
- inlineMarkup ((:[]) <$> anyChar) (B.rawInline target) '\'' id
+ inlineMarkup (T.singleton <$> anyChar) (B.rawInline target) '\'' id
-- Parser for markup indicated by a double character.
-- Inline markup is greedy and glued
@@ -404,33 +404,33 @@ inlineMarkup :: Monoid a
=> T2T a -- Content parser
-> (a -> Inlines) -- Constructor
-> Char -- Fence
- -> (String -> a) -- Special Case to handle ******
+ -> (Text -> a) -- Special Case to handle ******
-> T2T Inlines
inlineMarkup p f c special = try $ do
- start <- many1 (char c)
- let l = length start
+ start <- many1Char (char c)
+ let l = T.length start
guard (l >= 2)
when (l == 2) (void $ notFollowedBy space)
-- We must make sure that there is no space before the start of the
-- closing tags
- body <- optionMaybe (try $ manyTill (noneOf "\n\r")
+ body <- optionMaybe (try $ manyTillChar (noneOf "\n\r")
(try $ lookAhead (noneOf " " >> string [c,c] )))
case body of
Just middle -> do
lastChar <- anyChar
- end <- many1 (char c)
+ end <- many1Char (char c)
let parser inp = parseFromString' (mconcat <$> many p) inp
- let start' = case drop 2 start of
+ let start' = case T.drop 2 start of
"" -> mempty
xs -> special xs
- body' <- parser (middle ++ [lastChar])
- let end' = case drop 2 end of
+ body' <- parser (middle <> T.singleton lastChar)
+ let end' = case T.drop 2 end of
"" -> mempty
xs -> special xs
return $ f (start' `mappend` body' `mappend` end')
Nothing -> do -- Either bad or case such as *****
guard (l >= 5)
- let body' = replicate (l - 4) c
+ let body' = T.replicate (l - 4) $ T.singleton c
return $ f (special body')
link :: T2T Inlines
@@ -441,12 +441,12 @@ titleLink :: T2T Inlines
titleLink = try $ do
char '['
notFollowedBy space
- tokens <- sepBy1 (many $ noneOf " ]") space
+ tokens <- sepBy1 (manyChar $ noneOf " ]") space
guard (length tokens >= 2)
char ']'
let link' = last tokens
- guard $ not $ null link'
- let tit = unwords (init tokens)
+ guard $ not $ T.null link'
+ let tit = T.unwords (init tokens)
return $ B.link link' "" (B.text tit)
-- Link with image
@@ -455,7 +455,7 @@ imageLink = try $ do
char '['
body <- image
many1 space
- l <- manyTill (noneOf "\n\r ") (char ']')
+ l <- manyTillChar (noneOf "\n\r ") (char ']')
return (B.link l "" body)
macro :: T2T Inlines
@@ -466,7 +466,7 @@ macro = try $ do
maybe (return mempty) (\f -> B.str <$> asks f) (lookup name commands)
where
commands = [ ("date", date), ("mtime", mtime)
- , ("infile", infile), ("outfile", outfile)]
+ , ("infile", T.pack . infile), ("outfile", T.pack . outfile)]
-- raw URLs in text are automatically linked
url :: T2T Inlines
@@ -474,7 +474,7 @@ url = try $ do
(rawUrl, escapedUrl) <- try uri <|> emailAddress
return $ B.link rawUrl "" (B.str escapedUrl)
-uri :: T2T (String, String)
+uri :: T2T (Text, Text)
uri = try $ do
address <- t2tURI
return (address, escapeURI address)
@@ -486,25 +486,25 @@ uri = try $ do
--isT2TURI (parse t2tURI "" -> Right _) = True
--isT2TURI _ = False
-t2tURI :: T2T String
+t2tURI :: T2T Text
t2tURI = do
- start <- try ((++) <$> proto <*> urlLogin) <|> guess
- domain <- many1 chars
- sep <- many (char '/')
- form' <- option mempty ((:) <$> char '?' <*> many1 form)
- anchor' <- option mempty ((:) <$> char '#' <*> many anchor)
- return (start ++ domain ++ sep ++ form' ++ anchor')
+ start <- try ((<>) <$> proto <*> urlLogin) <|> guess
+ domain <- many1Char chars
+ sep <- manyChar (char '/')
+ form' <- option mempty (T.cons <$> char '?' <*> many1Char form)
+ anchor' <- option mempty (T.cons <$> char '#' <*> manyChar anchor)
+ return (start <> domain <> sep <> form' <> anchor')
where
protos = ["http", "https", "ftp", "telnet", "gopher", "wais"]
- proto = (++) <$> oneOfStrings protos <*> string "://"
- guess = (++) <$> (((++) <$> stringAnyCase "www" <*> option mempty ((:[]) <$> oneOf "23"))
- <|> stringAnyCase "ftp") <*> ((:[]) <$> char '.')
+ proto = (<>) <$> oneOfStrings protos <*> textStr "://"
+ guess = (<>) <$> (((<>) <$> stringAnyCase "www" <*> option mempty (T.singleton <$> oneOf "23"))
+ <|> stringAnyCase "ftp") <*> (T.singleton <$> char '.')
login = alphaNum <|> oneOf "_.-"
- pass = many (noneOf " @")
+ pass = manyChar (noneOf " @")
chars = alphaNum <|> oneOf "%._/~:,=$@&+-"
anchor = alphaNum <|> oneOf "%._0"
form = chars <|> oneOf ";*"
- urlLogin = option mempty $ try ((\x y z -> x ++ y ++ [z]) <$> many1 login <*> option mempty ((:) <$> char ':' <*> pass) <*> char '@')
+ urlLogin = option mempty $ try ((\x y z -> x <> y <> T.singleton z) <$> many1Char login <*> option mempty (T.cons <$> char ':' <*> pass) <*> char '@')
image :: T2T Inlines
@@ -512,12 +512,12 @@ image = try $ do
-- List taken from txt2tags source
let extensions = [".jpg", ".jpeg", ".gif", ".png", ".eps", ".bmp"]
char '['
- (path, ext) <- manyUntil (noneOf "\n\t\r ") (oneOfStrings extensions)
+ (path, ext) <- manyUntilChar (noneOf "\n\t\r ") (oneOfStrings extensions)
char ']'
- return $ B.image (path ++ ext) "" mempty
+ return $ B.image (path <> ext) "" mempty
-- Characters used in markup
-specialChars :: String
+specialChars :: [Char]
specialChars = "%*-_/|:+;"
tab :: T2T Char
@@ -526,8 +526,8 @@ tab = char '\t'
space :: T2T Char
space = char ' '
-spaces :: T2T String
-spaces = many space
+spaces :: T2T Text
+spaces = manyChar space
endline :: T2T Inlines
endline = try $ do
@@ -544,17 +544,17 @@ endline = try $ do
return B.softbreak
str :: T2T Inlines
-str = try $ B.str <$> many1 (noneOf $ specialChars ++ "\n\r ")
+str = try $ B.str <$> many1Char (noneOf $ specialChars ++ "\n\r ")
whitespace :: T2T Inlines
whitespace = try $ B.space <$ spaceChar
symbol :: T2T Inlines
-symbol = B.str . (:[]) <$> oneOf specialChars
+symbol = B.str . T.singleton <$> oneOf specialChars
-- Utility
-getTarget :: T2T String
+getTarget :: T2T Text
getTarget = do
mv <- lookupMeta "target" . stateMeta <$> getState
return $ case mv of
@@ -565,5 +565,5 @@ getTarget = do
atStart :: T2T ()
atStart = (sourceColumn <$> getPosition) >>= guard . (== 1)
-ignoreSpacesCap :: T2T String -> T2T String
-ignoreSpacesCap p = map toLower <$> (spaces *> p <* spaces)
+ignoreSpacesCap :: T2T Text -> T2T Text
+ignoreSpacesCap p = T.toLower <$> (spaces *> p <* spaces)