text
stringlengths
0
828
expression object from KB to a global dictionary.
:param kb_file: full path to file containing latex2unicode translations.
Defaults to CFG_ETCDIR/bibconvert/KB/latex-to-unicode.kb
:type kb_file: string
:return: dict of type: {'regexp_obj': regexp match object,
'table': dict of LaTeX -> Unicode mappings}
:rtype: dict
""""""
if kb_file is None:
kb_file = get_kb_filename()
try:
data = open(kb_file)
except IOError:
# File not found or similar
sys.stderr.write(
""\nCould not open LaTeX to Unicode KB file. ""
""Aborting translation.\n"")
return CFG_LATEX_UNICODE_TRANSLATION_CONST
latex_symbols = []
translation_table = {}
for line in data:
# The file has form of latex|--|utf-8. First decode to Unicode.
line = line.decode('utf-8')
mapping = line.split('|--|')
translation_table[mapping[0].rstrip('\n')] = mapping[1].rstrip('\n')
latex_symbols.append(re.escape(mapping[0].rstrip('\n')))
data.close()
CFG_LATEX_UNICODE_TRANSLATION_CONST[
'regexp_obj'] = re.compile(""|"".join(latex_symbols))
CFG_LATEX_UNICODE_TRANSLATION_CONST['table'] = translation_table"
4211,"def translate_to_ascii(values):
r""""""Transliterate the string into ascii representation.
Transliterate the string contents of the given sequence into ascii
representation.
Returns a sequence with the modified values if the module 'unidecode'
is available. Otherwise it will fall back to the inferior strip_accents
function.
For example: H\xc3\xb6hne becomes Hohne.
Note: Passed strings are returned as a list.
:param values: sequence of strings to transform
:type values: sequence
:return: sequence with values transformed to ascii
:rtype: sequence
""""""
if not values and not isinstance(values, str):
return values
if isinstance(values, str):
values = [values]
for index, value in enumerate(values):
if not value:
continue
unicode_text = decode_to_unicode(value)
if u""[?]"" in unicode_text:
decoded_text = []
for unicode_char in unicode_text:
decoded_char = unidecode(unicode_char)
# Skip unrecognized characters
if decoded_char != ""[?]"":
decoded_text.append(decoded_char)
ascii_text = ''.join(decoded_text).encode('ascii')
else:
ascii_text = unidecode(unicode_text).replace(
u""[?]"", u"""").encode('ascii')
values[index] = ascii_text
return values"
4212,"def xml_entities_to_utf8(text, skip=('lt', 'gt', 'amp')):
""""""Translate HTML or XML character references to UTF-8.
Removes HTML or XML character references and entities from a text string
and replaces them with their UTF-8 representation, if possible.
:param text: The HTML (or XML) source text.
:type text: string
:param skip: list of entity names to skip when transforming.
:type skip: iterable
:return: The plain text, as a Unicode string, if necessary.
@author: Based on http://effbot.org/zone/re-sub.htm#unescape-html
""""""
def fixup(m):
text = m.group(0)
if text[:2] == ""&#"":
# character reference
try:
if text[:3] == ""&#x"":
return unichr(int(text[3:-1], 16)).encode(""utf-8"")
else:
return unichr(int(text[2:-1])).encode(""utf-8"")
except ValueError:
pass