2010-12-20 19:33:05 -08:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2007-11-03 23:27:58 +00:00
|
|
|
{-
|
2010-03-23 13:31:09 -07:00
|
|
|
Copyright (C) 2007-2010 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
|
2010-03-23 13:31:09 -07:00
|
|
|
Copyright : Copyright (C) 2007-2010 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 )
|
2010-12-20 19:33:05 -08:00
|
|
|
import Data.List ( intercalate )
|
2007-11-03 23:27:58 +00:00
|
|
|
import Control.Monad.State
|
2010-12-20 19:33:05 -08:00
|
|
|
import Text.Pandoc.Pretty
|
2009-12-31 01:13:08 +00:00
|
|
|
import Text.Pandoc.Templates ( renderTemplate )
|
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
|
|
|
|
}
|
2009-12-31 01:13:08 +00:00
|
|
|
in evalState (pandocToConTeXt options document) defaultWriterState
|
2007-11-03 23:27:58 +00:00
|
|
|
|
2009-12-31 01:13:08 +00:00
|
|
|
pandocToConTeXt :: WriterOptions -> Pandoc -> State WriterState String
|
|
|
|
pandocToConTeXt options (Pandoc (Meta title authors date) blocks) = do
|
2010-12-20 19:33:05 -08:00
|
|
|
let colwidth = if writerWrapText options
|
|
|
|
then Just $ writerColumns options
|
|
|
|
else Nothing
|
2009-12-31 01:16:55 +00:00
|
|
|
titletext <- if null title
|
|
|
|
then return ""
|
2010-12-20 19:33:05 -08:00
|
|
|
else liftM (render colwidth) $ inlineListToConTeXt title
|
|
|
|
authorstext <- mapM (liftM (render colwidth) . inlineListToConTeXt) authors
|
2009-12-31 01:16:55 +00:00
|
|
|
datetext <- if null date
|
|
|
|
then return ""
|
2010-12-20 19:33:05 -08:00
|
|
|
else liftM (render colwidth) $ inlineListToConTeXt date
|
|
|
|
body <- blockListToConTeXt blocks
|
|
|
|
let main = render colwidth $ body
|
2009-12-31 01:13:08 +00:00
|
|
|
let context = writerVariables options ++
|
|
|
|
[ ("toc", if writerTableOfContents options then "yes" else "")
|
|
|
|
, ("body", main)
|
|
|
|
, ("title", titletext)
|
2009-12-31 01:16:00 +00:00
|
|
|
, ("date", datetext) ] ++
|
|
|
|
[ ("author", a) | a <- authorstext ]
|
2009-12-31 01:13:08 +00:00
|
|
|
return $ if writerStandalone options
|
|
|
|
then renderTemplate context $ writerTemplate options
|
|
|
|
else main
|
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{}"
|
2010-10-24 19:31:06 -07:00
|
|
|
'[' -> "{[}"
|
|
|
|
']' -> "{]}"
|
2008-07-11 01:24:15 +00:00
|
|
|
'_' -> "\\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
|
2010-12-20 19:33:05 -08:00
|
|
|
-> State WriterState Doc
|
|
|
|
blockToConTeXt Null = return empty
|
|
|
|
blockToConTeXt (Plain lst) = inlineListToConTeXt lst
|
2010-03-16 06:45:47 +00:00
|
|
|
blockToConTeXt (Para [Image txt (src,_)]) = do
|
|
|
|
capt <- inlineListToConTeXt txt
|
2010-12-20 19:33:05 -08:00
|
|
|
return $ blankline $$ "\\placefigure[here,nonumber]" <> braces capt <>
|
|
|
|
braces ("\\externalfigure" <> brackets (text src)) <> blankline
|
2007-11-18 01:24:43 +00:00
|
|
|
blockToConTeXt (Para lst) = do
|
2010-12-20 19:33:05 -08:00
|
|
|
contents <- inlineListToConTeXt lst
|
|
|
|
return $ contents <> blankline
|
2007-11-18 01:24:43 +00:00
|
|
|
blockToConTeXt (BlockQuote lst) = do
|
|
|
|
contents <- blockListToConTeXt lst
|
2010-12-20 19:33:05 -08:00
|
|
|
return $ "\\startblockquote" $$ nest 0 contents $$ "\\stopblockquote" <> blankline
|
|
|
|
blockToConTeXt (CodeBlock _ str) =
|
2011-01-22 23:26:44 -08:00
|
|
|
return $ "\\starttyping" <> cr <> flush (text str) <> cr <> "\\stoptyping" $$ blankline
|
2010-12-20 19:33:05 -08:00
|
|
|
-- blankline because \stoptyping can't have anything after it, inc. '}'
|
2011-01-24 09:05:51 -08:00
|
|
|
blockToConTeXt (RawBlock "context" str) = return $ text str <> blankline
|
2011-01-23 10:55:56 -08:00
|
|
|
blockToConTeXt (RawBlock _ _ ) = return empty
|
2010-12-20 19:33:05 -08:00
|
|
|
blockToConTeXt (BulletList lst) = do
|
2007-11-18 01:24:43 +00:00
|
|
|
contents <- mapM listItemToConTeXt lst
|
2010-12-20 19:33:05 -08:00
|
|
|
return $ "\\startitemize" $$ vcat contents $$ text "\\stopitemize" <> blankline
|
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 ""
|
2008-09-08 06:36:28 +00:00
|
|
|
else "[" ++ intercalate "," 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]"
|
2010-03-24 10:51:38 -07:00
|
|
|
Example -> "[n]"
|
2007-11-16 06:20:25 +00:00
|
|
|
LowerRoman -> "[r]"
|
|
|
|
UpperRoman -> "[R]"
|
|
|
|
LowerAlpha -> "[a]"
|
|
|
|
UpperAlpha -> "[A]"
|
2008-07-13 23:53:21 +00:00
|
|
|
let specs = style'' ++ specs2
|
2010-12-20 19:33:05 -08:00
|
|
|
return $ "\\startitemize" <> text specs $$ vcat contents $$
|
|
|
|
"\\stopitemize" <> blankline
|
2007-11-18 01:24:43 +00:00
|
|
|
blockToConTeXt (DefinitionList lst) =
|
2010-12-20 19:33:05 -08:00
|
|
|
liftM vcat $ mapM defListItemToConTeXt lst
|
|
|
|
blockToConTeXt HorizontalRule = return $ "\\thinrule" <> blankline
|
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"
|
2011-01-16 09:08:19 -08:00
|
|
|
let level' = if writerChapters opts then level - 1 else level
|
|
|
|
return $ if level' >= 1 && level' <= 5
|
|
|
|
then char '\\' <> text (concat (replicate (level' - 1) "sub")) <>
|
2010-12-20 19:33:05 -08:00
|
|
|
text base <> char '{' <> contents <> char '}' <> blankline
|
2011-01-16 09:08:19 -08:00
|
|
|
else if level' == 0
|
|
|
|
then "\\chapter{" <> contents <> "}"
|
|
|
|
else contents <> blankline
|
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 colDescriptor colWidth alignment = (case alignment of
|
|
|
|
AlignLeft -> 'l'
|
|
|
|
AlignRight -> 'r'
|
|
|
|
AlignCenter -> 'c'
|
|
|
|
AlignDefault -> 'l'):
|
2009-11-28 03:22:33 +00:00
|
|
|
if colWidth == 0
|
|
|
|
then "|"
|
|
|
|
else ("p(" ++ printf "%.2f" colWidth ++ "\\textwidth)|")
|
2007-11-03 23:27:58 +00:00
|
|
|
let colDescriptors = "|" ++ (concat $
|
2009-11-28 03:22:33 +00:00
|
|
|
zipWith colDescriptor widths aligns)
|
2010-03-07 19:35:41 +00:00
|
|
|
headers <- if all null heads
|
|
|
|
then return empty
|
2010-12-20 19:33:05 -08:00
|
|
|
else liftM ($$ "\\HL") $ tableRowToConTeXt heads
|
2007-11-18 01:24:43 +00:00
|
|
|
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
|
2010-12-20 19:33:05 -08:00
|
|
|
return $ "\\placetable[here]" <> braces captionText' $$
|
|
|
|
"\\starttable" <> brackets (text colDescriptors) $$
|
|
|
|
"\\HL" $$ headers $$
|
|
|
|
vcat rows' $$ "\\HL" $$ "\\stoptable" <> blankline
|
2007-11-03 23:27:58 +00:00
|
|
|
|
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
|
2010-12-20 19:33:05 -08:00
|
|
|
return $ (vcat (map ("\\NC " <>) cols')) $$ "\\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 >>=
|
2010-12-20 19:33:05 -08:00
|
|
|
return . ("\\item" $$) . (nest 2)
|
2007-11-03 23:27:58 +00:00
|
|
|
|
2010-12-20 19:33:05 -08:00
|
|
|
defListItemToConTeXt :: ([Inline], [[Block]]) -> State WriterState Doc
|
2009-12-07 08:26:53 +00:00
|
|
|
defListItemToConTeXt (term, defs) = do
|
2007-11-18 01:24:43 +00:00
|
|
|
term' <- inlineListToConTeXt term
|
2010-12-20 19:33:05 -08:00
|
|
|
def' <- liftM vsep $ mapM blockListToConTeXt defs
|
|
|
|
return $ "\\startdescr" <> braces term' $$ nest 2 def' $$
|
|
|
|
"\\stopdescr" <> blankline
|
2007-12-29 09:31:45 +00:00
|
|
|
|
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
|
2010-12-20 19:33:05 -08:00
|
|
|
blockListToConTeXt lst = liftM vcat $ mapM blockToConTeXt lst
|
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
|
2010-12-20 19:33:05 -08:00
|
|
|
inlineListToConTeXt lst = liftM hcat $ mapM inlineToConTeXt lst
|
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
|
2010-12-20 19:33:05 -08:00
|
|
|
return $ braces $ "\\em " <> contents
|
2007-11-18 01:24:43 +00:00
|
|
|
inlineToConTeXt (Strong lst) = do
|
|
|
|
contents <- inlineListToConTeXt lst
|
2010-12-20 19:33:05 -08:00
|
|
|
return $ braces $ "\\bf " <> contents
|
2007-11-18 01:24:43 +00:00
|
|
|
inlineToConTeXt (Strikeout lst) = do
|
|
|
|
contents <- inlineListToConTeXt lst
|
2010-12-20 19:33:05 -08:00
|
|
|
return $ "\\overstrikes" <> braces contents
|
2007-11-18 01:24:43 +00:00
|
|
|
inlineToConTeXt (Superscript lst) = do
|
|
|
|
contents <- inlineListToConTeXt lst
|
2010-12-20 19:33:05 -08:00
|
|
|
return $ "\\high" <> braces contents
|
2007-11-18 01:24:43 +00:00
|
|
|
inlineToConTeXt (Subscript lst) = do
|
|
|
|
contents <- inlineListToConTeXt lst
|
2010-12-20 19:33:05 -08:00
|
|
|
return $ "\\low" <> braces contents
|
2008-07-15 23:26:06 +00:00
|
|
|
inlineToConTeXt (SmallCaps lst) = do
|
|
|
|
contents <- inlineListToConTeXt lst
|
2010-12-20 19:33:05 -08:00
|
|
|
return $ braces $ "\\sc " <> contents
|
2011-01-19 11:53:00 -08:00
|
|
|
inlineToConTeXt (Code str) | not ('{' `elem` str || '}' `elem` str) =
|
2010-12-20 19:33:05 -08:00
|
|
|
return $ "\\type" <> braces (text str)
|
2011-01-19 11:53:00 -08:00
|
|
|
inlineToConTeXt (Code str) =
|
|
|
|
return $ "\\mono" <> braces (text $ stringToConTeXt str)
|
2007-11-18 01:24:43 +00:00
|
|
|
inlineToConTeXt (Quoted SingleQuote lst) = do
|
|
|
|
contents <- inlineListToConTeXt lst
|
2010-12-20 19:33:05 -08:00
|
|
|
return $ "\\quote" <> braces contents
|
2007-11-18 01:24:43 +00:00
|
|
|
inlineToConTeXt (Quoted DoubleQuote lst) = do
|
|
|
|
contents <- inlineListToConTeXt lst
|
2010-12-20 19:33:05 -08:00
|
|
|
return $ "\\quotation" <> braces contents
|
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 '\''
|
2010-12-20 19:33:05 -08:00
|
|
|
inlineToConTeXt EmDash = return "---"
|
|
|
|
inlineToConTeXt EnDash = return "--"
|
|
|
|
inlineToConTeXt Ellipses = return "\\ldots{}"
|
2007-11-18 01:24:43 +00:00
|
|
|
inlineToConTeXt (Str str) = return $ text $ stringToConTeXt str
|
2010-12-20 19:33:05 -08:00
|
|
|
inlineToConTeXt (Math InlineMath str) =
|
|
|
|
return $ char '$' <> text str <> char '$'
|
|
|
|
inlineToConTeXt (Math DisplayMath str) =
|
|
|
|
return $ text "\\startformula " <> text str <> text " \\stopformula"
|
2011-01-23 10:55:56 -08:00
|
|
|
inlineToConTeXt (RawInline "context" str) = return $ text str
|
2011-01-24 22:13:27 -08:00
|
|
|
inlineToConTeXt (RawInline "tex" str) = return $ text str
|
2011-01-23 10:55:56 -08:00
|
|
|
inlineToConTeXt (RawInline _ _) = return empty
|
2010-12-20 19:33:05 -08:00
|
|
|
inlineToConTeXt (LineBreak) = return $ text "\\crlf" <> cr
|
|
|
|
inlineToConTeXt Space = return space
|
2007-11-18 01:24:43 +00:00
|
|
|
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
|
2010-12-20 19:33:05 -08:00
|
|
|
return $ "\\useURL" <> brackets (text ref) <> brackets (text src) <>
|
|
|
|
brackets empty <> brackets label <>
|
|
|
|
"\\from" <> brackets (text ref)
|
2010-03-16 06:45:47 +00:00
|
|
|
inlineToConTeXt (Image _ (src, _)) = do
|
2010-12-20 19:33:05 -08:00
|
|
|
return $ braces $ "\\externalfigure" <> brackets (text src)
|
2007-11-18 01:24:43 +00:00
|
|
|
inlineToConTeXt (Note contents) = do
|
|
|
|
contents' <- blockListToConTeXt contents
|
2010-12-20 19:33:05 -08:00
|
|
|
return $ text "\\footnote{" <>
|
2011-01-22 12:17:39 -08:00
|
|
|
nest 2 contents' <> char '}'
|