Skip to content

Commit

Permalink
Special treatment for lambda in Haskell source.
Browse files Browse the repository at this point in the history
  • Loading branch information
mgajda committed Jan 20, 2021
1 parent 7c79941 commit 22f4072
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 12 deletions.
37 changes: 30 additions & 7 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ import Data.Maybe (fromMaybe)
import qualified Data.Map as Map
import Data.Text(Text)
import qualified Data.Text as T

import qualified Token.Haskell (tokenizer)
import qualified Token.Skylighting
import Filter (renderBlock, renderInline)
import FindColumns (findColumns)
import Alignment (Processed)
import System.IO
import Render.Latex (latexPackages)
import System.IO

data Options = Options {
inlineSyntax :: Text
Expand All @@ -27,20 +28,42 @@ data Options = Options {
main :: IO ()
main = toJSONFilter runner

-- https://www.logicmatters.net/latex-for-logicians/symbols/
-- Just (MetaList [MetaBlocks [RawBlock (Format "tex") "\\usepackage{scalerel}"]])

-- | Main body of Pandoc filter.
-- Reads format option, metadata,
-- and calls `walk` with `blockFormatter`.
runner :: Maybe Format -> Pandoc -> IO Pandoc
runner (fromMaybe (Format "text") -> format) input@(Pandoc (Meta meta) _) = do
hPutStrLn stderr (show opts)
return $ walk (blockFormatter opts format) input
runner (fromMaybe (Format "text") -> format) input@(Pandoc (Meta meta) ast) = do
hPutStrLn stderr $ show opts
hPutStrLn stderr $ show $ Map.lookup "header-includes" meta
let top = if format == Format "latex"
then Pandoc (Meta $ Map.alter modifyIncludes "header-includes" meta) ast
else input
return $ walk (blockFormatter opts format) top
where
-- | Check default syntax for inline code
opts = case Map.lookup "inline-code" meta of
Nothing -> Options "haskell" -- default
Just (MetaString s) -> Options s -- never needed?
Just (MetaInlines [Str s]) -> Options s
Just (MetaString s ) -> Options s -- never needed?
Just (MetaInlines [Str s]) -> Options s
Just otherValue -> error $ "inline-code: meta should be a string but is: " <> show otherValue

-- | Add package dependencies to the meta "header-includes".
modifyIncludes :: Maybe MetaValue -> Maybe MetaValue
modifyIncludes = Just
. addTeXPackages latexPackages
. fromMaybe (MetaList [])
where
addTeXPackages :: [Text] -> MetaValue -> MetaValue
addTeXPackages = addTeXInclude
. T.unlines
. fmap (\name -> "\\usepackage{" <> name <> "}")
addTeXInclude :: Text -> MetaValue -> MetaValue
addTeXInclude rawTeX (MetaList ls) =
MetaList (MetaBlocks [RawBlock (Format "tex") rawTeX]:ls)

-- | Select the desired format output then process it.
-- Run tokenizer, analysis, and formatter.
--
Expand Down
1 change: 1 addition & 0 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ dependencies:
- blaze-markup
- HaTeX
- pandoc-types
- pandoc-lens
- optics-core
- optics-th
- skylighting
Expand Down
7 changes: 5 additions & 2 deletions pandoc-filter-indent.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ cabal-version: 1.12
--
-- see: https://github.com/sol/hpack
--
-- hash: 54950c09cdef3ae347be5565c51f866814024d8f3304665a2873a0acdf7c41a2
-- hash: 54d01b49a8b6f04c42145197372388471c0dc46ca4c7ca9623f6b08d2853cd25

name: pandoc-filter-indent
version: 0.3.0.0
version: 0.3.1.0
synopsis: Pandoc filter formatting Haskell code fragments using GHC lexer.
description: Formats marked code fragments, and allows `pandoc` to safely process rest of your literate program:
.
Expand Down Expand Up @@ -73,6 +73,7 @@ library
, ghc-syntax-highlighter ==0.0.5.0
, optics-core
, optics-th
, pandoc-lens
, pandoc-types
, skylighting
, text ==1.2.4.0
Expand All @@ -96,6 +97,7 @@ executable pandoc-filter-indent
, optics-th
, optparse-applicative
, pandoc-filter-indent
, pandoc-lens
, pandoc-types
, skylighting
, text ==1.2.4.0
Expand All @@ -119,6 +121,7 @@ test-suite pandoc-filter-indent-test
, optics-core
, optics-th
, pandoc-filter-indent
, pandoc-lens
, pandoc-types
, quickcheck-text
, skylighting
Expand Down
10 changes: 7 additions & 3 deletions src/Render/Latex.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ViewPatterns #-}
-- | Render analyzed input into LaTeX table.
module Render.Latex(latexFromColSpans, latexInline) where
module Render.Latex(latexFromColSpans, latexInline, latexPackages) where

import Data.Text(Text)
import qualified Data.Text as T
Expand Down Expand Up @@ -100,6 +100,7 @@ formatToken (TOperator,">>" ) = mathop "gg"
formatToken (TOperator,">>>" ) = mathop "ggg"
formatToken (TOperator,"<<" ) = mathop "ll"
formatToken (TOperator,"<<<" ) = mathop "lll"
formatToken (TOther, "λ" ) = mathop "lambda" -- Haskell only?
--formatToken (TOperator,"-<" ) = mathop "prec"
formatToken (TOther, "-<" ) = mathop "prec"
formatToken (TOther, ">-" ) = mathop "succ"
Expand All @@ -114,7 +115,7 @@ formatToken (TOther, "->" ) = mathop "to"
formatToken (TOther, "=>" ) = mathop "Rightarrow"
formatToken (TOperator,"==>" ) = mathop "implies"
formatToken (TOperator,"|->" ) = mathop "mapsto"
--formatToken (TOperator,"|=>" ) = mathop "Mapsto" -- not in amssymb
formatToken (TOperator,"|=>" ) = mathop "Mapsto" -- requires stmaryrd
formatToken (TOperator,"<>" ) = mathop "diamond"
formatToken (TOperator,"<$>" ) = mathop "mathbin{\\ooalign{\\raise.29ex\\hbox{$\\scriptscriptstyle\\$$}\\cr\\hss$\\!\\lozenge$\\hss}}"
formatToken (TOperator,"<*>" ) = mathop "mathbin{\\ooalign{\\raise.37ex\\hbox{$\\scriptscriptstyle{*}$}\\cr\\hss$\\!\\lozenge$\\hss}}"
Expand All @@ -130,7 +131,7 @@ formatToken (TVar , kwd ) = "\\emph{" <> protectText kwd <> "}"
formatToken (TNum , kwd ) = protectText kwd
formatToken (TKeyword, kwd ) = "\\textbf{" <> protectText kwd <> "}"
formatToken (TCons, cons ) = "\\textsc{" <> protectText cons <> "}"
formatToken (TOperator,"\\" ) = mathop "lambda"
--formatToken (TOperator,"\\" ) = mathop "lambda"
formatToken (TTikz mark,_ ) = mathop $ "tikzMark{" <> mark <> "}"
--formatToken (TOther, "`" ) = mathop "textasciigrave"
formatToken (TOther, "`" ) = protectText "`"
Expand All @@ -151,3 +152,6 @@ mathop code = "\\" <> code

prologue :: Text
prologue = T.concat ["\\usepackage{amssymb}"]

latexPackages :: [Text]
latexPackages = ["stmaryrd"]
2 changes: 2 additions & 0 deletions src/Token/Haskell.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ tokenizer = fmap ( joinEscapedOperators
-- and text content.
-- Only TikZ marks are recognized by looking up text content.
recognizeToken :: (Token, Text) -> (MyTok, Text)
recognizeToken (SymbolTok, "\\") = -- special treatment for lambda
(TOther, "λ" )
recognizeToken (CommentTok, tokText@(unTikzMark -> Just mark)) =
(TTikz mark, tokText)
recognizeToken (tokType, tokText) =
Expand Down
1 change: 1 addition & 0 deletions stack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ packages:
extra-deps:
- HaTeX-3.22.2.0@sha256:c491c38724d2c9beac25c5a11c1848e3e1e96bd08b3909bd9711870d3610a594,5407
- less-arbitrary-0.1.3.0@sha256:6955d2b869c9eb196a16126cf602b21a0447e56ff05b197f61d985cb0535c988,2691
- pandoc-lens-0.7.0@sha256:752120f2f39a108666fe08f7d4b4853779d1871e227301425a1dba6840f3bb62,1012
# Dev only
- ghc-check-0.3.0.1@sha256:651d9b2b75a5cbdb3c942117801bfa566079bcaf20360e4d0959f9e575c30b39,1070
- haskell-lsp-0.22.0.0@sha256:636af17378c10db3c784df697f1a0a450c694f1cf4a88acd0ed58be90c5295ec,5315
Expand Down
1 change: 1 addition & 0 deletions test/example.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Inline `<>` quote is here.
```{.haskell}
test :: forall a. a -> b -> c -> d -> e
test = do
(\x -> x)
(>>=)
x `elem` v
(>=>)
Expand Down

0 comments on commit 22f4072

Please sign in to comment.