scribble-math/katex-convert-unicode.rkt
2017-07-17 21:59:50 +02:00

107 lines
3.7 KiB
Racket
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#lang at-exp racket/base
(require racket/string)
(provide katex-convert-unicode)
(define (literal-alternatives→regexp literal-alternatives)
(string-append "("
(string-join (map regexp-quote literal-alternatives) "|")
")"))
(define (string-replace* str mathmode? sym→*)
(define →* (map (λ (x)
(cons (symbol->string (car x))
(cadr x)))
sym→*))
(define hash→* (make-immutable-hash →*))
(regexp-replace* (literal-alternatives→regexp (map car →*))
str
(λ (found . _)
(let ([replacement (hash-ref hash→* found)])
(if mathmode?
replacement
(string-append "$" replacement "$"))))))
(define (katex-convert-unicode str mathmode?)
(if (string? str)
(string-replace*
str
mathmode?
`([ "{}_0"]
[ "{}_1"]
[ "{}_2"]
[ "{}_3"]
[ "{}_4"]
[ "{}_5"]
[ "{}_6"]
[ "{}_7"]
[ "{}_8"]
[ "{}_9"]
[ "{}_i"]
[ "{}_j"]
[ "{}_k"]
[ "{}_l"]
[ "{}_m"]
[ "{}_n"]
[ "{}_o"]
[ "{}_x"]
[ "{}^0"]
[¹ "{}^1"]
[² "{}^2"]
[³ "{}^3"]
[ "{}^4"]
[ "{}^5"]
[ "{}^6"]
[ "{}^7"]
[ "{}^8"]
[ "{}^9"]
[ "{}^i"]
[ʲ "{}^j"]
[ "{}^k"]
[ˡ "{}^l"]
[ "{}^m"]
[ "{}^n"]
[ "{}^o"]
[ "\\subseteq{}" "\\ensuremath{\\subseteq}"]
[ "\\subset{}" "\\ensuremath{\\subset}"]
[ "\\supseteq{}" "\\ensuremath{\\supseteq}"]
[ "\\supset{}" "\\ensuremath{\\supset}"]
[ "\\rightarrow{}" "\\ensuremath{\\rightarrow}"]
[ "\\Rightarrow{}" "\\ensuremath{\\Rightarrow}"]
[ "\\leftarrow{}" "\\ensuremath{\\leftarrow}"]
[ "\\Leftarrow{}" "\\ensuremath{\\Leftarrow}"]
[ "\\leftrightarrow{}" "\\ensuremath{\\leftrightarrow}"]
[ "\\Leftrightarrow{}" "\\ensuremath{\\Leftrightarrow}"]
;; Partially extracted from my .XCompose generator
[ñ "\\tilde{n}" "{\\ifmmode\\tilde{n}\\else\\~{n}\\fi}"]
[Ñ "\\tilde{N}" "{\\ifmmode\\tilde{N}\\else\\~{N}\\fi}"]
[ "\\star{}" "\\ensuremath{\\star}"]
[ "\\ddot{\\star}}" "\\ensuremath{\\ddot{\\star}}"]
[ "^*" "^*"]
[ "\\langle{}" "\\ensuremath{\\mathsmaller{\\raisemath{.15ex}{\\langle}}}"]
[ "\\rangle{}" "\\ensuremath{\\mathsmaller{\\raisemath{.15ex}{\\rangle}}}"]
[ "\\cdots{}"]
[ "\\vdots{}"]
[ "\\iddots{}"]
[ "\\ddots{}"]
[ "\\mathbin{+\\mkern-6.5mu+}" "\\ensuremath{\\mathbin{+\\mkern-6.5mu+}}"]
[ "\\emptyset{}" "\\ensuremath{\\emptyset}"]
[ı⃗ "\\vec{\\i}}" "\\ensuremath{\\vec{\\i}"]
[ "\\oplus{}" "\\ensuremath{\\oplus}"]
[ "\\ominus{}" "\\ensuremath{\\ominus}"]
[ "\\Cup{}" "\\ensuremath{\\Cup}"]
;[ₗ "\\ensuremath{_{l}}"]
[ "::"]
[Λ "\\Lambda{}" "\\ensuremath{\\Lambda}"]
[ "\\forall{}"]
[ "\\exists{}"]
[ "\\equiv{}"]
[ "\\not\\equiv{}"]
[ "\\ldots{}"]
[ "\\cdots{}"]
[ "\\uddots{}"] ;; or \iddots from package mathdots, see http://tex.stackexchange.com/a/17650
[ "\\ddots{}"]
[ "\\notni{}"]
[ "\\mathcal{E}"]
))
str))