src/cm/converters/old_converters.py
changeset 0 40c8f766c9b8
equal deleted inserted replaced
-1:000000000000 0:40c8f766c9b8
       
     1 ########## converters
       
     2 from django.core.cache import cache
       
     3 
       
     4 # adapted [to django] from http://code.activestate.com/recipes/325205/
       
     5 def memoize(f):
       
     6     def g(*args, **kwargs):
       
     7         key = ( f.__name__, f, tuple(args), frozenset(kwargs.items()) )
       
     8         val = cache.get(key)
       
     9         if not val:
       
    10             val = f(*args, **kwargs)
       
    11             cache.set(key,val)
       
    12         return val
       
    13     return g
       
    14 
       
    15 
       
    16 def to_unicode(string):
       
    17     if type(string) != 'unicode':
       
    18         return string.decode('utf8')
       
    19     else:
       
    20         return string 
       
    21 
       
    22 def to_utf8(string):
       
    23     if type(string) != 'str':
       
    24         return string.encode('utf8')
       
    25     else:
       
    26         return string
       
    27  
       
    28 #@memoize
       
    29 def rst_to_html(rst):
       
    30     from docutils import core, io
       
    31     html, pub = _get_html_and_pub(rst)
       
    32     parts = pub.writer.parts
       
    33     return parts['stylesheet']+parts['body']
       
    34 
       
    35 #@memoize
       
    36 def rst_to_fullhtml(rst):
       
    37     html, pub = _get_html_and_pub(rst)
       
    38     parts = pub.writer.parts
       
    39     return html 
       
    40     #return '<html><head>' + parts['stylesheet'] + '</head><body>' + parts['body'] + '</body></html>' 
       
    41 
       
    42 def markdown_to_html(markdown):
       
    43     return _markdown_to_html(markdown)
       
    44 
       
    45 def markdown_to_fullhtml(markdown):
       
    46     return '<html><body>'+_markdown_to_html(markdown) + '</body></html>'
       
    47 
       
    48 def _markdown_to_html(markdown):
       
    49     from markdown import Markdown
       
    50     md = Markdown()
       
    51     html = md.convert(markdown)
       
    52     return html
       
    53     
       
    54 def _get_html_and_pub(rst):
       
    55     from docutils import core, io
       
    56     html, pub = core.publish_programmatically(
       
    57             source_class=io.StringInput, source=rst,
       
    58             source_path=None,
       
    59             destination_class=io.StringOutput, destination=None,
       
    60             destination_path=None,
       
    61             reader=None, reader_name='standalone',
       
    62             parser=None, parser_name='restructuredtext',
       
    63             writer=None, writer_name='HTML',
       
    64             settings=None, settings_spec=None, settings_overrides=None,
       
    65             config_section=None, enable_exit_status=None)
       
    66     return html, pub
       
    67 
       
    68 #@memoize
       
    69 def html_to_pdf(html):
       
    70     html = to_utf8(html)
       
    71     
       
    72     import sx.pisa3 as pisa
       
    73     import StringIO
       
    74     dst = StringIO.StringIO()
       
    75     result = pisa.CreatePDF(html, dst)
       
    76     if not result.err:    
       
    77         pdf = dst.getvalue()
       
    78         dst.close()
       
    79         return pdf
       
    80     else:
       
    81         return None
       
    82 
       
    83 # http://www.aaronsw.com/2002/html2text/
       
    84 #@memoize    
       
    85 def html_to_markdown(html):
       
    86     from com.ext.html2text import html2text
       
    87     return html2text(html)
       
    88 
       
    89 ########## formats
       
    90 
       
    91 FORMATS = {
       
    92            'HTML' : {'name': 'HTML', 
       
    93                      'to_format' : {'Markdown' : html_to_markdown, }
       
    94                      },
       
    95            'FULLHTML' : {'name': 'FULLHTML', 
       
    96                      'to_format' : {'PDF' : html_to_pdf, }
       
    97                      },
       
    98            'RST' : {'name': 'reStructuredText', 
       
    99                      'to_format' : {'HTML' : rst_to_html,
       
   100                                     'FULLHTML' : rst_to_fullhtml,
       
   101                                     }
       
   102                      },
       
   103                      
       
   104            'Markdown' : {'name': 'Markdown', 
       
   105                      'to_format' : {'HTML' : markdown_to_html,
       
   106                                     'FULLHTML' : markdown_to_fullhtml,
       
   107                                     }
       
   108                      },
       
   109            'Textile' : {'name': 'Textile', 
       
   110                      },
       
   111            'PDF' : {'name': 'PDF', 
       
   112                      },
       
   113             }            
       
   114 
       
   115 CHOICES_FORMATS = [ (k,v.get('name')) for k,v in FORMATS.items()]
       
   116 
       
   117 INPUT_FORMATS = ['RST','Markdown']
       
   118 
       
   119 DEFAULT_INPUT_FORMAT = 'Markdown'
       
   120 
       
   121 CHOICES_INPUT_FORMATS = [ (k,v.get('name')) for k,v in FORMATS.items() if k in INPUT_FORMATS]
       
   122 
       
   123 def get_supported_conversions(from_format):
       
   124     return FORMATS[from_format]['to_format'].keys()
       
   125 
       
   126 def is_supported_conversion(from_format, to_format):
       
   127     infos = FORMATS.get(from_format)
       
   128     return infos.get('to_format') and infos.get('to_format').get(to_format)
       
   129  
       
   130 def convert(content, from_format, to_format):
       
   131     if is_supported_conversion(from_format, to_format):
       
   132         infos = FORMATS.get(from_format)
       
   133         conv_fun = infos.get('to_format').get(to_format)
       
   134         return conv_fun(content)
       
   135     else:
       
   136         pass