git.iko.soy/typeable/pandoc
on master help sign in
Added support for internal links in ConTeXt writer.

Based on a patch by B. Scott Michel. Also simplified use of \hyphenateurl. We no longer try to go within an Inline list to find URLs. This is resource-heavy, and the main use case is autolinks, which can be readily recognized.

commit2c1569a0da04627b3c4a75c18fbb38d7776e68ed authorJohn MacFarlane <fiddlosopher@gmail.com> dateFri 30 Dec 2011, 14:32 (15 years ago) parente3dfb26
1 file changed+15−30
src/Text/Pandoc/Writers/ConTeXt.hs+15−30
src/Text/Pandoc/Writers/ConTeXt.hs+15−30
@@ -31,7 +31,7 @@ Conversion of 'Pandoc' format into ConTeXt.
3131module Text.Pandoc.Writers.ConTeXt ( writeConTeXt ) where
3232import Text.Pandoc.Definition
3333import Text.Pandoc.Shared
34−import Text.Pandoc.Generic (queryWith, bottomUp)
34+import Text.Pandoc.Generic (queryWith)
3535import Text.Printf ( printf )
3636import Data.List ( intercalate )
3737import Control.Monad.State
@@ -271,35 +271,29 @@ inlineToConTeXt (RawInline "tex" str) = return $ text str
271271inlineToConTeXt (RawInline _ _) = return empty
272272inlineToConTeXt (LineBreak) = return $ text "\\crlf" <> cr
273273inlineToConTeXt Space = return space
274−-- ConTeXT has its own way of printing links
275−inlineToConTeXt (Link [Code _ str] (src, tit)) = inlineToConTeXt (Link [Str str] (src, tit))
274+-- autolink
275+inlineToConTeXt (Link [Code _ str] (src, tit)) = inlineToConTeXt (Link
276+ [RawInline "context" "\\hyphenatedurl{", Str str, RawInline "context" "}"]
277+ (src, tit))
276278-- Handle HTML-like internal document references to sections
277279inlineToConTeXt (Link txt (('#' : ref), _)) = do
278− st <- get
279− let opts = stOptions st
280− let numberedSections = writerNumberSections opts
281− label <- inlineListToConTeXt $ bottomUp hyphenateURL (normalize txt)
282− let hasLabel = (not . isEmpty) label
283− let label' = if hasLabel
284− then label <+> text "("
285− else if numberedSections
286− then text "Section"
287− else empty
288− let label'' = braces $ if hasLabel
289− then text ")"
290− else empty
280+ opts <- gets stOptions
281+ label <- inlineListToConTeXt txt
291282 return $ text "\\in"
292− <> braces label'
293− <> braces label''
283+ <> braces (if writerNumberSections opts
284+ then label <+> text "(\\S"
285+ else label) -- prefix
286+ <> braces (if writerNumberSections opts
287+ then text ")"
288+ else empty) -- suffix
294289 <> brackets (text ref)
295290
296−-- Convert link's text, hyphenating URLs when they're seen (does deep list inspection)
297291inlineToConTeXt (Link txt (src, _)) = do
298292 st <- get
299293 let next = stNextRef st
300294 put $ st {stNextRef = next + 1}
301295 let ref = "url" ++ show next
302− label <- inlineListToConTeXt $ bottomUp hyphenateURL (normalize txt)
296+ label <- inlineListToConTeXt txt
303297 return $ "\\useURL"
304298 <> brackets (text ref)
305299 <> brackets (text $ escapeStringUsing [('#',"\\#")] src)
@@ -328,7 +322,7 @@ sectionHeader :: [Char]
328322 -> [Inline]
329323 -> State WriterState Doc
330324sectionHeader ident hdrLevel lst = do
331− contents <- (inlineListToConTeXt . normalize) lst
325+ contents <- inlineListToConTeXt lst
332326 st <- get
333327 let opts = stOptions st
334328 let level' = if writerChapters opts then hdrLevel - 1 else hdrLevel
@@ -343,12 +337,3 @@ sectionHeader ident hdrLevel lst = do
343337 then "\\chapter{" <> contents <> "}"
344338 else contents <> blankline
345339
346−-- | Convert absolute URLs/URIs to ConTeXt raw inlines so that they are hyphenated.
347−hyphenateURL :: Inline
348− -> Inline
349−hyphenateURL x =
350− case x of
351− (Str str) -> if isAbsoluteURI str
352− then (RawInline "context" ("\\hyphenatedurl{" ++ str ++ "}"))
353− else x
354− _otherwise -> x