1 | var extend = require('util')._extend;
|
2 | var parseCodeText = require('./utils').parseCodeText;
|
3 | var parseTags = require('./utils').parseTags;
|
4 | var prefixClass = require('./utils').prefixClass;
|
5 | var htmlize = require('./utils').htmlize;
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | exports.addClasses = function ($, pre) {
|
12 | $("*").addClass(pre());
|
13 | };
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | exports.sectionize = function ($, tag, pre, options) {
|
22 | options = extend({
|
23 | 'class': '',
|
24 | 'until': 'h1, h2, h3, section',
|
25 | }, options);
|
26 |
|
27 | $(tag).each(function (i) {
|
28 | var $heading = $(this);
|
29 | var $extras = $heading.nextUntil(options.until);
|
30 | $heading.before("<section class='"+options.class+"'>");
|
31 |
|
32 | var $div = $("section."+options.class).eq(-1);
|
33 | $div.addClass(pre('section-' + $heading.attr('id')));
|
34 | $div.append($heading.remove());
|
35 | $div.append($extras.remove());
|
36 | });
|
37 | };
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | exports.unpackExample = function (parent, pre, highlight) {
|
44 | var Cheerio = require('cheerio');
|
45 | var code = parent.text();
|
46 | var block = parseCodeText(code);
|
47 | var tags = parseTags(block.tag);
|
48 |
|
49 | if (tags.example) {
|
50 | var html = htmlize(block.code);
|
51 | var canvas = "<div class='"+pre('canvas')+"'>"+html+"</div>";
|
52 | var codeblock = "<pre class='"+pre('code')+"'>"+highlight(html)+"</pre>";
|
53 | var $block = Cheerio.load("<div class='"+pre('example')+"'>" + canvas + codeblock + "</div>");
|
54 |
|
55 | if (tags['class']) {
|
56 | klass = pre(tags['class']);
|
57 | $block(':root').addClass(klass);
|
58 | }
|
59 |
|
60 | parent.after($block.root());
|
61 | parent.remove();
|
62 | } else {
|
63 | klass = parent.find('code').attr('class');
|
64 | var m = klass.match(/lang-([a-z]+)/);
|
65 |
|
66 | if (m) {
|
67 | var lang = m[1];
|
68 | var Hljs = require('highlight.js');
|
69 | parent.html(Hljs.highlight(lang, parent.text()).value);
|
70 | parent.addClass(pre('lang-'+lang));
|
71 | parent.addClass(pre('code'));
|
72 | }
|
73 | }
|
74 | };
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 | exports.removeConfig = function ($) {
|
83 | var $h1 = $('h1#styleguide-options');
|
84 | $h1.nextUntil('h1').remove();
|
85 | $h1.remove();
|
86 | };
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 | exports.processConfig = function (src, options) {
|
93 | var Mdconf = require('./mdconf');
|
94 | try {
|
95 | var data = Mdconf(src, { normalizer: 'camelcase' });
|
96 | data = (data && data.styleguideOptions);
|
97 |
|
98 | if (data) extend(options, data);
|
99 | } catch (e) {
|
100 |
|
101 | }
|
102 | };
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 | exports.isolateTextBlocks = function ($, pre) {
|
109 | var Cheerio = require('cheerio');
|
110 |
|
111 | $('.'+pre('block')).each(function() {
|
112 | var $this = $(this);
|
113 |
|
114 |
|
115 | var noExample = ($this.html().indexOf(pre('example')) === -1);
|
116 | var noCode = ($this.html().indexOf(pre('code')) === -1);
|
117 | if (noExample && noCode) return;
|
118 |
|
119 | var $first = $('h3', this);
|
120 | var $text = $first.nextUntil('.'+pre('example')+', .'+pre('code'));
|
121 |
|
122 | var $block = Cheerio.load('<div>');
|
123 | $this.prepend($block.root());
|
124 |
|
125 | $block(':root').addClass(pre('text'));
|
126 | $block(':root').append($first);
|
127 | $block(':root').append($text);
|
128 | });
|
129 | };
|