Coverage for apps/ptf/templatetags/stringsfilter.py: 94%

61 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2024-02-28 09:09 +0000

1import re 

2 

3from django import template 

4from django.template.defaultfilters import stringfilter 

5 

6from ptf.cmds.xml import xml_utils 

7 

8register = template.Library() 

9 

10 

11@register.filter 

12def convert_jats_to_only_mathml(value): 

13 """Usage, {{ value_xml|convert_jats_to_only_mathml }} 

14 extract text from xml and preserve Mathml""" 

15 return xml_utils.get_text_from_xml_with_mathml(value, with_mathml=True) 

16 

17 

18@register.filter 

19def get_text_from_original_title_with_mathml(value, get_trans_title=False): 

20 """Usage, {{ value_xml|get_text_from_original_title_with_mathml }} 

21 extract text for principal lang from title_xml and preserve Mathml""" 

22 return xml_utils.get_text_from_original_title_with_mathml( 

23 value, with_mathml=True, get_trans_title=get_trans_title 

24 ) 

25 

26 

27@register.filter 

28@stringfilter 

29def addXmlnsToBookTitle(value): 

30 """Usage, {{ value|addXmlnsToBookTitle }} 

31 add xmlns b: before tag beginning by book""" 

32 regex = re.compile(r"< ( /{0,1} ) ( book ) ", re.VERBOSE) 

33 return regex.sub(r"<\1b:\2", value) 

34 

35 

36@register.filter 

37@stringfilter 

38def removeSpaceAttribute(value): 

39 text = value.replace('xml:space="preserve"', "") 

40 return text 

41 

42 

43@register.filter(name="split") 

44def split(value, key): 

45 """ 

46 Returns the value turned into a list. 

47 """ 

48 return value.split(key) 

49 

50 

51@register.filter("startswith") 

52def startswith(text, starts): 

53 if isinstance(text, str): 

54 return text.startswith(starts) 

55 return False 

56 

57 

58@register.filter 

59def is_translation(value): 

60 """ 

61 PIDs of translated articles end with "-@lang", using the ISO 639-1 language code 

62 """ 

63 result = False 

64 if value.find("-") > 0: 

65 value = value.split("-")[-1] 

66 if (len(value) == 2 and not value.isdigit()) or value == "Latn": 66 ↛ 67line 66 didn't jump to line 67, because the condition on line 66 was never true

67 result = True 

68 

69 return result 

70 

71 

72@register.filter 

73def allow_translation(doi): 

74 """ 

75 Allow translations only for articles edited by centre Mersenne 

76 """ 

77 return doi.find("10.5802/") == 0 

78 

79 

80@register.filter 

81@stringfilter 

82def eudml_filter(value): 

83 values = value.split(":") 

84 if len(values) > 0: 84 ↛ 86line 84 didn't jump to line 86, because the condition on line 84 was never false

85 value = values[-1] 

86 return value 

87 

88 

89@register.filter 

90@stringfilter 

91def normalize_span(value): 

92 return xml_utils.normalise_span(value) 

93 

94 

95@register.filter 

96@stringfilter 

97def remove_email(value): 

98 start = value.find("<email>") 

99 while start > 0: 

100 end = value.find("</email>") 

101 

102 if end > 0: 102 ↛ 105line 102 didn't jump to line 105, because the condition on line 102 was never false

103 value = value[0:start] + value[end + 8 :] 

104 

105 start = value.find("<email>") 

106 

107 return value