git.iko.soy/typeable/pandoc
on master help sign in
Merge pull request #1553 from mpickering/master

Txt2Tags reader: Header is now parsed only if standalone flag is set

commit17f71002ae099fe1bbd4ffda4a979bb6c5bab9e1 authorJohn MacFarlane <jgm@berkeley.edu> dateThu 21 Aug 2014, 16:42 (12 years ago) parent604e1daaa80805
5 files changed+48−16
src/Text/Pandoc/Readers/Txt2Tags.hs+43−12 tests/Tests/Old.hs+1−1 tests/Tests/Readers/Txt2Tags.hs+1−1 tests/txt2tags.native+1 tests/txt2tags.t2t+2−2
src/Text/Pandoc/Readers/Txt2Tags.hs+43−12
@@ -73,11 +73,13 @@ instance Default T2TMeta where
7373getT2TMeta :: [FilePath] -> FilePath -> IO T2TMeta
7474getT2TMeta inps out = do
7575 curDate <- formatTime defaultTimeLocale "%F" <$> getZonedTime
76− let getModTime = fmap (formatTime defaultTimeLocale "%F") .
76+ let getModTime = fmap (formatTime defaultTimeLocale "%T") .
7777 getModificationTime
78− curMtime <- catchIOError
79− (maximum <$> mapM getModTime inps)
80− (const (return ""))
78+ curMtime <- case inps of
79+ [] -> formatTime defaultTimeLocale "%T" <$> getZonedTime
80+ _ -> catchIOError
81+ (maximum <$> mapM getModTime inps)
82+ (const (return ""))
8183 return $ T2TMeta curDate curMtime (intercalate ", " inps) out
8284
8385-- | Read Txt2Tags from an input string returning a Pandoc document
@@ -91,13 +93,42 @@ readTxt2TagsNoMacros = readTxt2Tags def
9193
9294parseT2T :: T2T Pandoc
9395parseT2T = do
94− _ <- (Nothing <$ try blankline) <|> (Just <$> (count 3 anyLine))
96+ -- Parse header if standalone flag is set
97+ standalone <- getOption readerStandalone
98+ when standalone parseHeader
99+ body <- mconcat <$> manyTill block eof
100+ meta' <- stateMeta <$> getState
101+ return $ Pandoc meta' (B.toList body)
102+
103+parseHeader :: T2T ()
104+parseHeader = do
105+ () <$ try blankline <|> header
106+ meta <- stateMeta <$> getState
107+ optional blanklines
95108 config <- manyTill setting (notFollowedBy setting)
96109 -- TODO: Handle settings better
97− let settings = foldr (\(k,v) -> B.setMeta k (MetaString v)) nullMeta config
98− updateState (\s -> s {stateMeta = settings})
99− body <- mconcat <$> manyTill block eof
100− return $ Pandoc mempty (B.toList body)
110+ let settings = foldr (\(k,v) -> B.setMeta k (MetaString v)) meta config
111+ updateState (\s -> s {stateMeta = settings}) <* optional blanklines
112+
113+header :: T2T ()
114+header = titleline >> authorline >> dateline
115+
116+headerline :: B.ToMetaValue a => String -> T2T a -> T2T ()
117+headerline field p = (() <$ try blankline)
118+ <|> (p >>= updateState . B.setMeta field)
119+
120+titleline :: T2T ()
121+titleline =
122+ headerline "title" (trimInlines . mconcat <$> manyTill inline newline)
123+
124+authorline :: T2T ()
125+authorline =
126+ headerline "author" (sepBy author (char ';') <* newline)
127+ where
128+ author = trimInlines . mconcat <$> many (notFollowedBy (char ';' <|> newline) >> inline)
129+
130+dateline :: T2T ()
131+dateline = headerline "date" (trimInlines . mconcat <$> manyTill inline newline)
101132
102133type Keyword = String
103134type Value = String
@@ -242,7 +273,7 @@ indentWith n = count n space
242273
243274table :: T2T Blocks
244275table = try $ do
245− header <- fmap snd <$> option mempty (try headerRow)
276+ tableHeader <- fmap snd <$> option mempty (try headerRow)
246277 rows <- many1 (many commentLine *> tableRow)
247278 let columns = transpose rows
248279 let ncolumns = length columns
@@ -250,7 +281,7 @@ table = try $ do
250281 let rows' = map (map snd) rows
251282 let size = maximum (map length rows')
252283 let rowsPadded = map (pad size) rows'
253− let headerPadded = if (not (null header)) then pad size header else mempty
284+ let headerPadded = if (not (null tableHeader)) then pad size tableHeader else mempty
254285 return $ B.table mempty
255286 (zip aligns (replicate ncolumns 0.0))
256287 headerPadded rowsPadded
@@ -497,7 +528,7 @@ image = try $ do
497528
498529-- Characters used in markup
499530specialChars :: String
500−specialChars = "%*-_/|:+"
531+specialChars = "%*-_/|:+;"
501532
502533tab :: T2T Char
503534tab = char '\t'
tests/Tests/Old.hs+1−1
@@ -141,7 +141,7 @@ tests = [ testGroup "markdown"
141141 "haddock-reader.haddock" "haddock-reader.native"
142142 ]
143143 , testGroup "txt2tags"
144− [ test "reader" ["-r", "t2t", "-w", "native"]
144+ [ test "reader" ["-r", "t2t", "-w", "native", "-s"]
145145 "txt2tags.t2t" "txt2tags.native" ]
146146 , testGroup "epub" [
147147 test "features" ["-r", "epub", "-w", "native"]
tests/Tests/Readers/Txt2Tags.hs+1−1
@@ -12,7 +12,7 @@ import Data.Monoid (mempty, mconcat)
1212import Text.Pandoc.Readers.Txt2Tags
1313
1414t2t :: String -> Pandoc
15−t2t s = readTxt2Tags (T2TMeta "date" "mtime" "in" "out") def ('\n' : s)
15+t2t s = readTxt2Tags (T2TMeta "date" "mtime" "in" "out") def s
1616
1717infix 4 =:
1818(=:) :: ToString c
tests/txt2tags.native+1
@@ -1,3 +1,4 @@
1+Pandoc (Meta {unMeta = fromList [("author",MetaList [MetaInlines [Str "author"]]),("date",MetaInlines [Str "date"]),("includeconf",MetaString "rules.conf"),("title",MetaInlines [Str "Txt2tags",Space,Str "Markup",Space,Str "Rules"])]})
12[Para [Str "This",Space,Str "document",Space,Str "describes",Space,Str "all",Space,Str "the",Space,Str "details",Space,Str "about",Space,Str "each",Space,Str "txt2tags",Space,Str "mark.",Space,Str "The",Space,Str "target",Space,Str "audience",Space,Str "are",Space,Strong [Str "experienced"],Space,Str "users.",Space,Str "You",Space,Str "may",Space,Str "find",Space,Str "it",Space,Str "useful",Space,Str "if",Space,Str "you",Space,Str "want",Space,Str "to",Space,Str "master",Space,Str "the",Space,Str "marks",Space,Str "or",Space,Str "solve",Space,Str "a",Space,Str "specific",Space,Str "problem",Space,Str "about",Space,Str "a",Space,Str "mark."]
23,Para [Str "If",Space,Str "you",Space,Str "are",Space,Str "new",Space,Str "to",Space,Str "txt2tags",Space,Str "or",Space,Str "just",Space,Str "want",Space,Str "to",Space,Str "know",Space,Str "which",Space,Str "are",Space,Str "the",Space,Str "available",Space,Str "marks,",Space,Str "please",Space,Str "read",Space,Str "the",Space,Link [Str "Markup",Space,Str "Demo"] ("MARKUPDEMO",""),Str "."]
34,Para [Str "Note",Space,Str "1:",Space,Str "This",Space,Str "document",Space,Str "is",Space,Str "generated",Space,Str "directly",Space,Str "from",Space,Str "the",Space,Str "txt2tags",Space,Str "test-suite.",Space,Str "All",Space,Str "the",Space,Str "rules",Space,Str "mentioned",Space,Str "here",Space,Str "are",Space,Str "100%",Space,Str "in",Space,Str "sync",Space,Str "with",Space,Str "the",Space,Str "current",Space,Str "program",Space,Str "code."]
tests/txt2tags.t2t+2−2
@@ -1,6 +1,6 @@
11Txt2tags Markup Rules
2−
3−
2+author
3+date
44%!includeconf: rules.conf
55
66This document describes all the details about each txt2tags mark.