Robert Mařík avatar Robert Mařík committed 809472f

support for pictures inserted by TinyMCE editor

Comments (0)

Files changed (2)

 import HTMLParser
 import string
 import re
+import os
 
 def detect_math(text):
     r"""
 
 class Html2Latex(HTMLParser.HTMLParser):
 
-    def __init__(self, options = None, cell_id = None):
+    def __init__(self, options = None, cell_id = None, temp_dir = None):
         HTMLParser.HTMLParser.__init__(self)
         self.options = options
         self.cell_id = cell_id
+        self.temp_dir = temp_dir
         self.in_math_mode = False
         self.latex = ['']                # latex code
         self.data = ''
         r"""
         Replace tags which do not have open/close pair.
         
-        Supported tags are p, br, hr, a.
+        Supported tags are p, br, hr, a, img.
+
+        Tags <img .../> are inserted by TinyMCE.
         """
         self.add_data()
         self.latex.append('')
             exit(1)
             self.stack.append(attrs)
             self.handle_a()
+        elif tag == 'img':
+            # dict(attrs).get('src') looks like http://.... or file.png
+            where_is=dict(attrs).get('src')[0:4]
+            img_options = []
+            if dict(attrs).get('width') is not None:
+                img_options += ["width=%spx" % dict(attrs).get('width')]
+            if dict(attrs).get('height') is not None:
+               img_options += ["height=%spx" % dict(attrs).get('height')]
+            img_options = ",".join(img_options)
+            if where_is == 'http':
+                download = os.popen('cd %s; wget %s' % (self.temp_dir,dict(attrs).get('src')))
+                file = dict(attrs).get('src').split('/')
+                file = file[len(file)-1]
+                self.latex[-1] += "\\includegraphics[%s]{%s}" % (img_options,file)
+            else:
+                self.latex[-1] += "\\includegraphics[%s]{sage_worksheet/data/%s}" % (img_options,dict(attrs).get('src'))
         else:
             self.add_data()
             if self.options != None and not self.options.ignore_unknown_tags:
             else:
                 self.data += ref
 
-    def parse_html(self, text, OutputCell = False, cell_id = None):
+    def parse_html(self, text, OutputCell = False, cell_id = None, temp_dir = None):
         text = detect_math(text)
         self.cell_id = cell_id
-        #print text
+        self.temp_dir = temp_dir
         self.feed(text)        
 
         if OutputCell:
 """
 default_preamble = r"""
 \documentclass[a4paper]{article}
-\usepackage{xcolor,amsmath,graphics,hyperref}
+\usepackage{xcolor,amsmath,graphicx,hyperref}
 \usepackage{attachfile}
 \usepackage{colortbl}
 \usepackage[margin=1in]{geometry}
     def latex(self):
         latex_str = self.text
 
-        latex_str = self.html2latex.parse_html(latex_str)
+        latex_str = self.html2latex.parse_html(latex_str, temp_dir = temp_dir)
 
         return latex_str
 
     cell_type = 'input'
     language = 'sage'
 
-    def __init__(self, text, cell_id, options=None):
+    def __init__(self, text, cell_id, options = None):
         Cell.__init__(self, text, cell_id, options)
         self._detect_language()
 
     cell_type = "output"
     language = 'sage'
 
-    def __init__(self, text, cell_id, options=None, language='sage'):
+    def __init__(self, text, cell_id, options = None, language = 'sage'):
         Cell.__init__(self, text, cell_id, options)
         self.language = language
         self.html2latex = Html2Latex()
                         latex_str += parse_table(output, cell_id=self.cell_id)
                     else:
                         latex_str += "{\\color{blue}\n"
-                        latex_str += self.html2latex.parse_html(output, OutputCell=True, cell_id=self.cell_id)
+                        latex_str += self.html2latex.parse_html(output, OutputCell = True, cell_id = self.cell_id)
                         latex_str += "\n}\n"
                 verbatim = True
             latex_str += "\n\n" # paragraph between <html>...</html> blocks
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.