Ticket #11459: trac_11459-scripts.reviewer.patch

File trac_11459-scripts.reviewer.patch, 11.1 KB (added by kini, 10 years ago)

apply to $SAGE_LOCAL/bin

  • sage-rst2sws

    # HG changeset patch
    # User Keshav Kini <keshav.kini@gmail.com>
    # Date 1337989454 25200
    # Node ID c7ffd8827fd19048040d57367adaca1d1662ab91
    # Parent  de6fa5c1224bb7828b402924aa5ff1389311fd95
    Language, formatting
    
    diff --git a/sage-rst2sws b/sage-rst2sws
    a b  
    11#!/usr/bin/env python
    22r"""
    33sage-rst2sws
     4============
    45
    56Translate a rst file into a Sage worksheet file (.sws).
    67
    78Usage::
    89
    9     sage -rst2sws [options] <source> <destination>
     10    sage --rst2sws [options] <source> <destination>
    1011
    1112Print the help message::
    1213
    13     sage -rst2sws -h
     14    sage --rst2sws -h
    1415
    1516EXAMPLES::
    1617
    17     sage -rst2sws file.rst file.sws
     18    sage --rst2sws file.rst file.sws
    1819
    19 In RestructuredText, "an escaped character represents the character itself,
    20 and is prevented from playing a role in any markup interpretation. The
    21 backslash is removed from the output" [1,2]. So, if the backslashes of your
    22 ReST file are not escaped, they will get lost in the process. This is not
    23 practical for Sage since most of the documentation and examples are Python
    24 raw string where backslashes are not escaped. Use the following command to
    25 escape every backslashes of the input file::
     20In reStructuredText, "an escaped character represents the character
     21itself, and is prevented from playing a role in any markup
     22interpretation. The backslash is removed from the output" [1]_ [2]_. So,
     23if the backslashes of your reST file are not escaped, they will get lost
     24in the process. This is not practical for Sage since most of the
     25documentation and examples are Python raw strings where backslashes are
     26not escaped. Use the following command to escape all backslashes in the
     27input file::
    2628
    27     sage -rst2sws --escape-backslashes input.rst output.sws
     29    sage --rst2sws --escape-backslashes input.rst output.sws
    2830
    2931AUTHOR:
    3032
    31     - Sebastien Labbe (2011, June 14th, at Sage Days 31): Initial version
     33    - Sebastien Labbe (2011, June 14th, at Sage Days 31): Initial
     34      version
    3235
    3336REFERENCES:
    3437
    35     - [1] Escaping Mechanism, reStructuredText Markup Specification,
    36       http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html#escaping-mechanism
    37     - [2] Espcaping with backslashes, Quick reStructuredText Reference,
    38       http://docutils.sourceforge.net/docs/user/rst/quickref.html#escaping
    39 
     38.. [1] Escaping Mechanism, reStructuredText Markup Specification,
     39   http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html#escaping-mechanism
     40.. [2] Espcaping with backslashes, Quick reStructuredText Reference,
     41   http://docutils.sourceforge.net/docs/user/rst/quickref.html#escaping
    4042"""
    4143#############################################################################
    4244#       Copyright (C) 2011 Sebastien Labbe <slabqc at gmail.com>
     
    6264
    6365    About LaTeX:
    6466
    65         You can put LaTeX expression between backticks "`", dollars sign
     67        You can put LaTeX expression between backticks "`", dollar signs
    6668        "$" or double dollar signs "$$". Math role is not supported yet
    6769        (get involved!).
    6870
     
    7072
    7173    About backslashes:
    7274
    73         In ReStructuredText, backslashes must be escaped or otherwise they
    74         are ignored. This is not quite practical for Sage purposes since
    75         backslashes are never escaped in the docstrings. If you write
    76         `\alpha`, $\beta$ or $$\gamma$$, add the line
     75        In reStructuredText, backslashes must be escaped, otherwise they
     76        are ignored. This is not quite practical for Sage's purposes
     77        since backslashes are never escaped in the docstrings. If you
     78        write `\alpha`, $\beta$ or $$\gamma$$, add the line
    7779
    78             "..  escape-backslashes"
     80            ..  escape-backslashes
    7981
    80         to the file and this script will consider every backslashes as
     82        to the file and this script will consider all backslashes
    8183        escaped. Alternatively, you may use the available options."""
    8284parser = OptionParser(usage=usage)
    8385parser.add_option("--escape-backslashes",
    8486                  action="store_true", dest="escape_backslashes",
    85                   help="Escape every backslashes in the input file before execution. Default: disabled.")
     87                  help="Escape all backslashes in the input file before execution. Default: disabled.")
    8688parser.add_option("--no-escape-backslashes",
    8789                  action="store_true", dest="no_escape_backslashes",
    88                   help="Disable escaping every backslashes in the input file before execution. This overwrites the presence of the line '.. escape-backslashes' in the file which also enable escaping every backslashes in the file.")
     90                  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.")
    8991(options, args) = parser.parse_args()
    9092
    9193# Parse arguments
     
    104106    print "Unable to open source file for reading ('%s').  Exiting." % input
    105107    sys.exit(1)
    106108
    107 # docutils loses the alone backslashes, so we precede them with escapes
     109# docutils loses the single backslashes, so we precede them with escapes
    108110# A better solution would be to escape baskslashes only in the math sections
    109111import re
    110112if options.escape_backslashes:
     
    125127translator = docutilsHTMLProcessor()
    126128worksheet_txt = translator.process_doc_html(html)
    127129
    128 # create a Notebook
     130# create a Notebook object
    129131from sagenb.notebook.notebook import Notebook
    130132from sage.misc.misc import tmp_dir
    131133nb = Notebook(tmp_dir()+'.sagenb')
     
    137139# export the worksheet as sws
    138140nb.export_worksheet(W.filename(), output, title=title)
    139141
    140 # delete the Notebook
     142# delete the Notebook object
    141143nb.delete()
    142 
  • sage-rst2txt

    diff --git a/sage-rst2txt b/sage-rst2txt
    a b  
    11#!/usr/bin/env python
    22r"""
    33sage-rst2txt
     4============
    45
    56Translate a rst file into a worksheet txt file.
    67
    7 A worksheet txt file is a plain text file (no graphics) with html code and
    8 sage cells surrounded by {{{'s.
     8A worksheet txt file is a plain text file (no graphics) with html code
     9and sage cells surrounded by {{{'s.
    910
    1011Usage::
    1112
     
    2324
    2425    sage -rst2txt input.rst | less
    2526
    26 In RestructuredText, "an escaped character represents the character itself,
    27 and is prevented from playing a role in any markup interpretation. The
    28 backslash is removed from the output" [1,2]. So, if the backslashes of your
    29 ReST file are not escaped, they will get lost in the process. This is not
    30 practical for Sage since most of the documentation and examples are Python
    31 raw string where backslashes are not escaped. Use the following command to
    32 escape every backslashes of the input file::
     27In reStructuredText, "an escaped character represents the character
     28itself, and is prevented from playing a role in any markup
     29interpretation. The backslash is removed from the output" [1]_ [2]_. So,
     30if the backslashes of your reST file are not escaped, they will get lost
     31in the process. This is not practical for Sage since most of the
     32documentation and examples are Python raw string where backslashes are
     33not escaped. Use the following command to escape all backslashes in the
     34input file::
    3335
    3436    sage -rst2txt --escape-backslashes input.rst output.txt
    3537
    3638AUTHOR:
    3739
    3840    - Sebastien Labbe (2011, January 15th): Initial version
    39     - Sebastien Labbe (2011, June 14th, at Sage Days 31): Make it ready for
    40       inclusion into Sage.
     41    - Sebastien Labbe (2011, June 14th, at Sage Days 31): Make it ready
     42      for inclusion into Sage.
    4143
    4244REFERENCES:
    4345
    44     - [1] Escaping Mechanism, reStructuredText Markup Specification,
    45       http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html#escaping-mechanism
    46     - [2] Espcaping with backslashes, Quick reStructuredText Reference,
    47       http://docutils.sourceforge.net/docs/user/rst/quickref.html#escaping
     46.. [1] Escaping Mechanism, reStructuredText Markup Specification,
     47   http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html#escaping-mechanism
     48.. [2] Espcaping with backslashes, Quick reStructuredText Reference,
     49   http://docutils.sourceforge.net/docs/user/rst/quickref.html#escaping
    4850"""
    4951#############################################################################
    5052#       Copyright (C) 2011 Sebastien Labbe <slabqc at gmail.com>
     
    6062
    6163    sage -rst2txt [options] <source> [<destination>]
    6264
    63 Generates Sage worksheet text file from standalone reStructuredText source.
     65Generates Sage worksheet text file from standalone reStructuredText
     66source.
    6467
    6568Example:
    6669
     
    7073
    7174    About LaTeX:
    7275
    73         You can put LaTeX expression between backticks "`", dollars sign
     76        You can put LaTeX expression between backticks "`", dollar signs
    7477        "$" or double dollar signs "$$". Math role is not supported yet
    7578        (get involved!).
    7679
     
    7881
    7982    About backslashes:
    8083
    81         In ReStructuredText, backslashes must be escaped or otherwise they
    82         are ignored. This is not quite practical for Sage purposes since
    83         backslashes are never escaped in the docstrings. If you write
    84         `\alpha`, $\beta$ or $$\gamma$$, add the line
     84        In reStructuredText, backslashes must be escaped, otherwise they
     85        are ignored. This is not quite practical for Sage's purposes
     86        since backslashes are never escaped in the docstrings. If you
     87        write `\alpha`, $\beta$ or $$\gamma$$, add the line
    8588
    86             "..  escape-backslashes"
     89            ..  escape-backslashes
    8790
    88         to the file and this script will consider every backslashes as
     91        to the file and this script will consider all backslashes
    8992        escaped. Alternatively, you may use the available options."""
    90 
    9193parser = OptionParser(usage=usage)
    9294parser.add_option("--escape-backslashes",
    9395                  action="store_true", dest="escape_backslashes",
    94                   help="Escape every backslashes in the input file before execution. Default: disabled.")
     96                  help="Escape all backslashes in the input file before execution. Default: disabled.")
    9597parser.add_option("--no-escape-backslashes",
    9698                  action="store_true", dest="no_escape_backslashes",
    97                   help="Disable escaping every backslashes in the input file before execution. This overwrites the presence of the line '.. escape-backslashes' in the file which also enable escaping every backslashes in the file.")
    98 
     99                  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.")
    99100(options, args) = parser.parse_args()
    100101
    101102# Parse arguments
     
    114115    print "Unable to open source file for reading ('%s').  Exiting." % input
    115116    sys.exit(1)
    116117
    117 # docutils loses the alone backslashes, so we precede them with escapes
     118# docutils loses the single backslashes, so we prepend them with escapes
    118119# A better solution would be to escape baskslashes only in the math sections
    119120import re
    120121if options.escape_backslashes:
     
    139140        f.write(worksheet_txt)
    140141else:
    141142    print worksheet_txt
    142