wepub

Check-in [31f6f517d7]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Permit saved locations for multiple chapters under a book
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | master | trunk
Files: files | file ages | folders
SHA3-256:31f6f517d719871c1bc536bb1cddb666b27d072172e9e9e5f7a6cd8094c64aa8
User & Date: ajv-899-334-8894@vsta.org 2016-11-21 18:31:09
Context
2016-11-21
22:04
Software (un)license and basic README check-in: 23cd3446c1 user: ajv-899-334-8894@vsta.org tags: master, trunk
18:31
Permit saved locations for multiple chapters under a book check-in: 31f6f517d7 user: ajv-899-334-8894@vsta.org tags: master, trunk
18:30
Use the relative version of the URL for recent book references check-in: 2ba102d516 user: ajv-899-334-8894@vsta.org tags: master, trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to js/reader.js.

5
6
7
8
9
10
11



12
13
14
15
16
17
18
..
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
84







85
86
87
88
89
90
91

// User name
var who = null;

// Document path
var docname = null;




// Web element with text content which scrolls
var pane = null;

// Wrappers for XMLHttpRequest
function xhr_get(url, fn) {
    var req = new XMLHttpRequest();
    req.open("GET", url);
................................................................................
    }
    req.send(buf);
}

// Saved state for this user on this document
function start_state(txt) {
    var resp = JSON.parse(txt);







    // Position is saved as a float percentage 0..1, so
    //  scale fractional position against overall scroll
    //  height to set position.
    var d = document.scrollingElement;
    if (d != null) {
        d.scrollTop = d.scrollHeight * resp.pos;
    }

    // TBD, styling to patch into CSS?
}

// Push our position
function save_state() {
    var st = {};
    var d = document.scrollingElement;
    if (d != null) {
        st.pos = d.scrollTop / d.scrollHeight;









        xhr_put_json("/state.json?doc=" + encodeURIComponent(docname),
            JSON.stringify(st), null);
    }
}

// On page load, configure to track this user for this scrollable
//  text content of this document.  If there's a saved position,
//  scroll to it.
function reading(arg_who, arg_doc, arg_pane) {
    // Save for future
    who = arg_who;
    docname = arg_doc;
    pane = document.getElementById(arg_pane);








    // By default, start at top of document
    var d = document.scrollingElement;
    if (d != null) {
        d.scrollTop = 0;
    }








>
>
>







 







>
>
>
>
>
>






|










|
>
>
>
>
>
>
>
>
>













>
>
>
>
>
>
>







5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
..
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116

// User name
var who = null;

// Document path
var docname = null;

// Section number
var secnum = null;

// Web element with text content which scrolls
var pane = null;

// Wrappers for XMLHttpRequest
function xhr_get(url, fn) {
    var req = new XMLHttpRequest();
    req.open("GET", url);
................................................................................
    }
    req.send(buf);
}

// Saved state for this user on this document
function start_state(txt) {
    var resp = JSON.parse(txt);

    // See if there's a position for our section
    if (!(secnum in resp)) {
        // Just leave us at top
        return;
    }

    // Position is saved as a float percentage 0..1, so
    //  scale fractional position against overall scroll
    //  height to set position.
    var d = document.scrollingElement;
    if (d != null) {
        d.scrollTop = d.scrollHeight * resp[secnum];
    }

    // TBD, styling to patch into CSS?
}

// Push our position
function save_state() {
    var st = {};
    var d = document.scrollingElement;
    if (d != null) {
        // Which section?  Each chapter has its own position.
        st.secnum = secnum;

        // Record URL we're using
        st.url = document.URL;

        // Percentage position on scroll bar
        st.pos = parseFloat(d.scrollTop) / parseFloat(d.scrollHeight);

        // Send it over
        xhr_put_json("/state.json?doc=" + encodeURIComponent(docname),
            JSON.stringify(st), null);
    }
}

// On page load, configure to track this user for this scrollable
//  text content of this document.  If there's a saved position,
//  scroll to it.
function reading(arg_who, arg_doc, arg_pane) {
    // Save for future
    who = arg_who;
    docname = arg_doc;
    pane = document.getElementById(arg_pane);

    // Calculate section number
    var url = document.URL;
    var slidx = url.lastIndexOf('/');
    if (slidx > 0) {
        secnum = parseInt(url.slice(slidx+1));
    }

    // By default, start at top of document
    var d = document.scrollingElement;
    if (d != null) {
        d.scrollTop = 0;
    }

Changes to put.py.

1
2
3
4
5
6
7
8
9
10
11
12
..
20
21
22
23
24
25
26
27
28
29
















30
31
32
33
34
35
36
37
#
# put.py
#       Handle PUT operations to the ePub reader
#
import os, sys, urllib

class PUT_mixin(object):

    def __init__(self):
        self.dispatchers.append( ("PUT", self.put_state) )

    # Here's some fresh state?
................................................................................

        # Get a filesystem safe name, see if we can save the info
        try:
            os.mkdir("var/state/%s" % (self.user,))
        except:
            # Already exists, probably
            pass
        try:
            doc = urllib.quote_plus(urllib.unquote(doc))
            statepath = "var/state/%s/%s.json" % (self.user, doc)
















            f = open(statepath, "w")
            f.write(buf)
            f.close()
        except:
            sys.stderr.write("Can't save state to %s\n" % (statepath,))
            return False,None

        return True,self.send_result("", "text/html")




|







 







<
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

|



<


1
2
3
4
5
6
7
8
9
10
11
12
..
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
#
# put.py
#       Handle PUT operations to the ePub reader
#
import os, sys, urllib, json

class PUT_mixin(object):

    def __init__(self):
        self.dispatchers.append( ("PUT", self.put_state) )

    # Here's some fresh state?
................................................................................

        # Get a filesystem safe name, see if we can save the info
        try:
            os.mkdir("var/state/%s" % (self.user,))
        except:
            # Already exists, probably
            pass

        doc = urllib.quote_plus(urllib.unquote_plus(doc))
        statepath = "var/state/%s/%s.json" % (self.user, doc)

        # Get existing state
        try:
            f = open(statepath, "r")
            ovals = json.loads(f.read())
            f.close()
        except:
            ovals = {}

        # Update with this state
        newvals = json.loads(buf)
        ovals["url"] = str(newvals["url"])
        ovals[int(newvals["secnum"])] = float(newvals["pos"])

        # Try and save back
        try:
            f = open(statepath, "w")
            f.write(json.dumps(ovals))
            f.close()
        except:
            sys.stderr.write("Can't save state to %s\n" % (statepath,))


        return True,self.send_result("", "text/html")