Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

""" 

MathML output support 

 

In order to support MathML formatting, an object should define a special 

method _mathml_(self) that returns its MathML representation. 

""" 

 

#***************************************************************************** 

# 

# Sage: System for Algebra and Geometry Experimentation 

# 

# Copyright (C) 2005 William Stein <wstein@gmail.com> 

# 

# Distributed under the terms of the GNU General Public License (GPL) 

# 

# This code is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 

# General Public License for more details. 

# 

# The full text of the GPL is available at: 

# 

# http://www.gnu.org/licenses/ 

#***************************************************************************** 

from __future__ import absolute_import 

from six import iteritems, integer_types 

 

 

def list_function(x): 

return 'MATHML version of the list %s' % (x,) 

 

 

def tuple_function(x): 

return 'MATHML version of the tuple %s' % (x,) 

 

 

def bool_function(x): 

return 'MATHML version of %s' % (x,) 

 

 

def str_function(x): 

return 'MATHML version of the string %s' % (x,) 

 

# One can add to the latex_table in order to install latexing 

# functionality for other types. 

 

mathml_table = {list: list_function, 

tuple:tuple_function, 

bool:bool_function, 

str: str_function, 

float:str} 

for x in integer_types: 

mathml_table[x] = str 

 

class MathML(str): 

def __repr__(self): 

return str(self) 

 

 

def mathml(x): 

""" 

Output x formatted for inclusion in a MathML document. 

""" 

try: 

return MathML(x._mathml_()) 

except (AttributeError, TypeError): 

for k, f in iteritems(mathml_table): 

if isinstance(x, k): 

return MathML(f(x)) 

 

if x is None: 

return MathML("MATHML version of 'None'") 

 

return MathML(str_function(str(x)))