From 49336ee6eeecc352e248d1262ea1b46070e00243 Mon Sep 17 00:00:00 2001 From: Marc Schreiber Date: Tue, 2 May 2017 10:48:57 +0200 Subject: Add basic \textcolor support to LaTeX reader --- src/Text/Pandoc/Readers/LaTeX.hs | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src') diff --git a/src/Text/Pandoc/Readers/LaTeX.hs b/src/Text/Pandoc/Readers/LaTeX.hs index a54238206..6252293d7 100644 --- a/src/Text/Pandoc/Readers/LaTeX.hs +++ b/src/Text/Pandoc/Readers/LaTeX.hs @@ -680,6 +680,8 @@ inlineCommands = M.fromList $ , ("nohyphens", tok) , ("textnhtt", ttfamily) , ("nhttfamily", ttfamily) + -- textcolor + , ("textcolor", textcolor) ] ++ map ignoreInlines -- these commands will be ignored unless --parse-raw is specified, -- in which case they will appear as raw latex blocks: @@ -756,6 +758,12 @@ dosiunitx = do emptyOr160 unit, unit] +textcolor :: PandocMonad m => LP m Inlines +textcolor = do + skipopts + color <- braced + spanWith ("",[],[("style","color: " ++ color)]) <$> tok + lit :: String -> LP m Inlines lit = pure . str -- cgit v1.2.3 From d9439808f2fe226aad027c8c9d0a38217a1e8c34 Mon Sep 17 00:00:00 2001 From: Marc Schreiber Date: Wed, 3 May 2017 12:00:30 +0200 Subject: Add block version of \textcolor --- src/Text/Pandoc/Readers/LaTeX.hs | 22 +++++++++++++++------- test/command/textcolor.md | 11 +++++++++++ 2 files changed, 26 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/Text/Pandoc/Readers/LaTeX.hs b/src/Text/Pandoc/Readers/LaTeX.hs index 6252293d7..1c1aa4c62 100644 --- a/src/Text/Pandoc/Readers/LaTeX.hs +++ b/src/Text/Pandoc/Readers/LaTeX.hs @@ -393,6 +393,8 @@ blockCommands = M.fromList $ , ("graphicspath", graphicsPath) -- hyperlink , ("hypertarget", braced >> grouped block) + -- textcolor + , ("textcolor", blockTextcolor) ] ++ map ignoreBlocks -- these commands will be ignored unless --parse-raw is specified, -- in which case they will appear as raw latex blocks @@ -414,6 +416,12 @@ blockCommands = M.fromList $ , "pagebreak" ] +blockTextcolor :: PandocMonad m => LP m Blocks +blockTextcolor = do + skipopts + color <- braced + return <$> divWith ("",[],[("style","color: " ++ color)]) $ block + graphicsPath :: PandocMonad m => LP m Blocks graphicsPath = do ps <- bgroup *> (manyTill braced egroup) @@ -681,7 +689,7 @@ inlineCommands = M.fromList $ , ("textnhtt", ttfamily) , ("nhttfamily", ttfamily) -- textcolor - , ("textcolor", textcolor) + , ("textcolor", inlineTextcolor) ] ++ map ignoreInlines -- these commands will be ignored unless --parse-raw is specified, -- in which case they will appear as raw latex blocks: @@ -693,6 +701,12 @@ inlineCommands = M.fromList $ , "pagebreak" ] +inlineTextcolor :: PandocMonad m => LP m Inlines +inlineTextcolor = do + skipopts + color <- braced + spanWith ("",[],[("style","color: " ++ color)]) <$> tok + ttfamily :: PandocMonad m => LP m Inlines ttfamily = (code . stringify . toList) <$> tok @@ -758,12 +772,6 @@ dosiunitx = do emptyOr160 unit, unit] -textcolor :: PandocMonad m => LP m Inlines -textcolor = do - skipopts - color <- braced - spanWith ("",[],[("style","color: " ++ color)]) <$> tok - lit :: String -> LP m Inlines lit = pure . str diff --git a/test/command/textcolor.md b/test/command/textcolor.md index 7719a1c6b..013e55d67 100644 --- a/test/command/textcolor.md +++ b/test/command/textcolor.md @@ -11,3 +11,14 @@ ^D [Para [Span ("",[],[("style","color: blue")]) [Str "Hello",Space,Strong [Str "World"]]]] ``` + +``` +% pandoc -f latex -t native +\textcolor{orange}{ + \begin{itemize} + \item Test + \end{itemize} +} +^D + +``` -- cgit v1.2.3 From 1728d4e60983e050be4bfb5b8c2e6065b4dd198a Mon Sep 17 00:00:00 2001 From: Marc Schreiber Date: Wed, 3 May 2017 13:39:38 +0200 Subject: \textcolor works as inline and block command --- src/Text/Pandoc/Readers/LaTeX.hs | 2 +- test/command/textcolor.md | 20 ++++++++++++-------- 2 files changed, 13 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/Text/Pandoc/Readers/LaTeX.hs b/src/Text/Pandoc/Readers/LaTeX.hs index 1c1aa4c62..58ed97b04 100644 --- a/src/Text/Pandoc/Readers/LaTeX.hs +++ b/src/Text/Pandoc/Readers/LaTeX.hs @@ -420,7 +420,7 @@ blockTextcolor :: PandocMonad m => LP m Blocks blockTextcolor = do skipopts color <- braced - return <$> divWith ("",[],[("style","color: " ++ color)]) $ block + divWith ("",[],[("style","color: " ++ color)]) <$> grouped block graphicsPath :: PandocMonad m => LP m Blocks graphicsPath = do diff --git a/test/command/textcolor.md b/test/command/textcolor.md index 013e55d67..aa3593822 100644 --- a/test/command/textcolor.md +++ b/test/command/textcolor.md @@ -1,24 +1,28 @@ ``` % pandoc -f latex -t native -\textcolor{red}{Hello World} +Hello \textcolor{red}{World} ^D -[Para [Span ("",[],[("style","color: red")]) [Str "Hello",Space,Str "World"]]] +[Para [Str "Hello",Space,Span ("",[],[("style","color: red")]) [Str "World"]]] ``` ``` % pandoc -f latex -t native -\textcolor{blue}{Hello \textbf{World}} +Hello \textcolor{blue}{\textbf{World}} ^D -[Para [Span ("",[],[("style","color: blue")]) [Str "Hello",Space,Strong [Str "World"]]]] +[Para [Str "Hello",Space,Span ("",[],[("style","color: blue")]) [Strong [Str "World"]]]] ``` ``` % pandoc -f latex -t native \textcolor{orange}{ - \begin{itemize} - \item Test - \end{itemize} +\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"]]]]] ``` -- cgit v1.2.3 From 4ed6d9165672917cb9450578c8f7d84121ecfc24 Mon Sep 17 00:00:00 2001 From: Marc Schreiber Date: Thu, 4 May 2017 16:48:27 +0200 Subject: \textcolor will be parse as span at the beginning of a paragraph --- src/Text/Pandoc/Readers/LaTeX.hs | 2 +- test/command/textcolor.md | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/Text/Pandoc/Readers/LaTeX.hs b/src/Text/Pandoc/Readers/LaTeX.hs index 58ed97b04..a34be46e2 100644 --- a/src/Text/Pandoc/Readers/LaTeX.hs +++ b/src/Text/Pandoc/Readers/LaTeX.hs @@ -420,7 +420,7 @@ blockTextcolor :: PandocMonad m => LP m Blocks blockTextcolor = do skipopts color <- braced - divWith ("",[],[("style","color: " ++ color)]) <$> grouped block + divWith ("",[],[("style","color: " ++ color)]) <$> grouped block <* notFollowedBy inline graphicsPath :: PandocMonad m => LP m Blocks graphicsPath = do diff --git a/test/command/textcolor.md b/test/command/textcolor.md index aa3593822..59b0f6a16 100644 --- a/test/command/textcolor.md +++ b/test/command/textcolor.md @@ -5,6 +5,13 @@ Hello \textcolor{red}{World} [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}} -- cgit v1.2.3 From 29a4bdc68131d3925a55e0428b35c6a4f75f86e0 Mon Sep 17 00:00:00 2001 From: Marc Schreiber Date: Tue, 23 May 2017 17:31:42 -0300 Subject: Add suggestions of @jgm: parse bracketed stuff as inlines --- src/Text/Pandoc/Readers/LaTeX.hs | 12 +++++++++--- test/command/textcolor.md | 17 +++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/Text/Pandoc/Readers/LaTeX.hs b/src/Text/Pandoc/Readers/LaTeX.hs index a34be46e2..6b44df468 100644 --- a/src/Text/Pandoc/Readers/LaTeX.hs +++ b/src/Text/Pandoc/Readers/LaTeX.hs @@ -418,9 +418,15 @@ blockCommands = M.fromList $ blockTextcolor :: PandocMonad m => LP m Blocks blockTextcolor = do - skipopts - color <- braced - divWith ("",[],[("style","color: " ++ color)]) <$> grouped block <* notFollowedBy inline + skipopts + color <- braced + let constructor = divWith ("",[],[("style","color: " ++ color)]) + inlineContents <|> constructor <$> blockContents + where inlineContents = do + ils <- grouped inline + rest <- inlines + return (para (ils <> rest)) + blockContents = grouped block graphicsPath :: PandocMonad m => LP m Blocks graphicsPath = do diff --git a/test/command/textcolor.md b/test/command/textcolor.md index 59b0f6a16..47c41e62d 100644 --- a/test/command/textcolor.md +++ b/test/command/textcolor.md @@ -33,3 +33,20 @@ Hello \textcolor{blue}{\textbf{World}} [[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"]] +``` + -- cgit v1.2.3 From 181c56d4003aa83abed23b95a452c4890aa3797c Mon Sep 17 00:00:00 2001 From: Marc Schreiber Date: Thu, 1 Jun 2017 09:09:27 +0200 Subject: Add \colorbox support --- src/Text/Pandoc/Readers/LaTeX.hs | 22 ++++--- test/command/latex-color.md | 127 +++++++++++++++++++++++++++++++++++++++ test/command/textcolor.md | 60 ------------------ 3 files changed, 139 insertions(+), 70 deletions(-) create mode 100644 test/command/latex-color.md delete mode 100644 test/command/textcolor.md (limited to 'src') diff --git a/src/Text/Pandoc/Readers/LaTeX.hs b/src/Text/Pandoc/Readers/LaTeX.hs index 6b44df468..1d13f7107 100644 --- a/src/Text/Pandoc/Readers/LaTeX.hs +++ b/src/Text/Pandoc/Readers/LaTeX.hs @@ -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 diff --git a/test/command/latex-color.md b/test/command/latex-color.md new file mode 100644 index 000000000..9fd2585a3 --- /dev/null +++ b/test/command/latex-color.md @@ -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"]] +``` diff --git a/test/command/textcolor.md b/test/command/textcolor.md deleted file mode 100644 index 51d05b9f1..000000000 --- a/test/command/textcolor.md +++ /dev/null @@ -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"]] -``` - -- cgit v1.2.3