/usr/share/sagemath/bin/sage-rst2txt is in sagemath-common 8.1-7ubuntu1.
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 | #!/usr/bin/env python
r"""
sage-rst2txt
============
Translate a rst file into a worksheet txt file.
A worksheet txt file is a plain text file (no graphics) with html code
and sage cells surrounded by {{{'s.
Usage::
sage -rst2txt [options] <source> [<destination>]
Print the help message::
sage -rst2txt -h
EXAMPLES::
sage -rst2txt input.rst output.txt
::
sage -rst2txt input.rst | less
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 string where backslashes are
not escaped. Use the following command to escape all backslashes in the
input file::
sage -rst2txt --escape-backslashes input.rst output.txt
AUTHOR:
- Sebastien Labbe (2011, January 15th): Initial version
- Sebastien Labbe (2011, June 14th, at Sage Days 31): Make it ready
for inclusion into Sage.
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 -rst2txt [options] <source> [<destination>]
Generates Sage worksheet text file from standalone reStructuredText
source.
Example:
sage -rst2txt file.rst file.txt
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 not 1 <= len(args) <= 2:
parser.print_usage()
sys.exit(1)
input = args[0]
output = args[1] if len(args) == 2 else ''
# 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 prepend 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_string
html = publish_string(rst, writer_name='html')
# Do the translation html -> worksheet txt
from sagenb.notebook.docHTMLProcessor import docutilsHTMLProcessor
translator = docutilsHTMLProcessor()
worksheet_txt = translator.process_doc_html(html)
# Write the output
if output:
with open(output, "w") as f:
f.write(worksheet_txt)
else:
print(worksheet_txt)
|