Skip to content

Commit 0b05264

Browse files
authored
Merge pull request #265 from sogaiu/expose-example-filename-converter
Expose example filename creation via script
2 parents 125c160 + 442644c commit 0b05264

File tree

3 files changed

+53
-19
lines changed

3 files changed

+53
-19
lines changed

README.md

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,34 @@ documentation tool, but of course is written in and is a dialect of Janet. See
4242

4343
## Adding Examples
4444

45-
Simply add a file with the name of the binding you are giving examples for to the examples
46-
directory, with the `.janet` suffix. If the binding includes the `/` character, replace it with
47-
an underscore - this works because no bindings in the core use an underscore. For example, the
48-
binding `array/new` has examples in the `examples/array_new.janet` file.
49-
50-
If such a file already exists, you can simply append your example code the existing file.
51-
52-
When building the site, the new examples will be included in the generated documentation. Make
53-
sure that your example has correct janet syntax, as syntax errors will cause the entire site
54-
to not build. If the example has valid syntax (has a 0 exit code when loaded with
55-
`janet -k example/my-fn.janet`), there may be a bug in the mendoza janet syntax
56-
highlighter and you open a bug in mendoza.
45+
Simply add a file with the name of the binding you are giving examples
46+
for to the `examples` directory, with the `.janet` suffix.
47+
48+
To cope with some of Janet's symbols having names with characters that
49+
are not-so-friendly to certain filesystem and/or operating system
50+
combinations, an escaping scheme is used.
51+
52+
For a given symbol, use the `content/api/examples.janet` script to
53+
generate an appropriate filename. For example, for `array/new`,
54+
invoking:
55+
56+
```
57+
$ janet content/api/examples.janet array/new
58+
```
59+
60+
should give the output:
61+
62+
```
63+
array_47new.janet
64+
```
65+
66+
If such a file already exists, you can simply append your example code
67+
to the existing file.
68+
69+
When building the site, the new examples will be included in the
70+
generated documentation. Make sure that your example has correct janet
71+
syntax, as syntax errors will cause the entire site to not build. If
72+
the example has valid syntax (has a 0 exit code when loaded with
73+
`janet -k example/my-fn.janet`), there may be a bug in the mendoza
74+
janet syntax highlighter in which case please open a bug in mendoza.
75+

content/api/examples.janet

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
(def replacer
2+
(peg/compile
3+
~(% (any (+ (/ '(set "%*/:<>?")
4+
,|(string "_" (0 $))) '1)))))
5+
6+
(defn sym-to-filename
7+
``
8+
Convert a symbol to a filename. Certain filenames are not allowed on
9+
various operating systems.
10+
``
11+
[fname]
12+
(string "examples/" ((peg/match replacer fname) 0) ".janet"))
13+
14+
(defn main
15+
[& args]
16+
(def symbol-name (get args 1))
17+
(assert symbol-name "please specify a symbol name")
18+
(print (string/slice (sym-to-filename symbol-name)
19+
(length "examples/"))))
20+

content/api/gen-docs.janet

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Generate documentation
22

33
(import mendoza/markup-env :as mdz)
4+
(import ./examples :as ex)
45

56
(defn- remove-extra-spaces
67
"Many docstrings have extra spaces that don't look
@@ -11,17 +12,11 @@
1112
(set ret (string/replace " " " " ret)))
1213
ret)
1314

14-
(def- replacer (peg/compile ~(% (any (+ (/ '(set "%*/:<>?") ,|(string "_" (0 $))) '1)))))
15-
(defn- sym-to-filename
16-
"Convert a symbol to a filename. Certain filenames are not allowed on various operating systems."
17-
[fname]
18-
(string "examples/" ((peg/match replacer fname) 0) ".janet"))
19-
2015
(defn- check-example
2116
"Check if a binding has related examples. If so, load them
2217
and get their content."
2318
[sym]
24-
(def path (sym-to-filename sym))
19+
(def path (ex/sym-to-filename sym))
2520
(when (= :file (os/stat path :mode))
2621
(def src (slurp path))
2722
src))

0 commit comments

Comments
 (0)