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
|
2011-12-30 14:32:49 -08:00
|
|
|
import Text.Pandoc.Generic (queryWith)
|
2007-11-03 23:27:58 +00:00
|
|
|
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 )
|
2011-12-30 11:08:42 -08:00
|
|
|
import Network.URI ( isURI, unEscapeString )
|
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
|
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
|
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) ] ++
|
2011-12-30 11:42:05 -08:00
|
|
|
[ ("number-sections", "yes") | writerNumberSections options ] ++
|
2012-03-07 10:09:17 -08:00
|
|
|
[ ("mainlang", maybe "" (reverse . takeWhile (/=',') . reverse)
|
|
|
|
(lookup "lang" $ writerVariables options)) ] ++
|
2009-12-31 01:16:00 +00:00
|
|
|
[ ("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{}"
|
2012-04-29 14:20:19 -07:00
|
|
|
'%' -> "\\letterpercent "
|
2008-07-11 01:24:15 +00:00
|
|
|
'~' -> "\\lettertilde{}"
|
|
|
|
'&' -> "\\&"
|
|
|
|
'#' -> "\\#"
|
|
|
|
'<' -> "\\letterless{}"
|
|
|
|
'>' -> "\\lettermore{}"
|
2010-10-24 19:31:06 -07:00
|
|
|
'[' -> "{[}"
|
|
|
|
']' -> "{]}"
|
2008-07-11 01:24:15 +00:00
|
|
|
'_' -> "\\letterunderscore{}"
|
|
|
|
'\160' -> "~"
|
2011-12-27 15:45:34 -08:00
|
|
|
'\x2014' -> "---"
|
|
|
|
'\x2013' -> "--"
|
|
|
|
'\x2019' -> "'"
|
|
|
|
'\x2026' -> "\\ldots{}"
|
2008-07-11 01:24:15 +00:00
|
|
|
x -> [x]
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
-- | Escape string for ConTeXt
|
|
|
|
stringToConTeXt :: String -> String
|
|
|
|
stringToConTeXt = concatMap escapeCharForConTeXt
|
|
|
|
|
2011-09-06 20:48:33 -07:00
|
|
|
-- | Convert Elements to ConTeXt
|
|
|
|
elementToConTeXt :: WriterOptions -> Element -> State WriterState Doc
|
|
|
|
elementToConTeXt _ (Blk block) = blockToConTeXt block
|
|
|
|
elementToConTeXt opts (Sec level _ id' title' elements) = do
|
|
|
|
header' <- sectionHeader id' level title'
|
|
|
|
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.
|
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-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
|
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
|
2011-09-06 20:48:33 -07:00
|
|
|
-- If this is ever executed, provide a default for the reference identifier.
|
|
|
|
blockToConTeXt (Header level lst) = sectionHeader "" 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
|
|
|
|
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
|
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
|
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-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)
|
2011-01-26 20:44:25 -08:00
|
|
|
inlineToConTeXt (Code _ str) =
|
2011-01-19 11:53:00 -08:00
|
|
|
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 (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
|
2011-12-30 14:32:49 -08:00
|
|
|
-- autolink
|
|
|
|
inlineToConTeXt (Link [Code _ str] (src, tit)) = inlineToConTeXt (Link
|
|
|
|
[RawInline "context" "\\hyphenatedurl{", Str str, RawInline "context" "}"]
|
|
|
|
(src, tit))
|
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
|
|
|
|
label <- inlineListToConTeXt txt
|
2011-09-06 20:48:33 -07:00
|
|
|
return $ text "\\in"
|
2011-12-30 14:32:49 -08:00
|
|
|
<> braces (if writerNumberSections opts
|
|
|
|
then label <+> text "(\\S"
|
|
|
|
else label) -- prefix
|
|
|
|
<> braces (if writerNumberSections opts
|
|
|
|
then text ")"
|
|
|
|
else empty) -- suffix
|
2011-09-06 20:48:33 -07:00
|
|
|
<> brackets (text ref)
|
|
|
|
|
2011-09-01 13:36:11 -07:00
|
|
|
inlineToConTeXt (Link txt (src, _)) = do
|
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
|
2011-12-30 14:32:49 -08:00
|
|
|
label <- 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)
|
2011-09-06 20:48:33 -07:00
|
|
|
<> brackets empty
|
|
|
|
<> brackets label
|
|
|
|
<> "\\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 _ = []
|
|
|
|
let codeBlocks = queryWith codeBlock contents
|
|
|
|
return $ if null codeBlocks
|
|
|
|
then text "\\footnote{" <> nest 2 contents' <> char '}'
|
|
|
|
else text "\\startbuffer " <> nest 2 contents' <>
|
|
|
|
text "\\stopbuffer\\footnote{\\getbuffer}"
|
2011-09-06 20:48:33 -07:00
|
|
|
|
|
|
|
-- | Craft the section header, inserting the secton reference, if supplied.
|
|
|
|
sectionHeader :: [Char]
|
|
|
|
-> Int
|
|
|
|
-> [Inline]
|
|
|
|
-> State WriterState Doc
|
|
|
|
sectionHeader ident 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
|
|
|
|
return $ if level' >= 1 && level' <= 5
|
|
|
|
then char '\\'
|
|
|
|
<> text (concat (replicate (level' - 1) "sub"))
|
|
|
|
<> text "section"
|
|
|
|
<> (if (not . null) ident then brackets (text ident) else empty)
|
|
|
|
<> braces contents
|
|
|
|
<> blankline
|
|
|
|
else if level' == 0
|
|
|
|
then "\\chapter{" <> contents <> "}"
|
|
|
|
else contents <> blankline
|
|
|
|
|