Skip to content

Commit 424f14a

Browse files
committed
lib/markdown2: introduce maths mode
Signed-off-by: Alexandre Terrasa <[email protected]>
1 parent 1f86870 commit 424f14a

File tree

6 files changed

+391
-0
lines changed

6 files changed

+391
-0
lines changed

lib/markdown2/markdown_html_rendering.nit

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,20 @@ module markdown_html_rendering
1818
import markdown_rendering
1919
import markdown_github
2020
import markdown_wikilinks
21+
import markdown_maths
22+
23+
import md5
2124

2225
# Markdown document renderer to HTML
2326
class HtmlRenderer
2427
super MdRenderer
2528

29+
# Output directory for Maths mode images
30+
#
31+
# If `null`, do not generate images.
32+
# Default: `null`.
33+
var maths_img_outdir: nullable String = null is writable
34+
2635
# HTML output under construction
2736
private var html: Buffer is noinit
2837

@@ -456,3 +465,30 @@ redef class MdWikilink
456465
v.add_raw "</wiki>"
457466
end
458467
end
468+
469+
# Math mode
470+
471+
redef class MdMaths
472+
redef fun render_html(v) do
473+
var out_dir = v.maths_img_outdir
474+
475+
if out_dir == null then
476+
v.add_raw opening_delimiter
477+
v.add_raw literal or else ""
478+
v.add_raw closing_delimiter
479+
return
480+
end
481+
482+
# generate image
483+
out_dir.mkdir
484+
var maths = literal or else ""
485+
var out = "{out_dir}/{maths.md5}.png"
486+
if not out.file_exists then
487+
sys.system "tex2im -o {out} -z -n -r 200x200 \"{maths.escape_to_sh}\""
488+
end
489+
490+
v.add_raw "<img alt=\""
491+
v.add_text literal or else ""
492+
v.add_raw "\" src=\"{out}\" />"
493+
end
494+
end

lib/markdown2/markdown_latex_rendering.nit

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module markdown_latex_rendering
1818
import markdown_rendering
1919
import markdown_github
2020
import markdown_wikilinks
21+
import markdown_maths
2122

2223
# Markdown document renderer to LaTeX
2324
class LatexRenderer
@@ -436,3 +437,13 @@ redef class MdWikilink
436437
v.add_raw "\}"
437438
end
438439
end
440+
441+
# Math mode
442+
443+
redef class MdMaths
444+
redef fun render_latex(v) do
445+
v.add_raw "$"
446+
v.add_raw literal or else ""
447+
v.add_raw "$"
448+
end
449+
end

lib/markdown2/markdown_man_rendering.nit

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module markdown_man_rendering
1818
import markdown_rendering
1919
import markdown_github
2020
import markdown_wikilinks
21+
import markdown_maths
2122

2223
# Markdown document renderer to Manpage
2324
class ManRenderer
@@ -256,3 +257,13 @@ redef class MdWikilink
256257
v.add ")"
257258
end
258259
end
260+
261+
# Math mode
262+
263+
redef class MdMaths
264+
redef fun render_man(v) do
265+
v.add "$"
266+
v.add literal or else ""
267+
v.add "$"
268+
end
269+
end

lib/markdown2/markdown_maths.nit

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
# This file is part of NIT ( http://www.nitlanguage.org ).
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# Markdown Maths mode
16+
#
17+
# Allows to write Maths equation with the LaTeX Maths mode.
18+
#
19+
# This mode requires `tex2im` to be installed and added to the path.
20+
# See: <http://www.nought.de/tex2im.php>
21+
module markdown_maths
22+
23+
intrude import markdown_inline_parsing
24+
intrude import markdown_block_parsing
25+
26+
redef class MdParser
27+
28+
# Enable maths mode
29+
var maths_mode = false is writable
30+
31+
redef var inline_parser is lazy do
32+
var parser = super
33+
parser.maths_mode = maths_mode
34+
return parser
35+
end
36+
end
37+
38+
redef class MdInlineParser
39+
40+
# Enable maths mode
41+
private var maths_mode = false
42+
43+
redef var delimiter_processors is lazy do
44+
var processors = super
45+
if maths_mode then
46+
processors.add new MdMathsProcessor
47+
end
48+
return processors
49+
end
50+
end
51+
52+
# Maths equation processor
53+
class MdMathsProcessor
54+
super MdDelimiterProcessor
55+
56+
# Maths equation delimiter
57+
#
58+
# Default is `$`.
59+
var delimiter_char = '$'
60+
61+
redef var min_length = 1
62+
redef fun opening_delimiter do return delimiter_char
63+
redef fun closing_delimiter do return delimiter_char
64+
redef fun delimiter_use(opener, closer) do return 1
65+
66+
redef fun process(opener, closer, delimiter_use) do
67+
var node = new MdMaths(
68+
new MdLocation(
69+
opener.location.line_start, opener.location.column_start,
70+
closer.location.line_end, closer.location.column_end),
71+
opening_delimiter.to_s)
72+
73+
var buffer = new Buffer
74+
var tmp = opener.next
75+
while tmp != null and tmp != closer do
76+
var next = tmp.next
77+
tmp.maths_literal(buffer)
78+
tmp.unlink
79+
tmp = next
80+
end
81+
node.literal = buffer.to_s
82+
opener.insert_after(node)
83+
end
84+
end
85+
86+
# Math equation node
87+
class MdMaths
88+
super MdDelimited
89+
90+
# Literal Maths string
91+
var literal: nullable String = null
92+
end
93+
94+
# Inline nodes
95+
96+
redef class MdNode
97+
98+
# Return the content of the node as a literal string
99+
private fun maths_literal(buffer: Buffer) do
100+
var node = first_child
101+
while node != null do
102+
node.maths_literal(buffer)
103+
node = node.next
104+
end
105+
end
106+
end
107+
108+
redef class MdCode
109+
redef fun maths_literal(buffer) do buffer.append "{delimiter}{literal}{delimiter}"
110+
end
111+
112+
redef class MdDelimited
113+
redef fun maths_literal(buffer) do
114+
buffer.append opening_delimiter
115+
super
116+
buffer.append closing_delimiter
117+
end
118+
end
119+
120+
redef class MdHtmlInline
121+
redef fun maths_literal(buffer) do buffer.append literal
122+
end
123+
124+
redef class MdLinkOrImage
125+
redef fun maths_literal(buffer) do
126+
if self isa MdLink and is_autolink then
127+
buffer.append "<{destination}>"
128+
return
129+
end
130+
if self isa MdImage then
131+
buffer.append "!"
132+
end
133+
buffer.append "["
134+
super
135+
buffer.append "]"
136+
buffer.append "({destination})"
137+
end
138+
end
139+
140+
redef class MdLink
141+
redef fun maths_literal(buffer) do buffer.append "[{title or else ""}]({destination})"
142+
end
143+
144+
redef class MdText
145+
redef fun maths_literal(buffer) do buffer.append literal
146+
end

lib/markdown2/markdown_md_rendering.nit

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module markdown_md_rendering
1818
import markdown_rendering
1919
import markdown_github
2020
import markdown_wikilinks
21+
import markdown_maths
2122

2223
# Markdown document renderer to Markdown
2324
class MarkdownRenderer
@@ -390,3 +391,13 @@ redef class MdWikilink
390391
v.add_raw "]]"
391392
end
392393
end
394+
395+
# Math mode
396+
397+
redef class MdMaths
398+
redef fun render_md(v) do
399+
v.add_raw "$"
400+
v.add_raw literal or else ""
401+
v.add_raw "$"
402+
end
403+
end

0 commit comments

Comments
 (0)