1. Benoit Chesneau
  2. Pypaste

Commits

ben...@pollen  committed e62804f

make more html5 compliant, use time tag.

  • Participants
  • Parent commits de6d7c2
  • Branches default

Comments (0)

Files changed (9)

File friendpaste/template.py

View file
 
 from friendpaste import settings
 from friendpaste.http import FPResponse, FPRequest
-from friendpaste.utils import local, timesince, datetimestr_topython
+from friendpaste.utils import local, timesince, datetimestr_topython,\
+datetime_tojson
 
 template_env = Environment(loader=FileSystemLoader(settings.TEMPLATES_PATH))
 template_env.charset = 'utf-8'
 template_env.globals['DEBUG'] = settings.DEBUG
 template_env.globals['SITE_URI'] = settings.SITE_URI
 template_env.globals['ALL_COLORSHEME'] = list(get_all_styles())
-
+template_env.filters['rfc3339'] = datetime_tojson
 
 def re_escape(value):
     return re.escape(value)
 template_env.filters['re_escape'] = re_escape
 
-
+def format_datetime(value):
+    value = datetimestr_topython(value)
+    return value.strftime("%a %b %d %Y at %H:%M")
+template_env.filters['formatdatetime'] = format_datetime
 
 class PasteHtmlFormatter(formatters.HtmlFormatter):
     def wrap(self, source, outfile):

File friendpaste/views.py

View file
 from friendpaste.http import FPResponse, send_json
 from friendpaste.models import Paste, Fork
 from friendpaste.template import render_response, highlighter, pretty_type, shighlight
-from friendpaste.utils import local, local_manager, datetimestr_topython, strptime, make_hash
+from friendpaste.utils import local, local_manager, datetimestr_topython,\
+strptime, make_hash, datetime_tojson
 
 
 def _get_lexers():
     snippet, revisions = Paste.with_revisions(local.db, id, request.values.get('rev'))
      
     revisions = [snippet] + revisions
+    revisions_ = []
+    if revisions and revisions is not None:
+        revisions_ = [rev._data for rev in revisions] 
+
     if 'application/json' in request.accept_mimetypes:
-        if revisions and revisions is not None:      
-            return send_json([rev._data for rev in revisions])
-        return send_json([])
+        return send_json(revisions_)
 
     if snippet is None:
         raise NotFound
-    return render_response('paste/revisions.html', snippet=snippet, revisions=revisions)
+    return render_response('paste/revisions.html', snippet=snippet, revisions=revisions_)
     
     
 def highlight(request):

File static/css/src/layout.css

View file
     -moz-user-select: none; /* mozilla */
     user-select: none; /* css3 */
     -khtml-user-select: none;
+    -o-user-select: none;
 }
 
 .line-gutter-backdrop {
 
 #bottom {
     display: block;
+    position: relative;
     clear: both;
     width: 100%;
     margin-top: 15px;

File static/js/src/base.js

View file
 */
 
 
+
 // override dom and some common javascript elements
 base2.DOM.bind(document);
 base2.DOM.bind(window);
 {
     var func = this;
     var args = Array.prototype.slice.call(arguments, 1);
-    return function() { return func.apply(thisObject, args.concat(Array.prototype.slice.call(arguments, 0))) };
+    return function() { 
+        return func.apply(thisObject, args.concat(Array.prototype.slice.call(arguments, 0))) 
+    };
+}
+
+Date.parseRFC3339 = function (string) {
+    var date=new Date(0);
+    var match = string.match(/(\d{4})-(\d\d)-(\d\d)\s*(?:[\sT]\s*(\d\d):(\d\d)(?::(\d\d))?(\.\d*)?\s*(Z|([-+])(\d\d):(\d\d))?)?/);
+    if (!match) return;
+    if (match[2]) match[2]--;
+    if (match[7]) match[7] = (match[7]+'000').substring(1,4);
+    var field = [null,'FullYear','Month','Date','Hours','Minutes','Seconds','Milliseconds'];
+    for (var i=1; i<=7; i++) if (match[i]) date['setUTC'+field[i]](match[i]);
+    if (match[9]) date.setTime(date.getTime()+(match[9]=='-'?1:-1)*(match[10]*3600000+match[11]*60000) );
+    return date.getTime();
+}
+
+
+// Localize the display of <time> elements
+function localizeDates() {
+    var lastdate = '';
+    var now = new Date();
+
+    document.querySelectorAll('time').forEach(function(el, index) {
+        if (el.getAttribute('title') == "GMT") {
+            var date = new Date(Date.parseRFC3339(el.getAttribute('datetime')));
+            if (!date.getTime())
+                return;
+            diff = ((now.getTime() - date.getTime()) / 1000),
+            day_diff = Math.floor(diff / 86400);
+            if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 )
+                return;
+            var text = date.toLocaleString();
+            var title = date.toLocaleString();
+            
+            if (day_diff == 0) {
+                text = (diff < 60 && "Just Now" ||
+                diff < 120 && "1 minute ago" ||
+                diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
+                diff < 7200 && "1 hour ago" ||
+                diff < 86400 && Math.floor( diff / 3600 ) + " hours ago");
+                title = date.toLocaleTimeString();
+            } else {
+                hours = date.getHours();
+                minutes = date.getMinutes();
+                hours = (hours < 10) && "0" + hours || hours;
+                minutes = (minutes < 10) && "0" + minutes || minutes;
+                text = (day_diff == 1 && "Yesterday at " +  hours + ":" + minutes ||
+                el.textContent);
+                title = date.toLocaleString();
+            }
+            el.setAttribute('title', title);
+            el.textContent = "posted " + text;
+        }
+    });
+
 }
 
 function calculeOffset(element,attr){

File static/js/src/friendpaste.js

View file
                     this.setTheme.bindAsEventListener(this), false);
         document.querySelector("#show-linenos").addEventListener("click", 
                     this.toggleLineNumber.bindAsEventListener(this), false);
