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

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et: 

 

# Copyright 2014-2018 Florian Bruhin (The Compiler) <mail@qutebrowser.org> 

# 

# This file is part of qutebrowser. 

# 

# qutebrowser is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# qutebrowser 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. 

# 

# You should have received a copy of the GNU General Public License 

# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>. 

 

"""Utilities related to jinja2.""" 

 

import os 

import os.path 

import contextlib 

import traceback 

import mimetypes 

import html 

 

import jinja2 

import jinja2.exceptions 

from PyQt5.QtCore import QUrl 

 

from qutebrowser.utils import utils, urlutils, log 

 

 

html_fallback = """ 

<!DOCTYPE html> 

<html> 

<head> 

<meta charset="utf-8"> 

<title>Error while loading template</title> 

</head> 

<body> 

<p><span style="font-size:120%;color:red"> 

The %FILE% template could not be found!<br> 

Please check your qutebrowser installation 

</span><br> 

%ERROR% 

</p> 

</body> 

</html> 

""" 

 

 

class Loader(jinja2.BaseLoader): 

 

"""Jinja loader which uses utils.read_file to load templates. 

 

Attributes: 

_subdir: The subdirectory to find templates in. 

""" 

 

def __init__(self, subdir): 

self._subdir = subdir 

 

def get_source(self, _env, template): 

path = os.path.join(self._subdir, template) 

try: 

source = utils.read_file(path) 

except OSError as e: 

source = html_fallback.replace("%ERROR%", html.escape(str(e))) 

source = source.replace("%FILE%", html.escape(template)) 

log.misc.exception("The {} template could not be loaded from {}" 

.format(template, path)) 

# Currently we don't implement auto-reloading, so we always return True 

# for up-to-date. 

return source, path, lambda: True 

 

 

class Environment(jinja2.Environment): 

 

"""Our own jinja environment which is more strict.""" 

 

def __init__(self): 

super().__init__(loader=Loader('html'), 

autoescape=lambda _name: self._autoescape, 

undefined=jinja2.StrictUndefined) 

self.globals['resource_url'] = self._resource_url 

self.globals['file_url'] = urlutils.file_url 

self.globals['data_url'] = self._data_url 

self._autoescape = True 

 

@contextlib.contextmanager 

def no_autoescape(self): 

"""Context manager to temporarily turn off autoescaping.""" 

self._autoescape = False 

yield 

self._autoescape = True 

 

def _resource_url(self, path): 

"""Load images from a relative path (to qutebrowser). 

 

Arguments: 

path: The relative path to the image 

""" 

image = utils.resource_filename(path) 

return QUrl.fromLocalFile(image).toString(QUrl.FullyEncoded) 

 

def _data_url(self, path): 

"""Get a data: url for the broken qutebrowser logo.""" 

data = utils.read_file(path, binary=True) 

filename = utils.resource_filename(path) 

mimetype = mimetypes.guess_type(filename) 

assert mimetype is not None, path 

return urlutils.data_url(mimetype[0], data).toString() 

 

def getattr(self, obj, attribute): 

"""Override jinja's getattr() to be less clever. 

 

This means it doesn't fall back to __getitem__, and it doesn't hide 

AttributeError. 

""" 

return getattr(obj, attribute) 

 

 

def render(template, **kwargs): 

"""Render the given template and pass the given arguments to it.""" 

try: 

return environment.get_template(template).render(**kwargs) 

except jinja2.exceptions.UndefinedError: 

log.misc.exception("UndefinedError while rendering " + template) 

err_path = os.path.join('html', 'undef_error.html') 

err_template = utils.read_file(err_path) 

tb = traceback.format_exc() 

return err_template.format(pagename=template, traceback=tb) 

 

 

environment = Environment() 

js_environment = jinja2.Environment(loader=Loader('javascript'))