Changeset 113 for trunk

Show
Ignore:
Timestamp:
15-01-2008 23:52:28 (12 months ago)
Author:
s0undt3ch
Message:

Implemented rudimentary unidiff between pastes, and allow users to download pastes(No download for diffs yet, should we enabled that?).

Location:
trunk/pastie
Files:
1 added
6 modified

Legend:

Unmodified
Added
Removed
  • trunk/pastie/config/routing.py

    r111 r113  
    2626    map.connect('tagcloud', '/tags', controller='pastetags', action='index') 
    2727    map.connect('paste', '/:id', controller="pasties", action='show') 
    28     map.connect('pastetree', '', controller='pasties', action='tree') 
     28    map.connect('pastetree', '/tree/:id', controller='pasties', action='tree') 
     29    map.connect('rawpaste', '/download/:id', controller='pasties', action='download') 
     30    map.connect('diffpaste', '/diff/:id/:parent', controller='pasties', action='diff') 
     31    map.connect('replypaste', '/reply/:id', controller='pasties', action='new') 
    2932 
    3033 
  • trunk/pastie/controllers/pasties.py

    r112 r113  
    8989        c.id = int(id) 
    9090        return render('paste.tree') 
     91 
     92    def download(self, id): 
     93        if not id: 
     94            redirect_to('list') 
     95        paste = Session.query(Paste).get(int(id)) 
     96        if not paste: 
     97            abort(404) 
     98 
     99        mimetype = h.get_lexer_by_name(paste.language or 'text').mimetypes[0] 
     100        response.content_type__set(mimetype) 
     101        response.charset__set('utf-8') 
     102        response.write(paste.code) 
     103        return 
     104 
     105    def diff(self, id, parent): 
     106        c.langdict = langdict 
     107        c.paste = Session.query(Paste).get(int(id)) 
     108        c.parent = Session.query(Paste).get(int(parent)) 
     109#        c.diff = c.paste.compare_to(c.other) 
     110#        c.diff.language = 'diff' 
     111        return render('paste.diff') 
  • trunk/pastie/lib/highlight.py

    r35 r113  
    66import StringIO 
    77 
    8 __all__ = ['code_highlight', 'get_lexers'] 
     8__all__ = ['code_highlight', 'get_lexers', 'get_lexer_by_name'] 
    99 
    1010langdict = {} 
     
    9696                                lineanchorlinks=True, linenospecial=10) 
    9797 
    98 def code_highlight(code, truncate_lines=None): 
     98def code_highlight(code, truncate_lines=None, diff_to=None): 
    9999    source = code.code 
     100    if diff_to: 
     101        source = code.compare_to(diff_to) 
     102        print source 
    100103    if truncate_lines: 
    101104        split_source = source.split('\n') 
     
    104107            source.append('...') 
    105108            source = ''.join(source) 
     109 
    106110    lexer = get_lexer_by_name(code.language or 'text', stripall=True) 
     111    if diff_to: 
     112        lexex = get_lexer_by_name('diff') 
    107113    return XML(highlight(source, lexer, formatter).decode('utf-8')) 
    108114 
  • trunk/pastie/model/pasties.py

    r29 r113  
    22from datetime import datetime 
    33import math 
     4import difflib 
    45 
    56from sqlalchemy import desc, Column, ForeignKey, func, select, Table, types 
     
    8485            paste_id = paste.parent_id 
    8586 
     87    def compare_to(self, other, context_lines=4): 
     88        if not isinstance(other, Paste): 
     89            other = Session.query(Paste).get(int(other)) 
     90        udiff = u'\n'.join(difflib.unified_diff( 
     91            self.code.splitlines(), 
     92            other.code.splitlines(), 
     93            fromfile='Paste #%d' % self.id, 
     94            tofile='Paste #%d' % other.id, 
     95            lineterm='', 
     96            n=context_lines) 
     97        ) 
     98        return udiff 
     99 
    86100mapper(Paste, paste_table, 
    87101    properties=dict( 
  • trunk/pastie/public/css/main.css

    r49 r113  
    173173  float: right; 
    174174  background: url(../img/cell_bg.png) no-repeat left bottom; 
    175   font-weight: bold; 
     175  /*font-weight: bold;*/ 
    176176  font-size: 0.6em; 
    177177} 
  • trunk/pastie/templates/paste/show.html

    r111 r113  
    2929    <div id="paste"> 
    3030    <div id="paste_menu"><ul> 
     31      <li> 
     32        <a href="${h.url_for('replypaste', id=c.paste.id)}">Reply To Paste</a> 
     33      </li> 
     34      <li> 
     35        <a href="${h.url_for('rawpaste', id=c.paste.id)}">Download Paste</a> 
     36      </li> 
     37      <li py:if="c.paste.parent_id"> 
     38        <a href="${h.url_for('diffpaste', parent=c.paste.parent.id, id=c.paste.id)}" 
     39           title="Diferences between current and parent paste">Diff With Parent</a> 
     40      </li> 
    3141      <li py:if="c.paste.children or c.paste.parent_id"> 
    3242        <a href="${h.url_for('pastetree', id=c.paste.id)}">Show Tree</a> 
    33       </li> 
    34       <li> 
    35         <a href="${h.url_for('newpaste', id=c.paste.id)}">Reply To Paste</a> 
    3643      </li> 
    3744      <li style="display:none;"><a class="toggle_linenumbers"