|
| 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 |
0 commit comments