git.iko.soy/typeable/pandoc
on master help sign in
HTML reader: allow enabling `raw_tex` extension.

This now allows raw LaTeX environments, `\ref`, and `\eqref` to be parsed (which is helpful for translation HTML documents using MathJaX). Closes #1126.

commita2c4261b32d3ed3fd6562a5260d25a82d9f75e9b authorJohn MacFarlane <jgm@berkeley.edu> dateFri 24 Aug 2018, 18:04 (8 years ago) parent4dddfbc
3 files changed+59−4
MANUAL.txt+2−1 src/Text/Pandoc/Readers/HTML.hs+28−3 test/command/1126.md+29
MANUAL.txt+2−1
@@ -1917,7 +1917,8 @@ This extension can be enabled/disabled for the following formats
19171917(in addition to `markdown`):
19181918
19191919input formats
1920−: `latex`, `org`, `textile`
1920+: `latex`, `org`, `textile`, `html` (environments, `\ref`, and
1921+ `\eqref` only)
19211922
19221923output formats
19231924: `textile`, `commonmark`
src/Text/Pandoc/Readers/HTML.hs+28−3
@@ -68,11 +68,13 @@ import qualified Text.Pandoc.Builder as B
6868import Text.Pandoc.Class (PandocMonad (..))
6969import Text.Pandoc.CSS (foldOrElse, pickStyleAttrProps)
7070import Text.Pandoc.Definition
71+import Text.Pandoc.Readers.LaTeX (rawLaTeXInline)
72+import Text.Pandoc.Readers.LaTeX.Types (Macro)
7173import Text.Pandoc.Error
7274import Text.Pandoc.Logging
7375import Text.Pandoc.Options (
7476 Extension (Ext_epub_html_exts, Ext_empty_paragraphs, Ext_native_divs,
75− Ext_native_spans, Ext_raw_html, Ext_line_blocks),
77+ Ext_native_spans, Ext_raw_html, Ext_line_blocks, Ext_raw_tex),
7678 ReaderOptions (readerExtensions, readerStripComments),
7779 extensionEnabled)
7880import Text.Pandoc.Parsing hiding ((<|>))
@@ -102,7 +104,8 @@ readHtml opts inp = do
102104 (m:_) -> messageString m
103105 result <- flip runReaderT def $
104106 runParserT parseDoc
105− (HTMLState def{ stateOptions = opts } [] Nothing Set.empty M.empty [])
107+ (HTMLState def{ stateOptions = opts }
108+ [] Nothing Set.empty M.empty [] M.empty)
106109 "source" tags
107110 case result of
108111 Right doc -> return doc
@@ -124,7 +127,8 @@ data HTMLState =
124127 baseHref :: Maybe URI,
125128 identifiers :: Set.Set String,
126129 headerMap :: M.Map Inlines String,
127− logMessages :: [LogMessage]
130+ logMessages :: [LogMessage],
131+ macros :: M.Map Text Macro
128132 }
129133
130134data HTMLLocal = HTMLLocal { quoteContext :: QuoteContext
@@ -907,9 +911,25 @@ pTagContents =
907911 <|> pStr
908912 <|> pSpace
909913 <|> smartPunctuation pTagContents
914+ <|> pRawTeX
910915 <|> pSymbol
911916 <|> pBad
912917
918+pRawTeX :: PandocMonad m => InlinesParser m Inlines
919+pRawTeX = do
920+ lookAhead $ try $ do
921+ char '\\'
922+ choice $ map (try . string) ["begin", "eqref", "ref"]
923+ guardEnabled Ext_raw_tex
924+ inp <- getInput
925+ st <- getState
926+ res <- lift $ runParserT (withRaw rawLaTeXInline) st "chunk" (T.unpack inp)
927+ case res of
928+ Left _ -> mzero
929+ Right (contents, raw) -> do
930+ _ <- count (length raw) anyChar
931+ return $ B.rawInline "tex" contents
932+
913933pStr :: PandocMonad m => InlinesParser m Inlines
914934pStr = do
915935 result <- many1 $ satisfy $ \c ->
@@ -923,6 +943,7 @@ isSpecial '\'' = True
923943isSpecial '.' = True
924944isSpecial '-' = True
925945isSpecial '$' = True
946+isSpecial '\\' = True
926947isSpecial '\8216' = True
927948isSpecial '\8217' = True
928949isSpecial '\8220' = True
@@ -1249,6 +1270,10 @@ isSpace _ = False
12491270
12501271-- Instances
12511272
1273+instance HasMacros HTMLState where
1274+ extractMacros = macros
1275+ updateMacros f st = st{ macros = f $ macros st }
1276+
12521277instance HasIdentifierList HTMLState where
12531278 extractIdentifierList = identifiers
12541279 updateIdentifierList f s = s{ identifiers = f (identifiers s) }
test/command/1126.mdnew file+29
@@ -0,0 +1,29 @@
1+```
2+% pandoc -f html -t latex
3+\begin{eqnarray}
4+A&=&B,\\
5+C&=&D
6+\end{eqnarray}
7+^D
8+\textbackslash{}begin\{eqnarray\}
9+A\&=\&B,\textbackslash{}\textbackslash{} C\&=\&D
10+\textbackslash{}end\{eqnarray\}
11+```
12+
13+```
14+% pandoc -f html+raw_tex -t latex
15+<p>See \eqref{myeq}.</p>
16+\begin{eqnarray}
17+A&=&B,\\
18+C&amp;=&amp;D
19+\\label{myeq}
20+\end{eqnarray}
21+^D
22+See \eqref{myeq}.
23+
24+\begin{eqnarray}
25+A&=&B,\\
26+C&=&D
27+\\label{myeq}
28+\end{eqnarray}
29+```