Coverage for src/ptf/templatetags/stringsfilter.py: 60%

93 statements  

« prev     ^ index     » next       coverage.py v7.6.4, created at 2024-11-05 09:56 +0000

1import re 

2 

3from unidecode import unidecode 

4 

5from django import template 

6from django.template.defaultfilters import stringfilter 

7from django.utils import timezone 

8from django.utils.translation import gettext as translate_text 

9 

10from ptf.cmds.xml import xml_utils 

11 

12register = template.Library() 

13 

14 

15@register.filter 

16def convert_jats_to_only_mathml(value): 

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

18 extract text from xml and preserve Mathml""" 

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

20 

21 

22@register.filter 

23def get_text_from_original_title_with_mathml(value, get_trans_title=False): 

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

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

26 return xml_utils.get_text_from_original_title_with_mathml( 

27 value, with_mathml=True, get_trans_title=get_trans_title 

28 ) 

29 

30 

31@register.filter 

32@stringfilter 

33def addXmlnsToBookTitle(value): 

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

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

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

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

38 

39 

40@register.filter 

41@stringfilter 

42def removeSpaceAttribute(value): 

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

44 return text 

45 

46 

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

48def split(value, key): 

49 """ 

50 Returns the value turned into a list. 

51 """ 

52 return value.split(key) 

53 

54 

55@register.filter("startswith") 

56def startswith(text, starts): 

57 if isinstance(text, str): 

58 return text.startswith(starts) 

59 return False 

60 

61 

62@register.filter 

63def is_translation(value): 

64 """ 

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

66 """ 

67 result = False 

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

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

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

71 result = True 

72 

73 return result 

74 

75 

76@register.filter 

77def allow_translation(doi): 

78 """ 

79 Allow translations only for articles edited by centre Mersenne 

80 """ 

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

82 

83 

84@register.filter 

85@stringfilter 

86def eudml_filter(value): 

87 values = value.split(":") 

88 if len(values) > 0: 88 ↛ 90line 88 didn't jump to line 90 because the condition on line 88 was always true

89 value = values[-1] 

90 return value 

91 

92 

93@register.filter 

94@stringfilter 

95def normalize_span(value): 

96 return xml_utils.normalise_span(value) 

97 

98 

99@register.filter 

100@stringfilter 

101def remove_email(value): 

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

103 while start > 0: 

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

105 

106 if end > 0: 106 ↛ 109line 106 didn't jump to line 109 because the condition on line 106 was always true

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

108 

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

110 

111 return value 

112 

113 

114@register.filter 

115@stringfilter 

116def first_letter_in_ascii(value): 

117 letter = value[0] 

118 new_letter = unidecode(letter).upper() 

119 return new_letter.upper() 

120 

121 

122@register.filter 

123@stringfilter 

124def get_volume_in_progress_msg(year): 

125 now = timezone.now() 

126 curyear = str(now.year) 

127 if curyear == year: 

128 return translate_text("Articles du volume en cours") 

129 else: 

130 return translate_text("Articles du volume") 

131 

132 

133@register.filter 

134def is_volume_with_numbers(value): 

135 is_with_numbers = True 

136 

137 if "-" in value: 

138 parts = value.split("-") 

139 for part in parts: 

140 try: 

141 int(part) 

142 except ValueError: 

143 is_with_numbers = False 

144 else: 

145 try: 

146 int(value) 

147 except ValueError: 

148 is_with_numbers = False 

149 

150 return is_with_numbers