#!/usr/bin/env sage-python23
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()
