git.iko.soy/typeable/pandoc
on master help sign in
Syntax highlight for inline code of OpenDocument (#6711)

To implement Syntax highlighting for OpenDocument, inlineToOpenDocument in OpenDocument Writer is updated based on Docx Writer. This commit is only for inline Code because update of CodeBlock needs structual change of output document. Currently, styles are not generated automatically in styles.xml. To implement it, additional commit for ODT Writer is needed. Although styles are not included in styles.xml, output file can be shown in LibreOffice(7.0.0.3) like normal characters.

commit7d97bf7a8cd37f7438b331f6799eeed6e4b74c3d authorniszet <niszet0016@gmail.com> dateFri 2 Oct 2020, 01:55 (6 years ago) parent46dffbd
1 file changed+18−3
src/Text/Pandoc/Writers/OpenDocument.hs+18−3
src/Text/Pandoc/Writers/OpenDocument.hs+18−3
@@ -19,7 +19,7 @@ import Control.Monad.State.Strict hiding (when)
1919import Data.Char (chr)
2020import Data.List (sortOn, sortBy, foldl')
2121import qualified Data.Map as Map
22−import Data.Maybe (fromMaybe)
22+import Data.Maybe (fromMaybe, isNothing)
2323import Data.Ord (comparing)
2424import qualified Data.Set as Set
2525import Data.Text (Text)
@@ -40,6 +40,8 @@ import Text.Pandoc.Writers.Shared
4040import qualified Text.Pandoc.Writers.AnnotatedTable as Ann
4141import Text.Pandoc.XML
4242import Text.Printf (printf)
43+import Text.Pandoc.Highlighting (highlight)
44+import Skylighting
4345
4446-- | Auxiliary function to convert Plain block to Para.
4547plainToPara :: Block -> Block
@@ -563,7 +565,7 @@ inlineToOpenDocument o ils
563565 SoftBreak
564566 | writerWrapText o == WrapPreserve
565567 -> return $ preformatted "\n"
566− | otherwise ->return space
568+ | otherwise -> return space
567569 Span attr xs -> withLangFromAttr attr (inlinesToOpenDocument o xs)
568570 LineBreak -> return $ selfClosingTag "text:line-break" []
569571 Str s -> return $ handleSpaces $ escapeStringForXML s
@@ -575,7 +577,14 @@ inlineToOpenDocument o ils
575577 Subscript l -> withTextStyle Sub $ inlinesToOpenDocument o l
576578 SmallCaps l -> withTextStyle SmallC $ inlinesToOpenDocument o l
577579 Quoted t l -> inQuotes t <$> inlinesToOpenDocument o l
578− Code _ s -> inlinedCode $ preformatted s
580+ Code attrs s -> if isNothing (writerHighlightStyle o)
581+ then unhighlighted s
582+ else case highlight (writerSyntaxMap o)
583+ formatOpenDocument attrs s of
584+ Right h -> return $ mconcat $ mconcat h
585+ Left msg -> do
586+ unless (T.null msg) $ report $ CouldNotHighlight msg
587+ unhighlighted s
579588 Math t s -> lift (texMathToInlines t s) >>=
580589 inlinesToOpenDocument o
581590 Cite _ l -> inlinesToOpenDocument o l
@@ -588,6 +597,12 @@ inlineToOpenDocument o ils
588597 Image attr _ (s,t) -> mkImg attr s t
589598 Note l -> mkNote l
590599 where
600+ formatOpenDocument :: FormatOptions -> [SourceLine] -> [[Doc Text]]
601+ formatOpenDocument _fmtOpts = map (map toHlTok)
602+ toHlTok :: Token -> Doc Text
603+ toHlTok (toktype,tok) =
604+ inTags False "text:span" [("text:style-name", (T.pack $ show toktype))] $ preformatted tok
605+ unhighlighted s = inlinedCode $ preformatted s
591606 preformatted s = handleSpaces $ escapeStringForXML s
592607 inlinedCode s = return $ inTags False "text:span" [("text:style-name", "Source_Text")] s
593608 mkLink s t = inTags False "text:a" [ ("xlink:type" , "simple")