diff --git a/.gitignore b/.gitignore index 1192926..f53dd5f 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,6 @@ *.pandoc.pdf +dist/ +filters/*.hi +filters/*.o +filters/usual-fun +filters/nice-frac diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..992edc4 --- /dev/null +++ b/LICENSE @@ -0,0 +1,3 @@ +As long as you retain this notice you can do whatever you want with this stuff. +If we meet some day, and you think this stuff is worth it, you can buy me a beer +in return. diff --git a/Makefile b/Makefile index 4308a74..6b0fda6 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,29 @@ SOURCES = $(wildcard *.md) OUT = $(SOURCES:.md=.pdf) -all: $(OUT) +HS_FILTERS_NAMES = nice-frac usual-fun +PY_FILTERS_NAMES = pandoc-svg.py +EXT_FILTERS = pandoc-crossref -$(OUT): $(SOURCES) - pandoc --smart -t latex $< --filter=filters/pandoc-svg.py --filter=pandoc-crossref -o $@ +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): + pandoc -S -t latex $(basename $@).md $(addprefix --filter=, $(FILTERS)) -o $@ + +$(HS_FILTERS): + ghc --make $@.hs -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 diff --git a/README.md b/README.md index dabd936..0fbd102 100644 --- a/README.md +++ b/README.md @@ -7,21 +7,19 @@ Pandoc. Includes various Pandoc filters: * Automatically convert `SVG` files to `PDF` and rewrite the image link to - include them in the document easily. +include them in the document easily. * Include [pandoc-crossref](https://github.com/lierdakil/pandoc-crossref) for - easy numbering and referencing. +easy numbering and referencing. +* Convert simple fractions like "(n / k)" to LaTeX's "\\frac{n}{k}" when n and +k are integers in a math environment. + +* 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 `\`. ## TODO -* automatically convertfunction 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 :) diff --git a/filters/nice-frac.hs b/filters/nice-frac.hs new file mode 100644 index 0000000..a4732a4 --- /dev/null +++ b/filters/nice-frac.hs @@ -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]+)" + +-- Replace fractions like "(42 / 1)" by "\frac{42}{1}" +fracReg :: Regex +fracReg = mkRegex $ + "\\(" ++ space ++ num ++ space ++ "/" ++ space ++ num ++ space ++ "\\)" +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 + diff --git a/filters/usual-fun.hs b/filters/usual-fun.hs new file mode 100644 index 0000000..00fb098 --- /dev/null +++ b/filters/usual-fun.hs @@ -0,0 +1,33 @@ +-- usual-fun.hs + +import Text.Pandoc.JSON +import Text.Regex +import Data.List (intercalate) + +-- Association list for usual functions +usual :: String +usual = intercalate "|" [ + "cos", "sin", "tan", "cotan", + "arccos", "arcsin", "arctan", + "exp", "log"] + +-- Remove the '\' before usual functions' names if present +rmBSReg :: Regex +rmBSReg = mkRegex $ "(\\\\)(" ++ usual ++ ")\\>" +rmBS :: String -> String +rmBS s = subRegex rmBSReg s "\\2" + +-- Add a '\' before usual functions' names +addBSReg :: Regex +addBSReg = mkRegex $ "\\<(" ++ usual ++ ")\\>" +addBS :: String -> String +addBS s = subRegex addBSReg s "\\\\\\0" + +-- Apply the substitution to all Latex parts on the AST +niceFrac :: Inline -> Inline +niceFrac (Math t s) = Math t (addBS . rmBS $ s) +niceFrac x = x + +main :: IO () +main = toJSONFilter niceFrac + diff --git a/pandoc-boilerplate.cabal b/pandoc-boilerplate.cabal new file mode 100644 index 0000000..5e63a31 --- /dev/null +++ b/pandoc-boilerplate.cabal @@ -0,0 +1,25 @@ +-- Initial pandoc-boilerplate.cabal generated by cabal init. For further +-- documentation, see http://haskell.org/cabal/users-guide/ + +name: pandoc-boilerplate +version: 0.1.0.0 +-- synopsis: +-- description: +homepage: https://github.com/Phyks/pandoc_boilerplate +license: Beerware +license-file: LICENSE +author: Martin Pépin +maintainer: martin.pepin@netcourrier.com +-- copyright: +-- category: +build-type: Simple +extra-source-files: +cabal-version: >=1.10 + +library + -- exposed-modules: + -- other-modules: + -- other-extensions: + build-depends: pandoc, regex-compat, base >=4.6 && <4.7 + -- hs-source-dirs: + default-language: Haskell2010