misc/pysymbols: Support for sub/superscripts in code blocks.

Inline code from a Markdown source (`like this`) is typically translated
without the assistance of Pygments. As a result we don't get automatic
subscript and superscript support, and need to roll our own. This translation
is pretty blunt and fragile. Expect it to fall over in a TeX error if you pass,
e.g., a "\<^bsub>" without a closing "\<^esub>".
This commit is contained in:
Matthew Fernandez 2015-10-15 15:02:45 +11:00
parent d0693fc7d5
commit 5294d36438
1 changed files with 16 additions and 2 deletions

View File

@ -11,7 +11,7 @@
# @TAG(NICTA_BSD)
#
import codecs, collections, numbers, types
import codecs, collections, numbers, re, types
class IsaSymbolsException(Exception):
pass
@ -91,6 +91,7 @@ class Translator(object):
# Translation dictionaries that we'll lazily initialise later.
self._utf8_to_ascii = None
self._utf8_to_tex = None
self._hshifts_tex = None # Handling for sub-/super-scripts
def encode(self, data):
for symbol in self.symbols:
@ -128,7 +129,20 @@ class Translator(object):
if s.ascii_text.startswith('\\<') and
s.ascii_text.endswith('>')}
return ''.join(self._utf8_to_tex.get(c, c) for c in data)
if self._hshifts_tex is None:
# XXX: Hardcoded
self._hshifts_tex = (
(re.compile(r'\\<\^sub>(.)'), r'\\textsubscript{\1}'),
(re.compile(r'\\<\^sup>(.)'), r'\\textsuperscript{\1}'),
(re.compile(r'\\<\^bold>(.)'), r'\\textbf{\1}'),
(re.compile(r'\\<\^bsub>'), r'\\textsubscript{'),
(re.compile(r'\\<\^bsup>'), r'\\textsuperscript{'),
(re.compile(r'\\<\^esu\(b|p\)>'), '}'),
)
return ''.join(self._utf8_to_tex.get(c, c) for c in
reduce(lambda a, (regex, rep): regex.sub(rep, a), self._hshifts_tex,
data))
def make_translator(symbols_file):
assert isinstance(symbols_file, basestring)