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