Skip to content

Commit

Permalink
ConTeXt writer: support tagging extension [API Change]
Browse files Browse the repository at this point in the history
Paragraphs are enclosed by `\bpar` and `\epar` commands, and `highlight`
commands are used for emphasis. This results in much better tagging in
PDF output.
  • Loading branch information
tarleb committed Jan 14, 2023
1 parent 8f394a1 commit 422ffa1
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 15 deletions.
3 changes: 3 additions & 0 deletions data/templates/default.context
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ $endif$
\setupxtable[foot][]
\setupxtable[lastrow][bottomframe=on]

$if(emphasis-commands)$
$emphasis-commands$
$endif$
$if(highlighting-commands)$
$highlighting-commands$
$endif$
Expand Down
2 changes: 2 additions & 0 deletions src/Text/Pandoc/Extensions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ data Extension =
| Ext_subscript -- ^ Subscript using ~this~ syntax
| Ext_superscript -- ^ Superscript using ^this^ syntax
| Ext_styles -- ^ Read styles that pandoc doesn't know
| Ext_tagging -- ^ Output optimized for PDF tagging
| Ext_task_lists -- ^ Parse certain list items as task list items
| Ext_table_captions -- ^ Pandoc-style table captions
| Ext_tex_math_dollars -- ^ TeX math between $..$ or $$..$$
Expand Down Expand Up @@ -602,6 +603,7 @@ getAllExtensions f = universalExtensions <> getAll f
[ Ext_smart
, Ext_raw_tex
, Ext_ntb
, Ext_tagging
]
getAll "textile" = autoIdExtensions <>
extensionsFromList
Expand Down
46 changes: 35 additions & 11 deletions src/Text/Pandoc/Writers/ConTeXt.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import Text.Pandoc.Writers.Shared
import Text.Printf (printf)

import qualified Data.List.NonEmpty as NonEmpty
import qualified Data.Map.Strict as Map
import qualified Text.Pandoc.Writers.AnnotatedTable as Ann

data WriterState =
Expand All @@ -52,6 +53,7 @@ data WriterState =
, stNextRef :: Int -- number of next URL reference
, stOptions :: WriterOptions -- writer options
, stOrderedListLevel :: Int -- level of ordered list
, stEmphasisCommands :: Map.Map Text (Doc Text)
}

-- | Table type
Expand All @@ -75,6 +77,7 @@ writeConTeXt options document =
, stNextRef = 1
, stOptions = options
, stOrderedListLevel = 0
, stEmphasisCommands = mempty
}
in evalStateT (pandocToConTeXt options document) defaultWriterState

Expand Down Expand Up @@ -123,6 +126,8 @@ pandocToConTeXt options (Pandoc meta blocks) = do
| all isDigit (d:ds) -> resetField "papersize"
(T.pack ('A':d:ds))
_ -> id)
$ defField "emphasis-commands"
(mconcat $ Map.elems (stEmphasisCommands st))
$ (case writerHighlightStyle options of
Just sty | stHighlighting st ->
defField "highlighting-commands" (styleToConTeXt sty)
Expand Down Expand Up @@ -185,10 +190,20 @@ blockToConTeXt (Div attr@(_,"section":_,_)
footer' <- sectionFooter attr level
innerContents <- blockListToConTeXt xs
return $ header' $$ innerContents $$ footer'
blockToConTeXt (Plain lst) = inlineListToConTeXt lst
blockToConTeXt (Plain lst) = do
opts <- gets stOptions
contents <- inlineListToConTeXt lst
return $
if isEnabled Ext_tagging opts
then "\\bpar{}" <> contents <> "\\epar{}"
else contents
blockToConTeXt (Para lst) = do
opts <- gets stOptions
contents <- inlineListToConTeXt lst
return $ contents <> blankline
return $
if isEnabled Ext_tagging opts
then "\\bpar" $$ contents $$ "\\epar" <> blankline
else contents <> blankline
blockToConTeXt (LineBlock lns) = do
let emptyToBlankline doc = if isEmpty doc
then blankline
Expand Down Expand Up @@ -551,19 +566,31 @@ inlineListToConTeXt lst = liftM hcat $ mapM inlineToConTeXt $ addStruts lst
isSpacey (Str (T.uncons -> Just ('\160',_))) = True
isSpacey _ = False

highlightInlines :: PandocMonad m
=> Text -> (Doc Text) -> [Inline]
-> WM m (Doc Text)
highlightInlines name style inlines = do
opts <- gets stOptions
contents <- inlineListToConTeXt inlines
if not (isEnabled Ext_tagging opts)
then return $ braces (style <> space <> contents)
else do
let cmd = "\\definehighlight " <> brackets (literal name) <>
brackets ("style=" <> braces style)
modify (\st -> st{ stEmphasisCommands =
Map.insert name cmd (stEmphasisCommands st) })
return $ "\\" <> literal name <> braces contents

-- | Convert inline element to ConTeXt
inlineToConTeXt :: PandocMonad m
=> Inline -- ^ Inline to convert
-> WM m (Doc Text)
inlineToConTeXt (Emph lst) = do
contents <- inlineListToConTeXt lst
return $ braces $ "\\em " <> contents
inlineToConTeXt (Emph lst) = highlightInlines "emph" "\\em" lst
inlineToConTeXt (Strong lst) = highlightInlines "strong" "\\bf" lst
inlineToConTeXt (SmallCaps lst) = highlightInlines "smallcaps" "\\sc" lst
inlineToConTeXt (Underline lst) = do
contents <- inlineListToConTeXt lst
return $ "\\underbar" <> braces contents
inlineToConTeXt (Strong lst) = do
contents <- inlineListToConTeXt lst
return $ braces $ "\\bf " <> contents
inlineToConTeXt (Strikeout lst) = do
contents <- inlineListToConTeXt lst
return $ "\\overstrikes" <> braces contents
Expand All @@ -573,9 +600,6 @@ inlineToConTeXt (Superscript lst) = do
inlineToConTeXt (Subscript lst) = do
contents <- inlineListToConTeXt lst
return $ "\\low" <> braces contents
inlineToConTeXt (SmallCaps lst) = do
contents <- inlineListToConTeXt lst
return $ braces $ "\\sc " <> contents
inlineToConTeXt (Code (_ident, classes, _kv) str) = do
let rawCode =
pure . literal $
Expand Down
8 changes: 4 additions & 4 deletions test/writer.context
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ markdown test suite.
\startsectionlevel[title={Level 2 with an \goto{embedded
link}[url(/url)]},reference={level-2-with-an-embedded-link}]

\startsectionlevel[title={Level 3 with
{\em emphasis}},reference={level-3-with-emphasis}]
\startsectionlevel[title={Level 3 with {\em
emphasis}},reference={level-3-with-emphasis}]

\startsectionlevel[title={Level 4},reference={level-4}]

Expand All @@ -105,8 +105,8 @@ link}[url(/url)]},reference={level-2-with-an-embedded-link}]

\startsectionlevel[title={Level 1},reference={level-1}]

\startsectionlevel[title={Level 2 with
{\em emphasis}},reference={level-2-with-emphasis}]
\startsectionlevel[title={Level 2 with {\em
emphasis}},reference={level-2-with-emphasis}]

\startsectionlevel[title={Level 3},reference={level-3}]

Expand Down

0 comments on commit 422ffa1

Please sign in to comment.