-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathhypermark.py
More file actions
65 lines (47 loc) · 1.5 KB
/
hypermark.py
File metadata and controls
65 lines (47 loc) · 1.5 KB
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
import hashlib
import os
import re
import hashlib
from copy import deepcopy
from markdown import markdown
GRUBER_URLINTEXT_PAT = re.compile(ur'(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?\xab\xbb\u201c\u201d\u2018\u2019]))')
def extract_links(text):
links = set()
for url in(mgroups[0] for mgroups in GRUBER_URLINTEXT_PAT.findall(text)):
if url.startswith('http'):
links.add(url)
return list(links)
class Hypertext(object):
def __init__(self):
self.state = {}
self.encoding = 'utf-8'
def __repr__(self):
return '<Hypertext {}>'.format(self.hash[:10])
@classmethod
def _from_text(cls, text):
# TODO: "Get" the text from the input. (e.g. convert it if not text.)
self = cls()
self.text = text
return self
@property
def text(self):
return self.__text
@text.setter
def text(self, value):
self.__text = value
@property
def content(self):
return self.text.encode(self.encoding)
@property
def links(self):
return extract_links(self.text)
@property
def hash(self):
return unicode(hashlib.sha1(self.content).hexdigest())
@property
def html(self):
return markdown(self.text)
def filter(self, **kwargs):
return deepcopy(self)
def text(content):
return Hypertext._from_text(content)