git.iko.soy/typeable/pandoc
on master help sign in
Added support for Cite to Markdown reader, and conditional support for citeproc module.

+ The citeproc cabal configuration option sets the _CITEPROC macro, which conditionally includes code for handling citations. + Added Text.Pandoc.Biblio module. + Made highlighting option default to False. git-svn-id: https://pandoc.googlecode.com/svn/trunk@1376 788f1e2b-df1e-0410-8736-df70ead52e1b

commit1bfe1b84a8692c5e2ea8a036208f61612b48e9fb authorfiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b> dateMon 4 Aug 2008, 03:15 (18 years ago) parent4719c78
7 files changed+197−16
Main.hs+58−13 Text/Pandoc/Biblio.hs+76 Text/Pandoc/Definition.hs+2 Text/Pandoc/Readers/Markdown.hs+41−2 Text/Pandoc/Shared.hs+6 Text/Pandoc/Writers/MediaWiki.hs+2 pandoc.cabal+12−1
Main.hs+58−13
@@ -47,6 +47,10 @@ import System.IO ( stdout, stderr )
4747#else
4848import System.IO
4949#endif
50+#ifdef _CITEPROC
51+import Text.CSL
52+import Text.Pandoc.Biblio
53+#endif
5054
5155copyrightMessage :: String
5256copyrightMessage = "\nCopyright (C) 2006-7 John MacFarlane\n" ++
@@ -55,15 +59,25 @@ copyrightMessage = "\nCopyright (C) 2006-7 John MacFarlane\n" ++
5559 "warranty, not even for merchantability or fitness for a particular purpose."
5660
5761compileInfo :: String
58−compileInfo = "Compiled" ++
62+compileInfo =
5963#ifdef _UTF8
60− " with UTF-8 support" ++
64+ " +utf8" ++
65+#else
66+ " -utf8" ++
67+#endif
68+#ifdef _CITEPROC
69+ " +citeproc" ++
70+#else
71+ " -citeproc" ++
72+#endif
73+#ifdef _HIGHLIGHTING
74+ " +highlighting" ++
6175#else
62− " without UTF-8 support" ++
76+ " -highlighting" ++
6377#endif
6478 if null languages
65− then " and without syntax highlighting support."
66− else " and with syntax highlighting support for:\n" ++
79+ then "\n"
80+ else "\nCompiled with syntax highlighting support for:\n" ++
6781 (unlines $ map unwords $ chunk 5 $ map (\s -> s ++ replicate (15 - length s) ' ') languages)
6882
6983-- | Splits a list into groups of at most n.
@@ -137,6 +151,10 @@ data Opt = Opt
137151 , optReferenceLinks :: Bool -- ^ Use reference links in writing markdown, rst
138152 , optWrapText :: Bool -- ^ Wrap text
139153 , optSanitizeHTML :: Bool -- ^ Sanitize HTML
154+#ifdef _CITEPROC
155+ , optModsFile :: String
156+ , optCslFile :: String
157+#endif
140158 }
141159
142160-- | Defaults for command-line options.
@@ -166,6 +184,10 @@ defaultOpts = Opt
166184 , optReferenceLinks = False
167185 , optWrapText = True
168186 , optSanitizeHTML = False
187+#ifdef _CITEPROC
188+ , optModsFile = []
189+ , optCslFile = []
190+#endif
169191 }
170192
171193-- | A list of functions, each transforming the options data structure
@@ -333,13 +355,24 @@ options =
333355 exitWith ExitSuccess)
334356 "FORMAT")
335357 "" -- "Print default header for FORMAT"
336−
358+#ifdef _CITEPROC
359+ , Option "" ["mods"]
360+ (ReqArg
361+ (\arg opt -> return opt { optModsFile = arg} )
362+ "FILENAME")
363+ ""
364+ , Option "" ["csl"]
365+ (ReqArg
366+ (\arg opt -> return opt { optCslFile = arg} )
367+ "FILENAME")
368+ ""
369+#endif
337370 , Option "" ["dump-args"]
338371 (NoArg
339372 (\opt -> return opt { optDumpArgs = True }))
340373 "" -- "Print output filename and arguments to stdout."
341374
342− , Option "" ["ignore-args"]
375+ , Option "" ["ignore-args"]
343376 (NoArg
344377 (\opt -> return opt { optIgnoreArgs = True }))
345378 "" -- "Ignore command-line arguments."
@@ -348,7 +381,7 @@ options =
348381 (NoArg
349382 (\_ -> do
350383 prg <- getProgName
351− hPutStrLn stderr (prg ++ " " ++ pandocVersion ++ "\n" ++ compileInfo ++
384+ hPutStrLn stderr (prg ++ " " ++ pandocVersion ++ compileInfo ++
352385 copyrightMessage)
353386 exitWith $ ExitFailure 4))
354387 "" -- "Print version"
@@ -464,6 +497,10 @@ main = do
464497 , optReferenceLinks = referenceLinks
465498 , optWrapText = wrap
466499 , optSanitizeHTML = sanitize
500+#ifdef _CITEPROC
501+ , optModsFile = modsFile
502+ , optCslFile = cslFile
503+#endif
467504 } = opts
468505
469506 if dumpArgs
@@ -513,11 +550,18 @@ main = do
513550
514551 let standalone' = (standalone && not strict) || writerName' == "odt"
515552
553+#ifdef _CITEPROC
554+ refs <- if null modsFile then return [] else readModsColletionFile modsFile
555+#endif
556+
516557 let startParserState =
517558 defaultParserState { stateParseRaw = parseRaw,
518559 stateTabStop = tabStop,
519560 stateSanitizeHTML = sanitize,
520561 stateStandalone = standalone',
562+#ifdef _CITEPROC
563+ stateCitations = map citeKey refs,
564+#endif
521565 stateSmart = smart || writerName' `elem`
522566 ["latex", "context"],
523567 stateColumns = columns,
@@ -564,11 +608,12 @@ main = do
564608 then putStrLn
565609 else writeFile outputFile . (++ "\n")
566610
567− (readSources sources) >>= writeOutput .
568− writer writerOptions .
569− reader startParserState .
570− tabFilter tabStop .
571− joinWithSep "\n"
611+ fmap (reader startParserState . tabFilter tabStop . joinWithSep "\n")
612+ (readSources sources) >>=
613+#ifdef _CITEPROC
614+ processBiblio cslFile refs >>=
615+#endif
616+ writeOutput . writer writerOptions
572617
573618 where
574619 readSources [] = mapM readSource ["-"]
Text/Pandoc/Biblio.hsnew file+76
@@ -0,0 +1,76 @@
1+{-# LANGUAGE PatternGuards, CPP #-}
2+{-
3+Copyright (C) 2008 Andrea Rossato <andrea.rossato@ing.unitn.it>
4+
5+This program is free software; you can redistribute it and/or modify
6+it under the terms of the GNU General Public License as published by
7+the Free Software Foundation; either version 2 of the License, or
8+(at your option) any later version.
9+
10+This program is distributed in the hope that it will be useful,
11+but WITHOUT ANY WARRANTY; without even the implied warranty of
12+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+GNU General Public License for more details.
14+
15+You should have received a copy of the GNU General Public License
16+along with this program; if not, write to the Free Software
17+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18+-}
19+
20+{- |
21+ Module : Text.Pandoc.Biblio
22+ Copyright : Copyright (C) 2008 Andrea Rossato
23+ License : GNU GPL, version 2 or above
24+
25+ Maintainer : Andrea Rossato <andrea.rossato@ing.unitn.it>
26+ Stability : alpha
27+ Portability : portable
28+-}
29+
30+#ifdef _CITEPROC
31+module Text.Pandoc.Biblio ( processBiblio ) where
32+
33+import Control.Monad ( when )
34+import Data.List
35+import Text.CSL
36+import Text.Pandoc.Definition
37+
38+#else
39+module Text.Pandoc.Biblio () where
40+#endif
41+
42+#ifdef _CITEPROC
43+processBiblio :: String -> [Reference] -> Pandoc -> IO Pandoc
44+processBiblio cf r p
45+ = if null r then return p
46+ else do
47+ when (null cf) $ error "Missing the needed citation style file"
48+ csl <- readCSLFile cf
49+ let groups = queryPandoc getCite p
50+ citations = zip groups . processCitations csl r $ groups
51+ Pandoc m b = processPandoc (processCite citations) p
52+ return $ Pandoc m $ b ++ renderBiblio csl r p
53+
54+renderBiblio :: Style -> [Reference] -> Pandoc -> [Block]
55+renderBiblio s r p
56+ = map (read . renderPandoc') $ processBibliography s refs
57+ where cits = nub . map fst . concat . queryPandoc getCite $ p
58+ refs = getRefs r $ zip cits (repeat "")
59+
60+processCite :: [([Target],[FormattedOutput])] -> Inline -> Inline
61+processCite cs il
62+ | Cite t _ <- il = Cite t (process t)
63+ | otherwise = il
64+ where
65+ process t = case elemIndex t (map fst cs) of
66+ Just i -> read . renderPandoc $ snd (cs !! i)
67+ Nothing -> [Str ("Error processing " ++ show t)]
68+
69+getCite :: Inline -> [[(String,String)]]
70+getCite i | Cite t _ <- i = [t]
71+ | otherwise = []
72+
73+getRefs :: [Reference] -> [Target] -> [Reference]
74+getRefs r = map $ getReference r
75+
76+#endif
Text/Pandoc/Definition.hs+2
@@ -125,8 +125,10 @@ data Inline
125125 | Note [Block] -- ^ Footnote or endnote
126126 deriving (Show, Eq, Read, Typeable, Data)
127127
128+-- | Applies a transformation to matching elements in a Pandoc document.
128129processPandoc :: Typeable a => (a -> a) -> Pandoc -> Pandoc
129130processPandoc f = everywhere (mkT f)
130131
132+-- | Runs a query on matching elements in a Pandoc document.
131133queryPandoc :: Typeable a => (a -> [b]) -> Pandoc -> [b]
132134queryPandoc f = everything (++) ([] `mkQ` f)
Text/Pandoc/Readers/Markdown.hs+41−2
@@ -1,3 +1,4 @@
1+{-# LANGUAGE CPP #-}
12{-
23Copyright (C) 2006-8 John MacFarlane <jgm@berkeley.edu>
34
@@ -34,7 +35,7 @@ module Text.Pandoc.Readers.Markdown (
3435import Data.List ( transpose, isPrefixOf, isSuffixOf, lookup, sortBy, findIndex )
3536import Data.Ord ( comparing )
3637import Data.Char ( isAlphaNum, isAlpha, isLower, isDigit )
37−import Data.Maybe ( fromMaybe )
38+import Data.Maybe
3839import Text.Pandoc.Definition
3940import Text.Pandoc.Shared
4041import Text.Pandoc.Readers.LaTeX ( rawLaTeXInline, rawLaTeXEnvironment )
@@ -173,7 +174,7 @@ parseMarkdown = do
173174 setPosition startPos
174175 -- now parse it for real...
175176 (title, author, date) <- option ([],[],"") titleBlock
176− blocks <- parseBlocks
177+ blocks <- parseBlocks
177178 return $ Pandoc (Meta title author date) $ filter (/= Null) blocks
178179
179180--
@@ -804,6 +805,9 @@ inlineParsers = [ abbrev
804805 , note
805806 , inlineNote
806807 , link
808+#ifdef _CITEPROC
809+ , inlineCitation
810+#endif
807811 , image
808812 , math
809813 , strikeout
@@ -1152,3 +1156,38 @@ rawHtmlInline' = do
11521156 else anyHtmlInlineTag
11531157 return $ HtmlInline result
11541158
1159+#ifdef _CITEPROC
1160+inlineCitation :: GenParser Char ParserState Inline
1161+inlineCitation = try $ do
1162+ failIfStrict
1163+ cit <- citeMarker
1164+ let citations = readWith parseCitation defaultParserState cit
1165+ mr <- mapM chkCit citations
1166+ if catMaybes mr /= []
1167+ then return $ Cite citations []
1168+ else fail "no citation found"
1169+
1170+chkCit :: Target -> GenParser Char ParserState (Maybe Target)
1171+chkCit t = do
1172+ st <- getState
1173+ case lookupKeySrc (stateKeys st) [Str $ fst t] of
1174+ Just _ -> fail "This is a link"
1175+ Nothing -> if elem (fst t) $ stateCitations st
1176+ then return $ Just t
1177+ else return $ Nothing
1178+
1179+citeMarker :: GenParser Char ParserState String
1180+citeMarker = string "[" >> manyTill (noneOf "\t\n") (string "]")
1181+
1182+parseCitation :: GenParser Char ParserState [(String,String)]
1183+parseCitation = try $ sepBy (parseLabel) (oneOf ";")
1184+
1185+parseLabel :: GenParser Char ParserState (String,String)
1186+parseLabel = try $ do
1187+ res <- sepBy (skipSpaces >> many1 (noneOf "@;\n\t")) (oneOf "@")
1188+ case res of
1189+ [lab,loc] -> return (lab, loc)
1190+ [lab] -> return (lab, "" )
1191+ _ -> return ("" , "" )
1192+
1193+#endif
Text/Pandoc/Shared.hs+6
@@ -629,6 +629,9 @@ data ParserState = ParserState
629629 stateQuoteContext :: QuoteContext, -- ^ Inside quoted environment?
630630 stateSanitizeHTML :: Bool, -- ^ Sanitize HTML?
631631 stateKeys :: KeyTable, -- ^ List of reference keys
632+#ifdef _CITEPROC
633+ stateCitations :: [String], -- ^ List of available citations
634+#endif
632635 stateNotes :: NoteTable, -- ^ List of notes
633636 stateTabStop :: Int, -- ^ Tab stop
634637 stateStandalone :: Bool, -- ^ Parse bibliographic info?
@@ -649,6 +652,9 @@ defaultParserState =
649652 stateQuoteContext = NoQuote,
650653 stateSanitizeHTML = False,
651654 stateKeys = [],
655+#ifdef _CITEPROC
656+ stateCitations = [],
657+#endif
652658 stateNotes = [],
653659 stateTabStop = 4,
654660 stateStandalone = False,
Text/Pandoc/Writers/MediaWiki.hs+2
@@ -342,6 +342,8 @@ inlineToMediaWiki opts (Quoted DoubleQuote lst) = do
342342 contents <- inlineListToMediaWiki opts lst
343343 return $ "&ldquo;" ++ contents ++ "&rdquo;"
344344
345+inlineToMediaWiki opts (Cite _ lst) = inlineListToMediaWiki opts lst
346+
345347inlineToMediaWiki _ EmDash = return "&mdash;"
346348
347349inlineToMediaWiki _ EnDash = return "&ndash;"
pandoc.cabal+12−1
@@ -57,7 +57,7 @@ Flag splitBase
5757 Default: True
5858Flag highlighting
5959 Description: Compile in support for syntax highlighting of code blocks.
60− Default: True
60+ Default: False
6161Flag executable
6262 Description: Build the pandoc executable.
6363 Default: True
@@ -67,6 +67,9 @@ Flag library
6767Flag utf8
6868 Description: Compile in support for UTF-8 input and output.
6969 Default: True
70+Flag citeproc
71+ Description: Compile in support for citeproc-hs bibliographic formatting.
72+ Default: False
7073
7174Library
7275 if flag(splitBase)
@@ -79,10 +82,14 @@ Library
7982 if flag(utf8)
8083 Build-depends: utf8-string
8184 cpp-options: -D_UTF8
85+ if flag(citeproc)
86+ Build-depends: citeproc-hs
87+ cpp-options: -D_CITEPROC
8288 Build-Depends: parsec < 3, xhtml, mtl, network, filepath, process, directory,
8389 template-haskell, bytestring
8490 Hs-Source-Dirs: .
8591 Exposed-Modules: Text.Pandoc,
92+ Text.Pandoc.Biblio,
8693 Text.Pandoc.Blocks,
8794 Text.Pandoc.Definition,
8895 Text.Pandoc.CharacterReferences,
@@ -136,7 +143,11 @@ Executable pandoc
136143 cpp-options: -D_HIGHLIGHTING
137144 if flag(utf8)
138145 cpp-options: -D_UTF8
146+ if flag(citeproc)
147+ Build-depends: citeproc-hs
148+ cpp-options: -D_CITEPROC
139149 if flag(executable)
140150 Buildable: True
141151 else
142152 Buildable: False
153+