v / cmd / tools / vdoc / testdata / output_formats / main.ansi
383 lines · 326 sloc · 10.36 KB · eb1d47b8c694a7eaf9ce5707777865771f5d3965
Raw
1Description
2This is an example of a an .md file, used for adding more rich text
3documentation in a project or module.
4This is a link to the main V site.
5This is a bold text.
6This is a script <script>console.log('hi from README.md');</script> .
7Examples
8Functions that return different literals:
9Example of a function returning boolean:
10fn is_odd(x int) bool {
11 if x % 2 == 0 {
12 return false
13 }
14 return true
15}
16
17Another example of a function returning a string:
18fn answer() string {
19 return '42'
20}
21
22This example shows a function returning a string with interpolation:
23fn str_with_interplation() string {
24 return 'this string has ${42:6} interpolation in it.'
25}
26
27Processing command line args
28import os
29
30fn main() {
31 dump(os.args)
32 dump(os.args.len)
33 assert os.args.len > 0
34
35 // Test escape characters like for `&` and `<`
36 mut arr := [1, 2, 3]
37 mut ref := &arr
38 arr << 4
39
40 ch := chan bool{cap: 1}
41 ch <- true
42}
43
44A JWT example (test syntax highlighting)
45import crypto.hmac
46import crypto.sha256
47import encoding.base64
48import json
49import time
50
51struct JwtHeader {
52 alg string
53 typ string
54}
55
56struct JwtPayload {
57 sub string
58 name string
59 iat int
60}
61
62fn main() {
63 sw := time.new_stopwatch()
64 secret := 'your-256-bit-secret'
65 token := make_token(secret)
66 ok := auth_verify(secret, token)
67 dt := sw.elapsed().microseconds()
68 println('token: ${token}')
69 println('auth_verify(secret, token): ${ok}')
70 println('Elapsed time: ${dt} uS')
71}
72
73fn make_token(secret string) string {
74 header := base64.url_encode(json.encode(JwtHeader{'HS256', 'JWT'}).bytes())
75 payload := base64.url_encode(json.encode(JwtPayload{'1234567890', 'John Doe', 1516239022}).bytes())
76 signature := base64.url_encode(hmac.new(secret.bytes(), '${header}.${payload}'.bytes(),
77 sha256.sum, sha256.block_size))
78 jwt := '${header}.${payload}.${signature}'
79 return jwt
80}
81
82fn auth_verify(secret string, token string) bool {
83 token_split := token.split('.')
84 signature_mirror := hmac.new(secret.bytes(), '${token_split[0]}.${token_split[1]}'.bytes(),
85 sha256.sum, sha256.block_size)
86 signature_from_token := base64.url_decode(token_split[2])
87 return hmac.equal(signature_from_token, signature_mirror)
88}
89
90Other language specifiers
91#include <iostream>
92#include <map>
93
94std::map<std::string, int> my_map {
95 {"KEY_1", 0},
96 {"KEY_2", 10},
97};
98
99for (const auto &[key, value] : my_map) {
100 std::cout << key << ": " << value << ", ";
101}
102std::cout << "\n";
103
104doc1 := toml.parse_text(<string content>) or { panic(err) }
105doc2 := toml.parse_file(<file path>) or { panic(err) }
106
107Escape html in strings
108const html = '<!DOCTYPE html>
109<html lang="en">
110 <head>
111 <style>
112 body {
113 background: linear-gradient(to right, #274060, #1B2845);
114 color: GhostWhite;
115 font-family: sans-serif;
116 text-align: center;
117 }
118 </style>
119 </head>
120 <body>
121 <h1>Your App Content!</h1>
122 <button onclick="callV()">Call V!</button>
123 </body>
124 <script>
125 async function callV() {
126 // Call a V function that takes an argument and returns a value.
127 const res = await window.my_v_func(\'Hello from JS!\');
128 console.log(res);
129 }
130 </script>
131</html>'
132
133Regular markdown list point 1
134List point 2
135List point 3
136
137Numbered markdown list point 1
138List point 2
139List point 3
140
141A code block without a specific language should be rendered verbatim:
142.
143├── static/
144│ ├── css/
145│ │ └── main.css
146│ └── js/
147│ └── main.js
148└── main.v
149
150The s tags here in the code block, should be rendered verbatim, not interpreted as HTML ones:
151h:m:s // 5:02:33
152m:s.mi<s> // 2:33.015
153s.mi<s> // 33.015s
154mi.mc<ms> // 15.007ms
155mc.ns<ns> // 7.234us
156ns<ns> // 234ns
157
158The End.
159module main
160 ## Description
161
162 This is an example of a an .md file, used for adding more rich text
163 documentation in a project or module.
164
165 This is a [link](https://vlang.io/) to the main V site.
166
167 This is a <b>bold text</b>.
168
169 This is a script `<script>console.log('hi from README.md');</script>` .
170
171 ## Examples
172
173 ### Functions that return different literals:
174
175 Example of a function returning boolean:
176 ```v
177 fn is_odd(x int) bool {
178 if x % 2 == 0 {
179 return false
180 }
181 return true
182 }
183 ```
184
185 Another example of a function returning a string:
186 ```v
187 fn answer() string {
188 return '42'
189 }
190 ```
191
192 This example shows a function returning a string with interpolation:
193 ```v
194 fn str_with_interplation() string {
195 return 'this string has ${42:6} interpolation in it.'
196 }
197 ```
198
199 ### Processing command line args
200
201 ```v
202 import os
203
204 fn main() {
205 dump(os.args)
206 dump(os.args.len)
207 assert os.args.len > 0
208
209 // Test escape characters like for `&` and `<`
210 mut arr := [1, 2, 3]
211 mut ref := &arr
212 arr << 4
213
214 ch := chan bool{cap: 1}
215 ch <- true
216 }
217 ```
218
219 ### A JWT example (test syntax highlighting)
220
221 ```v
222 import crypto.hmac
223 import crypto.sha256
224 import encoding.base64
225 import json
226 import time
227
228 struct JwtHeader {
229 alg string
230 typ string
231 }
232
233 struct JwtPayload {
234 sub string
235 name string
236 iat int
237 }
238
239 fn main() {
240 sw := time.new_stopwatch()
241 secret := 'your-256-bit-secret'
242 token := make_token(secret)
243 ok := auth_verify(secret, token)
244 dt := sw.elapsed().microseconds()
245 println('token: ${token}')
246 println('auth_verify(secret, token): ${ok}')
247 println('Elapsed time: ${dt} uS')
248 }
249
250 fn make_token(secret string) string {
251 header := base64.url_encode(json.encode(JwtHeader{'HS256', 'JWT'}).bytes())
252 payload := base64.url_encode(json.encode(JwtPayload{'1234567890', 'John Doe', 1516239022}).bytes())
253 signature := base64.url_encode(hmac.new(secret.bytes(), '${header}.${payload}'.bytes(),
254 sha256.sum, sha256.block_size))
255 jwt := '${header}.${payload}.${signature}'
256 return jwt
257 }
258
259 fn auth_verify(secret string, token string) bool {
260 token_split := token.split('.')
261 signature_mirror := hmac.new(secret.bytes(), '${token_split[0]}.${token_split[1]}'.bytes(),
262 sha256.sum, sha256.block_size)
263 signature_from_token := base64.url_decode(token_split[2])
264 return hmac.equal(signature_from_token, signature_mirror)
265 }
266 ```
267
268 ### Other language specifiers
269
270 ```cpp
271 #include <iostream>
272 #include <map>
273
274 std::map<std::string, int> my_map {
275 {"KEY_1", 0},
276 {"KEY_2", 10},
277 };
278
279 for (const auto &[key, value] : my_map) {
280 std::cout << key << ": " << value << ", ";
281 }
282 std::cout << "\n";
283 ```
284
285 ```v ignore
286 doc1 := toml.parse_text(<string content>) or { panic(err) }
287 doc2 := toml.parse_file(<file path>) or { panic(err) }
288 ```
289
290 ### Escape html in strings
291
292 ```v
293 const html = '<!DOCTYPE html>
294 <html lang="en">
295 <head>
296 <style>
297 body {
298 background: linear-gradient(to right, #274060, #1B2845);
299 color: GhostWhite;
300 font-family: sans-serif;
301 text-align: center;
302 }
303 </style>
304 </head>
305 <body>
306 <h1>Your App Content!</h1>
307 <button onclick="callV()">Call V!</button>
308 </body>
309 <script>
310 async function callV() {
311 // Call a V function that takes an argument and returns a value.
312 const res = await window.my_v_func(\'Hello from JS!\');
313 console.log(res);
314 }
315 </script>
316 </html>'
317 ```
318
319 - Regular markdown list point 1
320 - List point 2
321 - List point 3
322
323 1. Numbered markdown list point 1
324 2. List point 2
325 3. List point 3
326
327 A code block without a specific language should be rendered verbatim:
328 ```
329 .
330 ├── static/
331 │ ├── css/
332 │ │ └── main.css
333 │ └── js/
334 │ └── main.js
335 └── main.v
336 ```
337
338 The s tags here in the code block, should be rendered verbatim, not interpreted as HTML ones:
339 ```
340 h:m:s // 5:02:33
341 m:s.mi<s> // 2:33.015
342 s.mi<s> // 33.015s
343 mi.mc<ms> // 15.007ms
344 mc.ns<ns> // 7.234us
345 ns<ns> // 234ns
346 ```
347
348 The End.
349
350
351const omega = 3 // should be first
352const alpha = 5 // should be in the middle
353const beta = 2 // should be at the end
354fn abc()
355 abc - should be last
356fn def()
357 def - should be first
358fn xyz()
359 xyz - should be in the middle a small script <script>console.log('hello');</script> bold text <b>bold</b> end underlined text <u>underline</u> end a link [main v repo](https://github.com/vlang/v)
360fn MyXMLDocument.abc(text string) ?(string, int)
361 MyXMLDocument.abc does something too... I just do not know what.
362fn MyXMLDocument.from_file(path string) !MyXMLDocument
363 MyXMLDocument.from_text processes the file path, and returns an error
364fn MyXMLDocument.from_text(text string) ?MyXMLDocument
365 MyXMLDocument.from_text processes text and produces none
366struct MyXMLDocument {
367 path string
368}
369 MyXMLDocument is here just to test the different combinations of methods/output types
370fn (x &MyXMLDocument) instance_from_file(path string) !MyXMLDocument
371 instance_from_file does stuff with path
372fn (x &MyXMLDocument) instance_from_text(text string) ?MyXMLDocument
373 instance_from_text does stuff with text
374fn (x &MyXMLDocument) instance_abc(text string) ?(string, int)
375 instance_abc does stuff too
376fn (x &MyXMLDocument) instance_void()
377 instance_void does stuff too
378fn (x &MyXMLDocument) instance_int() int
379 instance_int does stuff too
380fn (x &MyXMLDocument) instance_result() !
381 instance_error does stuff too
382fn (x &MyXMLDocument) instance_option() ?
383 instance_option does stuff too
384