git.iko.soy/typeable/pandoc
on master help sign in
Add support for KaTeX HTML math

Closes #1626

commit515a120d0425318d6c8b95c4d8f8d0b1a48193d2 authormpickering <matthewtpickering@gmail.com> dateThu 25 Sep 2014, 18:23 (12 years ago) parent575c76e
3 files changed+52−3
pandoc.hs+32−3 src/Text/Pandoc/Options.hs+1 src/Text/Pandoc/Writers/HTML.hs+19
pandoc.hs+32−3
@@ -58,7 +58,7 @@ import qualified Control.Exception as E
5858import Control.Exception.Extensible ( throwIO )
5959import qualified Text.Pandoc.UTF8 as UTF8
6060import Control.Monad (when, unless, (>=>))
61−import Data.Maybe (isJust)
61+import Data.Maybe (isJust, fromMaybe)
6262import Data.Foldable (foldrM)
6363import Network.URI (parseURI, isURI, URI(..))
6464import qualified Data.ByteString.Lazy as B
@@ -68,7 +68,7 @@ import qualified Data.Map as M
6868import Data.Yaml (decode)
6969import qualified Data.Yaml as Yaml
7070import qualified Data.Text as T
71−import Control.Applicative ((<$>))
71+import Control.Applicative ((<$>), (<|>))
7272import Text.Pandoc.Readers.Txt2Tags (getT2TMeta)
7373import Data.Monoid
7474
@@ -205,6 +205,8 @@ data Opt = Opt
205205 , optExtractMedia :: Maybe FilePath -- ^ Path to extract embedded media
206206 , optTrace :: Bool -- ^ Print debug information
207207 , optTrackChanges :: TrackChanges -- ^ Accept or reject MS Word track-changes.
208+ , optKaTeXStylesheet :: Maybe String -- ^ Path to stylesheet for KaTeX
209+ , optKaTeXJS :: Maybe String -- ^ Path to js file for KaTeX
208210 }
209211
210212-- | Defaults for command-line options.
@@ -263,6 +265,8 @@ defaultOpts = Opt
263265 , optExtractMedia = Nothing
264266 , optTrace = False
265267 , optTrackChanges = AcceptChanges
268+ , optKaTeXStylesheet = Nothing
269+ , optKaTeXJS = Nothing
266270 }
267271
268272-- | A list of functions, each transforming the options data structure
@@ -818,6 +822,21 @@ options =
818822 return opt { optHTMLMathMethod = MathJax url'})
819823 "URL")
820824 "" -- "Use MathJax for HTML math"
825+ , Option "" ["katex"]
826+ (OptArg
827+ (\arg opt ->
828+ return opt
829+ { optKaTeXJS =
830+ arg <|> Just "http://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.1.0/katex.min.js"})
831+ "URL")
832+ "" -- Use KaTeX for HTML Math
833+
834+ , Option "" ["katex-stylesheet"]
835+ (ReqArg
836+ (\arg opt ->
837+ return opt { optKaTeXStylesheet = Just arg })
838+ "URL")
839+ "" -- Set the KaTeX Stylesheet location
821840
822841 , Option "" ["gladtex"]
823842 (NoArg
@@ -860,6 +879,7 @@ options =
860879
861880 ]
862881
882+
863883addMetadata :: String -> MetaValue -> M.Map String MetaValue
864884 -> M.Map String MetaValue
865885addMetadata k v m = case M.lookup k m of
@@ -1027,7 +1047,7 @@ main = do
10271047 , optHighlight = highlight
10281048 , optHighlightStyle = highlightStyle
10291049 , optChapters = chapters
1030− , optHTMLMathMethod = mathMethod
1050+ , optHTMLMathMethod = mathMethod'
10311051 , optReferenceODT = referenceODT
10321052 , optReferenceDocx = referenceDocx
10331053 , optEpubStylesheet = epubStylesheet
@@ -1056,6 +1076,8 @@ main = do
10561076 , optExtractMedia = mbExtractMedia
10571077 , optTrace = trace
10581078 , optTrackChanges = trackChanges
1079+ , optKaTeXStylesheet = katexStylesheet
1080+ , optKaTeXJS = katexJS
10591081 } = opts
10601082
10611083 when dumpArgs $
@@ -1063,6 +1085,13 @@ main = do
10631085 mapM_ (\arg -> UTF8.hPutStrLn stdout arg) args
10641086 exitWith ExitSuccess
10651087
1088+ let csscdn = "http://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.1.0/katex.min.css"
1089+ let mathMethod =
1090+ case (katexJS, katexStylesheet) of
1091+ (Nothing, _) -> mathMethod'
1092+ (Just js, ss) -> KaTeX js (fromMaybe csscdn ss)
1093+
1094+
10661095 -- --bibliography implies -F pandoc-citeproc for backwards compatibility:
10671096 let needsCiteproc = isJust (M.lookup "bibliography" metadata) &&
10681097 optCiteMethod opts `notElem` [Natbib, Biblatex] &&
src/Text/Pandoc/Options.hs+1
@@ -251,6 +251,7 @@ data HTMLMathMethod = PlainMath
251251 | WebTeX String -- url of TeX->image script.
252252 | MathML (Maybe String) -- url of MathMLinHTML.js
253253 | MathJax String -- url of MathJax.js
254+ | KaTeX String String -- url of stylesheet and katex.js
254255 deriving (Show, Read, Eq)
255256
256257data CiteMethod = Citeproc -- use citeproc to render them
src/Text/Pandoc/Writers/HTML.hs+19
@@ -157,6 +157,10 @@ pandocToHtml opts (Pandoc meta blocks) = do
157157 H.script ! A.src (toValue url)
158158 ! A.type_ "text/javascript"
159159 $ mempty
160+ KaTeX js css ->
161+ (H.script ! A.src (toValue js) $ mempty) <>
162+ (H.link ! A.rel "stylesheet" ! A.href (toValue css)) <>
163+ (H.script ! A.type_ "text/javascript" $ toHtml renderKaTeX)
160164 _ -> case lookup "mathml-script" (writerVariables opts) of
161165 Just s | not (writerHtml5 opts) ->
162166 H.script ! A.type_ "text/javascript"
@@ -728,6 +732,10 @@ inlineToHtml opts inline =
728732 case t of
729733 InlineMath -> "\\(" ++ str ++ "\\)"
730734 DisplayMath -> "\\[" ++ str ++ "\\]"
735+ KaTeX _ _ -> return $ H.span ! A.class_ "math" $
736+ toHtml (case t of
737+ InlineMath -> str
738+ DisplayMath -> "\\displaystyle " ++ str)
731739 PlainMath -> do
732740 x <- inlineListToHtml opts (texMathToInlines t str)
733741 let m = H.span ! A.class_ "math" $ x
@@ -829,3 +837,14 @@ blockListToNote opts ref blocks =
829837 Just EPUB3 -> noteItem ! customAttribute "epub:type" "footnote"
830838 _ -> noteItem
831839 return $ nl opts >> noteItem'
840+
841+-- Javascript snippet to render all KaTeX elements
842+renderKaTeX :: String
843+renderKaTeX = unlines [
844+ "window.onload = function(){var mathElements = document.getElementsByClassName(\"math\");"
845+ , "for (var i=0; i < mathElements.length; i++)"
846+ , "{"
847+ , " var texText = mathElements[i].firstChild"
848+ , " katex.render(texText.data, mathElements[i])"
849+ , "}}"
850+ ]