Skip to content

Commit

Permalink
OpenDocument writer: implement syntax highlighting.
Browse files Browse the repository at this point in the history
Still unimplemented: global background colors, line numbers.

Closes #6710, obsoletes #6717.
  • Loading branch information
jgm committed Aug 5, 2023
1 parent 889dcbe commit 0873895
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 51 deletions.
6 changes: 4 additions & 2 deletions data/odt/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,16 @@ xmlns:css3t="http://www.w3.org/TR/css3-text/" office:version="1.3">
fo:font-size="10pt" style:font-name-asian="Courier New"
style:font-size-asian="10pt"
style:font-name-complex="Courier New"
style:font-size-complex="10pt" />
style:font-size-complex="10pt"
fo:language="zxx" />
</style:style>
<style:style style:name="Source_Text" style:family="text">
<style:text-properties style:font-name="Courier New"
fo:font-size="10pt" style:font-name-asian="Courier New"
style:font-size-asian="10pt"
style:font-name-complex="Courier New"
style:font-size-complex="10pt" />
style:font-size-complex="10pt"
fo:language="zxx" />
</style:style>
<style:style style:name="Definition_20_Term"
style:display-name="Definition Term" style:family="paragraph"
Expand Down
24 changes: 14 additions & 10 deletions src/Text/Pandoc/Writers/OpenDocument.hs
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,9 @@ blockToOpenDocument o = \case
if isNothing (writerHighlightStyle o)
then unhighlighted s
else case highlight (writerSyntaxMap o) formatOpenDocument attrs s of
Right h -> flush . vcat <$> mapM inPreformattedTags h
Right h -> return $ flush . vcat $ map (inTags True "text:p"
[("text:style-name",
"Preformatted_20_Text")] . hcat) h
Left msg -> do
unless (T.null msg) $ report $ CouldNotHighlight msg
unhighlighted s
Expand Down Expand Up @@ -642,7 +644,8 @@ inlineToOpenDocument o ils
Note l -> mkNote l
where
unhighlighted s = inlinedCode $ preformatted s
inlinedCode s = return $ inTags False "text:span" [("text:style-name", "Source_Text")] s
inlinedCode s = return $ inTags False "text:span"
[("text:style-name", "Source_Text")] s
mkImg (_, _, kvs) s _ = do
id' <- gets stImageId
modify (\st -> st{ stImageId = id' + 1 })
Expand Down Expand Up @@ -922,21 +925,22 @@ withLangFromAttr (_,_,kvs) action =
action

styleToOpenDocument :: Style -> Doc Text
styleToOpenDocument style = vcat (parStyle : map toStyle alltoktypes)
styleToOpenDocument style = vcat (map toStyle alltoktypes)
where alltoktypes = enumFromTo KeywordTok NormalTok
toStyle toktype = inTags True "style:style" [("style:name", tshow toktype),
("style:family", "text")] $
selfClosingTag "style:text-properties"
(tokColor toktype ++ tokBgColor toktype)
(tokColor toktype ++ tokBgColor toktype ++
[("fo:font-style", "italic") |
tokFeature tokenItalic toktype ] ++
[("fo:font-weight", "bold") |
tokFeature tokenBold toktype ] ++
[("style:text-underline-style", "solid") |
tokFeature tokenUnderline toktype ])
tokStyles = tokenStyles style
tokFeatures f toktype = maybe False f $ M.lookup toktype tokStyles
tokFeature f toktype = maybe False f $ M.lookup toktype tokStyles
tokColor toktype = maybe [] (\c -> [("fo:color", T.pack (fromColor c))])
$ (tokenColor =<< M.lookup toktype tokStyles)
`mplus` defaultColor style
tokBgColor toktype = maybe [] (\c -> [("fo:background-color", T.pack (fromColor c))])
$ (tokenBackground =<< M.lookup toktype tokStyles)
`mplus` backgroundColor style
parStyle = inTags True "w:style" [("style:name", "SourceCode"),
("style:family", "paragraph"),
("style:class", "text")] mempty

24 changes: 11 additions & 13 deletions test/command/6792.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@
<style:font-face style:name="Courier New" style:font-family-generic="modern" style:font-pitch="fixed" svg:font-family="'Courier New'" />
</office:font-face-decls>
<office:styles>
<w:style style:name="SourceCode" style:family="paragraph" style:class="text">
</w:style>
<style:style style:name="KeywordTok" style:family="text">
<style:text-properties fo:color="#007020" />
<style:text-properties fo:color="#007020" fo:font-weight="bold" />
</style:style>
<style:style style:name="DataTypeTok" style:family="text">
<style:text-properties fo:color="#902000" />
Expand Down Expand Up @@ -58,19 +56,19 @@
<style:text-properties fo:color="#bb6688" />
</style:style>
<style:style style:name="ImportTok" style:family="text">
<style:text-properties fo:color="#008000" />
<style:text-properties fo:color="#008000" fo:font-weight="bold" />
</style:style>
<style:style style:name="CommentTok" style:family="text">
<style:text-properties fo:color="#60a0b0" />
<style:text-properties fo:color="#60a0b0" fo:font-style="italic" />
</style:style>
<style:style style:name="DocumentationTok" style:family="text">
<style:text-properties fo:color="#ba2121" />
<style:text-properties fo:color="#ba2121" fo:font-style="italic" />
</style:style>
<style:style style:name="AnnotationTok" style:family="text">
<style:text-properties fo:color="#60a0b0" />
<style:text-properties fo:color="#60a0b0" fo:font-style="italic" fo:font-weight="bold" />
</style:style>
<style:style style:name="CommentVarTok" style:family="text">
<style:text-properties fo:color="#60a0b0" />
<style:text-properties fo:color="#60a0b0" fo:font-style="italic" fo:font-weight="bold" />
</style:style>
<style:style style:name="OtherTok" style:family="text">
<style:text-properties fo:color="#007020" />
Expand All @@ -82,7 +80,7 @@
<style:text-properties fo:color="#19177c" />
</style:style>
<style:style style:name="ControlFlowTok" style:family="text">
<style:text-properties fo:color="#007020" />
<style:text-properties fo:color="#007020" fo:font-weight="bold" />
</style:style>
<style:style style:name="OperatorTok" style:family="text">
<style:text-properties fo:color="#666666" />
Expand All @@ -103,16 +101,16 @@
<style:text-properties />
</style:style>
<style:style style:name="InformationTok" style:family="text">
<style:text-properties fo:color="#60a0b0" />
<style:text-properties fo:color="#60a0b0" fo:font-style="italic" fo:font-weight="bold" />
</style:style>
<style:style style:name="WarningTok" style:family="text">
<style:text-properties fo:color="#60a0b0" />
<style:text-properties fo:color="#60a0b0" fo:font-style="italic" fo:font-weight="bold" />
</style:style>
<style:style style:name="AlertTok" style:family="text">
<style:text-properties fo:color="#ff0000" />
<style:text-properties fo:color="#ff0000" fo:font-weight="bold" />
</style:style>
<style:style style:name="ErrorTok" style:family="text">
<style:text-properties fo:color="#ff0000" />
<style:text-properties fo:color="#ff0000" fo:font-weight="bold" />
</style:style>
<style:style style:name="NormalTok" style:family="text">
<style:text-properties />
Expand Down
24 changes: 11 additions & 13 deletions test/command/8256.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ Testing.
<style:font-face style:name="Courier New" style:font-family-generic="modern" style:font-pitch="fixed" svg:font-family="'Courier New'" />
</office:font-face-decls>
<office:styles>
<w:style style:name="SourceCode" style:family="paragraph" style:class="text">
</w:style>
<style:style style:name="KeywordTok" style:family="text">
<style:text-properties fo:color="#007020" />
<style:text-properties fo:color="#007020" fo:font-weight="bold" />
</style:style>
<style:style style:name="DataTypeTok" style:family="text">
<style:text-properties fo:color="#902000" />
Expand Down Expand Up @@ -52,19 +50,19 @@ Testing.
<style:text-properties fo:color="#bb6688" />
</style:style>
<style:style style:name="ImportTok" style:family="text">
<style:text-properties fo:color="#008000" />
<style:text-properties fo:color="#008000" fo:font-weight="bold" />
</style:style>
<style:style style:name="CommentTok" style:family="text">
<style:text-properties fo:color="#60a0b0" />
<style:text-properties fo:color="#60a0b0" fo:font-style="italic" />
</style:style>
<style:style style:name="DocumentationTok" style:family="text">
<style:text-properties fo:color="#ba2121" />
<style:text-properties fo:color="#ba2121" fo:font-style="italic" />
</style:style>
<style:style style:name="AnnotationTok" style:family="text">
<style:text-properties fo:color="#60a0b0" />
<style:text-properties fo:color="#60a0b0" fo:font-style="italic" fo:font-weight="bold" />
</style:style>
<style:style style:name="CommentVarTok" style:family="text">
<style:text-properties fo:color="#60a0b0" />
<style:text-properties fo:color="#60a0b0" fo:font-style="italic" fo:font-weight="bold" />
</style:style>
<style:style style:name="OtherTok" style:family="text">
<style:text-properties fo:color="#007020" />
Expand All @@ -76,7 +74,7 @@ Testing.
<style:text-properties fo:color="#19177c" />
</style:style>
<style:style style:name="ControlFlowTok" style:family="text">
<style:text-properties fo:color="#007020" />
<style:text-properties fo:color="#007020" fo:font-weight="bold" />
</style:style>
<style:style style:name="OperatorTok" style:family="text">
<style:text-properties fo:color="#666666" />
Expand All @@ -97,16 +95,16 @@ Testing.
<style:text-properties />
</style:style>
<style:style style:name="InformationTok" style:family="text">
<style:text-properties fo:color="#60a0b0" />
<style:text-properties fo:color="#60a0b0" fo:font-style="italic" fo:font-weight="bold" />
</style:style>
<style:style style:name="WarningTok" style:family="text">
<style:text-properties fo:color="#60a0b0" />
<style:text-properties fo:color="#60a0b0" fo:font-style="italic" fo:font-weight="bold" />
</style:style>
<style:style style:name="AlertTok" style:family="text">
<style:text-properties fo:color="#ff0000" />
<style:text-properties fo:color="#ff0000" fo:font-weight="bold" />
</style:style>
<style:style style:name="ErrorTok" style:family="text">
<style:text-properties fo:color="#ff0000" />
<style:text-properties fo:color="#ff0000" fo:font-weight="bold" />
</style:style>
<style:style style:name="NormalTok" style:family="text">
<style:text-properties />
Expand Down
24 changes: 11 additions & 13 deletions test/writer.opendocument
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
<style:font-face style:name="Courier New" style:font-family-generic="modern" style:font-pitch="fixed" svg:font-family="'Courier New'" />
</office:font-face-decls>
<office:styles>
<w:style style:name="SourceCode" style:family="paragraph" style:class="text">
</w:style>
<style:style style:name="KeywordTok" style:family="text">
<style:text-properties fo:color="#007020" />
<style:text-properties fo:color="#007020" fo:font-weight="bold" />
</style:style>
<style:style style:name="DataTypeTok" style:family="text">
<style:text-properties fo:color="#902000" />
Expand Down Expand Up @@ -40,19 +38,19 @@
<style:text-properties fo:color="#bb6688" />
</style:style>
<style:style style:name="ImportTok" style:family="text">
<style:text-properties fo:color="#008000" />
<style:text-properties fo:color="#008000" fo:font-weight="bold" />
</style:style>
<style:style style:name="CommentTok" style:family="text">
<style:text-properties fo:color="#60a0b0" />
<style:text-properties fo:color="#60a0b0" fo:font-style="italic" />
</style:style>
<style:style style:name="DocumentationTok" style:family="text">
<style:text-properties fo:color="#ba2121" />
<style:text-properties fo:color="#ba2121" fo:font-style="italic" />
</style:style>
<style:style style:name="AnnotationTok" style:family="text">
<style:text-properties fo:color="#60a0b0" />
<style:text-properties fo:color="#60a0b0" fo:font-style="italic" fo:font-weight="bold" />
</style:style>
<style:style style:name="CommentVarTok" style:family="text">
<style:text-properties fo:color="#60a0b0" />
<style:text-properties fo:color="#60a0b0" fo:font-style="italic" fo:font-weight="bold" />
</style:style>
<style:style style:name="OtherTok" style:family="text">
<style:text-properties fo:color="#007020" />
Expand All @@ -64,7 +62,7 @@
<style:text-properties fo:color="#19177c" />
</style:style>
<style:style style:name="ControlFlowTok" style:family="text">
<style:text-properties fo:color="#007020" />
<style:text-properties fo:color="#007020" fo:font-weight="bold" />
</style:style>
<style:style style:name="OperatorTok" style:family="text">
<style:text-properties fo:color="#666666" />
Expand All @@ -85,16 +83,16 @@
<style:text-properties />
</style:style>
<style:style style:name="InformationTok" style:family="text">
<style:text-properties fo:color="#60a0b0" />
<style:text-properties fo:color="#60a0b0" fo:font-style="italic" fo:font-weight="bold" />
</style:style>
<style:style style:name="WarningTok" style:family="text">
<style:text-properties fo:color="#60a0b0" />
<style:text-properties fo:color="#60a0b0" fo:font-style="italic" fo:font-weight="bold" />
</style:style>
<style:style style:name="AlertTok" style:family="text">
<style:text-properties fo:color="#ff0000" />
<style:text-properties fo:color="#ff0000" fo:font-weight="bold" />
</style:style>
<style:style style:name="ErrorTok" style:family="text">
<style:text-properties fo:color="#ff0000" />
<style:text-properties fo:color="#ff0000" fo:font-weight="bold" />
</style:style>
<style:style style:name="NormalTok" style:family="text">
<style:text-properties />
Expand Down

0 comments on commit 0873895

Please sign in to comment.