1 | ```javascript
|
2 | var html = require('commonform-html')
|
3 | ```
|
4 |
|
5 | Call the exported function with a Common Form, receive a string of HTML:
|
6 |
|
7 | ```javascript
|
8 | var assert = require('assert')
|
9 |
|
10 | assert.equal(
|
11 | html({content: ['Just a test']}),
|
12 | '<div class="article"><p>Just a test</p></div>'
|
13 | )
|
14 |
|
15 | assert.equal(
|
16 | html({
|
17 | content: [
|
18 | {
|
19 | heading: 'A',
|
20 | form: {content: ['This is A']}
|
21 | },
|
22 | {
|
23 | heading: 'B',
|
24 | form: {content: ['This is B']}
|
25 | }
|
26 | ]
|
27 | }),
|
28 | [
|
29 | '<div class="article">',
|
30 | '<div class="section">',
|
31 | '<h1>A</h1>',
|
32 | '<p>This is A</p>',
|
33 | '</div>',
|
34 | '<div class="section">',
|
35 | '<h1>B</h1>',
|
36 | '<p>This is B</p>',
|
37 | '</div>',
|
38 | '</div>'
|
39 | ]
|
40 | .join('')
|
41 | )
|
42 | ```
|
43 |
|
44 | You can also pass an `Array` of fill-in-the-blank values:
|
45 |
|
46 | ```javascript
|
47 | assert.equal(
|
48 | html(
|
49 | {
|
50 | content: [
|
51 | {blank: ''},
|
52 | {form: {content: ['Another ', {blank: ''}]}}
|
53 | ]
|
54 | },
|
55 | [
|
56 | {blank: ['content', 0], value: 'Joe'},
|
57 | {blank: ['content', 1, 'form', 'content', 1], value: 'Bob'}
|
58 | ]
|
59 | ),
|
60 | (
|
61 | '<div class="article">' +
|
62 | '<p><span class="blank">Joe</span></p>' +
|
63 | '<div class="section">' +
|
64 | '<p>Another <span class="blank">Bob</span></p>' +
|
65 | '</div>' +
|
66 | '</div>'
|
67 | )
|
68 | )
|
69 | ```
|
70 |
|
71 | A final argument of `{ html5: true }` specifies HTML5 output:
|
72 |
|
73 | ```javascript
|
74 | assert.equal(
|
75 | html({content: ['Just a test']}, [], {html5: true}),
|
76 | '<article><p>Just a test</p></article>'
|
77 | )
|
78 |
|
79 | assert.equal(
|
80 | html(
|
81 | {
|
82 | content: [
|
83 | 'First text defines a ',
|
84 | {definition: 'Term'},
|
85 | {
|
86 | heading: 'A',
|
87 | form: {content: ['This is A']}
|
88 | },
|
89 | 'Middle text uses a ',
|
90 | {use: 'Term'},
|
91 | {
|
92 | heading: 'B',
|
93 | form: {content: ['This is B']}
|
94 | },
|
95 | 'Last text references ',
|
96 | {reference: 'Elsewhere'}
|
97 | ]
|
98 | },
|
99 | [],
|
100 | {html5: true}
|
101 | ),
|
102 | [
|
103 | '<article>', // not <div class="article">
|
104 | '<p>',
|
105 | 'First text defines a ',
|
106 | '<dfn>Term</dfn>', // not <span class="definition">
|
107 | '</p>',
|
108 | '<section>', // not <div class="section">
|
109 | '<h1>A</h1>',
|
110 | '<p>This is A</p>',
|
111 | '</section>',
|
112 | '<p>',
|
113 | 'Middle text uses a ',
|
114 | '<span class="term">Term</span>',
|
115 | '</p>',
|
116 | '<section>',
|
117 | '<h1>B</h1>',
|
118 | '<p>This is B</p>',
|
119 | '</section>',
|
120 | '<p>',
|
121 | 'Last text references ',
|
122 | '<span class="reference">Elsewhere</span>',
|
123 | '</p>',
|
124 | '</article>'
|
125 | ]
|
126 | .join('')
|
127 | )
|
128 | ```
|
129 |
|
130 | The option `{ lists: true }` renders any series of forms without headings as an ordered list:
|
131 |
|
132 | ```javascript
|
133 | assert.equal(
|
134 | html(
|
135 | {
|
136 | content: [
|
137 | {
|
138 | heading: 'First-Level Heading',
|
139 | form: {
|
140 | content: [
|
141 | 'Here comes a list.',
|
142 | {form: {content: ['Apple']}},
|
143 | {form: {content: ['Orange']}},
|
144 | 'And another one!',
|
145 | {
|
146 | form: {
|
147 | content: [
|
148 | 'Red',
|
149 | {form: {content: ['Red-Orange']}},
|
150 | {form: {content: ['Red-Yellow']}}
|
151 | ]
|
152 | }
|
153 | },
|
154 | {form: {content: ['Purple']}},
|
155 | {
|
156 | form: {
|
157 | content: [
|
158 | {form: {content: ['More']}},
|
159 | {form: {content: ['Even More']}}
|
160 | ]
|
161 | }
|
162 | },
|
163 | 'Last text!'
|
164 | ]
|
165 | }
|
166 | }
|
167 | ]
|
168 | },
|
169 | [],
|
170 | {html5: true, lists: true}
|
171 | ),
|
172 | [
|
173 | '<article>',
|
174 | '<section>',
|
175 | '<h1>First-Level Heading</h1>',
|
176 | '<p>Here comes a list.</p>',
|
177 | '<ol>',
|
178 | '<li><p>Apple</p></li>',
|
179 | '<li><p>Orange</p></li>',
|
180 | '</ol>',
|
181 | '<p>And another one!</p>',
|
182 | '<ol>',
|
183 | '<li>',
|
184 | '<p>Red</p>',
|
185 | '<ol>',
|
186 | '<li><p>Red-Orange</p></li>',
|
187 | '<li><p>Red-Yellow</p></li>',
|
188 | '</ol>',
|
189 | '</li>',
|
190 | '<li><p>Purple</p></li>',
|
191 | '<li>',
|
192 | '<ol>',
|
193 | '<li><p>More</p></li>',
|
194 | '<li><p>Even More</p></li>',
|
195 | '</ol>',
|
196 | '</li>',
|
197 | '</ol>',
|
198 | '<p>Last text!</p>',
|
199 | '</section>',
|
200 | '</article>'
|
201 | ]
|
202 | .join('')
|
203 | )
|
204 | ```
|
205 |
|
206 | You can also set a title, edition, or both:
|
207 |
|
208 | ```javascript
|
209 | assert.equal(
|
210 | html(
|
211 | {content: ['Hello, ', {blank: ''}]},
|
212 | [{blank: ['content', 1], value: 'Joe'}],
|
213 | {title: 'Welcome'}
|
214 | ),
|
215 | [
|
216 | '<div class="article">',
|
217 | '<h1>Welcome</h1>',
|
218 | '<p>',
|
219 | 'Hello, <span class="blank">Joe</span>',
|
220 | '</p>',
|
221 | '</div>'
|
222 | ]
|
223 | .join('')
|
224 | )
|
225 |
|
226 | assert.equal(
|
227 | html(
|
228 | {content: ['Hello, ', {blank: ''}]},
|
229 | [{blank: ['content', 1], value: 'Joe'}],
|
230 | {title: 'Welcome', edition: '1e'}
|
231 | ),
|
232 | [
|
233 | '<div class="article">',
|
234 | '<h1>Welcome</h1>',
|
235 | '<p class="edition">1e</p>',
|
236 | '<p>',
|
237 | 'Hello, <span class="blank">Joe</span>',
|
238 | '</p>',
|
239 | '</div>'
|
240 | ]
|
241 | .join('')
|
242 | )
|
243 | ```
|
244 |
|
245 | Set `options.hash` to print the form hash at the top:
|
246 |
|
247 | ```javascript
|
248 | assert.equal(
|
249 | html(
|
250 | {content: ['Hello, ', {blank: ''}]},
|
251 | [{blank: ['content', 1], value: 'Joe'}],
|
252 | {title: 'Welcome', edition: '1e', hash: true}
|
253 | ),
|
254 | [
|
255 | '<div class="article">',
|
256 | '<h1>Welcome</h1>',
|
257 | '<p class="edition">1e</p>',
|
258 | '<p class="hash"><code>' +
|
259 | 'd36c54da27de611b3a9ce7d08638bbd2' +
|
260 | '00cf5f3bb41d59320d04bba02ca48f85' +
|
261 | '</code></p>',
|
262 | '<p>',
|
263 | 'Hello, <span class="blank">Joe</span>',
|
264 | '</p>',
|
265 | '</div>'
|
266 | ]
|
267 | .join('')
|
268 | )
|
269 | ```
|