Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib/markdown2: introduce LaTeX/Maths mode #2721

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
36 changes: 36 additions & 0 deletions lib/markdown2/markdown_html_rendering.nit
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,20 @@ module markdown_html_rendering
import markdown_rendering
import markdown_github
import markdown_wikilinks
import markdown_maths

import md5

# Markdown document renderer to HTML
class HtmlRenderer
super MdRenderer

# Output directory for Maths mode images
#
# If `null`, do not generate images.
# Default: `null`.
var maths_img_outdir: nullable String = null is writable

# HTML output under construction
private var html: Buffer is noinit

Expand Down Expand Up @@ -456,3 +465,30 @@ redef class MdWikilink
v.add_raw "</wiki>"
end
end

# Math mode

redef class MdMaths
redef fun render_html(v) do
var out_dir = v.maths_img_outdir

if out_dir == null then
v.add_raw opening_delimiter
v.add_raw literal or else ""
v.add_raw closing_delimiter
return
end

# generate image
out_dir.mkdir
var maths = literal or else ""
var out = "{out_dir}/{maths.md5}.png"
if not out.file_exists then
sys.system "tex2im -o {out} -z -n -r 200x200 \"{maths.escape_to_sh}\""
end

v.add_raw "<img alt=\""
v.add_text literal or else ""
v.add_raw "\" src=\"{out}\" />"
end
end
11 changes: 11 additions & 0 deletions lib/markdown2/markdown_latex_rendering.nit
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module markdown_latex_rendering
import markdown_rendering
import markdown_github
import markdown_wikilinks
import markdown_maths

# Markdown document renderer to LaTeX
class LatexRenderer
Expand Down Expand Up @@ -436,3 +437,13 @@ redef class MdWikilink
v.add_raw "\}"
end
end

# Math mode

redef class MdMaths
redef fun render_latex(v) do
v.add_raw "$"
v.add_raw literal or else ""
v.add_raw "$"
end
end
11 changes: 11 additions & 0 deletions lib/markdown2/markdown_man_rendering.nit
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module markdown_man_rendering
import markdown_rendering
import markdown_github
import markdown_wikilinks
import markdown_maths

# Markdown document renderer to Manpage
class ManRenderer
Expand Down Expand Up @@ -256,3 +257,13 @@ redef class MdWikilink
v.add ")"
end
end

# Math mode

redef class MdMaths
redef fun render_man(v) do
v.add "$"
v.add literal or else ""
v.add "$"
end
end
146 changes: 146 additions & 0 deletions lib/markdown2/markdown_maths.nit
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
# This file is part of NIT ( http://www.nitlanguage.org ).
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Markdown Maths mode
#
# Allows to write Maths equation with the LaTeX Maths mode.
#
# This mode requires `tex2im` to be installed and added to the path.
# See: <http://www.nought.de/tex2im.php>
module markdown_maths

intrude import markdown_inline_parsing
intrude import markdown_block_parsing

redef class MdParser

# Enable maths mode
var maths_mode = false is writable

redef var inline_parser is lazy do
var parser = super
parser.maths_mode = maths_mode
return parser
end
end

redef class MdInlineParser

# Enable maths mode
private var maths_mode = false

redef var delimiter_processors is lazy do
var processors = super
if maths_mode then
processors.add new MdMathsProcessor
end
return processors
end
end

# Maths equation processor
class MdMathsProcessor
super MdDelimiterProcessor

# Maths equation delimiter
#
# Default is `$`.
var delimiter_char = '$'

redef var min_length = 1
redef fun opening_delimiter do return delimiter_char
redef fun closing_delimiter do return delimiter_char
redef fun delimiter_use(opener, closer) do return 1

redef fun process(opener, closer, delimiter_use) do
var node = new MdMaths(
new MdLocation(
opener.location.line_start, opener.location.column_start,
closer.location.line_end, closer.location.column_end),
opening_delimiter.to_s)

var buffer = new Buffer
var tmp = opener.next
while tmp != null and tmp != closer do
var next = tmp.next
tmp.maths_literal(buffer)
tmp.unlink
tmp = next
end
node.literal = buffer.to_s
opener.insert_after(node)
end
end

# Math equation node
class MdMaths
super MdDelimited

# Literal Maths string
var literal: nullable String = null
end

# Inline nodes

redef class MdNode

# Return the content of the node as a literal string
private fun maths_literal(buffer: Buffer) do
var node = first_child
while node != null do
node.maths_literal(buffer)
node = node.next
end
end
end

redef class MdCode
redef fun maths_literal(buffer) do buffer.append "{delimiter}{literal}{delimiter}"
end

redef class MdDelimited
redef fun maths_literal(buffer) do
buffer.append opening_delimiter
super
buffer.append closing_delimiter
end
end

redef class MdHtmlInline
redef fun maths_literal(buffer) do buffer.append literal
end

redef class MdLinkOrImage
redef fun maths_literal(buffer) do
if self isa MdLink and is_autolink then
buffer.append "<{destination}>"
return
end
if self isa MdImage then
buffer.append "!"
end
buffer.append "["
super
buffer.append "]"
buffer.append "({destination})"
end
end

redef class MdLink
redef fun maths_literal(buffer) do buffer.append "[{title or else ""}]({destination})"
end

redef class MdText
redef fun maths_literal(buffer) do buffer.append literal
end
11 changes: 11 additions & 0 deletions lib/markdown2/markdown_md_rendering.nit
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module markdown_md_rendering
import markdown_rendering
import markdown_github
import markdown_wikilinks
import markdown_maths

# Markdown document renderer to Markdown
class MarkdownRenderer
Expand Down Expand Up @@ -390,3 +391,13 @@ redef class MdWikilink
v.add_raw "]]"
end
end

# Math mode

redef class MdMaths
redef fun render_md(v) do
v.add_raw "$"
v.add_raw literal or else ""
v.add_raw "$"
end
end
Loading