• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1window.initStyleGuide = function(init) {
2  // Runs the callback on every element matched by the query selector.
3  function find(querySelector, callback) {
4    var elements = [].slice.call(document.querySelectorAll(querySelector));
5    for (var i = 0; i < elements.length; i++) {
6      callback(elements[i]);
7    }
8  }
9  // Add the tocDiv at the top.
10  var title = document.getElementsByTagName('h1')[0];
11  var toc = document.createElement('div');
12  toc.id = 'tocDiv';
13  toc.className = 'vertical_toc';
14  title.parentNode.insertBefore(toc, title.nextSibling);
15
16  // If a paragraph starts with (e.g.) "Note:" or "Tip:" then add
17  // that "callout class" to its element.
18  find('p', function(paragraph) {
19    var match = /^([a-z]+):/i.exec(paragraph.textContent);
20    if (match) {
21      paragraph.classList.add(match[1].toLowerCase());
22    }
23  });
24
25  // Fill in text for intra-document links, ensuring that links
26  // remain up-to-date even if sections are moved or renumbered.
27  // This triggers on any link with "??" as its text and a URL
28  // starting with "#", and the filled-in text is exactly the same
29  // as the text of the referenced section heading.
30  find('a[href^="#"]', function(link) {
31    var href = link.getAttribute('href');
32    var heading = document.getElementById(href.substring(1));
33    // Fill in link text with heading title
34    if (heading && link.textContent == '??') {
35      link.textContent = heading.textContent;
36    }
37  });
38
39  // Hoedown renders fenced code blocks incompatibly with what
40  // prettify expects. As a result, prettify doesn't handle them
41  // properly. Fix it by moving the code directly into the pre.
42  find('pre > code', function(code) {
43    var pre = code.parentElement;
44    pre.className = code.className;
45    pre.innerHTML = code.innerHTML;
46  });
47
48  // Run the normal init function.
49  init();
50
51  // Call the pretty-printer after we've fixed up the code blocks.
52  var pretty = document.createElement('script');
53  pretty.src = 'https://cdn.rawgit.com/google/code-prettify/master/loader/' +
54      'run_prettify.js';
55  document.body.appendChild(pretty);
56}.bind(null, window.initStyleGuide);
57