1
1
# -*- coding: utf-8 -*-
2
2
3
- # From sphinx.writers.websupport
3
+ import json
4
4
5
+ from docutils import nodes
5
6
from sphinx .writers .html import HTMLTranslator
6
7
7
8
import hasher
@@ -40,6 +41,89 @@ def is_commentable(node):
40
41
41
42
return False
42
43
44
+ kong_comments = """
45
+ {
46
+ "count": 2,
47
+ "next": null,
48
+ "previous": null,
49
+ "results": [
50
+ {
51
+ "date": "2015-02-24T19:03:53.992703",
52
+ "user": 1,
53
+ "text": "simple comment",
54
+ "node": {
55
+ "id": 619,
56
+ "version": {
57
+ "id": 104,
58
+ "project": {
59
+ "id": 2,
60
+ "name": "Kong",
61
+ "slug": "woot-kong",
62
+ "description": "",
63
+ "language": "en",
64
+ "repo": "http://github.com/ericholscher/django-kong",
65
+ "repo_type": "git",
66
+ "default_version": "latest",
67
+ "default_branch": null,
68
+ "documentation_type": "sphinx",
69
+ "users": [
70
+ 1
71
+ ]
72
+ },
73
+ "slug": "latest",
74
+ "identifier": "master",
75
+ "verbose_name": "latest",
76
+ "active": true,
77
+ "built": true,
78
+ "downloads": "{'htmlzip': '//localhost:8000/projects/woot-kong/downloads/htmlzip/latest/', 'epub': '//localhost:8000/projects/woot-kong/downloads/epub/latest/'}"
79
+ },
80
+ "current_hash": "nil-45fcb0188968b1be015a13a44508377b140a1639d872fcc7ee09639e7eb5eb71",
81
+ "last_commit": "fac9e2868d8fe86c2e7fcc3d4f02a43fe015f9b3",
82
+ "snapshots_count": "1",
83
+ "page": "index",
84
+ "project": 2
85
+ }
86
+ },
87
+ {
88
+ "date": "2015-02-24T19:04:00.174457",
89
+ "user": 1,
90
+ "text": "Another coment",
91
+ "node": {
92
+ "id": 620,
93
+ "version": {
94
+ "id": 104,
95
+ "project": {
96
+ "id": 2,
97
+ "name": "Kong",
98
+ "slug": "woot-kong",
99
+ "description": "",
100
+ "language": "en",
101
+ "repo": "http://github.com/ericholscher/django-kong",
102
+ "repo_type": "git",
103
+ "default_version": "latest",
104
+ "default_branch": null,
105
+ "documentation_type": "sphinx",
106
+ "users": [
107
+ 1
108
+ ]
109
+ },
110
+ "slug": "latest",
111
+ "identifier": "master",
112
+ "verbose_name": "latest",
113
+ "active": true,
114
+ "built": true,
115
+ "downloads": "{'htmlzip': '//localhost:8000/projects/woot-kong/downloads/htmlzip/latest/', 'epub': '//localhost:8000/projects/woot-kong/downloads/epub/latest/'}"
116
+ },
117
+ "current_hash": "nil-1a8ea4600ed6309a4a13a8b18380606b81485927d07247544270cecaec34c509",
118
+ "last_commit": "fac9e2868d8fe86c2e7fcc3d4f02a43fe015f9b3",
119
+ "snapshots_count": "1",
120
+ "page": "index",
121
+ "project": 2
122
+ }
123
+ }
124
+ ]
125
+ }
126
+ """
43
127
44
128
class UUIDTranslator (HTMLTranslator ):
45
129
@@ -51,6 +135,7 @@ class UUIDTranslator(HTMLTranslator):
51
135
def __init__ (self , builder , * args , ** kwargs ):
52
136
HTMLTranslator .__init__ (self , builder , * args , ** kwargs )
53
137
self .comment_class = 'sphinx-has-comment'
138
+ self .metadata = json .loads (kong_comments )['results' ]
54
139
55
140
def dispatch_visit (self , node ):
56
141
if is_commentable (node ):
@@ -61,13 +146,19 @@ def handle_visit_commentable(self, node):
61
146
# We will place the node in the HTML id attribute. If the node
62
147
# already has an id (for indexing purposes) put an empty
63
148
# span with the existing id directly before this node's HTML.
64
- self .update_hash (node , builder = self .builder )
149
+ hash_digest = self .update_hash (node , builder = self .builder )
65
150
if node .attributes ['ids' ]:
66
151
self .body .append ('<span id="%s"></span>'
67
152
% node .attributes ['ids' ][0 ])
68
- node .attributes ['ids' ] = ['%s' % hasher . hash_node ( node ) ]
153
+ node .attributes ['ids' ] = ['%s' % hash_digest ]
69
154
node .attributes ['classes' ].append (self .comment_class )
70
155
156
+ for obj in self .metadata :
157
+ if obj ['node' ]['current_hash' ] == hash_digest :
158
+ print "ADDING COMMEWNT"
159
+ comment = "[COMMENT] %s: %s" % (obj ['user' ], obj ['text' ])
160
+ node .insert (1 , nodes .paragraph (comment , comment ))
161
+
71
162
def update_hash (self , node , builder ):
72
163
"""
73
164
Take a node and compare it against existing hashes for this page.
@@ -79,11 +170,11 @@ def update_hash(self, node, builder):
79
170
if hash_digest not in hash_list :
80
171
match = hasher .compare_hash (hash_obj , hash_list )
81
172
if match :
82
- resp = builder .storage .update_node (old_hash = match , new_hash = hasher .hash_node (node ), commit = 'foobar' )
83
- return resp
173
+ builder .storage .update_node (old_hash = match , new_hash = hasher .hash_node (node ), commit = 'foobar' )
174
+ return hash_digest
84
175
# else:
85
176
# resp = builder.storage.add_node(id=hasher.hash_node(node),
86
177
# document=builder.current_docname,
87
178
# source=node.rawsource or node.astext())
88
179
89
- return None
180
+ return hash_digest
0 commit comments