Coverage for src/lcdoc/mkdocs/lp/plugs/python/pyplugs/comments/__init__.py: 90.32%

Shortcuts on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

27 statements  

1import os lp|index.md

2from functools import partial lp|index.md

3 

4from lcdoc.mkdocs.lp.plugs import python lp|index.md

5from lcdoc.mkdocs.tools import add_post_page_func, script lp|index.md

6 

7config, page, Session = (python.config, python.page, python.Session) lp|index.md

8 

9 

10dflts_comments = { 

11 # :docs:comments_defaults 

12 'theme': 'github-dark', # "github-light" "github-dark" "preferred-color-scheme" "github-dark-orange" "icy-dark" "dark-blue" "photon-dark" "boxy-light" 

13 'issue_term': 'pathname', # url, title, og:title (specific issue number and title not supported) 

14 # :docs:comments_defaults 

15} 

16 

17 

18T = '''<div id="utterance_comments"></div>''' lp|index.md

19gh = 'https://github.com/' lp|index.md

20marker = '<!--- utterance_comments -->' lp|index.md

21 

22 

23def add_fetcher_script(output, page, config, js=None, **kw): lp|index.md

24 return output.replace(marker, js) 

25 

26 

27reload_iframe = ''' 

28var scr = document.createElement('script'); 

29scr.setAttribute('src','https://utteranc.es/client.js'); 

30scr.setAttribute('repo','%(repo)s'); 

31scr.setAttribute('issue-term','%(issue_term)s'); 

32scr.setAttribute('theme','%(theme)s'); 

33scr.setAttribute('crossorigin','anonymous'); 

34document.getElementById('utterance_comments').appendChild(scr); 

35 

36''' 

37style = ''' 

38.utterances {max-width: 100% !important} 

39''' 

40# var cmt_el = document.getElementById('comments') 

41# if (typeof window.comment != "undefined") { 

42# cmt_el.innerHTML = window.comment 

43# } else { 

44# function wait_loaded (shot) { 

45# window.setTimeout(function() { 

46# if (document.getElementsByClassName('utterances-frame').length) { 

47# if (!shot) return wait_loaded(1); 

48# window.comment = document.getElementById('comments').innerHTML 

49# return 

50# } 

51# console.log('waiting for utterances comments...') 

52# return wait_loaded() 

53# }, 1000)} 

54# wait_loaded () 

55# } 

56 

57 

58def comments(s, **kw): lp|index.md

59 d = dict(dflts_comments) lp|index.md

60 d.update(kw) lp|index.md

61 repo = d.get('repo_url', config().get('repo_url')) lp|index.md

62 while repo[-1] == '/': lp|index.md

63 repo = repo[:-1] lp|index.md

64 if not gh in repo: 64 ↛ 65line 64 didn't jump to line 65, because the condition on line 64 was never truelp|index.md

65 python.app.die('comments only work with github', your_mkdocs_repo_url=repo) 

66 d['repo'] = repo.split(gh, 1)[1] lp|index.md

67 js = T lp|index.md

68 a = {'footer': {'script': reload_iframe % d, 'style': style}} lp|index.md

69 add_post_page_func(python.lpkw(), partial(add_fetcher_script, js=js)) lp|index.md

70 return {'nocache': True, 'res': marker, 'page_assets': {'comments': a}} lp|index.md

71 

72 

73register = {'comments': comments} lp|index.md