summaryrefslogtreecommitdiff
path: root/custom.js
blob: b8c01b5f732c5a06283ae1f3795683e95085e690 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
$([IPython.events]).on('notebook_loaded.Notebook', function(){
    /* first, checks if it isn't implemented yet */
    if (!String.prototype.format) {
      String.prototype.format = function() {
        var args = arguments;
        return this.replace(/{(\d+)}/g, function(match, number) { 
          return typeof args[number] != 'undefined'
            ? args[number]
            : match
          ;
        });
      };
    }

    $code_cell = $(".code_cell");

    var regex = /[+-]?\d*\.*\d+/g;
    var path_regex = /\/([^\/]*)\/[^/]*\.ipynb$/;
    var count = 1;

    /* fetching book name from path */
    var pathname = window.location.pathname;
    var strings = pathname.match(path_regex);
    var book = strings[1];
    console.log("Book:" + book);

    /* fetching chapter number */
    var $first_text_cell = $(".text_cell:first");
    /* chapter name string either in id or innerHTML */
    var chapter_name = "";
    chapter_name = $first_text_cell.find("h1").attr("id");
    if(!chapter_name) {
        chapter_name = $first_text_cell.find("h1").html();
    }
    var numbers = chapter_name.match(regex).map(function(v) { return parseFloat(v); });
    var chapter_number = Math.abs(numbers[0]);
    console.log("Chapter:" + chapter_number);

    $code_cell.each(function(index, element) {
        try {
            /* fetching example and page number */
            var $current_text_cell = $(this).prev();
            /* code headings can be h2 or h3 */
            var $heading;
            $heading = $current_text_cell.find("h2");
            if(!$heading.html())  {
                $heading = $current_text_cell.find("h3").first();
                console.log("########## Head" + $heading.html());
            }
            var heading_text = $heading.html();
            var numbers = heading_text.match(regex).map(function(v) { return parseFloat(v); });
            var example_no = Math.abs(numbers[0]);
            var page_no = Math.abs(numbers[1]);
            console.log("########## Exp:" + example_no + ", Pg:" + page_no);
            
            /* creating the link */
            var $link = $("<a></a>");
            var href = "http://tbc-python.fossee.in/comments/get/?book={0}&chapter={1}&example={2}&page={3}";
            href = href.format(book, chapter_number, example_no, page_no);
            $link.attr({
                href: href,
                class: "question",
                target: "_blank",
            });
            $(this).prepend($link);
            count++;
        } catch(e) {
            return;
        }
    });
});

/* hiding running/clusters links */
$("#tabs a").each(function(index, element) {
    var href = $(this).attr("href");
    console.log($(this).attr("href"));
    if(href == "#running" || href == "#clusters") {
        $(this).hide();
    }
});

var $branding = $("<a href='http://fossee.in' class='branding pull-right' target='_blank'></a>");
$("#ipython_notebook").after($branding);