/usr/lib/python2.7/dist-packages/sagenb/misc/worksheet2rst.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 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 | #!/usr/bin/python
# -*- coding: utf-8 -*-
r"""
Convert worksheet.html files into ReStructuredText documents
This is called by 'sage -sws2rst'. Can also be used as a commandline script
(if BeautifulSoup is installed):
``python worksheet2rst.py worksheet.html``
or
``cat worksheet.html | python worksheet2rst.py``
AUTHOR:
- Pablo Angulo Ardoy (2011-02-25): initial version
The content of worksheet.html is split into comments, code, and output
(the result of evaluating the code), as follows:
comments
{{{id=..|
code
///
results
}}}
Each kind of text is dealt with separately.
"""
from __future__ import absolute_import
#**************************************************
# Copyright (C) 2011 Pablo Angulo
#
# Distributed under the terms of the GPL License
#**************************************************
import sys
import os
import re
from .comments2rst import html2rst
from .results2rst import results2rst
import codecs
#We parse lines one by one but keep track of current scope
#comments
#{{{id=..|
#code
#///
#results
#}}}
#RESULT_TO_BE_DROPPED corresponds to a results section whose
#code was empty, and will be discarded, whether it's empty or not
class States(object):
COMMENT = 0
CODE = 1
RESULT = 2
RESULT_TO_BE_DROPPED = 3
# REs for splitting comments, code and results
START_CELL_RE = re.compile('^\{\{\{id=(\d*)\|')
END_CODE_RE = re.compile('^\/\/\/')
END_CELL_RE = re.compile('^\}\}\}')
#When to switch State, and which State to
transitions = {
States.COMMENT:(
START_CELL_RE,
States.CODE
),
States.CODE:(
END_CODE_RE,
States.RESULT),
States.RESULT:(
END_CELL_RE,
States.COMMENT),
States.RESULT_TO_BE_DROPPED:(
END_CELL_RE,
States.COMMENT)
}
def code_parser(text):
"""
Arguments:
INPUT:
- ``s``:sage code, may or may not start with "sage:"
OUTPUT:
- string -- rst text
EXAMPLES (not used for unit test, see
http://groups.google.com/group/sage-devel/browse_thread/thread/d82cb049ac102f3a)
: from sagenb.misc.worksheet2rst import code_parser
: s="a=2"
: code_parser(s)
'::\n\n sage: a=2'
: s="def f(n):\n return n+1\n"
: code_parser(s)
'::\n\n sage: def f(n):\n ....: return n+1'
: s="sage: def f(n):\nsage: return n+1\n"
: code_parser(s)
'::\n\n sage: def f(n):\n ....: return n+1'
"""
lines = ['::', '']
for s in text.splitlines():
l = s[6:] if s.startswith('sage: ') else s
if not l: continue
prefix = ' ....: ' if l[0] == ' ' else ' sage: '
lines.append(prefix + l)
return '\n'.join(lines)
HEADER_RE = re.compile(r'<h\d>')
def add_title_if_there_is_none(text):
if not HEADER_RE.search(text):
return '<h1>Please write a title for this worksheet!</h1>\n' + text
else:
return text
def worksheet2rst(s, images_dir=''):
"""Parses a string, tipically the content of the file
worksheet.html inside a sws file, and converts it into
rst compatible with Sage documentation.
INPUT:
- ``s`` -- string -- text, tipically the content of
worksheet.html
- ``images_dir`` -- string -- folder where images are stored
OUTPUT:
- string -- rst text
EXAMPLES (not used for unit test, see
http://groups.google.com/group/sage-devel/browse_thread/thread/d82cb049ac102f3a)
: from sagenb.misc.worksheet2rst import worksheet2rst
: worksheet2rst('<p>some text</p>\n{{{id=1|\nprint 2+2\n///\n4\n}}}')
u'.. -*- coding: utf-8 -*-\n\nPlease write a title for this worksheet!\n========================================\n\nsome text\n\n\n::\n\n sage: print 2+2\n 4\n\n.. end of output\n'
: s = '{{{id=2|\nshow(f)\n///\n<html><div class="math">\\sqrt{x}</div></html>\n}}}\n'
: worksheet2rst(s)
u'.. -*- coding: utf-8 -*-\n\nPlease write a title for this worksheet!\n========================================\n::\n\n sage: show(f)\n\n.. MATH::\n\n \\sqrt{x}\n\n.. end of output\n'
"""
s = add_title_if_there_is_none(s)
state = States.COMMENT
result = ['.. -*- coding: utf-8 -*-\n']
ls = []
last = 0
for line in s.splitlines():
regex, next_state= transitions[state]
m = regex.match(line)
if m:
if state == States.COMMENT:
last_cell_id = m.group(1)
img_path = images_dir + os.path.sep
result.append(html2rst(u'\n'.join(ls), img_path))
elif state == States.RESULT:
img_path = os.path.join(images_dir, 'cell_%s_'%last_cell_id)
result.append(results2rst(u'\n'.join(ls),
img_path))
result.append('')
result.append('.. end of output')
elif state == States.CODE:
if ls and any(ls):
result.append(code_parser(u'\n'.join(ls)))
else:
next_state = States.RESULT_TO_BE_DROPPED
ls = []
state = next_state
else:
ls.append(line)
if state == States.COMMENT:
img_path = images_dir + os.path.sep
result.append(html2rst(u'\n'.join(ls), img_path))
elif state == States.RESULT:
img_path = os.path.join(images_dir, 'cell_%s_'%last_cell_id)
result.append(result_parser(u'\n'.join(ls),
img_path))
result.append('')
result.append('.. end of output')
elif state == States.CODE:
result.append(code_parser(u'\n'.join(ls)))
return u'\n'.join(result)
if __name__=='__main__':
if len(sys.argv)>1:
fichero = codecs.open(sys.argv[1], mode='r', encoding='utf-8')
text = fichero.read()
fichero.close()
else:
text = sys.stdin.read()
images_dir = sys.argv[2] if len(sys.argv)>2 else ''
print(worksheet2rst(text, images_dir).encode('utf-8'))
|