| 7 | Translate a Sage worksheet file (.sws) into an rst file. |
| 8 | |
| 9 | Usage:: |
| 10 | |
| 11 | sage --sws2rst [-h] <source sws file> |
| 12 | |
| 13 | Print the help message:: |
| 14 | |
| 15 | sage --sws2rst -h |
| 16 | |
| 17 | EXAMPLES:: |
| 18 | |
| 19 | sage --sws2rst file.sws |
| 20 | |
| 21 | The sws file should be in the current directory; using an |
| 22 | absolute path will result in an error. |
| 23 | |
| 24 | AUTHOR: |
| 25 | |
| 26 | - Pablo Angulo (January 2011): Initial version |
| 27 | - Karl-Dieter Crisman (June 2012): Documentation |
| 28 | and minor refinements |
| 29 | """ |
| 30 | ############################################################################# |
| 31 | # Copyright (C) 2011 Pablo Angulo |
| 32 | # Distributed under the terms of the GNU General Public License (GPL) |
| 33 | # The full text of the GPL is available at: |
| 34 | # http://www.gnu.org/licenses/ |
| 35 | ############################################################################# |
38 | | cells_path = os.path.join(tempname,'sage_worksheet','cells') |
39 | | for cell in os.listdir(cells_path): |
40 | | cell_path = os.path.join(cells_path, cell) |
41 | | for image in os.listdir(cell_path): |
42 | | shutil.copy2(os.path.join(cell_path, image), |
43 | | os.path.join(images_dir, 'cell_%s_%s'%(cell,image))) |
44 | | |
| 74 | cells_path = os.path.join(tempname,'sage_worksheet','cells') |
| 75 | if os.path.exists(cells_path): |
| 76 | for cell in os.listdir(cells_path): |
| 77 | cell_path = os.path.join(cells_path, cell) |
| 78 | for image in os.listdir(cell_path): |
| 79 | if os.path.isfile(os.path.join(cell_path, image)): |
| 80 | shutil.copy2(os.path.join(cell_path, image), |
| 81 | os.path.join(images_dir, 'cell_%s_%s'%(cell,image))) |
| 82 | # could be Jmol image directory - code for future |
| 83 | #elif os.path.isdir(os.path.join(cell_path, image)): |
| 84 | # if image == '.jmol_images': |
| 85 | # for jmolimg in os.listdir(os.path.join(cell_path, image)): |
| 86 | # shutil.copy2(os.path.join(cell_path, image, jmolimg), |
| 87 | # os.path.join(images_dir, 'cell_%s_%s'%(cell,jmolimg))) |
| 88 | |
59 | | |
60 | | if __name__=='__main__': |
61 | | if len(sys.argv)<=1: |
62 | | print 'First argument should be a sws file' |
63 | | sys.exit() |
64 | | for file_name in sys.argv[1:]: |
65 | | print file_name |
| 105 | |
| 106 | |
| 107 | # Set the parser |
| 108 | usage = r""" |
| 109 | |
| 110 | sage -sws2rst [options] <source sws file> ... |
| 111 | |
| 112 | Translate a Sage worksheet file (.sws) into an reStructuredText |
| 113 | (.rst) file. At least one sws file argument is required; all sws |
| 114 | files will be parsed and translated. Spaces in the names of the |
| 115 | worksheet will be converted to underscores. |
| 116 | |
| 117 | Examples: |
| 118 | |
| 119 | sage --sws2rst file.sws |
| 120 | sage --sws2rst file1.sws file2.sws file3.sws |
| 121 | sage --sws2rst -h # this help message prints |
| 122 | sage --sws2rst --sphinxify # information about how to use |
| 123 | # Sphinx to compile your rst file |
| 124 | |
| 125 | Remark: |
| 126 | |
| 127 | The sws file(s) should be in the current directory; using an |
| 128 | absolute path will result in an error.""" |
| 129 | |
| 130 | sphinxify_text = r""" |
| 131 | |
| 132 | Once you have made your rst file, what can you do with it? |
| 133 | |
| 134 | If this is a file which is likely to become part of the Sage |
| 135 | standard documentation, you will want to edit the appropriate |
| 136 | file in $SAGE_ROOT/devel/sage/doc to include your file, or |
| 137 | simply include your file as appropriate. |
| 138 | |
| 139 | However, you may simply want to make great-looking documentation |
| 140 | for some other purpose out of your worksheet. The following |
| 141 | steps are one way to do so. |
| 142 | |
| 143 | - Assume that the generated .rst file is ``My_Project.rst``. |
| 144 | - Make a folder somewhere convenient to compile in, say, ``MyProject``. |
| 145 | - Then move your .rst file into that folder, and cd into it. |
| 146 | - Now the key is to use Sage's shell to run Sphinx on it! Run ``sage --sh``. |
| 147 | - Then type ``sphinx-quickstart`` and follow the instructions in the |
| 148 | Sphinx tutorial [1]_. You will probably want to choose to render math |
| 149 | with MathJax [2]_, but you can accept the defaults for the other options. |
| 150 | - Finally, edit ``index.rst`` by adding ``My_Project`` in the table of |
| 151 | contents, as detailed in the Sphinx tutorial [3]_. |
| 152 | - If you now type ``make html`` you should get a beautiful-looking web page |
| 153 | in ``_build/html``. If you did not have a header at the top of your worksheet, |
| 154 | you may get an error, but you can ignore this. |
| 155 | |
| 156 | REFERENCES: |
| 157 | |
| 158 | .. [1] First Steps with Sphinx, |
| 159 | http://sphinx.pocoo.org/tutorial.html |
| 160 | .. [2] MathJax, |
| 161 | http://www.mathjax.org/ |
| 162 | .. [3] Defining Document Structure, First Steps with Sphinx, |
| 163 | http://sphinx.pocoo.org/tutorial.html#defining-document-structure""" |
| 164 | |
| 165 | parser = OptionParser(usage=usage) |
| 166 | parser.add_option("--sphinxify", |
| 167 | action="store_true", dest="sphinxify", |
| 168 | help="Print information about how to use Sphinx to compile your rst file, then exit.") |
| 169 | (options, args) = parser.parse_args() |
| 170 | |
| 171 | # Parse option |
| 172 | if options.sphinxify: |
| 173 | print sphinxify_text |
| 174 | sys.exit(0) |
| 175 | |
| 176 | # Parse arguments |
| 177 | if len(args) < 1: |
| 178 | parser.print_usage() |
| 179 | sys.exit(1) |
| 180 | |
| 181 | for file_name in args: |
| 182 | print "Processing "+file_name |
| 183 | try: |