-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMarkdown.py
executable file
·86 lines (69 loc) · 2.22 KB
/
Markdown.py
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
84
85
86
#!/usr/bin/env python3
# encoding: utf-8
from Alfred import Tools
import html
import os
import re
import urllib.request
import urllib.error
import urllib.parse
class Markdown(object):
PANDOC = 'pandoc -f html-native_divs-native_spans -t gfm --strip-comments --markdown-headings=atx '
def __init__(self, url):
self.url = url
self.html = self._fetchHtml().decode('utf-8')
self.md = self._fetchMd()
def _fetchHtml(self):
try:
r = urllib.request.urlopen(self.url)
response = r.read()
except:
response = "<html><body><a href=\"" + self.url + "\">" + self.url + "</a></body></html>"
pass
return response
def _fetchMd(self):
try:
cmd = '{0} {1}'.format(self.PANDOC, self.url)
md = os.popen(cmd)
resp = md.read()
except:
resp = "[{0}]({0})".format(self.url)
pass
return resp
@staticmethod
def _htmlDecode(string):
string = urllib.parse.unquote(string)
# return string
return html.unescape(string)
def _markdownHeader(self):
return "---\n" \
"Title: {title}\n" \
"Created: {date}\n" \
"Tags: #WebClip\n" \
"Url: {url}\n" \
"---\n".format(date=Tools.getTodayDate(), url=self.getMdUrl(), title=self.getTitle())
def getHtml(self):
return self.html
def getMarkdownContent(self):
out = self._markdownHeader()
out += self.getMd()
return out
def getMd(self):
return self.md
def getMdUrl(self):
page_url = "[{0}]({1})".format(self.getTitle(), self.getUrl())
return page_url
def getTitle(self):
res = re.findall(r'<title>[\n\t\s]*(.+)[\n\t\s]*</title>', self.html, re.MULTILINE)
return self._htmlDecode(''.join(res))
def getUrl(self):
return self.url
def parseFilename(self, filename):
to_replace = ['/', '\\', ':']
tmp = filename.strip()
for i in to_replace:
tmp = tmp.replace(i, '-')
return tmp
def writeMarkdown(self, content, path):
with open(path, "w+") as file:
file.write(content)