@@ -36,6 +36,7 @@ import Text.Pandoc.Options
36import Text.Pandoc.Walk (query)
36import Text.Pandoc.Walk (query)
37import Text.Printf ( printf )
37import Text.Printf ( printf )
38import Data.List ( intercalate )
38import Data.List ( intercalate )
39+import Data.Char ( ord )
39import Control.Monad.State
40import Control.Monad.State
40import Text.Pandoc.Pretty
41import Text.Pandoc.Pretty
41import Text.Pandoc.Templates ( renderTemplate' )
42import Text.Pandoc.Templates ( renderTemplate' )
@@ -114,6 +115,13 @@ escapeCharForConTeXt opts ch =
114stringToConTeXt :: WriterOptions -> String -> String
115stringToConTeXt :: WriterOptions -> String -> String
115stringToConTeXt opts = concatMap (escapeCharForConTeXt opts)
116stringToConTeXt opts = concatMap (escapeCharForConTeXt opts)
119+toLabel :: String -> String
120+toLabel z = concatMap go z
122+ | elem x "\\#[]\",{}%()|=" = "ux" ++ printf "%x" (ord x)
117-- | Convert Elements to ConTeXt
125-- | Convert Elements to ConTeXt
118elementToConTeXt :: WriterOptions -> Element -> State WriterState Doc
126elementToConTeXt :: WriterOptions -> Element -> State WriterState Doc
119elementToConTeXt _ (Blk block) = blockToConTeXt block
127elementToConTeXt _ (Blk block) = blockToConTeXt block
@@ -286,15 +294,16 @@ inlineToConTeXt Space = return space
286-- Handle HTML-like internal document references to sections
294-- Handle HTML-like internal document references to sections
287inlineToConTeXt (Link txt (('#' : ref), _)) = do
295inlineToConTeXt (Link txt (('#' : ref), _)) = do
288 opts <- gets stOptions
296 opts <- gets stOptions
289− label <- inlineListToConTeXt txt
297+ contents <- inlineListToConTeXt txt
298+ let ref' = toLabel $ stringToConTeXt opts ref
290 return $ text "\\in"
299 return $ text "\\in"
291 <> braces (if writerNumberSections opts
300 <> braces (if writerNumberSections opts
292− then label <+> text "(\\S"
301+ then contents <+> text "(\\S"
293− else label) -- prefix
302+ else contents) -- prefix
294 <> braces (if writerNumberSections opts
303 <> braces (if writerNumberSections opts
295 then text ")"
304 then text ")"
296 else empty) -- suffix
305 else empty) -- suffix
297− <> brackets (text ref)
306+ <> brackets (text ref')
299inlineToConTeXt (Link txt (src, _)) = do
308inlineToConTeXt (Link txt (src, _)) = do
300 let isAutolink = txt == [Str (unEscapeString src)]
309 let isAutolink = txt == [Str (unEscapeString src)]
@@ -302,13 +311,13 @@ inlineToConTeXt (Link txt (src, _)) = do
302 let next = stNextRef st
311 let next = stNextRef st
303 put $ st {stNextRef = next + 1}
312 put $ st {stNextRef = next + 1}
304 let ref = "url" ++ show next
313 let ref = "url" ++ show next
305− label <- inlineListToConTeXt txt
314+ contents <- inlineListToConTeXt txt
306 return $ "\\useURL"
315 return $ "\\useURL"
307 <> brackets (text ref)
316 <> brackets (text ref)
308 <> brackets (text $ escapeStringUsing [('#',"\\#"),('%',"\\%")] src)
317 <> brackets (text $ escapeStringUsing [('#',"\\#"),('%',"\\%")] src)
309 <> (if isAutolink
318 <> (if isAutolink
310 then empty
319 then empty
311− else brackets empty <> brackets label)
320+ else brackets empty <> brackets contents)
312 <> "\\from"
321 <> "\\from"
313 <> brackets (text ref)
322 <> brackets (text ref)
314inlineToConTeXt (Image _ (src, _)) = do
323inlineToConTeXt (Image _ (src, _)) = do
@@ -337,6 +346,7 @@ sectionHeader (ident,classes,_) hdrLevel lst = do
337 st <- get
346 st <- get
338 let opts = stOptions st
347 let opts = stOptions st
339 let level' = if writerChapters opts then hdrLevel - 1 else hdrLevel
348 let level' = if writerChapters opts then hdrLevel - 1 else hdrLevel
349+ let ident' = toLabel ident
340 let (section, chapter) = if "unnumbered" `elem` classes
350 let (section, chapter) = if "unnumbered" `elem` classes
341 then (text "subject", text "title")
351 then (text "subject", text "title")
342 else (text "section", text "chapter")
352 else (text "section", text "chapter")
@@ -344,7 +354,7 @@ sectionHeader (ident,classes,_) hdrLevel lst = do
344 then char '\\'
354 then char '\\'
345 <> text (concat (replicate (level' - 1) "sub"))
355 <> text (concat (replicate (level' - 1) "sub"))
346 <> section
356 <> section
347− <> (if (not . null) ident then brackets (text ident) else empty)
357+ <> (if (not . null) ident' then brackets (text ident') else empty)
348 <> braces contents
358 <> braces contents
349 <> blankline
359 <> blankline
350 else if level' == 0
360 else if level' == 0