Add \colorbox support

This commit is contained in:
Marc Schreiber 2017-06-01 09:09:27 +02:00
parent b1d0c61f2d
commit 181c56d400
3 changed files with 139 additions and 70 deletions

View file

@ -393,8 +393,9 @@ blockCommands = M.fromList $
, ("graphicspath", graphicsPath)
-- hyperlink
, ("hypertarget", braced >> grouped block)
-- textcolor
, ("textcolor", blockTextcolor)
-- LaTeX colors
, ("textcolor", coloredBlock "color")
, ("colorbox", coloredBlock "background-color")
] ++ map ignoreBlocks
-- these commands will be ignored unless --parse-raw is specified,
-- in which case they will appear as raw latex blocks
@ -416,11 +417,11 @@ blockCommands = M.fromList $
, "pagebreak"
]
blockTextcolor :: PandocMonad m => LP m Blocks
blockTextcolor = do
coloredBlock :: PandocMonad m => String -> LP m Blocks
coloredBlock stylename = do
skipopts
color <- braced
let constructor = divWith ("",[],[("style","color: " ++ color)])
let constructor = divWith ("",[],[("style",stylename ++ ": " ++ color)])
inlineContents <|> constructor <$> blockContents
where inlineContents = do
ils <- grouped inline
@ -694,8 +695,9 @@ inlineCommands = M.fromList $
, ("nohyphens", tok)
, ("textnhtt", ttfamily)
, ("nhttfamily", ttfamily)
-- textcolor
, ("textcolor", inlineTextcolor)
-- LaTeX colors
, ("textcolor", coloredInline "color")
, ("colorbox", coloredInline "background-color")
] ++ map ignoreInlines
-- these commands will be ignored unless --parse-raw is specified,
-- in which case they will appear as raw latex blocks:
@ -707,11 +709,11 @@ inlineCommands = M.fromList $
, "pagebreak"
]
inlineTextcolor :: PandocMonad m => LP m Inlines
inlineTextcolor = do
coloredInline :: PandocMonad m => String -> LP m Inlines
coloredInline stylename = do
skipopts
color <- braced
spanWith ("",[],[("style","color: " ++ color)]) <$> tok
spanWith ("",[],[("style",stylename ++ ": " ++ color)]) <$> tok
ttfamily :: PandocMonad m => LP m Inlines
ttfamily = (code . stringify . toList) <$> tok

127
test/command/latex-color.md Normal file
View file

@ -0,0 +1,127 @@
# `\textcolor{}{}`
```
% pandoc -f latex -t native
Hello \textcolor{red}{World}
^D
[Para [Str "Hello",Space,Span ("",[],[("style","color: red")]) [Str "World"]]]
```
```
% pandoc -f latex -t native
\textcolor{red}{Hello} World
^D
[Para [Span ("",[],[("style","color: red")]) [Str "Hello"],Space,Str "World"]]
```
```
% pandoc -f latex -t native
Hello \textcolor{blue}{\textbf{World}}
^D
[Para [Str "Hello",Space,Span ("",[],[("style","color: blue")]) [Strong [Str "World"]]]]
```
```
% pandoc -f latex -t native
Hello \textcolor{blue}{\textbf{World}}.
^D
[Para [Str "Hello",Space,Span ("",[],[("style","color: blue")]) [Strong [Str "World"]],Str "."]]
```
```
% pandoc -f latex -t native
\textcolor{orange}{
\begin{itemize}
\item Item 1
\item Item 2
\end{itemize}
}
^D
[Div ("",[],[("style","color: orange")])
[BulletList
[[Para [Str "Item",Space,Str "1"]]
,[Para [Str "Item",Space,Str "2"]]]]]
```
```
% pandoc -f latex -t native
\textcolor{blue}{
\begin{itemize}
\item Item 1
\item Item 2
\end{itemize}
} some more text
^D
[Div ("",[],[("style","color: blue")])
[BulletList
[[Para [Str "Item",Space,Str "1"]]
,[Para [Str "Item",Space,Str "2"]]]]
,Para [Str "some",Space,Str "more",Space,Str "text"]]
```
# `\colorbox{}{}`
```
% pandoc -f latex -t native
Hello \colorbox{red}{World}
^D
[Para [Str "Hello",Space,Span ("",[],[("style","background-color: red")]) [Str "World"]]]
```
```
% pandoc -f latex -t native
\colorbox{red}{Hello} World
^D
[Para [Span ("",[],[("style","background-color: red")]) [Str "Hello"],Space,Str "World"]]
```
```
% pandoc -f latex -t native
Hello \colorbox{blue}{\textbf{World}}
^D
[Para [Str "Hello",Space,Span ("",[],[("style","background-color: blue")]) [Strong [Str "World"]]]]
```
```
% pandoc -f latex -t native
Hello \colorbox{blue}{\textbf{World}}.
^D
[Para [Str "Hello",Space,Span ("",[],[("style","background-color: blue")]) [Strong [Str "World"]],Str "."]]
```
```
% pandoc -f latex -t native
\colorbox{orange}{
\begin{minipage}{\textwidth}
\begin{itemize}
\item Item 1
\item Item 2
\end{itemize}
\end{minipage}
}
^D
[Div ("",[],[("style","background-color: orange")])
[BulletList
[[Para [Str "Item",Space,Str "1"]]
,[Para [Str "Item",Space,Str "2"]]]]]
```
```
% pandoc -f latex -t native
\colorbox{blue}{
\begin{minipage}{\textwidth}
\begin{itemize}
\item Item 1
\item Item 2
\end{itemize}
\end{minipage}
} some more text
^D
[Div ("",[],[("style","background-color: blue")])
[BulletList
[[Para [Str "Item",Space,Str "1"]]
,[Para [Str "Item",Space,Str "2"]]]]
,Para [Str "some",Space,Str "more",Space,Str "text"]]
```

View file

@ -1,60 +0,0 @@
```
% pandoc -f latex -t native
Hello \textcolor{red}{World}
^D
[Para [Str "Hello",Space,Span ("",[],[("style","color: red")]) [Str "World"]]]
```
```
% pandoc -f latex -t native
\textcolor{red}{Hello} World
^D
[Para [Span ("",[],[("style","color: red")]) [Str "Hello"],Space,Str "World"]]
```
```
% pandoc -f latex -t native
Hello \textcolor{blue}{\textbf{World}}
^D
[Para [Str "Hello",Space,Span ("",[],[("style","color: blue")]) [Strong [Str "World"]]]]
```
```
% pandoc -f latex -t native
Hello \textcolor{blue}{\textbf{World}}.
^D
[Para [Str "Hello",Space,Span ("",[],[("style","color: blue")]) [Strong [Str "World"]],Str "."]]
```
```
% pandoc -f latex -t native
\textcolor{orange}{
\begin{itemize}
\item Item 1
\item Item 2
\end{itemize}
}
^D
[Div ("",[],[("style","color: orange")])
[BulletList
[[Para [Str "Item",Space,Str "1"]]
,[Para [Str "Item",Space,Str "2"]]]]]
```
```
% pandoc -f latex -t native
\textcolor{blue}{
\begin{itemize}
\item Item 1
\item Item 2
\end{itemize}
} some more text
^D
[Div ("",[],[("style","color: blue")])
[BulletList
[[Para [Str "Item",Space,Str "1"]]
,[Para [Str "Item",Space,Str "2"]]]]
,Para [Str "some",Space,Str "more",Space,Str "text"]]
```