-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.html
179 lines (128 loc) · 6.5 KB
/
parse.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<!DOCTYPE html>
<html>
<head>
<title>parse.js</title>
<meta charset="UTF-8">
<link rel="stylesheet" media="all" href="public/stylesheets/normalize.css" />
<link rel="stylesheet" media="all" href="docs/docco.css" />
</head>
<body>
<div class="container">
<div class="page">
<div class="header">
<h1>parse.js</h1>
<div class="toc">
<h3>Table of Contents</h3>
<ol>
<li>
<a class="source" href="cli.html">
lib/cli.js
</a>
</li>
<li>
<a class="source" href="configure.html">
lib/configure.js
</a>
</li>
<li>
<a class="source" href="defaults.html">
lib/defaults.js
</a>
</li>
<li>
<a class="source" href="docco.html">
lib/docco.js
</a>
</li>
<li>
<a class="source" href="document.html">
lib/document.js
</a>
</li>
<li>
<a class="source" href="format.html">
lib/format.js
</a>
</li>
<li>
<a class="source" href="index.html">
lib/index.js
</a>
</li>
<li>
<a class="source" href="languages.html">
lib/languages.js
</a>
</li>
<li>
<a class="source" href="parse.html">
lib/parse.js
</a>
</li>
<li>
<a class="source" href="write.html">
lib/write.js
</a>
</li>
</ol>
</div>
</div>
<div class='highlight'><pre><span class="hljs-keyword">import</span> {getLanguage} <span class="hljs-keyword">from</span> <span class="hljs-string">"./languages"</span>;</pre></div>
<p>Given a string of source code, <strong>parse</strong> out each block of prose and the code that
follows it — by detecting which is which, line by line — and then create an
individual <strong>section</strong> for it. Each section is an object with <code>docsText</code> and
<code>codeText</code> properties, and eventually <code>docsHtml</code> and <code>codeHtml</code> as well.</p>
<div class='highlight'><pre><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> parse = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">source, code, config = {}</span>) </span>{
<span class="hljs-keyword">const</span> sections = [];
<span class="hljs-keyword">const</span> lang = getLanguage(source, config);
<span class="hljs-keyword">let</span> codeText, docsText;
<span class="hljs-keyword">let</span> hasCode = docsText = codeText = <span class="hljs-string">""</span>;
<span class="hljs-keyword">let</span> lines = code.split(<span class="hljs-string">"\n"</span>);
<span class="hljs-keyword">const</span> save = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
sections.push({docsText, codeText});
hasCode = docsText = codeText = <span class="hljs-string">""</span>;
};</pre></div>
<p>Our quick-and-dirty implementation of the literate programming style. Simply
invert the prose and code relationship on a per-line basis, and then continue as
normal below.</p>
<p>sections</p>
<div class='highlight'><pre> <span class="hljs-keyword">if</span> (lang.literate) {
<span class="hljs-keyword">const</span> litRegex = <span class="hljs-regexp">/^([ ]{4}|[ ]{0,3}\t)/</span>;
<span class="hljs-keyword">const</span> searchRegex = <span class="hljs-regexp">/^\s*$/</span>;
<span class="hljs-keyword">let</span> isText = <span class="hljs-literal">true</span>;
lines = lines.map(<span class="hljs-function">(<span class="hljs-params">line</span>) =></span> {
<span class="hljs-keyword">const</span> match = litRegex.exec(line);
<span class="hljs-keyword">if</span> (match) {
isText = <span class="hljs-literal">false</span>;
<span class="hljs-keyword">return</span> line.slice(match[<span class="hljs-number">0</span>].length);
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (line.search(searchRegex)) {
<span class="hljs-keyword">return</span> isText ? lang.symbol : <span class="hljs-string">""</span>;
}
isText = <span class="hljs-literal">true</span>;
<span class="hljs-keyword">return</span> <span class="hljs-string">`<span class="hljs-subst">${lang.symbol }</span> <span class="hljs-subst">${ line}</span>`</span>;
});
}
<span class="hljs-keyword">const</span> docsRegex = <span class="hljs-regexp">/^(---+|===+)$/</span>;
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> line <span class="hljs-keyword">of</span> lines) {
<span class="hljs-keyword">if</span> (line && line.match(lang.commentMatcher) && !line.match(lang.commentFilter)) {
<span class="hljs-keyword">if</span> (hasCode) {
save();
}
docsText += <span class="hljs-string">`<span class="hljs-subst">${line.replace(lang.commentMatcher, <span class="hljs-string">""</span>)}</span>\n`</span>;
<span class="hljs-keyword">if</span> (docsRegex.test(line)) {
save();
}
} <span class="hljs-keyword">else</span> {
hasCode = <span class="hljs-literal">true</span>;
codeText += <span class="hljs-string">`<span class="hljs-subst">${line }</span>\n`</span>;
}
}
save();
<span class="hljs-keyword">return</span> sections;
};
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> parse;</pre></div>
<div class="fleur">h</div>
</div>
</div>
</body>
</html>