-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshortcodes.py
126 lines (88 loc) · 3.42 KB
/
shortcodes.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
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
117
118
119
120
121
122
123
124
125
from os.path import isfile
from pagegen.utility import appropriate_markup, write_file
from pagegen.constants import DIRDEFAULTFILE
from urllib.parse import urlparse
from shutil import copyfile
def source_link(site, page):
"""Copy source file and generate link to it. The actual source files are copied using post_generate_page hook"""
html = ''
if page.headers['menu exclude'] == False or 'home page' in page.custom_headers.keys():
url = page.url_path
if url.endswith('/'):
url += DIRDEFAULTFILE
html = '<br /><a href="' + url + '.txt" title="Page source" target="_blank"><i class="fas code-999999"></i></a>'
return html
def figure(site, page, caption, alternative_text, src_path):
"""Make html figure"""
html = '<figure>\n'
html += '<img src="' + src_path + '" alt="' + alternative_text + '">\n'
html += '<figcaption>' + caption + '</figcaption>\n'
html += '</figure>\n'
return appropriate_markup(page, html)
def list_shortcodes(site, page):
"""List built-in shortcodes"""
sc_built_in_whitelist = [
'image',
'menu',
'page_url',
'list_authors',
'list_posts',
'tags',
'categories',
]
scs = site.shortcodes.__repr__()
html = '<table><tr><th>Shortcode name</th><th>Description</th></tr>'
for sc in scs.splitlines():
for bsc in sc_built_in_whitelist:
if sc.startswith(bsc + '('):
html += '<tr><td>' + sc + '</td><td>' + site.shortcodes[bsc].__doc__ + '</td></tr>'
break
html += '</table>'
return appropriate_markup(page, html)
def series_navigation(site, page):
"""Navigation for series (related pages/blog posts)"""
if not page.headers['series']:
return ''
series_id = page.headers['series']
# Check if we have series html cached
if 'series' in site.shortcodes.cache.keys() and series_id in site.shortcodes.cache['series'].keys():
html = site.shortcodes.cache['series'][series_id]
else:
html = ''
# Find other pages in series
sorted_posts = sorted(site.page_list, key=lambda d: d.headers['publish'])
for p in sorted_posts:
if p.headers['series'] == series_id:
html += '<li><a href="' + p.url_path + '">' + p.title + '</a></li>'
html = '<ol>' + html + '</ol>'
# Cache html for use by other pages in series
site.shortcodes.cache['series'] = { series_id: html }
return appropriate_markup(page, html)
def social_links(site, page, **links):
"""List social links, try to guess font awesome icon to use with each link.
Example:
<sc>social_links(Facebook='https://www.facebook.com/oliverjfields', Github='https://github.com/olvfie')</sc>
Requires font-awesome link in html, e.g.
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/brands.min.css" integrity="sha512-OivR4OdSsE1onDm/i3J3Hpsm5GmOVvr9r49K3jJ0dnsxVzZgaOJ5MfxEAxCyGrzWozL9uJGKz6un3A7L+redIQ==" crossorigin="anonymous" referrerpolicy="no-referrer" />
"""
html = '<ul>'
domain_icon_map = {
'facebook.com': 'fa-facebook-f',
'twitter.com': 'fa-twitter',
#'linkedin.com': 'fa-linkedin-in',
'github.com': 'fa-github',
'instagram.com': 'fa-instagram',
}
for name, link in links.items():
try:
pl = urlparse(link)
domain = pl.hostname.lower()
domain = domain.replace('www.', '')
if domain in domain_icon_map.keys():
icon = '<i class="fa-brands ' + domain_icon_map[domain] + '"></i> '
else:
icon = ''
except:
icon = ''
html += '<li><a href="' + link + '">' + icon + name + '</a></li>'
return appropriate_markup(page, html + '</ul>')