git.iko.soy/typeable/pandoc
on master help sign in
Txt2Tags Reader: Added recognition of macros
commit43304d6bd6ab920a150b0e27d24239151015ddb2 authorMatthew Pickering <matthewtpickering@gmail.com> dateSun 20 Jul 2014, 17:00 (12 years ago) parentab3589f
3 files changed+34−11
pandoc.hs+14−6 src/Text/Pandoc.hs+2−1 src/Text/Pandoc/Readers/Txt2Tags.hs+18−4
pandoc.hs+14−6
@@ -65,6 +65,9 @@ import qualified Data.Map as M
6565import Data.Yaml (decode)
6666import qualified Data.Yaml as Yaml
6767import qualified Data.Text as T
68+import Control.Applicative ((<$>))
69+import Text.Pandoc.Readers.Txt2Tags (getT2TMeta)
70+import Data.List (intersperse)
6871
6972copyrightMessage :: String
7073copyrightMessage = "\nCopyright (C) 2006-2014 John MacFarlane\n" ++
@@ -180,7 +183,7 @@ data Opt = Opt
180183 , optTeXLigatures :: Bool -- ^ Use TeX ligatures for quotes/dashes
181184 , optDefaultImageExtension :: String -- ^ Default image extension
182185 , optTrace :: Bool -- ^ Print debug information
183− , optTrackChanges :: TrackChanges -- ^ Accept or reject MS Word track-changes.
186+ , optTrackChanges :: TrackChanges -- ^ Accept or reject MS Word track-changes.
184187 }
185188
186189-- | Defaults for command-line options.
@@ -1055,9 +1058,14 @@ main = do
10551058 else e
10561059 Right w -> return w
10571060
1058− reader <- case getReader readerName' of
1059− Right r -> return r
1060− Left e -> err 7 e
1061+ let concatInput = concat (intersperse ", " sources)
1062+ reader <- if "t2t" == readerName'
1063+ then (mkStringReader .
1064+ readTxt2Tags) <$>
1065+ (getT2TMeta concatInput outputFile)
1066+ else case getReader readerName' of
1067+ Right r -> return r
1068+ Left e -> err 7 e
10611069
10621070 let standalone' = standalone || not (isTextFormat writerName') || pdfOutput
10631071
@@ -1189,11 +1197,11 @@ main = do
11891197 else return
11901198
11911199 doc <- case reader of
1192− StringReader r->
1200+ StringReader r->
11931201 readSources sources >>=
11941202 handleIncludes' . convertTabs . intercalate "\n" >>=
11951203 r readerOpts
1196− ByteStringReader r -> readFiles sources >>= r readerOpts
1204+ ByteStringReader r -> readFiles sources >>= r readerOpts
11971205
11981206
11991207 let doc0 = M.foldWithKey setMeta doc metadata
src/Text/Pandoc.hs+2−1
@@ -63,6 +63,7 @@ module Text.Pandoc
6363 , writers
6464 -- * Readers: converting /to/ Pandoc format
6565 , Reader (..)
66+ , mkStringReader
6667 , readDocx
6768 , readMarkdown
6869 , readMediaWiki
@@ -230,7 +231,7 @@ readers = [ ("native" , StringReader $ \_ s -> return $ readNative s)
230231 ,("latex" , mkStringReader readLaTeX)
231232 ,("haddock" , mkStringReader readHaddock)
232233 ,("docx" , mkBSReader readDocx)
233− ,("t2t" , mkStringReader readTxt2Tags)
234+ ,("t2t" , mkStringReader readTxt2TagsNoMacros)
234235 ]
235236
236237data Writer = PureStringWriter (WriterOptions -> Pandoc -> String)
src/Text/Pandoc/Readers/Txt2Tags.hs+18−4
@@ -12,7 +12,7 @@ import Text.Pandoc.Builder ( Inlines, Blocks, (<>)
1212import Text.Pandoc.Definition
1313import Text.Pandoc.Options
1414import Text.Pandoc.Shared (escapeURI,compactify', compactify'DL)
15−import Text.Pandoc.Parsing hiding (space, spaces, uri)
15+import Text.Pandoc.Parsing hiding (space, spaces, uri, macro)
1616import Control.Applicative ((<$>), (<$), (<*>), (<*), (*>))
1717import Data.Char (toLower)
1818import Data.List (transpose, intersperse, intercalate)
@@ -23,12 +23,14 @@ import Control.Monad (void, guard, when)
2323import Data.Default
2424import Control.Monad.Reader (Reader, runReader, asks)
2525
26−type T2T = Parser String ParserState
27−
26+import Data.Time.LocalTime (getZonedTime)
27+import System.Directory (getModificationTime)
28+import Data.Time.Format (formatTime)
29+import System.Locale (defaultTimeLocale)
30+import System.IO.Error (catchIOError)
2831
2932type T2T = ParserT String ParserState (Reader T2TMeta)
3033
31−
3234-- | An object for the T2T macros meta information
3335-- the contents of each field is simply substituted verbatim into the file
3436data T2TMeta = T2TMeta {
@@ -295,6 +297,7 @@ blockMarkupLine p f s = try (f <$> (string s *> space *> p))
295297comment :: Monoid a => T2T a
296298comment = try $ do
297299 atStart
300+ notFollowedBy macro
298301 mempty <$ (char '%' *> anyLine)
299302
300303-- Inline
@@ -306,6 +309,7 @@ inline :: T2T Inlines
306309inline = do
307310 choice
308311 [ endline
312+ , macro
309313 , commentLine
310314 , whitespace
311315 , url
@@ -405,6 +409,16 @@ imageLink = try $ do
405409 l <- manyTill (noneOf "\n\r ") (char ']')
406410 return (B.link l "" body)
407411
412+macro :: T2T Inlines
413+macro = try $ do
414+ name <- string "%%" *> oneOfStringsCI (map fst commands)
415+ optional (try $ enclosed (char '(') (char ')') anyChar)
416+ lookAhead (spaceChar <|> oneOf specialChars <|> newline)
417+ maybe (return mempty) (\f -> B.str <$> asks f) (lookup name commands)
418+ where
419+ commands = [ ("date", date), ("mtime", mtime)
420+ , ("infile", infile), ("outfile", outfile)]
421+
408422-- raw URLs in text are automatically linked
409423url :: T2T Inlines
410424url = try $ do