Skip to content

Commit

Permalink
Add TextColored constructor to TextType.
Browse files Browse the repository at this point in the history
[API change]

+ Support `\color` in TeX reader.
+ Support TextColored in TeX writer.
+ Support TextColored in Pandoc writer.

Other writers have been modified so they don't crash with TextColored,
but currently they just ignore it.

See #225.
  • Loading branch information
jgm committed Sep 28, 2024
1 parent d47229b commit aad4a07
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Text/TeXMath/Readers/TeX.hs
Original file line number Diff line number Diff line change
Expand Up @@ -679,9 +679,10 @@ styled c = do

colored :: Text -> TP Exp
colored "\\color" = do
_ <- inbraces -- skip the color
color <- T.pack <$> braces (many1 letter)
-- in the future we might add color to the types or to the styles
texSymbol <|> inbraces <|> texChar
contents <- unGrouped <$> (texSymbol <|> inbraces <|> texChar)
return $ EStyled (TextColored color) contents
colored _ = mzero

-- note: sqrt can be unary, \sqrt{2}, or binary, \sqrt[3]{2}
Expand Down
1 change: 1 addition & 0 deletions src/Text/TeXMath/Shared.hs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ getMMLType t = fromMaybe "normal" (fst <$> M.lookup t textTypesMap)

-- | Maps TextType to corresponding LaTeX command
getLaTeXTextCommand :: Env -> TextType -> T.Text
getLaTeXTextCommand _ (TextColored color) = "\\color{" <> color <> "}"
getLaTeXTextCommand e t =
let textCmd = fromMaybe "\\mathrm"
(snd <$> M.lookup t textTypesMap) in
Expand Down
1 change: 1 addition & 0 deletions src/Text/TeXMath/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ data TextType = TextNormal
| TextBoldScript
| TextBoldFraktur
| TextSansSerifItalic
| TextColored T.Text
deriving (Show, Read, Eq, Ord, Data, Typeable)

data FormType = FPrefix | FPostfix | FInfix deriving (Show, Ord, Eq)
Expand Down
4 changes: 4 additions & 0 deletions src/Text/TeXMath/Writers/OMML.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ writeOMML dt = container . concatMap (showExp [])
mnode :: Node t => String -> t -> Element
mnode s = node (QName s Nothing (Just "m"))

wnode :: Node t => String -> t -> Element
wnode s = node (QName s Nothing (Just "w"))

-- Kept as String for Text.XML.Light
mnodeA :: Node t => String -> String -> t -> Element
mnodeA s v = add_attr (Attr (QName "val" Nothing (Just "m")) v) . mnode s
Expand Down Expand Up @@ -122,6 +125,7 @@ setProps tt =
TextBoldFraktur -> [sty "b", scr "fraktur"]
TextSansSerifItalic -> [sty "i", scr "sans-serif"]
TextSansSerifBoldItalic -> [sty "bi", scr "sans-serif"]
TextColored color -> [] -- TODO
where sty x = mnodeA "sty" x ()
scr x = mnodeA "scr" x ()

Expand Down
2 changes: 2 additions & 0 deletions src/Text/TeXMath/Writers/Pandoc.hs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ renderStr (Just tt) s =
TextBoldFraktur -> [Strong [Str $ toUnicode tt s]]
TextSansSerifItalic -> [Emph [Str s]]
TextSansSerifBoldItalic -> [Strong [Emph [Str s]]]
TextColored color ->
[Span ("",[],[("style","color: " <> color <> ";")]) [Str s]]

expToInlines :: Maybe TextType -> Exp -> Maybe [Inline]
expToInlines tt (ENumber s) = Just $ renderStr tt s
Expand Down
5 changes: 5 additions & 0 deletions test/reader/tex/color.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<<< tex
\color{blue}{x}

>>> native
[ EStyled (TextColored "blue") [ EIdentifier "x" ] ]
5 changes: 5 additions & 0 deletions test/writer/tex/color.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<<< native
[ EStyled (TextColored "blue") [ EIdentifier "x" ] ]

>>> tex
\color{blue}{x}

0 comments on commit aad4a07

Please sign in to comment.