/usr/lib/python2.7/dist-packages/sagenb/misc/results2rst.py is in python-sagenb 1.0.1+ds1-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 | # -*- coding: utf-8 -*-
r"""
Convert output from code cells in the notebook into ReStructuredText
This is called by sws2rst
- Pablo Angulo Ardoy (2011-02-25): initial version
"""
#**************************************************
# Copyright (C) 2011 Pablo Angulo
#
# Distributed under the terms of the GPL License
#**************************************************
import re
IMAGES_DIR = 'images/'
#We parse lines one by one but keep track of current scope
#similarly to worksheet2rst.py
#Results are split into different types. Some are discarded
class States(object):
NORMAL = 0
HTML = 1
MATH = 2
TRACEBACK = 3
class LineTypes(object):
PLAIN = 0
IMAGE = 1
LATEX = 2
HTML = 3
TRACE = 4
class ResultsParser(object):
"""Auxiliary class for results2rst
"""
def __init__(self, images_dir):
##Order matters, place more restrictive regex's before more general ones
##If no regex matches, line will be discarded
##a self transition is needes to produce any output
self.transitions = {
States.NORMAL:[
#IMAGE
(re.compile(r"^\<html\>\<font color='black'\>"
r"\<img src='cell\://(.*?)'\>"
r"\</font\>\</html\>"),
"\n.. image:: " + images_dir + "\\1\n :align: center\n",
LineTypes.IMAGE,
States.NORMAL),
#SELF-CONTAINED MATH
(re.compile(r"^\<html\>\<div class=\"math\"\>"
r"\\newcommand\{\\Bold\}\[1\]\{\\mathbf\{\#1\}\}"
r"(.*?)\</div\>\</html\>$"),
"\n.. MATH::\n\n \\1\n",
LineTypes.LATEX,
States.NORMAL),
#SELF-CONTAINED MATH - BIS
(re.compile(r"^\<html\>\<div class=\"math\"\>"
r"(.*?)\</div\>\</html\>$"),
"\n.. MATH::\n\n \\1",
LineTypes.LATEX,
States.NORMAL),
#START Traceback
(re.compile(r"^(Traceback.*)"),
" Traceback (most recent call last):",
LineTypes.TRACE,
States.TRACEBACK),
#START MATH
(re.compile(r"^\<html\>\<div class=\"math\"\>"
r"\\newcommand\{\\Bold\}\[1\]\{\\mathbf\{\#1\}\}(.*?)"),
"\n.. MATH::\n\n \\1",
LineTypes.LATEX,
States.MATH),
#SELF-CONTAINED HTML
(re.compile(r"^\<html\>.*</html\>$"),
" <html>...</html>",
LineTypes.HTML,
States.NORMAL),
#START HTML
(re.compile(r"^\<html\>.*"),
" <html>...</html>",
LineTypes.HTML,
States.HTML),
#CONTINUE NORMAL
(re.compile("(.*)"),
" \\1",
LineTypes.PLAIN,
States.NORMAL),
],
States.MATH:[
#END MATH
(re.compile(r"(.*?)\</div\>\</html\>$"),
" \\1",
LineTypes.LATEX,
States.NORMAL),
#CONTINUE MATH
(re.compile("(.*)"),
" \\1",
LineTypes.LATEX,
States.MATH),
],
States.TRACEBACK:[
#END Traceback
(re.compile(r"^(\S.*)"),
" ...\n \\1",
LineTypes.TRACE,
States.NORMAL),
],
States.HTML:[
#END HTML
(re.compile(r".*</html\>$"),
"",
LineTypes.HTML,
States.NORMAL),
],
}
def parse(self, text):
result_plain = []
result_show = []
state = States.NORMAL
for line in text.splitlines():
for regex, replacement, line_type, new_state in self.transitions[state]:
if regex.match(line):
result = result_plain if line_type in (LineTypes.PLAIN, LineTypes.HTML)\
else result_show
result.append( regex.sub(replacement, line))
state = new_state
break
result_plain.extend(result_show)
return '\n'.join(result_plain)
def results2rst(text, images_dir):
r"""Converts the result of evaluation of notebook cells
into rst compatible with Sage documentation.
Several common patterns are identified, and treated
accordingly. Some patterns are dropped, while others
are not recognized.
Currently, latex and images are recognized and converted.
INPUT:
- ``text`` -- string -- a chunk of HTML text
- ``images_dir`` -- string -- folder where images are stored
OUTPUT:
- string -- rst text
EXAMPLES::
sage: from sagenb.misc.results2rst import results2rst
sage: s="<html><font color='black'><img src='cell://sage0.png'></font></html>"
sage: results2rst(s,'')
'\n.. image:: sage0.png\n :align: center\n'
sage: results2rst("4",'')
' 4'
sage: s=r'<html><div class="math">\newcommand{\Bold}[1]{\mathbf{#1}}\frac{3}{2}</div></html>'
sage: results2rst(s,'')
'\n.. MATH::\n\n \\frac{3}{2}\n'
"""
Parser = ResultsParser(images_dir)
return Parser.parse(text)
|