src/cm/converters/pandoc_converters.py
changeset 261 b60ab54b6782
parent 259 0371caf8bcc6
child 351 9245a73f5787
equal deleted inserted replaced
260:9075dc2fb93c 261:b60ab54b6782
     1 # python 2.5 compat
     1 # python 2.5 compat
     2 from __future__ import with_statement
     2 from __future__ import with_statement
     3 from cm.utils.cache import memoize
     3 from cm.utils.cache import memoize, dj_memoize
     4 ######
     4 ######
     5 ## This module requires pandoc v > 1.0 (pandoc & markdown executables) 
     5 ## This module requires pandoc v > 1.0 (pandoc & markdown executables) 
     6 ######
     6 ######
     7 
     7 
     8 from subprocess import Popen, PIPE, call
     8 from subprocess import Popen, PIPE, call
    35 
    35 
    36 DEFAULT_INPUT_FORMAT = 'markdown'
    36 DEFAULT_INPUT_FORMAT = 'markdown'
    37 
    37 
    38 _PANDOC_ENCODING = 'utf8'
    38 _PANDOC_ENCODING = 'utf8'
    39 
    39 
    40 @memoize
    40 @dj_memoize
    41 def pandoc_convert(content, from_format, to_format, full=False, raw=False):
    41 def pandoc_convert(content, from_format, to_format, full=False, raw=False):
    42     """
    42     """
    43     Convert markdown content to pdf
    43     Convert markdown content to pdf
    44     
    44     
    45     >>> res = pandoc_convert('<span>dssd', 'html', 'pdf')
    45     >>> res = pandoc_convert('<span>dssd', 'html', 'pdf')
    70         content = fp.read()
    70         content = fp.read()
    71         fp.close()
    71         fp.close()
    72 
    72 
    73     return content
    73     return content
    74 
    74 
    75 @memoize
    75 @dj_memoize
    76 def do_tidy(content=None, file_name=None):
    76 def do_tidy(content=None, file_name=None):
    77     """
    77     """
    78     Tidy (html) content
    78     Tidy (html) content
    79     
    79     
    80     >>> res = do_tidy('<span>sdd')
    80     >>> res = do_tidy('<span>sdd')
   107 LATEX_HEADER_PATH = os.path.join(*_tmp_)
   107 LATEX_HEADER_PATH = os.path.join(*_tmp_)
   108 
   108 
   109 if not os.path.isfile(LATEX_HEADER_PATH):
   109 if not os.path.isfile(LATEX_HEADER_PATH):
   110     raise Exception('LATEX_HEADER_PATH is not a file!')
   110     raise Exception('LATEX_HEADER_PATH is not a file!')
   111 
   111 
   112 @memoize
   112 @dj_memoize
   113 def pandoc_markdown2pdf(content=None, file_name=None):
   113 def pandoc_markdown2pdf(content=None, file_name=None):
   114     """
   114     """
   115     Convert markdown content to pdf
   115     Convert markdown content to pdf
   116     
   116     
   117     >>> pdf_content = pandoc_markdown2pdf('# dssd')
   117     >>> pdf_content = pandoc_markdown2pdf('# dssd')
   152     return pdf_content
   152     return pdf_content
   153     
   153     
   154 # TODO: manage images in pandoc (?)
   154 # TODO: manage images in pandoc (?)
   155 # TODO: use tidy to cleanup html
   155 # TODO: use tidy to cleanup html
   156 
   156 
   157 @memoize
   157 @dj_memoize
   158 def pandoc_pandoc(content, from_format, to_format, full=False, raw=False):
   158 def pandoc_pandoc(content, from_format, to_format, full=False, raw=False):
   159     """
   159     """
   160     Convert content (should be unicode) from from_format to to_format
   160     Convert content (should be unicode) from from_format to to_format
   161     (if full: includes header & co [html, latex])
   161     (if full: includes header & co [html, latex])
   162     Returns out (unicode), err
   162     Returns out (unicode), err