Commits

Maciej Fijalkowski  committed c9ce8d5

more stuff

  • Participants
  • Parent commits 6da6c08

Comments (0)

Files changed (3)

File codeviewer/overview.jinja

+<html>
+  <head>
+   <script type="text/javascript" src="jquery.min.js"></script>
+   <script type="text/javascript" src="jquery.scrollTo-1.4.2-min.js"></script>
+   <script type="text/javascript" src="jquery.viewport.mini.js"></script>
+   <script type="text/javascript" src="y.js"></script>
+   <link rel="stylesheet" href="pygments.css">
+   <link rel="stylesheet" href="x.css">
+  </head>
+  <body>
+    <div id="main-code-browser">
+    {% for i, item in tree %}
+       <div id="w-{{i+1}}" class="box box-{{ item.kind }}">
+         {{ item.get_overview() }}
+         {% if item.kind == "cls" %}
+            {% for meth in item.methods %}
+            <div class="box box-{{ meth.kind }}">
+              {{ meth.get_overview() }}
+            </div>
+            {% endfor %}
+         {% endif %}
+       </div>
+    {% endfor %}
+    </div>
+  </body>
+</html>

File codeviewer/y.js

+
+function KEYS() {
+    this.DOWN = 40;
+    this.UP   = 38;
+    this.CTRL = 17;
+    this.PAGE_UP = 33;
+    this.PAGE_DOWN = 34;
+    this.L = 76;
+    
+    this.accepts = function(key) {
+        return (key == this.DOWN || key == this.UP || key == this.CTRL ||
+                key == this.PAGE_UP || key == this.PAGE_DOWN || key == this.L);
+    };
+
+    this.arrows = function(key) {
+        return key == this.DOWN || key == this.UP;
+    };
+
+    this.pages = function(key) {
+        return key == this.PAGE_UP || key == this.PAGE_DOWN;
+    };
+
+    this.ctrl_pressed = false;
+
+}
+keys = new KEYS();
+
+MIN_POSITION = 100;
+prev_style = null;
+single_element_height = null;
+MAX_NUM = 100;
+
+function hex(v) {
+    return Math.round(v * 255).toString(16);
+}
+
+function colorize(v) {
+    var red, green, blue;
+    if (v > 1)
+        v = 1;
+    if (v < 0)
+        v = 0;
+    //v = v*v;
+    v = ((1 - v) * 0x88 / 0x100) + (0xF8 - 0x88) / 0x100;
+    return "#" + hex(v) + hex(v) + "F8";
+}
+
+function escape(l) {
+    return l.replace("<", "&lt;").replace(">", "&gt;");
+}
+
+function ondown(ev) {
+    var elem, pos, cur, ofs, counter = 0, num;
+    var keycode = ev.keyCode;
+
+    if (keys.accepts(keycode)) {
+        ev.preventDefault();
+        if (keycode == keys.CTRL) {
+            keys.ctrl_pressed = true;
+            return;
+        }
+        cur = $(".active");
+        if (keycode == keys.L && keys.ctrl_pressed) {
+            ofs = cur.offset().top - $(window).height() / 2;
+            if (ofs < 0) {
+                ofs = 0;
+            }
+            $.scrollTo(ofs);
+            return;
+        }
+        if (keys.arrows(keycode)) {
+            if (keycode == keys.UP) {
+                counter = -1;
+            } else {
+                counter = 1;
+            }
+        } else if (keys.pages(keycode)) {
+            pos = document.body.scrollTop;
+            if (keycode == keys.PAGE_UP) {
+                counter = -$(window).height() / single_element_height;
+            } else {
+                counter = $(window).height() / single_element_height;
+            }
+            counter = Math.round(counter);
+        }
+        if (counter != 0) {
+            elem = cur;
+            num = parseInt(elem.attr("id").substr(2)) + counter;
+            if (num < 1) {
+                num = 1;
+            }
+            if (num > MAX_NUM) {
+                num = MAX_NUM;
+            }
+            elem = $("#w-" + num);
+            activate(elem);
+            pos = elem.position().top - document.body.scrollTop;
+            if (pos < MIN_POSITION) {
+                $.scrollTo("-=" + (MIN_POSITION - pos), 0.5);
+            }
+            if (pos > $(window).height() - MIN_POSITION) {
+                $.scrollTo("+=" + (pos - $(window).height() + MIN_POSITION), 0.5);
+            }
+        }
+    }
+}
+
+function onup(ev) {
+    if (ev.keyCode == keys.CTRL) {
+        keys.ctrl_pressed = false;
+    }
+}
+
+function activate(elem) {
+    prev_elem = $(".active");
+    if (prev_elem) {
+        prev_elem.toggleClass("active");
+    }
+    elem.toggleClass('active');
+}
+
+$(document).ready(function () {
+    $(document).keydown(ondown);
+    $(document).keyup(onup);
+    activate($("#w-1"));
+    MAX_NUM = $("#main-code-browser").children().length;
+});

File codeviewer/ydemo.py

+
+import ast
+from jinja2 import Template
+
+class Code(object):
+    kind = 'code'
+    
+    def __init__(self, elems):
+        self.elems = elems
+
+    def get_overview(self):
+        if len(self.elems) == 1:
+            return "Code, 1 line"
+        return "Code, %d lines" % len(self.elems)
+
+class Class(object):
+    kind = 'cls'
+    
+    def __init__(self, name, methods):
+        self.name    = name
+        self.methods = methods
+
+    def get_overview(self):
+        return 'Class %s' % self.name
+
+class Function(object):
+    kind = 'func'
+    
+    def __init__(self, name):
+        self.name = name
+
+    def get_overview(self):
+        return 'Function %s' % self.name
+
+class Method(Function):
+    def get_overview(self):
+        return 'Method %s' % self.name
+
+class Walker(object):
+    def walk(self, code):
+        return self.walk_body(ast.parse(code).body)
+
+    def walk_chunk(self, elems, i):
+        start_i = i
+        lineno = elems[i].lineno
+        i += 1
+        while i < len(elems) and elems[i].lineno - lineno <= 1:
+            lineno = elems[i].lineno
+            i += 1
+        return Code(elems[start_i:i]), i
+
+    def walk_class(self, elem):
+        return Class(elem.name, self.walk_body(elem.body, inside_class=True))
+
+    def walk_function(self, elem, inside_class):
+        if inside_class:
+            return Method(elem.name)
+        return Function(elem.name)
+        
+    def walk_body(self, elems, inside_class=False):
+        i = 0
+        chunks = []
+        while i < len(elems):
+            elem = elems[i]
+            if isinstance(elem, ast.ClassDef):
+                chunks.append(self.walk_class(elem))
+                i += 1
+            elif isinstance(elem, ast.FunctionDef):
+                chunks.append(self.walk_function(elem,
+                                                 inside_class=inside_class))
+                i += 1
+            else:
+                chunk, i = self.walk_chunk(elems, i)
+                chunks.append(chunk)
+        return chunks
+
+code = open('/home/fijal/src/pypy/pypy/translator/goal/richards.py', 'r').read()
+tree = Walker().walk(code)
+with open("y.html", "w") as f:
+    f.write(Template(open("overview.jinja").read()).render(tree=enumerate(tree)))