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

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

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

""" 

HTML Fragments 

 

This module defines a HTML fragment class, which holds a piece of 

HTML. This is primarily used in browser-based notebooks, though it 

might be useful for creating static pages as well. 

""" 

from __future__ import absolute_import 

 

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

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

# 

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

# as published by the Free Software Foundation; either version 2 of 

# the License, or (at your option) any later version. 

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

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

 

import warnings 

from sage.misc.latex import latex 

from sage.misc.sage_eval import sage_eval 

from sage.structure.sage_object import SageObject 

from sage.misc.superseded import deprecation 

from sage.misc.decorators import rename_keyword 

 

 

class HtmlFragment(str, SageObject): 

r""" 

A HTML fragment. 

 

This is a piece of HTML, usually not a complete document. For 

example, just a ``<div>...</div>`` piece and not the entire 

``<html>...</html>``. 

 

EXAMPLES:: 

 

sage: from sage.misc.html import HtmlFragment 

sage: HtmlFragment('<b>test</b>') 

<b>test</b> 

 

.. automethod:: _rich_repr_ 

""" 

 

def _rich_repr_(self, display_manager, **kwds): 

""" 

Rich Output Magic Method 

 

See :mod:`sage.repl.rich_output` for details. 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output import get_display_manager 

sage: dm = get_display_manager() 

sage: h = sage.misc.html.HtmlFragment('<b>old</b>') 

sage: h._rich_repr_(dm) # the doctest backend does not support html 

OutputPlainText container 

""" 

OutputHtml = display_manager.types.OutputHtml 

if OutputHtml in display_manager.supported_output(): 

return OutputHtml(self) 

else: 

return display_manager.types.OutputPlainText(self) 

 

 

def math_parse(s): 

r""" 

Replace TeX-``$`` with Mathjax equations. 

 

Turn the HTML-ish string s that can have \$\$ and \$'s in it into 

pure HTML. See below for a precise definition of what this means. 

 

INPUT: 

 

- ``s`` -- a string 

 

OUTPUT: 

 

A :class:`HtmlFragment` instance. 

 

Do the following: 

 

* Replace all ``\$ text \$``\'s by 

``<script type="math/tex"> text </script>`` 

* Replace all ``\$\$ text \$\$``\'s by 

``<script type="math/tex; mode=display"> text </script>`` 

* Replace all ``\ \$``\'s by ``\$``\'s. Note that in 

the above two cases nothing is done if the ``\$`` 

is preceeded by a backslash. 

* Replace all ``\[ text \]``\'s by 

``<script type="math/tex; mode=display"> text </script>`` 

 

EXAMPLES:: 

 

sage: pretty_print(sage.misc.html.math_parse('This is $2+2$.')) 

This is <script type="math/tex">2+2</script>. 

sage: pretty_print(sage.misc.html.math_parse('This is $$2+2$$.')) 

This is <script type="math/tex; mode=display">2+2</script>. 

sage: pretty_print(sage.misc.html.math_parse('This is \\[2+2\\].')) 

This is <script type="math/tex; mode=display">2+2</script>. 

sage: pretty_print(sage.misc.html.math_parse(r'This is \[2+2\].')) 

This is <script type="math/tex; mode=display">2+2</script>. 

 

TESTS:: 

 

sage: sage.misc.html.math_parse(r'This \$\$is $2+2$.') 

This $$is <script type="math/tex">2+2</script>. 

""" 

# first replace \\[ and \\] by <script type="math/tex; mode=display"> 

# and </script>, respectively. 

while True: 

i = s.find('\\[') 

if i == -1: 

break 

else: 

s = s[:i] + '<script type="math/tex; mode=display">' + s[i+2:] 

j = s.find('\\]') 

if j == -1: # missing right-hand delimiter, so add one 

s = s + '</script>' 

else: 

s = s[:j] + '</script>' + s[j+2:] 

 

# Below t always has the "parsed so far" version of s, and s is 

# just the part of the original input s that hasn't been parsed. 

t = '' 

while True: 

i = s.find('$') 

if i == -1: 

# No dollar signs -- definitely done. 

return HtmlFragment(t + s) 

elif i > 0 and s[i-1] == '\\': 

# A dollar sign with a backslash right before it, so 

# we ignore it by sticking it in the parsed string t 

# and skip to the next iteration. 

t += s[:i-1] + '$' 

s = s[i+1:] 

continue 

elif i+1 < len(s) and s[i+1] == '$': 

# Found a math environment. Double dollar sign so display mode. 

disp = '; mode=display' 

else: 

# Found math environment. Single dollar sign so default mode. 

disp = '' 

 

# Now find the matching $ sign and form the html string. 

 

if len(disp) > 0: 

j = s[i+2:].find('$$') 

if j == -1: 

j = len(s) 

s += '$$' 

else: 

j += i + 2 

txt = s[i+2:j] 

else: 

