2007-11-03 23:27:58 +00:00
|
|
|
{-
|
2008-01-08 17:26:16 +00:00
|
|
|
Copyright (C) 2007-8 John MacFarlane <jgm@berkeley.edu>
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
-}
|
|
|
|
|
|
|
|
{- |
|
|
|
|
Module : Text.Pandoc.Writers.ConTeXt
|
2008-01-08 17:26:16 +00:00
|
|
|
Copyright : Copyright (C) 2007-8 John MacFarlane
|
2007-11-03 23:27:58 +00:00
|
|
|
License : GNU GPL, version 2 or above
|
|
|
|
|
|
|
|
Maintainer : John MacFarlane <jgm@berkeley.edu>
|
|
|
|
Stability : alpha
|
|
|
|
Portability : portable
|
|
|
|
|
|
|
|
Conversion of 'Pandoc' format into ConTeXt.
|
|
|
|
-}
|
|
|
|
module Text.Pandoc.Writers.ConTeXt ( writeConTeXt ) where
|
|
|
|
import Text.Pandoc.Definition
|
|
|
|
import Text.Pandoc.Shared
|
|
|
|
import Text.Printf ( printf )
|
2008-01-16 02:18:23 +00:00
|
|
|
import Data.List ( isSuffixOf )
|
2007-11-03 23:27:58 +00:00
|
|
|
import Control.Monad.State
|
2007-11-15 03:20:05 +00:00
|
|
|
import Text.PrettyPrint.HughesPJ hiding ( Str )
|
2007-11-03 23:27:58 +00:00
|
|
|
|
2007-11-18 01:24:43 +00:00
|
|
|
data WriterState =
|
|
|
|
WriterState { stNextRef :: Int -- number of next URL reference
|
|
|
|
, stOrderedListLevel :: Int -- level of ordered list
|
|
|
|
, stOptions :: WriterOptions -- writer options
|
|
|
|
}
|
|
|
|
|
2008-07-13 23:53:21 +00:00
|
|
|
orderedListStyles :: [[Char]]
|
2007-11-18 01:24:43 +00:00
|
|
|
orderedListStyles = cycle ["[n]","[a]", "[r]", "[g]"]
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
-- | Convert Pandoc to ConTeXt.
|
|
|
|
writeConTeXt :: WriterOptions -> Pandoc -> String
|
2007-11-18 01:24:43 +00:00
|
|
|
writeConTeXt options document =
|
|
|
|
let defaultWriterState = WriterState { stNextRef = 1
|
|
|
|
, stOrderedListLevel = 0
|
|
|
|
, stOptions = options
|
|
|
|
}
|
|
|
|
in render $
|
|
|
|
evalState (pandocToConTeXt options document) defaultWriterState
|
2007-11-03 23:27:58 +00:00
|
|
|
|
2007-11-15 03:20:05 +00:00
|
|
|
pandocToConTeXt :: WriterOptions -> Pandoc -> State WriterState Doc
|
2007-11-03 23:27:58 +00:00
|
|
|
pandocToConTeXt options (Pandoc meta blocks) = do
|
2007-11-18 01:24:43 +00:00
|
|
|
main <- blockListToConTeXt blocks
|
2007-11-15 03:20:05 +00:00
|
|
|
let before = if null (writerIncludeBefore options)
|
|
|
|
then empty
|
|
|
|
else text $ writerIncludeBefore options
|
|
|
|
let after = if null (writerIncludeAfter options)
|
|
|
|
then empty
|
|
|
|
else text $ writerIncludeAfter options
|
|
|
|
let body = before $$ main $$ after
|
2008-07-13 23:53:21 +00:00
|
|
|
head' <- if writerStandalone options
|
|
|
|
then contextHeader options meta
|
|
|
|
else return empty
|
2007-11-03 23:27:58 +00:00
|
|
|
let toc = if writerTableOfContents options
|
2007-11-15 03:20:05 +00:00
|
|
|
then text "\\placecontent\n"
|
|
|
|
else empty
|
2007-11-03 23:27:58 +00:00
|
|
|
let foot = if writerStandalone options
|
2007-11-15 03:20:05 +00:00
|
|
|
then text "\\stoptext\n"
|
|
|
|
else empty
|
2008-07-13 23:53:21 +00:00
|
|
|
return $ head' $$ toc $$ body $$ foot
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
-- | Insert bibliographic information into ConTeXt header.
|
|
|
|
contextHeader :: WriterOptions -- ^ Options, including ConTeXt header
|
|
|
|
-> Meta -- ^ Meta with bibliographic information
|
2007-11-15 03:20:05 +00:00
|
|
|
-> State WriterState Doc
|
2007-11-03 23:27:58 +00:00
|
|
|
contextHeader options (Meta title authors date) = do
|
|
|
|
titletext <- if null title
|
2007-11-15 03:20:05 +00:00
|
|
|
then return empty
|
2007-11-18 01:24:43 +00:00
|
|
|
else inlineListToConTeXt title
|
2007-11-03 23:27:58 +00:00
|
|
|
let authorstext = if null authors
|
|
|
|
then ""
|
|
|
|
else if length authors == 1
|
|
|
|
then stringToConTeXt $ head authors
|
|
|
|
else stringToConTeXt $ (joinWithSep ", " $
|
|
|
|
init authors) ++ " & " ++ last authors
|
|
|
|
let datetext = if date == ""
|
|
|
|
then ""
|
|
|
|
else stringToConTeXt date
|
2007-11-15 03:20:05 +00:00
|
|
|
let titleblock = text "\\doctitle{" <> titletext <> char '}' $$
|
|
|
|
text ("\\author{" ++ authorstext ++ "}") $$
|
|
|
|
text ("\\date{" ++ datetext ++ "}")
|
|
|
|
let header = text $ writerHeader options
|
|
|
|
return $ header $$ titleblock $$ text "\\starttext\n\\maketitle\n"
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
-- escape things as needed for ConTeXt
|
|
|
|
|
|
|
|
escapeCharForConTeXt :: Char -> String
|
|
|
|
escapeCharForConTeXt ch =
|
|
|
|
case ch of
|
2008-07-11 01:24:15 +00:00
|
|
|
'{' -> "\\letteropenbrace{}"
|
|
|
|
'}' -> "\\letterclosebrace{}"
|
|
|
|
'\\' -> "\\letterbackslash{}"
|
|
|
|
'$' -> "\\$"
|
|
|
|
'|' -> "\\letterbar{}"
|
|
|
|
'^' -> "\\letterhat{}"
|
|
|
|
'%' -> "\\%"
|
|
|
|
'~' -> "\\lettertilde{}"
|
|
|
|
'&' -> "\\&"
|
|
|
|
'#' -> "\\#"
|
|
|
|
'<' -> "\\letterless{}"
|
|
|
|
'>' -> "\\lettermore{}"
|
|
|
|
'_' -> "\\letterunderscore{}"
|
|
|
|
'\160' -> "~"
|
|
|
|
x -> [x]
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
-- | Escape string for ConTeXt
|
|
|
|
stringToConTeXt :: String -> String
|
|
|
|
stringToConTeXt = concatMap escapeCharForConTeXt
|
|
|
|
|
|
|
|
-- | Convert Pandoc block element to ConTeXt.
|
2007-11-18 01:24:43 +00:00
|
|
|
blockToConTeXt :: Block
|
2007-12-29 09:31:45 +00:00
|
|
|
-> State WriterState BlockWrapper
|
|
|
|
blockToConTeXt Null = return $ Reg empty
|
2007-11-18 01:24:43 +00:00
|
|
|
blockToConTeXt (Plain lst) = do
|
|
|
|
st <- get
|
|
|
|
let options = stOptions st
|
|
|
|
contents <- wrapTeXIfNeeded options False inlineListToConTeXt lst
|
2007-12-29 09:31:45 +00:00
|
|
|
return $ Reg contents
|
2007-11-18 01:24:43 +00:00
|
|
|
blockToConTeXt (Para lst) = do
|
|
|
|
st <- get
|
|
|
|
let options = stOptions st
|
|
|
|
contents <- wrapTeXIfNeeded options False inlineListToConTeXt lst
|
2007-12-29 09:31:45 +00:00
|
|
|
return $ Pad contents
|
2007-11-18 01:24:43 +00:00
|
|
|
blockToConTeXt (BlockQuote lst) = do
|
|
|
|
contents <- blockListToConTeXt lst
|
2007-12-29 09:31:45 +00:00
|
|
|
return $ Pad $ text "\\startblockquote" $$ contents $$ text "\\stopblockquote"
|
2008-02-09 03:18:22 +00:00
|
|
|
blockToConTeXt (CodeBlock _ str) =
|
2007-12-29 09:31:45 +00:00
|
|
|
return $ Reg $ text $ "\\starttyping\n" ++ str ++ "\n\\stoptyping\n"
|
|
|
|
-- \n because \stoptyping can't have anything after it, inc. }
|
2008-07-13 23:53:21 +00:00
|
|
|
blockToConTeXt (RawHtml _) = return $ Reg empty
|
2007-11-18 01:24:43 +00:00
|
|
|
blockToConTeXt (BulletList lst) = do
|
|
|
|
contents <- mapM listItemToConTeXt lst
|
2007-12-29 09:31:45 +00:00
|
|
|
return $ Pad $ text "\\startitemize" $$ vcat contents $$ text "\\stopitemize"
|
2008-07-13 23:53:21 +00:00
|
|
|
blockToConTeXt (OrderedList (start, style', delim) lst) = do
|
2007-11-18 01:24:43 +00:00
|
|
|
st <- get
|
|
|
|
let level = stOrderedListLevel st
|
|
|
|
put $ st {stOrderedListLevel = level + 1}
|
|
|
|
contents <- mapM listItemToConTeXt lst
|
|
|
|
put $ st {stOrderedListLevel = level}
|
2007-11-16 06:20:25 +00:00
|
|
|
let start' = if start == 1 then "" else "start=" ++ show start
|
|
|
|
let delim' = case delim of
|
|
|
|
DefaultDelim -> ""
|
|
|
|
Period -> "stopper=."
|
|
|
|
OneParen -> "stopper=)"
|
|
|
|
TwoParens -> "left=(,stopper=)"
|
|
|
|
let width = maximum $ map length $ take (length contents)
|
2008-07-13 23:53:21 +00:00
|
|
|
(orderedListMarkers (start, style', delim))
|
2007-11-16 06:20:25 +00:00
|
|
|
let width' = (toEnum width + 1) / 2
|
2008-07-15 00:32:21 +00:00
|
|
|
let width'' = if width' > (1.5 :: Double)
|
2007-11-16 06:20:25 +00:00
|
|
|
then "width=" ++ show width' ++ "em"
|
|
|
|
else ""
|
|
|
|
let specs2Items = filter (not . null) [start', delim', width'']
|
|
|
|
let specs2 = if null specs2Items
|
|
|
|
then ""
|
|
|
|
else "[" ++ joinWithSep "," specs2Items ++ "]"
|
2008-07-13 23:53:21 +00:00
|
|
|
let style'' = case style' of
|
2007-11-18 01:24:43 +00:00
|
|
|
DefaultStyle -> orderedListStyles !! level
|
2007-11-16 06:20:25 +00:00
|
|
|
Decimal -> "[n]"
|
|
|
|
LowerRoman -> "[r]"
|
|
|
|
UpperRoman -> "[R]"
|
|
|
|
LowerAlpha -> "[a]"
|
|
|
|
UpperAlpha -> "[A]"
|
2008-07-13 23:53:21 +00:00
|
|
|
let specs = style'' ++ specs2
|
2007-12-29 09:31:45 +00:00
|
|
|
return $ Pad $ text ("\\startitemize" ++ specs) $$ vcat contents $$
|
|
|
|
text "\\stopitemize"
|
2007-11-18 01:24:43 +00:00
|
|
|
blockToConTeXt (DefinitionList lst) =
|
2007-12-29 09:31:45 +00:00
|
|
|
mapM defListItemToConTeXt lst >>= return . Pad . wrappedBlocksToDoc
|
|
|
|
blockToConTeXt HorizontalRule = return $ Pad $ text "\\thinrule"
|
2007-11-18 01:24:43 +00:00
|
|
|
blockToConTeXt (Header level lst) = do
|
|
|
|
contents <- inlineListToConTeXt lst
|
|
|
|
st <- get
|
|
|
|
let opts = stOptions st
|
2007-11-15 03:20:05 +00:00
|
|
|
let base = if writerNumberSections opts then "section" else "subject"
|
2007-12-29 09:31:45 +00:00
|
|
|
return $ Pad $ if level >= 1 && level <= 5
|
|
|
|
then char '\\' <> text (concat (replicate (level - 1) "sub")) <>
|
|
|
|
text base <> char '{' <> contents <> char '}'
|
|
|
|
else contents
|
2007-11-18 01:24:43 +00:00
|
|
|
blockToConTeXt (Table caption aligns widths heads rows) = do
|
2007-11-03 23:27:58 +00:00
|
|
|
let colWidths = map printDecimal widths
|
|
|
|
let colDescriptor colWidth alignment = (case alignment of
|
|
|
|
AlignLeft -> 'l'
|
|
|
|
AlignRight -> 'r'
|
|
|
|
AlignCenter -> 'c'
|
|
|
|
AlignDefault -> 'l'):
|
|
|
|
"p(" ++ colWidth ++ "\\textwidth)|"
|
|
|
|
let colDescriptors = "|" ++ (concat $
|
|
|
|
zipWith colDescriptor colWidths aligns)
|
2007-11-18 01:24:43 +00:00
|
|
|
headers <- tableRowToConTeXt heads
|
|
|
|
captionText <- inlineListToConTeXt caption
|
2007-11-15 03:20:05 +00:00
|
|
|
let captionText' = if null caption then text "none" else captionText
|
2007-11-18 01:24:43 +00:00
|
|
|
rows' <- mapM tableRowToConTeXt rows
|
2007-12-29 09:31:45 +00:00
|
|
|
return $ Pad $ text "\\placetable[here]{" <> captionText' <> char '}' $$
|
2007-11-15 03:20:05 +00:00
|
|
|
text "\\starttable[" <> text colDescriptors <> char ']' $$
|
|
|
|
text "\\HL" $$ headers $$ text "\\HL" $$
|
2007-12-29 09:31:45 +00:00
|
|
|
vcat rows' $$ text "\\HL\n\\stoptable"
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
printDecimal :: Float -> String
|
|
|
|
printDecimal = printf "%.2f"
|
|
|
|
|
2008-07-13 23:53:21 +00:00
|
|
|
tableRowToConTeXt :: [[Block]] -> State WriterState Doc
|
2007-11-18 01:24:43 +00:00
|
|
|
tableRowToConTeXt cols = do
|
|
|
|
cols' <- mapM blockListToConTeXt cols
|
2007-11-15 03:20:05 +00:00
|
|
|
return $ (vcat (map (text "\\NC " <>) cols')) $$
|
|
|
|
text "\\NC\\AR"
|
2007-11-03 23:27:58 +00:00
|
|
|
|
2008-07-13 23:53:21 +00:00
|
|
|
listItemToConTeXt :: [Block] -> State WriterState Doc
|
2007-11-18 01:24:43 +00:00
|
|
|
listItemToConTeXt list = blockListToConTeXt list >>=
|
2008-08-09 16:50:46 +00:00
|
|
|
return . (text "\\item" $$) . (nest 2)
|
2007-11-03 23:27:58 +00:00
|
|
|
|
2008-07-13 23:53:21 +00:00
|
|
|
defListItemToConTeXt :: ([Inline], [Block]) -> State WriterState BlockWrapper
|
2007-11-18 01:24:43 +00:00
|
|
|
defListItemToConTeXt (term, def) = do
|
|
|
|
term' <- inlineListToConTeXt term
|
|
|
|
def' <- blockListToConTeXt def
|
2007-12-29 09:31:45 +00:00
|
|
|
return $ Pad $ text "\\startdescr{" <> term' <> char '}' $$ def' $$ text "\\stopdescr"
|
|
|
|
|
2007-11-03 23:27:58 +00:00
|
|
|
-- | Convert list of block elements to ConTeXt.
|
2007-11-18 01:24:43 +00:00
|
|
|
blockListToConTeXt :: [Block] -> State WriterState Doc
|
2007-12-29 09:31:45 +00:00
|
|
|
blockListToConTeXt lst = mapM blockToConTeXt lst >>= return . wrappedBlocksToDoc
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
-- | Convert list of inline elements to ConTeXt.
|
2007-11-18 01:24:43 +00:00
|
|
|
inlineListToConTeXt :: [Inline] -- ^ Inlines to convert
|
2007-11-15 03:20:05 +00:00
|
|
|
-> State WriterState Doc
|
2007-11-18 01:24:43 +00:00
|
|
|
inlineListToConTeXt lst = mapM inlineToConTeXt lst >>= return . hcat
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
-- | Convert inline element to ConTeXt
|
2007-11-18 01:24:43 +00:00
|
|
|
inlineToConTeXt :: Inline -- ^ Inline to convert
|
2007-11-15 03:20:05 +00:00
|
|
|
-> State WriterState Doc
|
2007-11-18 01:24:43 +00:00
|
|
|
inlineToConTeXt (Emph lst) = do
|
|
|
|
contents <- inlineListToConTeXt lst
|
2007-11-15 03:20:05 +00:00
|
|
|
return $ text "{\\em " <> contents <> char '}'
|
2007-11-18 01:24:43 +00:00
|
|
|
inlineToConTeXt (Strong lst) = do
|
|
|
|
contents <- inlineListToConTeXt lst
|
2007-11-15 03:20:05 +00:00
|
|
|
return $ text "{\\bf " <> contents <> char '}'
|
2007-11-18 01:24:43 +00:00
|
|
|
inlineToConTeXt (Strikeout lst) = do
|
|
|
|
contents <- inlineListToConTeXt lst
|
2007-11-15 03:20:05 +00:00
|
|
|
return $ text "\\overstrikes{" <> contents <> char '}'
|
2007-11-18 01:24:43 +00:00
|
|
|
inlineToConTeXt (Superscript lst) = do
|
|
|
|
contents <- inlineListToConTeXt lst
|
2007-11-15 03:20:05 +00:00
|
|
|
return $ text "\\high{" <> contents <> char '}'
|
2007-11-18 01:24:43 +00:00
|
|
|
inlineToConTeXt (Subscript lst) = do
|
|
|
|
contents <- inlineListToConTeXt lst
|
2007-11-15 03:20:05 +00:00
|
|
|
return $ text "\\low{" <> contents <> char '}'
|
2008-07-15 23:26:06 +00:00
|
|
|
inlineToConTeXt (SmallCaps lst) = do
|
|
|
|
contents <- inlineListToConTeXt lst
|
|
|
|
return $ text "{\\sc " <> contents <> char '}'
|
2007-11-18 01:24:43 +00:00
|
|
|
inlineToConTeXt (Code str) = return $ text $ "\\type{" ++ str ++ "}"
|
|
|
|
inlineToConTeXt (Quoted SingleQuote lst) = do
|
|
|
|
contents <- inlineListToConTeXt lst
|
2007-11-15 03:20:05 +00:00
|
|
|
return $ text "\\quote{" <> contents <> char '}'
|
2007-11-18 01:24:43 +00:00
|
|
|
inlineToConTeXt (Quoted DoubleQuote lst) = do
|
|
|
|
contents <- inlineListToConTeXt lst
|
2007-11-15 03:20:05 +00:00
|
|
|
return $ text "\\quotation{" <> contents <> char '}'
|
2008-08-04 03:15:12 +00:00
|
|
|
inlineToConTeXt (Cite _ lst) = inlineListToConTeXt lst
|
2007-11-18 01:24:43 +00:00
|
|
|
inlineToConTeXt Apostrophe = return $ char '\''
|
|
|
|
inlineToConTeXt EmDash = return $ text "---"
|
|
|
|
inlineToConTeXt EnDash = return $ text "--"
|
|
|
|
inlineToConTeXt Ellipses = return $ text "\\ldots{}"
|
|
|
|
inlineToConTeXt (Str str) = return $ text $ stringToConTeXt str
|
2007-11-29 08:09:31 +00:00
|
|
|
inlineToConTeXt (Math str) = return $ char '$' <> text str <> char '$'
|
2007-11-18 01:24:43 +00:00
|
|
|
inlineToConTeXt (TeX str) = return $ text str
|
2008-07-13 23:53:21 +00:00
|
|
|
inlineToConTeXt (HtmlInline _) = return empty
|
2007-11-18 01:24:43 +00:00
|
|
|
inlineToConTeXt (LineBreak) = return $ text "\\crlf\n"
|
|
|
|
inlineToConTeXt Space = return $ char ' '
|
|
|
|
inlineToConTeXt (Link [Code str] (src, tit)) = -- since ConTeXt has its own
|
|
|
|
inlineToConTeXt (Link [Str str] (src, tit)) -- way of printing links...
|
|
|
|
inlineToConTeXt (Link txt (src, _)) = do
|
|
|
|
st <- get
|
|
|
|
let next = stNextRef st
|
|
|
|
put $ st {stNextRef = next + 1}
|
2007-11-03 23:27:58 +00:00
|
|
|
let ref = show next
|
2007-11-18 01:24:43 +00:00
|
|
|
label <- inlineListToConTeXt txt
|
2007-11-22 17:33:12 +00:00
|
|
|
return $ text "\\useURL[" <> text ref <> text "][" <> text src <>
|
2007-11-15 03:20:05 +00:00
|
|
|
text "][][" <> label <> text "]\\from[" <> text ref <> char ']'
|
2007-11-18 01:24:43 +00:00
|
|
|
inlineToConTeXt (Image alternate (src, tit)) = do
|
|
|
|
alt <- inlineListToConTeXt alternate
|
2007-11-15 03:20:05 +00:00
|
|
|
return $ text "\\placefigure\n[]\n[fig:" <> alt <> text "]\n{" <>
|
|
|
|
text tit <> text "}\n{\\externalfigure[" <> text src <> text "]}"
|
2007-11-18 01:24:43 +00:00
|
|
|
inlineToConTeXt (Note contents) = do
|
|
|
|
contents' <- blockListToConTeXt contents
|
2007-12-04 04:14:16 +00:00
|
|
|
let rawnote = stripTrailingNewlines $ render contents'
|
|
|
|
-- note: a \n before } is needed when note ends with a \stoptyping
|
|
|
|
let optNewline = "\\stoptyping" `isSuffixOf` rawnote
|
|
|
|
return $ text "\\footnote{" <>
|
|
|
|
text rawnote <> (if optNewline then char '\n' else empty) <> char '}'
|
2007-11-03 23:27:58 +00:00
|
|
|
|