+ 
     },
 
     listenPasteActions: function() {
             document.querySelector(".pasteHistory").innerHTML = tcontent;
             self.snippet.classList.add("hidden");
             self.revisions.classList.remove("hidden");
+            localizeDates();
             resizeBottom();
         }, "json");
         

File templates/base.html

View file
         src="/static/js/friendpaste-libs.js?20081122"></script>
     {% endif %}
     <script>
+        localizeDates();
         resizeBottom();
         window.onresize = resizeBottom;
     </script>

File templates/paste/diff.html

View file
 <section id="snippet_view">
     <article id="snippet">
         <header>
-            <h2><a href="/{{ snippet.pasteid }}">{{ snippet.pasteid }}</a> changes</h2>
+            <h2><a href="/{{ snippet.pasteid }}">{{ snippet.pasteid
+                    }}</a> changeset</h2>
             <div id="info">
-                {{ snippet['updated']|datetimeformat }} 
+                <time title="GMT" datetime="{{snippet.updated|rfc3339 }}">{{ snippet.updated|formatdatetime }}</time> 
                 <table>
                     <tr><th>Changeset</th><td><a href="/{{ snippet.pasteid }}?rev={{ rev }}">{{ rev }}</a> (b)</td></tr>
                     <tr><th>Parent</th><td><a href="/{{ snippet.pasteid }}?rev={{ old_rev }}">{{ old_rev }}</a> (a)</td></tr>
                     <form action="/{{ snippet.pasteid }}" method="get">
                         <input type="submit" value="Back to the paste..." />
                     </form>
-                </ul></li>
+                </li></ul>
             </div>
 
             <p id="dl"><strong>Download in other format:</strong><br /><a href="/{{ snippet.pasteid }}/changeset?rev={{ rev }}&amp;format=raw">Unified diff</a></p>

File templates/paste/fork.html

View file
         new Resizeable("#paste_snippet");
 
     // init source code editor
-        new Editor("#paste_snippet");
+    new Editor("#paste_snippet");
 
     document.querySelector("#paste_snippet").focus();
 </script>

File templates/paste/view.html

View file
             
             <div id="info">
                 <p>Revision <a href="/{{ snippet.pasteid }}?rev={{
-                    snippet.nb_revision }}">{{ snippet.nb_revision }}</a> ({{
-                snippet.updated|timesince }}) - <a href="{{
+                    snippet.nb_revision }}">{{ snippet.nb_revision
+                    }}</a> (<time title="GMT" datetime="{{
+                snippet.updated|rfc3339 }}">{{
+                snippet.updated|formatdatetime }}</time>) - <a href="{{
                     url_for('paste/changeset', id=snippet.pasteid,
                     rev=snippet.nb_revision) }}">Diff</a>
                 </p>