j = s[i+2:].find('$') 

if j == -1: 

j = len(s) 

s += '$' 

else: 

j += i + 2 

txt = s[i+1:j] 

 

t += s[:i] + '<script type="math/tex%s">%s</script>'%(disp, 

' '.join(txt.splitlines())) 

s = s[j+1:] 

if len(disp) > 0: 

s = s[1:] 

return HtmlFragment(t) 

 

 

class HTMLFragmentFactory(SageObject): 

 

def _repr_(self): 

""" 

Return string representation 

 

OUTPUT: 

 

String. 

 

EXAMPLES:: 

 

sage: html 

Create HTML output (see html? for details) 

""" 

return 'Create HTML output (see html? for details)' 

 

def __call__(self, obj): 

r""" 

Construct a HTML fragment 

 

INPUT: 

 

- ``obj`` -- anything. An object for which you want a HTML 

representation. 

 

OUTPUT: 

 

A :class:`HtmlFragment` instance. 

 

EXAMPLES:: 

 

sage: h = html('<hr>'); pretty_print(h) 

<hr> 

sage: type(h) 

<class 'sage.misc.html.HtmlFragment'> 

 

sage: pretty_print(html(1/2)) 

<script type="math/tex">\frac{1}{2}</script> 

 

sage: pretty_print(html('<a href="http://sagemath.org">sagemath</a>')) 

<a href="http://sagemath.org">sagemath</a> 

""" 

# Prefer dedicated _html_() method 

try: 

result = obj._html_() 

except AttributeError: 

pass 

else: 

if not isinstance(result, HtmlFragment): 

warnings.warn('_html_() did not return a HtmlFragment') 

return HtmlFragment(result) 

else: 

return result 

# Otherwise: convert latex to html 

try: 

result = obj._latex_() 

except AttributeError: 

pass 

else: 

return math_parse('${0}$'.format(obj._latex_())) 

# If all else fails 

return math_parse(str(obj)) 

 

def eval(self, s, locals=None): 

r""" 

Evaluate embedded <sage> tags 

 

INPUT: 

 

- ``s`` -- string. 

 

- ``globals`` -- dictionary. The global variables when 

evaluating ``s``. Default: the current global variables. 

 

OUTPUT: 

 

A :class:`HtmlFragment` instance. 

 

EXAMPLES:: 

 

sage: a = 123 

sage: html.eval('<sage>a</sage>') 

<script type="math/tex">123</script> 

sage: html.eval('<sage>a</sage>', locals={'a': 456}) 

<script type="math/tex">456</script> 

""" 

if locals is None: 

from sage.repl.user_globals import get_globals 

locals = get_globals() 

s = str(s) 

s = math_parse(s) 

t = '' 

while len(s) > 0: 

i = s.find('<sage>') 

if i == -1: 

t += s 

break 

j = s.find('</sage>') 

if j == -1: 

t += s 

break 

t += s[:i] + '<script type="math/tex">%s</script>'%\ 

latex(sage_eval(s[6+i:j], locals=locals)) 

s = s[j+7:] 

return HtmlFragment(t) 

 

def iframe(self, url, height=400, width=800): 

r""" 

Generate an iframe HTML fragment 

 

INPUT: 

 

- ``url`` -- string. A url, either with or without URI scheme 

(defaults to "http"), or an absolute file path. 

 

- ``height`` -- the number of pixels for the page height. 

Defaults to 400. 

 

- ``width`` -- the number of pixels for the page width. 

Defaults to 800. 

 

OUTPUT: 

 

A :class:`HtmlFragment` instance. 

 

EXAMPLES:: 

 

sage: pretty_print(html.iframe("sagemath.org")) 

<iframe height="400" width="800" 

src="http://sagemath.org"></iframe> 

sage: pretty_print(html.iframe("http://sagemath.org",30,40)) 

<iframe height="30" width="40" 

src="http://sagemath.org"></iframe> 

sage: pretty_print(html.iframe("https://sagemath.org",30)) 

<iframe height="30" width="800" 

src="https://sagemath.org"></iframe> 

sage: pretty_print(html.iframe("/home/admin/0/data/filename")) 

<iframe height="400" width="800" 

src="file:///home/admin/0/data/filename"></iframe> 

sage: pretty_print(html.iframe('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA' 

....: 'AUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBA' 

....: 'AO9TXL0Y4OHwAAAABJRU5ErkJggg=="')) 

<iframe height="400" width="800"  

src="http://data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==""></iframe> 

""" 

if url.startswith('/'): 

url = 'file://{0}'.format(url) 

elif '://' not in url: 

url = 'http://{0}'.format(url) 

return HtmlFragment('<iframe height="{0}" width="{1}" src="{2}"></iframe>' 

.format(height, width, url)) 

 

 

html = HTMLFragmentFactory() 

html.__doc__ = HTMLFragmentFactory.__call__.__doc__