Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
4e04b48
new file: README.md
Oct 11, 2020
4812569
modified: displaymath2svg README.md
Oct 11, 2020
d51a163
modified: README.md
Oct 11, 2020
2d30334
modified: README.md
Oct 11, 2020
ac2e714
Privacy code auditing
Oct 11, 2020
45b55e3
modified: displaymath2svg README.md
Oct 11, 2020
a160c06
modified: README.md
Oct 11, 2020
797a596
displaymath2svg.lua test added
Oct 11, 2020
f6d72c9
displaymath2svg
Oct 11, 2020
82fa204
modified: README.md
Oct 11, 2020
8db789e
MIT License
Oct 11, 2020
113413f
MathML comment
Oct 11, 2020
3102fb5
math2svg.lua
Oct 12, 2020
1f96b0d
README.md
Oct 12, 2020
6848665
refactoring of math2svg.lua
Oct 12, 2020
2121998
pandoc.Image when output format is not HTML.
Oct 12, 2020
277c007
License
Oct 12, 2020
74a4445
install instructions
Oct 12, 2020
afdfe46
MathML newcommands
Oct 12, 2020
1516ed5
improved inline SVG
Oct 12, 2020
fbdf9ce
argumentlist & reduced description
Oct 13, 2020
a38c802
macros removed in favour of header-includes
Oct 13, 2020
dd19ae1
sample.yaml
Oct 13, 2020
c8975ea
configuration over --metadata key values
Oct 14, 2020
0d9e3e3
README.md math2svg.lua
Oct 15, 2020
e40396e
Documentation completed (#1)
stroobandt Oct 15, 2020
6c1410d
README.md
Oct 15, 2020
b52cf39
README.md
Oct 16, 2020
15ff17c
README.md
Oct 16, 2020
a0a1d58
README.md
Oct 16, 2020
db42ee6
120 column lines & math2svg_tex2svg key value
Oct 16, 2020
0d3876c
README.md privacy statement
Oct 16, 2020
237ce0c
Makefile
Oct 16, 2020
0691b00
<span class="math display">
Oct 16, 2020
02354d0
description
Oct 17, 2020
473c360
.travis.yml
Oct 17, 2020
59fd787
README.md
Oct 26, 2020
6811a49
README.md 80 columns
Oct 26, 2020
0ccfcc1
README.md 80 columns
Oct 26, 2020
4dd39ad
math2svg.lua 80 columns
Oct 26, 2020
4719dfb
Typo in README.md
stroobandt Oct 26, 2020
bb6c273
.travis.yml
Oct 26, 2020
c94c9c0
.travis.yml
Oct 26, 2020
58935cb
.travis.yml
Oct 27, 2020
55cfd17
.travis.yml
stroobandt Oct 27, 2020
4601caa
Dockerfile
stroobandt Jan 14, 2021
267b99a
Dockerfile
stroobandt Jan 14, 2021
803c7a2
year 2021
stroobandt Jan 14, 2021
f70e9c5
README.md
stroobandt Jan 16, 2021
9f2f99d
README.md
stroobandt Jan 16, 2021
1b7efba
Merge branch 'master' into master
tarleb Jan 16, 2021
05f602a
Merge remote-tracking branch 'upstream/master'
stroobandt Jan 18, 2021
7e5a49f
Merge branch 'master' of github.com:stroobandt/lua-filters
stroobandt Jan 18, 2021
89489da
SVG caching
stroobandt Jan 19, 2021
d6c8c6b
SVG caching
stroobandt Jan 19, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion math2svg/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ No Internet connection is required when generating or viewing SVG formulas,
resulting in both absolute privacy and offline, standalone robustness.


## Version history

- 2021-01-19: SVG caching
- 2021-01-16: Initial release


## Requirements

First, use the package manager of your operating system to install `pandoc`,
Expand Down Expand Up @@ -54,7 +60,7 @@ pandoc --mathml --filter='math2svg.lua'

The math2svg filter is entirely configurable over
[`--metadata` key value pairs](pandoc.metadata).
Nine configuration keys are available with sensible default values.
Ten configuration keys are available with sensible default values.
Hence, depending on your system and intentions, not all keys are necessarily
required.

Expand All @@ -69,6 +75,7 @@ required.
|`math2svg_ex`|`6`|
|`math2svg_width`|`100`|
|`math2svg_extensions`|`''`|
|`math2svg_cache`|`true`|


### Key value `math2svg_tex2svg`
Expand Down Expand Up @@ -138,6 +145,11 @@ This MathJaX extension can be loaded by specifying the string `'TeX/AMSmath'`
as the value of the `math2svg_extensions` key.


### Key value `math2svg_cache`
Caching avoids repetition of the SVG rendering of identical math formulas,
both for display and inline math. Caching is on by default.


### Adding `header-includes`
It might turn out useful to systematically include LaTeX macros, for example as
shown below, a series of `\newcommand`.
Expand Down
54 changes: 44 additions & 10 deletions math2svg/math2svg.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
-- VERSION 2021-01-19


-- DESCRIPTION
--
-- This Lua filter for Pandoc converts LaTeX math to MathJax generated
Expand Down Expand Up @@ -75,6 +78,17 @@ local extensions = ''
-- /usr/local/lib/node_modules/mathjax-node-cli/node_modules/mathjax/unpacked/\
-- extensions/

-- Speed up conversion by caching SVG.
local cache = true

local _cache = {}
_cache.DisplayMath = {}
_cache.InlineMath = {}

local tags = {}
tags.DisplayMath = {'<span class="math display">', '</span>'}
tags.InlineMath = {'<span class="math inline">', '</span>'}


function Meta(meta)

Expand All @@ -87,14 +101,15 @@ function Meta(meta)
ex = tostring(meta.math2svg_ex or ex)
width = tostring(meta.math2svg_width or width)
extensions = tostring(meta.math2svg_extensions or extensions)
cache = tostring(meta.math2svg_cache or cache)

end


function Math(elem)

local svg = nil
local tags = nil

local argumentlist = {
'--speech', speech,
'--linebreaks', linebreaks,
Expand All @@ -117,26 +132,45 @@ function Math(elem)
--extensions extra MathJax extensions [default: ""]
-- e.g. 'Safe,TeX/noUndefined'

if elem.mathtype == 'DisplayMath' and display2svg then
svg = pandoc.pipe(tex2svg, argumentlist, '')
tags = {'<span class="math display">', '</span>'}
if (elem.mathtype == 'DisplayMath' and display2svg)
or (elem.mathtype == 'InlineMath' and inline2svg ) then

if cache then
-- Attempt to retrieve cache.
svg = _cache[elem.mathtype][elem.text]
end

if not svg then

if elem.mathtype == 'InlineMath' then
-- Add the --inline argument to the argument list.
table.insert(argumentlist, 1, '--inline')
end

-- Generate SVG.
svg = pandoc.pipe(tex2svg, argumentlist, '')

if cache then
-- Add to cache.
_cache[elem.mathtype][elem.text] = svg
end

elseif elem.mathtype == 'InlineMath' and inline2svg then
table.insert(argumentlist, 1, '--inline')
svg = pandoc.pipe(tex2svg, argumentlist, '')
tags = {'<span class="math inline">', '</span>'}
end

end

-- Return
if svg then

if FORMAT:match '^html.?' then
svg = tags[1] .. svg .. tags[2]
svg = tags[elem.mathtype][1] .. svg .. tags[elem.mathtype][2]
return pandoc.RawInline(FORMAT, svg)

else
local filename = pandoc.sha1(svg) .. '.svg'
pandoc.mediabag.insert(filename, 'image/svg+xml', svg)
return pandoc.Image('', filename)

end

else
Expand All @@ -145,7 +179,7 @@ function Math(elem)

end

end
end -- function


-- Redefining the execution order.
Expand Down