git.iko.soy/typeable/pandoc
on master help sign in
ConTeXt writer: add function toLabel

This function can be used to sanitize reference labels so that they do not contain any of the illegal characters \#[]",{}%()|= . Currently only Links have their labels sanitized, because they are the only Elements that use passed labels.

commit84b75a1c2a0067021031e5545ab50a521f966907 authorMark Szepieniec <mszepien@gmail.com> dateSun 31 Aug 2014, 16:00 (12 years ago) parent4455905
1 file changed+17−7
src/Text/Pandoc/Writers/ConTeXt.hs+17−7
src/Text/Pandoc/Writers/ConTeXt.hs+17−7
@@ -36,6 +36,7 @@ import Text.Pandoc.Options
3636import Text.Pandoc.Walk (query)
3737import Text.Printf ( printf )
3838import Data.List ( intercalate )
39+import Data.Char ( ord )
3940import Control.Monad.State
4041import Text.Pandoc.Pretty
4142import Text.Pandoc.Templates ( renderTemplate' )
@@ -114,6 +115,13 @@ escapeCharForConTeXt opts ch =
114115stringToConTeXt :: WriterOptions -> String -> String
115116stringToConTeXt opts = concatMap (escapeCharForConTeXt opts)
116117
118+-- | Sanitize labels
119+toLabel :: String -> String
120+toLabel z = concatMap go z
121+ where go x
122+ | elem x "\\#[]\",{}%()|=" = "ux" ++ printf "%x" (ord x)
123+ | otherwise = [x]
124+
117125-- | Convert Elements to ConTeXt
118126elementToConTeXt :: WriterOptions -> Element -> State WriterState Doc
119127elementToConTeXt _ (Blk block) = blockToConTeXt block
@@ -286,15 +294,16 @@ inlineToConTeXt Space = return space
286294-- Handle HTML-like internal document references to sections
287295inlineToConTeXt (Link txt (('#' : ref), _)) = do
288296 opts <- gets stOptions
289− label <- inlineListToConTeXt txt
297+ contents <- inlineListToConTeXt txt
298+ let ref' = toLabel $ stringToConTeXt opts ref
290299 return $ text "\\in"
291300 <> braces (if writerNumberSections opts
292− then label <+> text "(\\S"
293− else label) -- prefix
301+ then contents <+> text "(\\S"
302+ else contents) -- prefix
294303 <> braces (if writerNumberSections opts
295304 then text ")"
296305 else empty) -- suffix
297− <> brackets (text ref)
306+ <> brackets (text ref')
298307
299308inlineToConTeXt (Link txt (src, _)) = do
300309 let isAutolink = txt == [Str (unEscapeString src)]
@@ -302,13 +311,13 @@ inlineToConTeXt (Link txt (src, _)) = do
302311 let next = stNextRef st
303312 put $ st {stNextRef = next + 1}
304313 let ref = "url" ++ show next
305− label <- inlineListToConTeXt txt
314+ contents <- inlineListToConTeXt txt
306315 return $ "\\useURL"
307316 <> brackets (text ref)
308317 <> brackets (text $ escapeStringUsing [('#',"\\#"),('%',"\\%")] src)
309318 <> (if isAutolink
310319 then empty
311− else brackets empty <> brackets label)
320+ else brackets empty <> brackets contents)
312321 <> "\\from"
313322 <> brackets (text ref)
314323inlineToConTeXt (Image _ (src, _)) = do
@@ -337,6 +346,7 @@ sectionHeader (ident,classes,_) hdrLevel lst = do
337346 st <- get
338347 let opts = stOptions st
339348 let level' = if writerChapters opts then hdrLevel - 1 else hdrLevel
349+ let ident' = toLabel ident
340350 let (section, chapter) = if "unnumbered" `elem` classes
341351 then (text "subject", text "title")
342352 else (text "section", text "chapter")
@@ -344,7 +354,7 @@ sectionHeader (ident,classes,_) hdrLevel lst = do
344354 then char '\\'
345355 <> text (concat (replicate (level' - 1) "sub"))
346356 <> section
347− <> (if (not . null) ident then brackets (text ident) else empty)
357+ <> (if (not . null) ident' then brackets (text ident') else empty)
348358 <> braces contents
349359 <> blankline
350360 else if level' == 0