Merge pull request #8 from Kerl13/master

Usual functions' names & simple fractions
This commit is contained in:
Lucas Verney 2016-03-25 10:12:24 +01:00
commit 2faf08895a
7 changed files with 121 additions and 13 deletions

5
.gitignore vendored
View File

@ -1 +1,6 @@
*.pandoc.pdf
dist/
filters/*.hi
filters/*.o
filters/usual-fun
filters/nice-frac

3
LICENSE Normal file
View File

@ -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.

View File

@ -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

View File

@ -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 :)

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]+)"
-- 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

33
filters/usual-fun.hs Normal file
View File

@ -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

25
pandoc-boilerplate.cabal Normal file
View File

@ -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