Add a filter for simple fractions

This commit is contained in:
Kerl13 2016-03-23 18:56:19 +01:00
parent 02f660aa5e
commit f3735c4e47
3 changed files with 49 additions and 7 deletions

View File

@ -1,11 +1,29 @@
SOURCES = $(wildcard *.md)
OUT = $(SOURCES:.md=.pdf)
all: $(OUT)
HS_FILTERS_NAMES = nice-frac
PY_FILTERS_NAMES = pandoc-svg.py
EXT_FILTERS = pandoc-crossref
HS_FILTERS = $(addprefix filters/, $(HS_FILTERS_NAMES))
PY_FILTERS = $(addprefix filters/, $(PY_FILTERS_NAMES))
FILTERS = $(PY_FILTERS) $(HS_FILTERS) $(EXT_FILTERS)
.SUFFIXES: .hs
all: $(HS_FILTERS) $(OUT)
$(OUT): $(SOURCES)
pandoc --smart -t latex $< --filter=filters/pandoc-svg.py --filter=pandoc-crossref -o $@
pandoc --smart -t latex $< $(addprefix --filter=, $(FILTERS)) -o $@
$(HS_FILTERS): $(addsuffix .hs, $(HS_FILTERS))
ghc --make -O2 $< -o $@
clean:
rm -f $(OUT)
rm -f $(addprefix filters/, $(HS_FILTERS))
find . -name "*.pandoc.pdf" -delete
deepclean: clean
rm -f $(HS_FILTERS)
rm -f filters/*.hi filters/*.o

View File

@ -12,16 +12,14 @@ Includes various Pandoc filters:
* Include [pandoc-crossref](https://github.com/lierdakil/pandoc-crossref) for
easy numbering and referencing.
* Convert fractions like "(n/k)" to "\frac{n}{k}" when n and k are integers in
math environment.
## TODO
* automatically convertfunction names (cos, sin, exp, log) to their
* automatically convert function names (cos, sin, exp, log) to their
LaTeX equivalent (\cos and so on) when in a mathematical environment,
then taking out the useless leading `\`.
* not having to use \frac{}{} to write fractions, but instead
automatically convert simple fractions in math environment to \frac{}{}
(for instance convert "(1 / 2)" to "\frac{1}{2}")
* anything else like this that could help writing more readable LaTeX
code :)

26
filters/nice-frac.hs Normal file
View File

@ -0,0 +1,26 @@
-- nice-frac.hs
import Text.Pandoc.JSON
import Text.Regex
-- Usefull regex elements
space :: String
space = "[ \\t\\n\\r]*"
num :: String
num = "([0-9]+)"
-- Substitute latex fractions like '\frac{42}{1}' for (42 / 1)
fracReg :: Regex
fracReg = mkRegex $
"\\(" ++ space ++ num ++ space ++ "/" ++ space ++ num ++ "\\)"
frac :: String -> String
frac s = subRegex fracReg s "\\frac{\\1}{\\2}"
-- Apply the substitution to all Latex parts on the AST
niceFrac :: Inline -> Inline
niceFrac (Math t s) = Math t (frac s)
niceFrac x = x
main :: IO ()
main = toJSONFilter niceFrac