This file is indexed.

/usr/share/pyshared/libopensesame/html.py is in opensesame 0.27.4-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#-*- coding:utf-8 -*-

"""
This file is part of OpenSesame.

OpenSesame 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.

OpenSesame 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 OpenSesame.  If not, see <http://www.gnu.org/licenses/>.
"""

from HTMLParser import HTMLParser
from libopensesame import debug

class html(HTMLParser):

	"""
	A simple HTML parser that deals with a subset of HTML tags and renders text
	onto an openexp canvas. Currently, the following tags are supported: <b>,
	<i>, <br />, and <span>. For <span> you can pass size and style keywords.
	"""

	valid_end_tags = u'i', u'b', u'u', u'span'
	valid_start_tags = u'i', u'b', u'u', u'span', u'br'

	def handle_data(self, data):

		"""
		Receive a single piece of text that has the same style

		Arguments:
		data -- a data string
		"""

		style = self.style()
		while len(data) > 0:
			i = data[1:].find(u' ')
			if i < 0:
				break
			word = data[:i+1]
			data = data[i+1:]
			self.paragraph.append( (word, style) )
		self.paragraph.append( (data, style) )

	def handle_endtag(self, tag):

		"""
		Handle a closing tag

		Arguments:
		tag -- the closing tag
		"""

		if tag not in self.valid_end_tags:
			return

		if self.current_tag != tag:
			debug.msg(u'Warning: expecting closing tag for %s, got %s' % \
				(self.current_tag, tag), reason=u'warning')

		self.pop_style()

	def handle_starttag(self, tag, attrs):

		"""
		Handle an opening tag

		Arguments:
		tag -- the closing tag
		attrs -- the tag attributes
		"""

		if tag not in self.valid_start_tags:
			return

		if tag == u'br':
			self.text.append(self.paragraph)
			self.paragraph = []
			return

		self.current_tag = tag

		if tag == u'span':
			style = {}
			for var, val in attrs:
				style[str(var)] = val
			self.push_style(**style)
		elif tag == u'b':
			self.push_style(bold=True)
		elif tag == u'i':
			self.push_style(italic=True)
		elif tag == u'u':
			self.push_style(underline=True)
		else:
			debug.msg(u'Unrecognized tag: %s' % tag)

	def render(self, text, x, y, canvas, max_width=None, center=False, \
		color=None, html=True):

		"""
		Render an HTML formatted string onto a canvas

		Arguments:
		text -- the text string
		x -- the left-most coordinate
		y -- the top coordinate
		canvas -- an openexp canvas

		Keyword arguments:
		max_width -- the maximum width, after which line wrapping should occur,
					 or None to wrap at screen edge (default=None)
		center -- indicates whether the text should be center aligned
				  (default=False)
		color -- indicates the color of the text or None for canvas default
				 (default=None)
		html -- indicates whether HTML should be parsed (default=True)
		"""

		debug.msg(text)

		# Make sure that it's a string
		text = canvas.experiment.unistr(text)

		# Convert line breaks to HTML break tags
		text = text.replace(u'\n', u'<br />')

		# Initialize the style
		self.canvas = canvas
		self.default_style = {
			u'style' : canvas.font_style,
			u'bold' : canvas.font_bold,
			u'italic' : canvas.font_italic,
			u'color' : canvas.fgcolor,
			u'size' : canvas.font_size,
			u'underline' : canvas.font_underline
			}
		backup_style = self.default_style.copy()

		# Optionally override color
		if color != None:
			self.default_style[u'color'] = color

		# Set the maximum width
		if max_width == None:
			max_x = canvas.experiment.width
		else:
			if center:
				max_x = x + max_width/2
			else:
				max_x = x + max_width

		# First parse the HTML
		self.text = []
		self.paragraph = []
		self.style_stack = []
		self.current_tag = None
		self.push_style()

		# Optionally parse HTML
		if html:
			self.feed(text)
		else:
			self.handle_data(text)
		self.text.append(self.paragraph)

		# If we want to center the next, we need a dry run to calculate all the
		# line lengths and determine the vertical and horizontal offset for each
		# line
		if center:
			l_x_offset = []
			_y = y
			for paragraph in self.text:
				_x = x
				dy = canvas.text_size(u'dummy')[1]
				for word, style in paragraph:

					# Set the style
					canvas.set_font(style[u'style'], int(style[u'size']), \
						bold=style[u'bold'], italic=style[u'italic'], \
						underline=style[u'underline'])

					# Line wrap if we run out of the screen
					dx, dy = canvas.text_size(word)
					if _x+dx > max_x + (max_x-x):
						l_x_offset.append(-(_x-x)/2)
						_x = x
						_y += dy
						word = word.lstrip()

					# Draw!
					_x += dx
				l_x_offset.append(-(_x-x)/2)
				_y += dy
			l_x_offset.reverse()
			y_offset = -(_y-y)/2

		# Now render it onto the canvas
		if center:
			_y = y+y_offset
		else:
			_y = y
		for paragraph in self.text:
			if center:
				_x = x+l_x_offset.pop()
			else:
				_x = x
			dy = canvas.text_size(u'dummy')[1]
			for word, style in paragraph:

				# Set the style
				canvas.set_font(style[u'style'], int(style[u'size']), \
					bold=style[u'bold'], italic=style[u'italic'], underline= \
					style[u'underline'])
				canvas.set_fgcolor(style[u'color'])

				# Line wrap if we run out of the screen
				dx, dy = canvas.text_size(word)
				if _x+dx > max_x:
					if center:
						_x = x+l_x_offset.pop()
					else:
						_x = x
					_y += dy
					word = word.lstrip()

				# Draw!
				canvas._text(word, _x, _y)
				_x += dx
			_y += dy

		# Restore the canvas font and colors
		canvas.set_fgcolor(backup_style[u'color'])
		canvas.set_font(backup_style[u'style'], int(backup_style[u'size']), \
			bold=backup_style[u'bold'], italic=backup_style[u'italic'])

	def pop_style(self):

		"""Pop a style from the style stack"""

		self.style_stack.pop()
		if len(self.style_stack) == 0:
			self.push_style()

	def push_style(self, **keywords):

		"""
		Push a new style onto the style stack

		Keyword arguments:
		**keywords -- a keyword dictionary with style attributes
		"""

		if len(self.style_stack) == 0:
			current_style = self.default_style.copy()
		else:
			current_style = self.style_stack[-1].copy()
		for tag, val in keywords.iteritems():
			current_style[tag] = val
		self.style_stack.append(current_style)

	def style(self):

		"""
		Get the current style

		Returns:
		A style dictionary
		"""

		return self.style_stack[-1].copy()