/usr/share/sagemath/bin/sage-rst2sws is in sagemath-common 7.4-9.
This file is owned by root:root, with mode 0o755.
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 | #!/usr/bin/env python
r"""
sage-rst2sws
============
Translate a rst file into a Sage worksheet file (.sws).
Usage::
sage --rst2sws [options] <source> <destination>
Print the help message::
sage --rst2sws -h
EXAMPLES::
sage --rst2sws file.rst file.sws
In reStructuredText, "an escaped character represents the character
itself, and is prevented from playing a role in any markup
interpretation. The backslash is removed from the output" [1]_ [2]_. So,
if the backslashes of your reST file are not escaped, they will get lost
in the process. This is not practical for Sage since most of the
documentation and examples are Python raw strings where backslashes are
not escaped. Use the following command to escape all backslashes in the
input file::
sage --rst2sws --escape-backslashes input.rst output.sws
AUTHOR:
- Sebastien Labbe (2011, June 14th, at Sage Days 31): Initial
version
REFERENCES:
.. [1] Escaping Mechanism, reStructuredText Markup Specification,
http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html#escaping-mechanism
.. [2] Espcaping with backslashes, Quick reStructuredText Reference,
http://docutils.sourceforge.net/docs/user/rst/quickref.html#escaping
"""
#############################################################################
# Copyright (C) 2011 Sebastien Labbe <slabqc at gmail.com>
# Distributed under the terms of the GNU General Public License (GPL)
# The full text of the GPL is available at:
# http://www.gnu.org/licenses/
#############################################################################
import os, sys
from optparse import OptionParser
# Set the parser
usage = r"""
sage -rst2sws [options] <source> <destination>
Generates Sage worksheet (.sws) from standalone reStructuredText source.
Example:
sage -rst2sws file.rst file.sws
Remarks:
About LaTeX:
You can put LaTeX expression between backticks "`", dollar signs
"$" or double dollar signs "$$". Math role is not supported yet
(get involved!).
Examples: `\\alpha`, $\\beta$ or $$\\gamma$$.
About backslashes:
In reStructuredText, backslashes must be escaped, otherwise they
are ignored. This is not quite practical for Sage's purposes
since backslashes are never escaped in the docstrings. If you
write `\alpha`, $\beta$ or $$\gamma$$, add the line
.. escape-backslashes
to the file and this script will consider all backslashes
escaped. Alternatively, you may use the available options."""
parser = OptionParser(usage=usage)
parser.add_option("--escape-backslashes",
action="store_true", dest="escape_backslashes",
help="Escape all backslashes in the input file before execution. Default: disabled.")
parser.add_option("--no-escape-backslashes",
action="store_true", dest="no_escape_backslashes",
help="Disable escaping all backslashes in the input file before execution. This overrides the presence of the line '.. escape-backslashes' in the file, which can also be used to escape all backslashes in the file.")
(options, args) = parser.parse_args()
# Parse arguments
if len(args) != 2:
parser.print_usage()
sys.exit(1)
input = args[0]
output = args[1]
# Read the ReST input
try:
with open(input, 'r') as f:
rst = f.read()
except IOError as e:
print("IOError: {}".format(e))
print("Unable to open source file for reading ('{}'). Exiting.".format(input))
sys.exit(1)
# docutils loses the single backslashes, so we precede them with escapes
# A better solution would be to escape baskslashes only in the math sections
import re
if options.escape_backslashes:
rst = rst.replace('\\','\\\\')
elif options.no_escape_backslashes:
pass
elif re.search(r'^\.\.[ \t]+escape-backslashes', rst, re.MULTILINE) is not None:
rst = rst.replace('\\','\\\\')
# Do the translation rst -> html (using docutils)
from docutils.core import publish_parts
D = publish_parts(rst, writer_name='html')
title = D['title']
html = D['whole']
# Do the translation html -> worksheet txt
from sagenb.notebook.docHTMLProcessor import docutilsHTMLProcessor
translator = docutilsHTMLProcessor()
worksheet_txt = translator.process_doc_html(html)
# create a Notebook object
from sagenb.notebook.notebook import Notebook
from sage.misc.all import tmp_dir
nb = Notebook(tmp_dir()+'.sagenb')
nb.user_manager().create_default_users('password')
# create a worksheet
W = nb.create_new_worksheet(title, 'admin')
W.edit_save(worksheet_txt)
# export the worksheet as sws
nb.export_worksheet(W.filename(), output, title=title)
# delete the Notebook object
nb.delete()
|