MCPcopy Create free account
hub / github.com/PageBot/PageBot / write_head

Method write_head

Lib/pagebot/elements/page.py:512–623  ·  view source on GitHub ↗

Builds the page head. There are 3 option (all including the ... ): 1 As HTML string (info.headHtml is defined as not None) 2 As path a html file, containing the string between ... . 3 Constructed from info contect, page attributes and style

(self, view)

Source from the content-addressed store, hash-verified

510 self.write_body(view, path)
511
512 def write_head(self, view):
513 """Builds the page head. There are 3 option (all including the
514 <head>...</head>):
515
516 1 As HTML string (info.headHtml is defined as not None)
517 2 As path a html file, containing the string between <head>...</head>.
518 3 Constructed from info contect, page attributes and styles.
519 """
520 context = view.context
521 b = context.b
522
523 if self.headCode is not None:
524 b.addHtml(self.headCode)
525 elif self.headPath is not None:
526 b.importHtml(self.headPath) # Add HTML content of file, if path is not None and the file exists.
527 else:
528 b.head()
529
530 # Add Google Analytics if accounts numbers are defined.
531 if view.googleAdsAccount is not None and view.googleAnalyticsId is not None:
532 gaScript_XXX = """<!-- Global Site Tag (gtag.js) - Google Analytics -->
533 <script async src="https://www.googletagmanager.com/gtag/js?id=%(googleAnalyticsId)s"></script>
534 <script>
535 window.dataLayer = window.dataLayer || [];
536 function gtag(){dataLayer.push(arguments);}
537 gtag('js', new Date());
538
539 gtag('config', '%(googleAnalyticsId)s');
540 </script>
541 """ % dict(googleAnalyticsId=view.googleAnalyticsId)
542 gaScript = """
543 <script>
544 (function(i,s,o,g,r,a,m){
545 i['GoogleAnalyticsObject']=r;
546 i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},
547 i[r].l=1*new Date();
548 a=s.createElement(o),m=s.getElementsByTagName(o)[0];
549 a.async=1;
550 a.src=g;
551 m.parentNode.insertBefore(a,m)
552 })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
553
554 ga('create', '%(googleAnalyticsId)s', 'auto');
555 ga('send', 'pageview');
556 </script>
557 """ % dict(googleAnalyticsId=view.googleAnalyticsId)
558 b.addHtml(gaScript)
559
560 b.meta(charset=self.css('encoding')) # Default utf-8
561 # Try to find the page name, in sequence order of importance.
562 b.title_(self.title or self.name or self.url.split('/')[-1])
563
564 b.meta(httpequiv='X-UA-Compatible', content='IE=edge,chrome=1')
565
566 # Devices
567 if self.viewPort is not None: # Not supposed to be None. Check anyway
568 b.comment('Mobile viewport')
569 b.meta(name='viewport', content=self.viewPort)

Callers 1

write_htmlMethod · 0.95

Calls 12

addHtmlMethod · 0.80
importHtmlMethod · 0.80
headMethod · 0.80
metaMethod · 0.80
title_Method · 0.80
commentMethod · 0.80
linkMethod · 0.80
styleMethod · 0.80
_styleMethod · 0.80
_headMethod · 0.80
cssMethod · 0.45
build_cssMethod · 0.45

Tested by

no test coverage detected