Module: Latexmath::Preprocessor
- Defined in:
- lib/latexmath/parser/preprocessor.rb
Overview
The legacy LaTeX input preprocessing (Plurimath::Latex::Parser at the pinned commit, see docs/parser-contract.adoc), step for step:
- remember every \mbox... and \text... group of the input;
- decode HTML entities, then encode the whole text with hexadecimal entities;
- remove spaces that do not follow a backslash and runs of newlines; turn "\ " into "\"; turn & and " back into & and "; turn a backslash before an encoded newline into "\ " unless it ends a row break; drop the remaining encoded newlines;
- put the remembered groups back as the caller wrote them.
Internal.
Constant Summary collapse
- TEXT_OPEN =
/\\(?:mbox|text)\{/- TEXT_OPEN_LENGTH =
"\\mbox{".length
- ENTITIES =
HTMLEntities.new
Class Method Summary collapse
-
.call(input) ⇒ Object
The grammar input for the normalized UTF-8 caller
input. - .gsub_space_and_unicodes(text) ⇒ Object
-
.replace_text_groups(text) ⇒ Object
Legacy: text.gsub(TEXT_REGEX) { ... }, keeping its result encoding.
-
.text_groups(text) ⇒ Object
Legacy: the matches of %r(\(?:mbox|text)[^]+}), as index ranges.
Class Method Details
.call(input) ⇒ Object
The grammar input for the normalized UTF-8 caller input.
27 28 29 30 31 32 |
# File 'lib/latexmath/parser/preprocessor.rb', line 27 def call(input) text_functions = text_groups(input).map { |range| input[range] } text = ENTITIES.encode(ENTITIES.decode(input), :hexadecimal) text = gsub_space_and_unicodes(text) replace_text_groups(text) { text_functions.shift } end |
.gsub_space_and_unicodes(text) ⇒ Object
67 68 69 70 71 72 73 74 75 |
# File 'lib/latexmath/parser/preprocessor.rb', line 67 def gsub_space_and_unicodes(text) text .gsub(/((?<!\\) )|\n+/, "") .gsub("\\\\ ", "\\\\\\\\") .gsub("&", "&") .gsub(""", "\"") .gsub(/(?<!\\\\)\\
/, "\\ ") .gsub("
", "") end |
.replace_text_groups(text) ⇒ Object
Legacy: text.gsub(TEXT_REGEX) { ... }, keeping its result encoding.
57 58 59 60 61 62 63 64 65 |
# File 'lib/latexmath/parser/preprocessor.rb', line 57 def replace_text_groups(text) out = String.new(encoding: text.encoding) pos = 0 text_groups(text).each do |range| out << text[pos...range.begin] << yield.to_s pos = range.end + 1 end out << text[pos..] end |
.text_groups(text) ⇒ Object
Legacy: the matches of %r(\(?:mbox|text)[^]+}), as index ranges. Each match runs from an opener to the first "}" after it, with at least one character between; an opener followed directly by "}" does not match there and the search goes on one character later. Written out without the backtracking regexp, which is polynomial on input with many unclosed openers.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/latexmath/parser/preprocessor.rb', line 40 def text_groups(text) groups = [] pos = 0 while (start = text.index(TEXT_OPEN, pos)) body = start + TEXT_OPEN_LENGTH close = text.index("}", body) or break if close == body pos = start + 1 else groups << (start..close) pos = close + 1 end end groups end |