# 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
|
|
1 | 1 | #!/usr/bin/env python |
2 | 2 | r""" |
3 | 3 | sage-rst2sws |
| 4 | ============ |
4 | 5 | |
5 | 6 | Translate a rst file into a Sage worksheet file (.sws). |
6 | 7 | |
7 | 8 | Usage:: |
8 | 9 | |
9 | | sage -rst2sws [options] <source> <destination> |
| 10 | sage --rst2sws [options] <source> <destination> |
10 | 11 | |
11 | 12 | Print the help message:: |
12 | 13 | |
13 | | sage -rst2sws -h |
| 14 | sage --rst2sws -h |
14 | 15 | |
15 | 16 | EXAMPLES:: |
16 | 17 | |
17 | | sage -rst2sws file.rst file.sws |
| 18 | sage --rst2sws file.rst file.sws |
18 | 19 | |
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:: |
| 20 | In reStructuredText, "an escaped character represents the character |
| 21 | itself, and is prevented from playing a role in any markup |
| 22 | interpretation. The backslash is removed from the output" [1]_ [2]_. So, |
| 23 | if the backslashes of your reST file are not escaped, they will get lost |
| 24 | in the process. This is not practical for Sage since most of the |
| 25 | documentation and examples are Python raw strings where backslashes are |
| 26 | not escaped. Use the following command to escape all backslashes in the |
| 27 | input file:: |
26 | 28 | |
27 | | sage -rst2sws --escape-backslashes input.rst output.sws |
| 29 | sage --rst2sws --escape-backslashes input.rst output.sws |
28 | 30 | |
29 | 31 | AUTHOR: |
30 | 32 | |
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 |
32 | 35 | |
33 | 36 | REFERENCES: |
34 | 37 | |
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 |
40 | 42 | """ |
41 | 43 | ############################################################################# |
42 | 44 | # Copyright (C) 2011 Sebastien Labbe <slabqc at gmail.com> |
… |
… |
|
62 | 64 | |
63 | 65 | About LaTeX: |
64 | 66 | |
65 | | You can put LaTeX expression between backticks "`", dollars sign |
| 67 | You can put LaTeX expression between backticks "`", dollar signs |
66 | 68 | "$" or double dollar signs "$$". Math role is not supported yet |
67 | 69 | (get involved!). |
68 | 70 | |
… |
… |
|
70 | 72 | |
71 | 73 | About backslashes: |
72 | 74 | |
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 |
77 | 79 | |
78 | | ".. escape-backslashes" |
| 80 | .. escape-backslashes |
79 | 81 | |
80 | | to the file and this script will consider every backslashes as |
| 82 | to the file and this script will consider all backslashes |
81 | 83 | escaped. Alternatively, you may use the available options.""" |
82 | 84 | parser = OptionParser(usage=usage) |
83 | 85 | parser.add_option("--escape-backslashes", |
84 | 86 | 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.") |
86 | 88 | parser.add_option("--no-escape-backslashes", |
87 | 89 | 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.") |
89 | 91 | (options, args) = parser.parse_args() |
90 | 92 | |
91 | 93 | # Parse arguments |
… |
… |
|
104 | 106 | print "Unable to open source file for reading ('%s'). Exiting." % input |
105 | 107 | sys.exit(1) |
106 | 108 | |
107 | | # docutils loses the alone backslashes, so we precede them with escapes |
| 109 | # docutils loses the single backslashes, so we precede them with escapes |
108 | 110 | # A better solution would be to escape baskslashes only in the math sections |
109 | 111 | import re |
110 | 112 | if options.escape_backslashes: |
… |
… |
|
125 | 127 | translator = docutilsHTMLProcessor() |
126 | 128 | worksheet_txt = translator.process_doc_html(html) |
127 | 129 | |
128 | | # create a Notebook |
| 130 | # create a Notebook object |
129 | 131 | from sagenb.notebook.notebook import Notebook |
130 | 132 | from sage.misc.misc import tmp_dir |
131 | 133 | nb = Notebook(tmp_dir()+'.sagenb') |
… |
… |
|
137 | 139 | # export the worksheet as sws |
138 | 140 | nb.export_worksheet(W.filename(), output, title=title) |
139 | 141 | |
140 | | # delete the Notebook |
| 142 | # delete the Notebook object |
141 | 143 | nb.delete() |
142 | | |
diff --git a/sage-rst2txt b/sage-rst2txt
a
|
b
|
|
1 | 1 | #!/usr/bin/env python |
2 | 2 | r""" |
3 | 3 | sage-rst2txt |
| 4 | ============ |
4 | 5 | |
5 | 6 | Translate a rst file into a worksheet txt file. |
6 | 7 | |
7 | | A worksheet txt file is a plain text file (no graphics) with html code and |
8 | | sage cells surrounded by {{{'s. |
| 8 | A worksheet txt file is a plain text file (no graphics) with html code |
| 9 | and sage cells surrounded by {{{'s. |
9 | 10 | |
10 | 11 | Usage:: |
11 | 12 | |
… |
… |
|
23 | 24 | |
24 | 25 | sage -rst2txt input.rst | less |
25 | 26 | |
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:: |
| 27 | In reStructuredText, "an escaped character represents the character |
| 28 | itself, and is prevented from playing a role in any markup |
| 29 | interpretation. The backslash is removed from the output" [1]_ [2]_. So, |
| 30 | if the backslashes of your reST file are not escaped, they will get lost |
| 31 | in the process. This is not practical for Sage since most of the |
| 32 | documentation and examples are Python raw string where backslashes are |
| 33 | not escaped. Use the following command to escape all backslashes in the |
| 34 | input file:: |
33 | 35 | |
34 | 36 | sage -rst2txt --escape-backslashes input.rst output.txt |
35 | 37 | |
36 | 38 | AUTHOR: |
37 | 39 | |
38 | 40 | - 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. |
41 | 43 | |
42 | 44 | REFERENCES: |
43 | 45 | |
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 |
48 | 50 | """ |
49 | 51 | ############################################################################# |
50 | 52 | # Copyright (C) 2011 Sebastien Labbe <slabqc at gmail.com> |
… |
… |
|
60 | 62 | |
61 | 63 | sage -rst2txt [options] <source> [<destination>] |
62 | 64 | |
63 | | Generates Sage worksheet text file from standalone reStructuredText source. |
| 65 | Generates Sage worksheet text file from standalone reStructuredText |
| 66 | source. |
64 | 67 | |
65 | 68 | Example: |
66 | 69 | |
… |
… |
|
70 | 73 | |
71 | 74 | About LaTeX: |
72 | 75 | |
73 | | You can put LaTeX expression between backticks "`", dollars sign |
| 76 | You can put LaTeX expression between backticks "`", dollar signs |
74 | 77 | "$" or double dollar signs "$$". Math role is not supported yet |
75 | 78 | (get involved!). |
76 | 79 | |
… |
… |
|
78 | 81 | |
79 | 82 | About backslashes: |
80 | 83 | |
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 |
85 | 88 | |
86 | | ".. escape-backslashes" |
| 89 | .. escape-backslashes |
87 | 90 | |
88 | | to the file and this script will consider every backslashes as |
| 91 | to the file and this script will consider all backslashes |
89 | 92 | escaped. Alternatively, you may use the available options.""" |
90 | | |
91 | 93 | parser = OptionParser(usage=usage) |
92 | 94 | parser.add_option("--escape-backslashes", |
93 | 95 | 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.") |
95 | 97 | parser.add_option("--no-escape-backslashes", |
96 | 98 | 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.") |
99 | 100 | (options, args) = parser.parse_args() |
100 | 101 | |
101 | 102 | # Parse arguments |
… |
… |
|
114 | 115 | print "Unable to open source file for reading ('%s'). Exiting." % input |
115 | 116 | sys.exit(1) |
116 | 117 | |
117 | | # docutils loses the alone backslashes, so we precede them with escapes |
| 118 | # docutils loses the single backslashes, so we prepend them with escapes |
118 | 119 | # A better solution would be to escape baskslashes only in the math sections |
119 | 120 | import re |
120 | 121 | if options.escape_backslashes: |
… |
… |
|
139 | 140 | f.write(worksheet_txt) |
140 | 141 | else: |
141 | 142 | print worksheet_txt |
142 | | |