2010-12-20 19:33:05 -08:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2007-11-03 23:27:58 +00:00
|
|
|
{-
|
2015-04-26 10:18:29 -07:00
|
|
|
Copyright (C) 2007-2015 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
|
2015-04-26 10:18:29 -07:00
|
|
|
Copyright : Copyright (C) 2007-2015 John MacFarlane
|
2012-07-26 22:32:53 -07:00
|
|
|
License : GNU GPL, version 2 or above
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
Maintainer : John MacFarlane <jgm@berkeley.edu>
|
2012-07-26 22:32:53 -07:00
|
|
|
Stability : alpha
|
2007-11-03 23:27:58 +00:00
|
|
|
Portability : portable
|
|
|
|
|
|
|
|
Conversion of 'Pandoc' format into ConTeXt.
|
|
|
|
-}
|
|
|
|
module Text.Pandoc.Writers.ConTeXt ( writeConTeXt ) where
|
|
|
|
import Text.Pandoc.Definition
|
|
|
|
import Text.Pandoc.Shared
|
2013-07-01 20:47:26 -07:00
|
|
|
import Text.Pandoc.Writers.Shared
|
2012-07-26 22:59:56 -07:00
|
|
|
import Text.Pandoc.Options
|
2013-08-10 18:13:38 -07:00
|
|
|
import Text.Pandoc.Walk (query)
|
2007-11-03 23:27:58 +00:00
|
|
|
import Text.Printf ( printf )
|
2014-04-30 08:58:10 -07:00
|
|
|
import Data.List ( intercalate )
|
2014-08-31 16:00:17 +02:00
|
|
|
import Data.Char ( ord )
|
2007-11-03 23:27:58 +00:00
|
|
|
import Control.Monad.State
|
2010-12-20 19:33:05 -08:00
|
|
|
import Text.Pandoc.Pretty
|
2013-05-10 22:53:35 -07:00
|
|
|
import Text.Pandoc.Templates ( renderTemplate' )
|
2011-12-30 11:08:42 -08:00
|
|
|
import Network.URI ( isURI, unEscapeString )
|
2007-11-03 23:27:58 +00:00
|
|
|
|
2012-07-26 22:32:53 -07:00
|
|
|
data WriterState =
|
2007-11-18 01:24:43 +00:00
|
|
|
WriterState { stNextRef :: Int -- number of next URL reference
|
|
|
|
, stOrderedListLevel :: Int -- level of ordered list
|
|
|
|
, stOptions :: WriterOptions -- writer options
|
|
|
|
}
|
|
|
|
|
2013-01-07 20:12:13 -08:00
|
|
|
orderedListStyles :: [Char]
|
|
|
|
orderedListStyles = cycle "narg"
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
-- | Convert Pandoc to ConTeXt.
|
|
|
|
writeConTeXt :: WriterOptions -> Pandoc -> String
|
2012-07-26 22:32:53 -07:00
|
|
|
writeConTeXt options document =
|
2007-11-18 01:24:43 +00:00
|
|
|
let defaultWriterState = WriterState { stNextRef = 1
|
|
|
|
, stOrderedListLevel = 0
|
|
|
|
, stOptions = options
|
2012-07-26 22:32:53 -07: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
|
2013-05-10 22:53:35 -07:00
|
|
|
pandocToConTeXt options (Pandoc meta blocks) = do
|
2010-12-20 19:33:05 -08:00
|
|
|
let colwidth = if writerWrapText options
|
|
|
|
then Just $ writerColumns options
|
|
|
|
else Nothing
|
2013-07-01 20:47:26 -07:00
|
|
|
metadata <- metaToJSON options
|
2013-05-10 22:53:35 -07:00
|
|
|
(fmap (render colwidth) . blockListToConTeXt)
|
|
|
|
(fmap (render colwidth) . inlineListToConTeXt)
|
|
|
|
meta
|
2011-09-06 20:48:33 -07:00
|
|
|
body <- mapM (elementToConTeXt options) $ hierarchicalize blocks
|
2011-12-30 16:28:44 -08:00
|
|
|
let main = (render colwidth . vcat) body
|
2013-06-27 22:42:55 -07:00
|
|
|
let context = defField "toc" (writerTableOfContents options)
|
|
|
|
$ defField "placelist" (intercalate ("," :: String) $
|
2013-01-05 18:14:52 -08:00
|
|
|
take (writerTOCDepth options + if writerChapters options
|
|
|
|
then 0
|
|
|
|
else 1)
|
|
|
|
["chapter","section","subsection","subsubsection",
|
|
|
|
"subsubsubsection","subsubsubsubsection"])
|
2013-06-27 22:42:55 -07:00
|
|
|
$ defField "body" main
|
|
|
|
$ defField "number-sections" (writerNumberSections options)
|
|
|
|
$ defField "mainlang" (maybe ""
|
2013-05-10 22:53:35 -07:00
|
|
|
(reverse . takeWhile (/=',') . reverse)
|
|
|
|
(lookup "lang" $ writerVariables options))
|
2013-06-29 22:14:01 -07:00
|
|
|
$ metadata
|
2009-12-31 01:13:08 +00:00
|
|
|
return $ if writerStandalone options
|
2013-05-10 22:53:35 -07:00
|
|
|
then renderTemplate' (writerTemplate options) context
|
2009-12-31 01:13:08 +00:00
|
|
|
else main
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
-- escape things as needed for ConTeXt
|
|
|
|
|
2012-05-11 22:58:49 -07:00
|
|
|
escapeCharForConTeXt :: WriterOptions -> Char -> String
|
|
|
|
escapeCharForConTeXt opts ch =
|
|
|
|
let ligatures = writerTeXLigatures opts in
|
2007-11-03 23:27:58 +00:00
|
|
|
case ch of
|
2012-06-27 15:05:13 -07:00
|
|
|
'{' -> "\\{"
|
|
|
|
'}' -> "\\}"
|
2008-07-11 01:24:15 +00:00
|
|
|
'\\' -> "\\letterbackslash{}"
|
|
|
|
'$' -> "\\$"
|
|
|
|
'|' -> "\\letterbar{}"
|
2012-05-31 19:15:42 -07:00
|
|
|
'%' -> "\\letterpercent{}"
|
2008-07-11 01:24:15 +00:00
|
|
|
'~' -> "\\lettertilde{}"
|
|
|
|
'#' -> "\\#"
|
2010-10-24 19:31:06 -07:00
|
|
|
'[' -> "{[}"
|
|
|
|
']' -> "{]}"
|
2008-07-11 01:24:15 +00:00
|
|
|
'\160' -> "~"
|
2012-05-11 22:58:49 -07:00
|
|
|
'\x2014' | ligatures -> "---"
|
|
|
|
'\x2013' | ligatures -> "--"
|
|
|
|
'\x2019' | ligatures -> "'"
|
2011-12-27 15:45:34 -08:00
|
|
|
'\x2026' -> "\\ldots{}"
|
2008-07-11 01:24:15 +00:00
|
|
|
x -> [x]
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
-- | Escape string for ConTeXt
|
2012-05-11 22:58:49 -07:00
|
|
|
stringToConTeXt :: WriterOptions -> String -> String
|
|
|
|
stringToConTeXt opts = concatMap (escapeCharForConTeXt opts)
|
2007-11-03 23:27:58 +00:00
|
|
|
|
2014-08-31 16:00:17 +02:00
|
|
|
-- | Sanitize labels
|
|
|
|
toLabel :: String -> String
|
|
|
|
toLabel z = concatMap go z
|
|
|
|
where go x
|
2015-01-05 14:41:54 +11:00
|
|
|
| elem x ("\\#[]\",{}%()|=" :: String) = "ux" ++ printf "%x" (ord x)
|
2014-08-31 16:00:17 +02:00
|
|
|
| otherwise = [x]
|
|
|
|
|
2011-09-06 20:48:33 -07:00
|
|
|
-- | Convert Elements to ConTeXt
|
|
|
|
elementToConTeXt :: WriterOptions -> Element -> State WriterState Doc
|
|
|
|
elementToConTeXt _ (Blk block) = blockToConTeXt block
|
2013-02-18 10:55:08 -08:00
|
|
|
elementToConTeXt opts (Sec level _ attr title' elements) = do
|
|
|
|
header' <- sectionHeader attr level title'
|
2011-09-06 20:48:33 -07:00
|
|
|
innerContents <- mapM (elementToConTeXt opts) elements
|
2011-12-30 16:28:44 -08:00
|
|
|
return $ vcat (header' : innerContents)
|
2011-09-06 20:48:33 -07:00
|
|
|
|
2007-11-03 23:27:58 +00:00
|
|
|
-- | Convert Pandoc block element to ConTeXt.
|
2012-07-26 22:32:53 -07:00
|
|
|
blockToConTeXt :: Block
|
2010-12-20 19:33:05 -08:00
|
|
|
-> State WriterState Doc
|
|
|
|
blockToConTeXt Null = return empty
|
|
|
|
blockToConTeXt (Plain lst) = inlineListToConTeXt lst
|
2013-01-15 08:45:46 -08:00
|
|
|
-- title beginning with fig: indicates that the image is a figure
|
|
|
|
blockToConTeXt (Para [Image txt (src,'f':'i':'g':':':_)]) = do
|
2010-03-16 06:45:47 +00:00
|
|
|
capt <- inlineListToConTeXt txt
|
2013-11-30 16:16:35 -08:00
|
|
|
return $ blankline $$ "\\placefigure" <> braces capt <>
|
2010-12-20 19:33:05 -08:00
|
|
|
braces ("\\externalfigure" <> brackets (text src)) <> blankline
|
2012-07-26 22:32:53 -07: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-07-19 16:29:33 -07:00
|
|
|
return $ flush ("\\starttyping" <> cr <> 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
|
2015-05-28 17:46:26 -07:00
|
|
|
blockToConTeXt (Div (ident,_,_) bs) = do
|
|
|
|
contents <- blockListToConTeXt bs
|
|
|
|
if null ident
|
|
|
|
then return contents
|
|
|
|
else return $
|
2015-07-01 11:30:28 -07:00
|
|
|
("\\reference" <> brackets (text $ toLabel ident) <> braces empty <>
|
|
|
|
"%") $$ contents
|
2010-12-20 19:33:05 -08:00
|
|
|
blockToConTeXt (BulletList lst) = do
|
2007-11-18 01:24:43 +00:00
|
|
|
contents <- mapM listItemToConTeXt lst
|
2013-01-07 20:12:13 -08:00
|
|
|
return $ ("\\startitemize" <> if isTightList lst
|
|
|
|
then brackets "packed"
|
|
|
|
else empty) $$
|
|
|
|
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
|
2012-07-26 22:32:53 -07:00
|
|
|
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 -> ""
|
2012-07-26 22:32:53 -07:00
|
|
|
Period -> "stopper=."
|
|
|
|
OneParen -> "stopper=)"
|
2007-11-16 06:20:25 +00:00
|
|
|
TwoParens -> "left=(,stopper=)"
|
2012-07-26 22:32:53 -07:00
|
|
|
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
|
2012-07-26 22:32:53 -07:00
|
|
|
let width'' = if width' > (1.5 :: Double)
|
|
|
|
then "width=" ++ show width' ++ "em"
|
2007-11-16 06:20:25 +00:00
|
|
|
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 ++ "]"
|
2013-01-07 20:12:13 -08:00
|
|
|
let style'' = '[': (case style' of
|
|
|
|
DefaultStyle -> orderedListStyles !! level
|
|
|
|
Decimal -> 'n'
|
|
|
|
Example -> 'n'
|
|
|
|
LowerRoman -> 'r'
|
|
|
|
UpperRoman -> 'R'
|
|
|
|
LowerAlpha -> 'a'
|
|
|
|
UpperAlpha -> 'A') :
|
|
|
|
if isTightList lst then ",packed]" else "]"
|
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
|
2011-09-06 20:48:33 -07:00
|
|
|
-- If this is ever executed, provide a default for the reference identifier.
|
2013-02-18 10:55:08 -08:00
|
|
|
blockToConTeXt (Header level attr lst) = sectionHeader attr level lst
|
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
|
2012-07-26 22:32:53 -07:00
|
|
|
AlignLeft -> 'l'
|
2007-11-03 23:27:58 +00:00
|
|
|
AlignRight -> 'r'
|
|
|
|
AlignCenter -> 'c'
|
|
|
|
AlignDefault -> 'l'):
|
2009-11-28 03:22:33 +00:00
|
|
|
if colWidth == 0
|
|
|
|
then "|"
|
|
|
|
else ("p(" ++ printf "%.2f" colWidth ++ "\\textwidth)|")
|
2012-07-26 22:32:53 -07: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
|
2012-07-26 22:32:53 -07:00
|
|
|
else liftM ($$ "\\HL") $ tableRowToConTeXt heads
|
|
|
|
captionText <- inlineListToConTeXt caption
|
|
|
|
rows' <- mapM tableRowToConTeXt rows
|
2013-11-30 16:16:35 -08:00
|
|
|
return $ "\\placetable" <> (if null caption
|
|
|
|
then brackets "none"
|
|
|
|
else empty)
|
2013-06-28 21:30:27 -07:00
|
|
|
<> braces captionText $$
|
2010-12-20 19:33:05 -08:00
|
|
|
"\\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
|
2011-08-20 15:30:57 -07:00
|
|
|
return $ "\\startdescription" <> braces term' $$ nest 2 def' $$
|
|
|
|
"\\stopdescription" <> 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
|
2012-07-26 22:32:53 -07:00
|
|
|
inlineToConTeXt (Emph lst) = do
|
2007-11-18 01:24:43 +00:00
|
|
|
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-26 20:44:25 -08:00
|
|
|
inlineToConTeXt (Code _ str) | not ('{' `elem` str || '}' `elem` str) =
|
2010-12-20 19:33:05 -08:00
|
|
|
return $ "\\type" <> braces (text str)
|
2012-05-11 22:58:49 -07:00
|
|
|
inlineToConTeXt (Code _ str) = do
|
|
|
|
opts <- gets stOptions
|
|
|
|
return $ "\\mono" <> braces (text $ stringToConTeXt opts 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
|
2012-05-11 22:58:49 -07:00
|
|
|
inlineToConTeXt (Str str) = do
|
|
|
|
opts <- gets stOptions
|
|
|
|
return $ text $ stringToConTeXt opts str
|
2010-12-20 19:33:05 -08:00
|
|
|
inlineToConTeXt (Math InlineMath str) =
|
|
|
|
return $ char '$' <> text str <> char '$'
|
|
|
|
inlineToConTeXt (Math DisplayMath str) =
|
2012-05-29 12:43:11 -07:00
|
|
|
return $ text "\\startformula " <> text str <> text " \\stopformula" <> space
|
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
|
2011-09-06 20:48:33 -07:00
|
|
|
-- Handle HTML-like internal document references to sections
|
|
|
|
inlineToConTeXt (Link txt (('#' : ref), _)) = do
|
2011-12-30 14:32:49 -08:00
|
|
|
opts <- gets stOptions
|
2014-08-31 16:00:17 +02:00
|
|
|
contents <- inlineListToConTeXt txt
|
|
|
|
let ref' = toLabel $ stringToConTeXt opts ref
|
2015-07-01 12:11:25 -07:00
|
|
|
return $ text "\\goto"
|
|
|
|
<> braces contents
|
2014-08-31 16:00:17 +02:00
|
|
|
<> brackets (text ref')
|
2011-09-06 20:48:33 -07:00
|
|
|
|
2011-09-01 13:36:11 -07:00
|
|
|
inlineToConTeXt (Link txt (src, _)) = do
|
2014-08-12 15:49:20 -07:00
|
|
|
let isAutolink = txt == [Str (unEscapeString src)]
|
2007-11-18 01:24:43 +00:00
|
|
|
st <- get
|
|
|
|
let next = stNextRef st
|
|
|
|
put $ st {stNextRef = next + 1}
|
2011-12-30 11:08:42 -08:00
|
|
|
let ref = "url" ++ show next
|
2014-08-31 16:00:17 +02:00
|
|
|
contents <- inlineListToConTeXt txt
|
2011-09-06 20:48:33 -07:00
|
|
|
return $ "\\useURL"
|
|
|
|
<> brackets (text ref)
|
2012-04-29 14:20:19 -07:00
|
|
|
<> brackets (text $ escapeStringUsing [('#',"\\#"),('%',"\\%")] src)
|
2014-04-30 08:58:10 -07:00
|
|
|
<> (if isAutolink
|
|
|
|
then empty
|
2014-08-31 16:00:17 +02:00
|
|
|
else brackets empty <> brackets contents)
|
2011-09-06 20:48:33 -07:00
|
|
|
<> "\\from"
|
|
|
|
<> brackets (text ref)
|
2010-03-16 06:45:47 +00:00
|
|
|
inlineToConTeXt (Image _ (src, _)) = do
|
2011-12-30 11:08:42 -08:00
|
|
|
let src' = if isURI src
|
2011-07-16 14:04:19 -07:00
|
|
|
then src
|
|
|
|
else unEscapeString src
|
|
|
|
return $ braces $ "\\externalfigure" <> brackets (text src')
|
2007-11-18 01:24:43 +00:00
|
|
|
inlineToConTeXt (Note contents) = do
|
|
|
|
contents' <- blockListToConTeXt contents
|
2011-08-18 19:23:34 -07:00
|
|
|
let codeBlock x@(CodeBlock _ _) = [x]
|
|
|
|
codeBlock _ = []
|
2013-08-10 18:13:38 -07:00
|
|
|
let codeBlocks = query codeBlock contents
|
2011-08-18 19:23:34 -07:00
|
|
|
return $ if null codeBlocks
|
|
|
|
then text "\\footnote{" <> nest 2 contents' <> char '}'
|
|
|
|
else text "\\startbuffer " <> nest 2 contents' <>
|
|
|
|
text "\\stopbuffer\\footnote{\\getbuffer}"
|
2013-08-08 23:14:12 -07:00
|
|
|
inlineToConTeXt (Span _ ils) = inlineListToConTeXt ils
|
2011-09-06 20:48:33 -07:00
|
|
|
|
|
|
|
-- | Craft the section header, inserting the secton reference, if supplied.
|
2013-02-18 10:55:08 -08:00
|
|
|
sectionHeader :: Attr
|
2011-09-06 20:48:33 -07:00
|
|
|
-> Int
|
|
|
|
-> [Inline]
|
|
|
|
-> State WriterState Doc
|
2013-02-18 10:55:08 -08:00
|
|
|
sectionHeader (ident,classes,_) hdrLevel lst = do
|
2011-12-30 14:32:49 -08:00
|
|
|
contents <- inlineListToConTeXt lst
|
2011-09-06 20:48:33 -07:00
|
|
|
st <- get
|
|
|
|
let opts = stOptions st
|
|
|
|
let level' = if writerChapters opts then hdrLevel - 1 else hdrLevel
|
2014-08-31 16:00:17 +02:00
|
|
|
let ident' = toLabel ident
|
2013-02-18 10:55:08 -08:00
|
|
|
let (section, chapter) = if "unnumbered" `elem` classes
|
|
|
|
then (text "subject", text "title")
|
|
|
|
else (text "section", text "chapter")
|
2011-09-06 20:48:33 -07:00
|
|
|
return $ if level' >= 1 && level' <= 5
|
|
|
|
then char '\\'
|
|
|
|
<> text (concat (replicate (level' - 1) "sub"))
|
2013-02-18 10:55:08 -08:00
|
|
|
<> section
|
2014-08-31 16:00:17 +02:00
|
|
|
<> (if (not . null) ident' then brackets (text ident') else empty)
|
2011-09-06 20:48:33 -07:00
|
|
|
<> braces contents
|
|
|
|
<> blankline
|
|
|
|
else if level' == 0
|
2013-02-18 10:55:08 -08:00
|
|
|
then char '\\' <> chapter <> braces contents
|
2011-09-06 20:48:33 -07:00
|
|
|
else contents <> blankline
|
|
|
|
|