Commits

Robert Mařík committed 863a808

reindent and added beter algorithm for word wrap

Comments (0)

Files changed (1)

 #                  http://www.gnu.org/licenses/
 ###############################################################################
 
-"""
+r"""
 Poor man's Sage to TeX converter.
 
 The script converts some basic html to TeX sequences
 ./sage2tex file.sws [output]
 
 OUTPUT:
-The output is in file output.pdf. If the second argument is missing, the 
+The output is in file output.pdf. If the second argument is missing, the
 output is in file.pdf.
 
 Works for limited set of commands, only.
 import os
 import sys
 import glob
+import textwrap
 
 from pygments import highlight
 from pygments.lexers import PythonLexer, TexLexer, HtmlLexer
 
 colorize = Colorize()
 
-def line_breaks(text, columns=85):
+def line_breaks(text, columns=85, options=None):
     r"""
     Split the string into lines of length columns.
 
-    TODO: use a more clever algorithm
+    
+    - options.word_wrap = 0 : no wrapping
+    - options.word_wrap = 1 : wrapping using textwrap mpdule
+    - options.word_wrap = 2 : fixed line length
+    
+
     """
+    if options.word_wrap == 0:
+        return text
+    if options.word_wrap == 1:
+        return "\n".join(textwrap.wrap(text,columns))+"\n"
     newlines = ""
     for line in text.splitlines():
         while len(line) >= columns:
         newlines += line + '\n'
     return newlines
 
-#print colorize("print 1+1")
 
 class Cell:
     text = ""
 
 class OutputCell(Cell):
     r"""
-    Cell containning Sage output. 
+    Cell containning Sage output.
 
     The content of this cell depends on the fact whether the Typeset
     button in Sage notebook is checked or not. Could contains raw text
             if verbatim == True:
                 if output.strip() != "":
                     latex_str += "\\begin{Verbatim}[formatcom=\color{blue}]\n"
-                    latex_str += line_breaks(output)
+                    latex_str += line_breaks(output, options=self.options)
                     latex_str += "\\end{Verbatim}\n"
                 verbatim = False
             else:
                     latex_str += "\n\n\\includegraphics{sage_worksheet/cells/%s/%s}\n\n" % (self.cell_id, i)
                 elif i[-4:] == "jmol":
                     latex_str += "\n\n\\centerline{\\color{gray}\\fbox{\\textsc{3D picture skipped}}}\n\n"
-                
+
         return latex_str
 
     def parse_output(self):
             if language == 'latex':
                 current_cell = cell_list[-1].text
             #if current_cell.strip() != "":
-            cell_list.append(OutputCell(current_cell, current_cell_id, language = language))
+            cell_list.append(OutputCell(current_cell, current_cell_id, options=options, language = language))
             current_cell = ""
             continue
 
 usage: %prog [options] input [output]
 
 You may want to specify LaTeX preamble, author and language via configuration
-in .sws2tex config file in current directory or in your home directory 
+in .sws2tex config file in current directory or in your home directory
 (see .sws2tex_example). Command line options override this settings.
 """
 opt_parser = OptionParser(usage = usage)
                       action = "store_true", dest = "hide", default = False,
                       help = "do not print cell with %hide token")
 
+opt_parser.add_option("-W", "--word_wrap",
+                      action = "store", type = 'int', dest = "word_wrap", default = 1,
+                      help = "select method for word wrap 0 (no wrapping), 1 (textwraper), 2 (fixed line length)")
+
 (options, args) = opt_parser.parse_args()
 
 if len(args) < 2:
 
 from IPython.external.configobj import ConfigObj
 
-try: 
+try:
     if os.path.exists('.sws2tex'):
         filename = '.sws2tex'
     elif os.path.exists(os.path.expanduser("~/.sws2tex")):