From f3735c4e4796ba5d93dd2b35d2d3f685d561defc Mon Sep 17 00:00:00 2001 From: Kerl13 Date: Wed, 23 Mar 2016 18:56:19 +0100 Subject: [PATCH 1/4] Add a filter for simple fractions --- Makefile | 22 ++++++++++++++++++++-- README.md | 8 +++----- filters/nice-frac.hs | 26 ++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 filters/nice-frac.hs diff --git a/Makefile b/Makefile index 4308a74..eb74745 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/README.md b/README.md index dabd936..7d34c57 100644 --- a/README.md +++ b/README.md @@ -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 :) diff --git a/filters/nice-frac.hs b/filters/nice-frac.hs new file mode 100644 index 0000000..9cdd8b4 --- /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]+)" + +-- 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 + From 0efa4c9ed143fdea7daba83251b63c63d91e95e8 Mon Sep 17 00:00:00 2001 From: Kerl13 Date: Thu, 24 Mar 2016 09:27:55 +0100 Subject: [PATCH 2/4] Recognise usual functions even without the leading '\' --- Makefile | 10 +++++----- README.md | 16 ++++++++-------- filters/nice-frac.hs | 6 +++--- filters/usual-fun.hs | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 16 deletions(-) create mode 100644 filters/usual-fun.hs diff --git a/Makefile b/Makefile index eb74745..6b0fda6 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ SOURCES = $(wildcard *.md) OUT = $(SOURCES:.md=.pdf) -HS_FILTERS_NAMES = nice-frac +HS_FILTERS_NAMES = nice-frac usual-fun PY_FILTERS_NAMES = pandoc-svg.py EXT_FILTERS = pandoc-crossref @@ -13,11 +13,11 @@ FILTERS = $(PY_FILTERS) $(HS_FILTERS) $(EXT_FILTERS) all: $(HS_FILTERS) $(OUT) -$(OUT): $(SOURCES) - pandoc --smart -t latex $< $(addprefix --filter=, $(FILTERS)) -o $@ +$(OUT): + pandoc -S -t latex $(basename $@).md $(addprefix --filter=, $(FILTERS)) -o $@ -$(HS_FILTERS): $(addsuffix .hs, $(HS_FILTERS)) - ghc --make -O2 $< -o $@ +$(HS_FILTERS): + ghc --make $@.hs -o $@ clean: rm -f $(OUT) diff --git a/README.md b/README.md index 7d34c57..0fbd102 100644 --- a/README.md +++ b/README.md @@ -7,19 +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 fractions like "(n/k)" to "\frac{n}{k}" when n and k are integers in - math environment. +* 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 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 `\`. - * anything else like this that could help writing more readable LaTeX code :) diff --git a/filters/nice-frac.hs b/filters/nice-frac.hs index 9cdd8b4..a4732a4 100644 --- a/filters/nice-frac.hs +++ b/filters/nice-frac.hs @@ -9,12 +9,12 @@ space = "[ \\t\\n\\r]*" num :: String num = "([0-9]+)" --- Substitute latex fractions like '\frac{42}{1}' for (42 / 1) +-- Replace fractions like "(42 / 1)" by "\frac{42}{1}" fracReg :: Regex fracReg = mkRegex $ - "\\(" ++ space ++ num ++ space ++ "/" ++ space ++ num ++ "\\)" + "\\(" ++ space ++ num ++ space ++ "/" ++ space ++ num ++ space ++ "\\)" frac :: String -> String -frac s = subRegex fracReg s "\\frac{\\1}{\\2}" +frac s = subRegex fracReg s "\\\\frac{\\1}{\\2}" -- Apply the substitution to all Latex parts on the AST niceFrac :: Inline -> Inline 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 + From 3891cf7123a556f39ec9d63c158d1228a383580f Mon Sep 17 00:00:00 2001 From: Kerl13 Date: Thu, 24 Mar 2016 18:39:04 +0100 Subject: [PATCH 3/4] add a .cabal file --- LICENSE | 3 +++ pandoc-boilerplate.cabal | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 LICENSE create mode 100644 pandoc-boilerplate.cabal 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/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 From ef33ff0205c4a0bd5279286ee80bad795e9d341b Mon Sep 17 00:00:00 2001 From: Kerl13 Date: Thu, 24 Mar 2016 20:53:44 +0100 Subject: [PATCH 4/4] update .gitignore --- .gitignore | 5 +++++ 1 file changed, 5 insertions(+) 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