UNPKG

1.8 kBJavaScriptView Raw
1/**
2 * Get the tags and code out of the code text.
3 *
4 * parseCodeText('@example\nhello')
5 * => { tag: 'example', code: 'hello' }
6 *
7 * parseCodeText('hello')
8 * => { tag: null, code: 'hello' }
9 */
10
11exports.parseCodeText = function (code) {
12 var m = code.trim().match(/^@([^\n]+)/);
13
14 if (m) return { tag: m[1], code: code.substr(m[1].length+2) };
15 return { tag: null, code: code };
16};
17
18/**
19 * Parse tags
20 */
21
22exports.parseTags = function (str) {
23 if (typeof str !== 'string') return {};
24
25 var m;
26 var obj = {};
27 str = str.trim();
28
29 while (true) {
30 if (m = str.match(/^\.([a-z\-]+)\s*/)) {
31 if (!obj["class"]) obj["class"] = [];
32 obj["class"].push(m[1]);
33 } else if (m = str.match(/^([a-z\-]+)="([^"]+)"\s*/)) {
34 obj[m[1]] = m[2];
35 } else if (m = str.match(/^([a-z\-]+)='([^']+)'\s*/)) {
36 obj[m[1]] = m[2];
37 } else if (m = str.match(/^([a-z\-]+)=([^\s]+)\s*/)) {
38 obj[m[1]] = m[2];
39 } else if (m = str.match(/^([a-z\-]+)\s*/)) {
40 obj[m[1]] = true;
41 } else {
42 if (obj["class"]) obj["class"] = obj["class"].join(' ');
43 return obj;
44 }
45
46 // Trim
47 str = str.substr(m[0].length);
48 }
49};
50
51/**
52 * Prefixes classnames.
53 *
54 * prefixClass('white', 'sg') => 'sg-white'
55 * prefixClass('pad dark', 'sg') => 'sg-pad sg-dark'
56 */
57
58exports.prefixClass = function (klass, prefix) {
59 return klass.split(' ').map(function (n) {
60 return n.length > 0 ? (prefix + '-' + n) : n;
61 }).join(' ');
62};
63
64/**
65 * Processes a block of text as either HTML or Jade.
66 */
67
68exports.htmlize = function (src) {
69 // Mdconf processes them as arrays
70 if (src.constructor === Array) src = src[0];
71
72 if (src.substr(0, 1) === '<') {
73 return src;
74 } else {
75 var Jade = require('jade/lib/jade');
76 return Jade.render(src);
77 }
78};