git.iko.soy/typeable/pandoc
on master help sign in
HTML reader: allow enabling `raw_tex` extension.
a2c4261John MacFarlane
MANUAL.txt+2−1
@@ -1917,7 +1917,8 @@ This extension can be enabled/disabled for the following formats
1917(in addition to `markdown`):
1917(in addition to `markdown`):
1918
1918
1919input formats
1919input formats
1920−: `latex`, `org`, `textile`
1920+: `latex`, `org`, `textile`, `html` (environments, `\ref`, and
1921+ `\eqref` only)
1921
1922
1922output formats
1923output formats
1923: `textile`, `commonmark`
1924: `textile`, `commonmark`
src/Text/Pandoc/Readers/HTML.hs+28−3
@@ -68,11 +68,13 @@ import qualified Text.Pandoc.Builder as B
68import Text.Pandoc.Class (PandocMonad (..))
68import Text.Pandoc.Class (PandocMonad (..))
69import Text.Pandoc.CSS (foldOrElse, pickStyleAttrProps)
69import Text.Pandoc.CSS (foldOrElse, pickStyleAttrProps)
70import Text.Pandoc.Definition
70import Text.Pandoc.Definition
71+import Text.Pandoc.Readers.LaTeX (rawLaTeXInline)
72+import Text.Pandoc.Readers.LaTeX.Types (Macro)
71import Text.Pandoc.Error
73import Text.Pandoc.Error
72import Text.Pandoc.Logging
74import Text.Pandoc.Logging
73import Text.Pandoc.Options (
75import Text.Pandoc.Options (
74 Extension (Ext_epub_html_exts, Ext_empty_paragraphs, Ext_native_divs,
76 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),
76 ReaderOptions (readerExtensions, readerStripComments),
78 ReaderOptions (readerExtensions, readerStripComments),
77 extensionEnabled)
79 extensionEnabled)
78import Text.Pandoc.Parsing hiding ((<|>))
80import Text.Pandoc.Parsing hiding ((<|>))
@@ -102,7 +104,8 @@ readHtml opts inp = do
102 (m:_) -> messageString m
104 (m:_) -> messageString m
103 result <- flip runReaderT def $
105 result <- flip runReaderT def $
104 runParserT parseDoc
106 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)
106 "source" tags
109 "source" tags
107 case result of
110 case result of
108 Right doc -> return doc
111 Right doc -> return doc
@@ -124,7 +127,8 @@ data HTMLState =
124 baseHref :: Maybe URI,
127 baseHref :: Maybe URI,
125 identifiers :: Set.Set String,
128 identifiers :: Set.Set String,
126 headerMap :: M.Map Inlines String,
129 headerMap :: M.Map Inlines String,
127− logMessages :: [LogMessage]
130+ logMessages :: [LogMessage],
131+ macros :: M.Map Text Macro
128 }
132 }
129
133
130data HTMLLocal = HTMLLocal { quoteContext :: QuoteContext
134data HTMLLocal = HTMLLocal { quoteContext :: QuoteContext
@@ -907,9 +911,25 @@ pTagContents =
907 <|> pStr
911 <|> pStr
908 <|> pSpace
912 <|> pSpace
909 <|> smartPunctuation pTagContents
913 <|> smartPunctuation pTagContents
914+ <|> pRawTeX
910 <|> pSymbol
915 <|> pSymbol
911 <|> pBad
916 <|> pBad
912
917
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+
913pStr :: PandocMonad m => InlinesParser m Inlines
933pStr :: PandocMonad m => InlinesParser m Inlines
914pStr = do
934pStr = do
915 result <- many1 $ satisfy $ \c ->
935 result <- many1 $ satisfy $ \c ->
@@ -923,6 +943,7 @@ isSpecial '\'' = True
923isSpecial '.' = True
943isSpecial '.' = True
924isSpecial '-' = True
944isSpecial '-' = True
925isSpecial '$' = True
945isSpecial '$' = True
946+isSpecial '\\' = True
926isSpecial '\8216' = True
947isSpecial '\8216' = True
927isSpecial '\8217' = True
948isSpecial '\8217' = True
928isSpecial '\8220' = True
949isSpecial '\8220' = True
@@ -1249,6 +1270,10 @@ isSpace _ = False
1249
1270
1250-- Instances
1271-- Instances
1251
1272
1273+instance HasMacros HTMLState where
1274+ extractMacros = macros
1275+ updateMacros f st = st{ macros = f $ macros st }
1276+
1252instance HasIdentifierList HTMLState where
1277instance HasIdentifierList HTMLState where
1253 extractIdentifierList = identifiers
1278 extractIdentifierList = identifiers
1254 updateIdentifierList f s = s{ identifiers = f (identifiers s) }
1279 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+```