diff options
| author | Albert Krewinkel <albert@zeitkraut.de> | 2022-06-05 06:49:53 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-06-04 21:49:53 -0700 |
| commit | 9d268e56ed85249b002b6afed4af944e9e89348d (patch) | |
| tree | 4467348104e67378a3dd2f8c538a906c64cf3cbf /src | |
| parent | cc2849ccd0a8bb83d354e0b52b89569c3900db84 (diff) | |
ConTeXt writer: unify link handling (#8096)
Autolinks, i.e. links with content that's the same as the linked URL,
are now marked with the `\url` command. All other links, both internal
and external, are created with the `\goto` command, leading to shorter,
slightly more idiomatic code. As before, autolinks can still be styled
via `\setupurl`, other links via `\setupinteraction`.
Diffstat (limited to 'src')
| -rw-r--r-- | src/Text/Pandoc/Writers/ConTeXt.hs | 55 |
1 files changed, 29 insertions, 26 deletions
diff --git a/src/Text/Pandoc/Writers/ConTeXt.hs b/src/Text/Pandoc/Writers/ConTeXt.hs index 81f03e0ff..51674da87 100644 --- a/src/Text/Pandoc/Writers/ConTeXt.hs +++ b/src/Text/Pandoc/Writers/ConTeXt.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE LambdaCase #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE ViewPatterns #-} @@ -411,34 +412,36 @@ inlineToConTeXt SoftBreak = do WrapNone -> space WrapPreserve -> cr inlineToConTeXt Space = return space --- Handle HTML-like internal document references to sections -inlineToConTeXt (Link _ txt (T.uncons -> Just ('#', ref), _)) = do - opts <- gets stOptions - contents <- inlineListToConTeXt txt - let ref' = toLabel $ stringToConTeXt opts ref - return $ literal "\\goto" - <> braces contents - <> brackets (literal ref') - inlineToConTeXt (Link _ txt (src, _)) = do let isAutolink = txt == [Str (T.pack $ unEscapeString $ T.unpack src)] - st <- get - let next = stNextRef st - put $ st {stNextRef = next + 1} - let ref = "url" <> tshow next - contents <- inlineListToConTeXt txt - let escChar '#' = "\\#" - escChar '%' = "\\%" - escChar c = T.singleton c - let escContextURL = T.concatMap escChar - return $ "\\useURL" - <> brackets (literal ref) - <> brackets (literal $ escContextURL src) - <> (if isAutolink - then empty - else brackets empty <> brackets contents) - <> "\\from" - <> brackets (literal ref) + let escConTeXtURL = T.concatMap $ \case + '#' -> "\\#" + '%' -> "\\%" + c -> T.singleton c + if isAutolink + then do + next <- gets stNextRef + modify $ \st -> st {stNextRef = next + 1} + let ref = "url" <> tshow next + return $ mconcat + [ "\\useURL" + , brackets (literal ref) + , brackets (literal $ escConTeXtURL src) + , "\\from" + , brackets (literal ref) + ] + else do + contents <- inlineListToConTeXt txt + -- Handle HTML-like internal document references to sections + reference <- case T.uncons src of + Just ('#', ref) -> toLabel <$> + (stringToConTeXt <$> gets stOptions <*> pure ref) + _ -> pure $ "url(" <> escConTeXtURL src <> ")" + return $ mconcat + [ "\\goto" + , braces contents + , brackets (literal reference) + ] inlineToConTeXt (Image attr@(_,cls,_) _ (src, _)) = do opts <- gets stOptions let showDim dir = let d = literal (tshow dir) <> "=" |
