Skip to content

Commit

Permalink
Making language selcetion more flexible and compatible with babel/pol…
Browse files Browse the repository at this point in the history
…yglossia.
  • Loading branch information
felixfontein committed Jan 15, 2017
1 parent d700d44 commit a0d7cff
Showing 1 changed file with 28 additions and 20 deletions.
48 changes: 28 additions & 20 deletions v7/latex/latex/parser.py
Expand Up @@ -134,6 +134,26 @@ def __init__(self, environment_name, argument_count, eat_trailing_whitespace, de
class ParsingEnvironment(object):
"""Environmental data for parsing, like registered commands and environments."""

languages = {

This comment has been minimized.

Copy link
@Kwpolska

Kwpolska Jan 15, 2017

Member

This list feels pretty small, is it user-expandable?

This comment has been minimized.

Copy link
@felixfontein

felixfontein Jan 15, 2017

Author Contributor

Not yet. This (among others) is on my TODO list.

This comment has been minimized.

Copy link
@felixfontein

felixfontein Jan 15, 2017

Author Contributor

Commit 745a14d now allows plugins to touch the parsing environment, so they can extend the language list.

'arabic': {
'locale': 'ar',
'right_to_left': True,
},
'hebrew': {
'locale': 'he',
'right_to_left': True,
},
'english': {
'locale': 'en',
},
'german': {
'locale': 'de',
},
'greek': {
'locale': 'el',
}
}

def __init__(self):
"""Initialize default environment."""
self.commands = {}
Expand All @@ -154,8 +174,7 @@ def __init__(self):
self.register_command_WS("label", 1)
self.register_command_WS("ref", 2, None)
self.register_command_WS("symbol", 1)
self.register_command_WS("myArabic", 1)
self.register_command_WS("myHebrew", 1)
self.register_command_WS("foreignlanguage", 2)
self.register_command("qed", 0)
self.register_command("includegraphics", 2, None, accept_unknown_commands=True)
self.register_command("setlength", 2, accept_unknown_commands=True)
Expand Down Expand Up @@ -490,17 +509,10 @@ def __postprocess_text(self, text):
return text

def __add_language(self, element, language, inline=True):
right_to_left = False
locale = None
if language == 'arabic':
right_to_left = True
locale = 'ar'
elif language == 'hebrew':
right_to_left = True
locale = 'he'
else:
langdata = self.parsing_environment.languages.get(language)
if langdata is None:
raise Exception("Unknown language '{0}'!".format(language))
return tree.Language(element, right_to_left, locale, inline=inline)
return tree.Language(element, langdata.get('right_to_left', False), langdata['locale'], inline=inline)

def __parse_symbol(self, code):
try:
Expand Down Expand Up @@ -583,10 +595,8 @@ def add_to_current_word(part):
add_to_current_word(self.__get_tikz_picture(command))
elif command[0] == 'begin' and command[1] == 'pstricks':
add_to_current_word(self.__get_PSTricks_picture(command))
elif command[0] == 'myArabic':
add_to_current_word(self.__add_language(command[1][0], 'arabic'))
elif command[0] == 'myHebrew':
add_to_current_word(self.__add_language(command[1][0], 'hebrew'))
elif command[0] == 'foreignlanguage':
add_to_current_word(self.__add_language(command[1][1], command[1][0].recombine_as_text()))
elif command[0] in _replacement_commands:
add_to_current_word(tree.WordPart(_replacement_commands[command[0]]))
else:
Expand Down Expand Up @@ -1017,10 +1027,8 @@ def add_to_current_block(part):
destination_block.labels.append(command[1][0].recombine_as_text())
elif command[0] == 'includegraphics':
add_to_current_word(self.__get_includegraphics(command))
elif command[0] == 'myArabic':
add_to_current_word(self.__add_language(command[1][0], 'arabic'))
elif command[0] == 'myHebrew':
add_to_current_word(self.__add_language(command[1][0], 'hebrew'))
elif command[0] == 'foreignlanguage':
add_to_current_word(self.__add_language(command[1][1], command[1][0].recombine_as_text()))
elif command[0] in _replacement_commands:
add_to_current_word(tree.WordPart(_replacement_commands[command[0]]))
else:
Expand Down

0 comments on commit a0d7cff

Please sign in to comment.