summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2023-08-04 20:43:39 -0700
committerJohn MacFarlane <jgm@berkeley.edu>2023-08-04 20:43:39 -0700
commit08738954162e992b21ffa83baae7f82b10e1041f (patch)
treecad4f36cf91c4987f9292594d0fc05c018eedf8e
parent889dcbe99ce16f66108f7a8fc6dd064f9d9ce30e (diff)
OpenDocument writer: implement syntax highlighting.
Still unimplemented: global background colors, line numbers. Closes #6710, obsoletes #6717.
-rw-r--r--data/odt/styles.xml6
-rw-r--r--src/Text/Pandoc/Writers/OpenDocument.hs24
-rw-r--r--test/command/6792.md24
-rw-r--r--test/command/8256.md24
-rw-r--r--test/writer.opendocument24
5 files changed, 51 insertions, 51 deletions
diff --git a/data/odt/styles.xml b/data/odt/styles.xml
index ce98b73a1..d544a6750 100644
--- a/data/odt/styles.xml
+++ b/data/odt/styles.xml
@@ -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"
diff --git a/src/Text/Pandoc/Writers/OpenDocument.hs b/src/Text/Pandoc/Writers/OpenDocument.hs
index bad61a747..ce32c9f54 100644
--- a/src/Text/Pandoc/Writers/OpenDocument.hs
+++ b/src/Text/Pandoc/Writers/OpenDocument.hs
@@ -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
@@ -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 })
@@ -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
-
diff --git a/test/command/6792.md b/test/command/6792.md
index e7394c69e..ad7d5591e 100644
--- a/test/command/6792.md
+++ b/test/command/6792.md
@@ -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" />
@@ -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" />
@@ -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" />
@@ -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 />
diff --git a/test/command/8256.md b/test/command/8256.md
index 4712acedd..5ba6b39cc 100644
--- a/test/command/8256.md
+++ b/test/command/8256.md
@@ -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" />
@@ -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" />
@@ -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" />
@@ -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 />
diff --git a/test/writer.opendocument b/test/writer.opendocument
index b693bca2b..75faee891 100644
--- a/test/writer.opendocument
+++ b/test/writer.opendocument
@@ -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" />
@@ -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" />
@@ -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" />
@@ -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 />