1 | #!/usr/bin/env bash
|
2 | #
|
3 | # Usage:
|
4 | # build/doc.sh <function name>
|
5 |
|
6 | set -o nounset
|
7 | set -o pipefail
|
8 | set -o errexit
|
9 |
|
10 | # https://oilshell.org/release/$VERSION/
|
11 | # doc/
|
12 | # index.html
|
13 | # INSTALL.html
|
14 | # INSTALL-old.html
|
15 |
|
16 | readonly OIL_VERSION=$(head -n 1 oil-version.txt)
|
17 | export OIL_VERSION # for quick_ref.py
|
18 |
|
19 | THIS_DIR=$(readlink -f $(dirname $0))
|
20 | readonly THIS_DIR
|
21 | REPO_ROOT=$(cd $THIS_DIR/.. && pwd)
|
22 | readonly REPO_ROOT
|
23 |
|
24 |
|
25 | readonly HTML_BASE_DIR=_release/VERSION
|
26 |
|
27 |
|
28 | log() {
|
29 | echo "$@" 1>&2
|
30 | }
|
31 |
|
32 | #
|
33 | # Deps (similar to doctools/cmark.sh and build/codegen.sh)
|
34 | #
|
35 |
|
36 | readonly MANDOC_DIR='_deps/mdocml-1.14.1'
|
37 |
|
38 | download-mandoc() {
|
39 | mkdir -p _deps
|
40 | wget --no-clobber --directory _deps \
|
41 | https://mandoc.bsd.lv/snapshots/mdocml-1.14.1.tar.gz
|
42 | }
|
43 |
|
44 | build-mandoc() {
|
45 | cd $MANDOC_DIR
|
46 | ./configure
|
47 | make
|
48 | }
|
49 |
|
50 | mandoc() {
|
51 | $MANDOC_DIR/mandoc "$@"
|
52 | }
|
53 |
|
54 | # Places version is used
|
55 | #
|
56 | # - in --version
|
57 | # - in URL for every page? inside the binary
|
58 | # - in titles for index, install, osh-quick-ref TOC, etc.
|
59 | # - in deployment script
|
60 |
|
61 | # Run with environment variable
|
62 | help-gen() {
|
63 | PYTHONPATH=. doctools/help_gen.py "$@"
|
64 | }
|
65 |
|
66 | cmark() {
|
67 | # h2 and h3 are shown in TOC. The blog uses "legacy" h3 and h4.
|
68 | PYTHONPATH=. doctools/cmark.py --toc-tag h2 --toc-tag h3 --toc-pretty-href "$@"
|
69 | }
|
70 |
|
71 | readonly MARKDOWN_DOCS=(
|
72 | published
|
73 |
|
74 | # polished
|
75 | getting-started
|
76 | portability
|
77 | known-differences
|
78 | ysh-error
|
79 | error-handling
|
80 | error-catalog
|
81 | json
|
82 | hay
|
83 | simple-word-eval
|
84 | quirks
|
85 | warts
|
86 |
|
87 | eggex
|
88 | ysh-regex-api
|
89 | upgrade-breakage
|
90 | ysh-tour
|
91 |
|
92 | style-guide
|
93 | novelties
|
94 |
|
95 | proc-func
|
96 | block-literals
|
97 | objects
|
98 | types
|
99 |
|
100 | # Data language
|
101 | qsn
|
102 | qtt
|
103 | j8-notation
|
104 | # Protocol
|
105 | pretty-printing
|
106 | stream-table-process
|
107 | byo
|
108 | ysh-doc-processing
|
109 |
|
110 | lib-osh
|
111 |
|
112 | doc-toolchain
|
113 | doc-plugins
|
114 | idioms
|
115 | shell-idioms
|
116 | ysh-faq
|
117 |
|
118 | language-influences
|
119 | ysh-vs-python
|
120 | ysh-vs-shell
|
121 |
|
122 | syntactic-concepts
|
123 | syntax-feelings
|
124 | command-vs-expression-mode
|
125 |
|
126 | # needs polish
|
127 | # Note: docs about the YSH are prefixed 'ysh-'.
|
128 | # data-model and command-vs-expression-mode span both OSH and YSH
|
129 |
|
130 | index
|
131 | faq-doc
|
132 |
|
133 | options
|
134 |
|
135 | old/index
|
136 | old/project-tour
|
137 | old/legacy-array
|
138 | old/ysh-keywords
|
139 | old/modules
|
140 | old/expression-language
|
141 | old/word-language
|
142 | old/errors
|
143 | old/ysh-builtins
|
144 |
|
145 | io-builtins
|
146 | unicode
|
147 | framing
|
148 | xtrace
|
149 | headless
|
150 | completion
|
151 | strings
|
152 | variables
|
153 |
|
154 | # Internal stuff
|
155 | interpreter-state
|
156 | process-model
|
157 | architecture-notes
|
158 | parser-architecture
|
159 | )
|
160 |
|
161 | # Bug fix: Plain $(date) can output unicode characters (e.g. in Japanese
|
162 | # locale), which is loaded by Python into say u'\u5e74'. But the default
|
163 | # encoding in Python 2 is still 'ascii', which means that '%s' % u_str may
|
164 | # fail.
|
165 | #
|
166 | # I believe --rfc-e-mail should never output a Unicode character.
|
167 | #
|
168 | # A better fix would be to implement json_utf8.load(f), which doesn't decode
|
169 | # into unicode instances. This would remove useless conversions.
|
170 |
|
171 | readonly TIMESTAMP=$(date --rfc-email)
|
172 |
|
173 | split-and-render() {
|
174 | local src=${1:-doc/known-differences.md}
|
175 |
|
176 | local rel_path=${src%'.md'} # doc/known-differences
|
177 | local tmp_prefix=_tmp/$rel_path # temp dir for splitting
|
178 |
|
179 | local out=${2:-$HTML_BASE_DIR/$rel_path.html}
|
180 | local web_url=${3:-'../web'}
|
181 |
|
182 | mkdir -v -p $(dirname $out) $tmp_prefix
|
183 |
|
184 | # Also add could add css_files. The one in the file takes precedence always?
|
185 |
|
186 | # css_files: a space-separated list
|
187 | # all_docs_url: so we link from doc/foo.html -> doc/
|
188 |
|
189 | local css_files="$web_url/base.css $web_url/manual.css $web_url/toc.css $web_url/language.css $web_url/code.css"
|
190 |
|
191 | doctools/split_doc.py \
|
192 | -v build_timestamp="$TIMESTAMP" \
|
193 | -v oil_version="$OIL_VERSION" \
|
194 | -v css_files="$css_files" \
|
195 | -v all_docs_url='.' \
|
196 | -v repo_url="$src" \
|
197 | $src $tmp_prefix
|
198 |
|
199 | #ls -l _tmp/doc
|
200 | #head _tmp/doc/*
|
201 | #return
|
202 |
|
203 | # for ysh-tour code blocks
|
204 | local code_out=_tmp/code-blocks/$rel_path.txt
|
205 | mkdir -v -p $(dirname $code_out)
|
206 |
|
207 | cmark \
|
208 | --code-block-output $code_out \
|
209 | ${tmp_prefix}_meta.json ${tmp_prefix}_content.md > $out
|
210 |
|
211 | log "$tmp_prefix -> (doctools/cmark) -> $out"
|
212 | }
|
213 |
|
214 | render-from-kate() {
|
215 | ### Make it easier to configure Kate editor
|
216 |
|
217 | # It want to pass an absolute path
|
218 | # TODO: I can't figure out how to run this from Kate?
|
219 |
|
220 | local full_path=$1
|
221 |
|
222 | case $full_path in
|
223 | $REPO_ROOT/*)
|
224 | rel_path=${full_path#"$REPO_ROOT/"}
|
225 | echo "relative path = $rel_path"
|
226 | ;;
|
227 | *)
|
228 | die "$full_path should start with repo root $REPO_ROOT"
|
229 | ;;
|
230 | esac
|
231 |
|
232 | split-and-render $rel_path
|
233 | }
|
234 |
|
235 | # Special case for README
|
236 | # Do NOT split because we don't want front matter in the markdown source.
|
237 | render-only() {
|
238 | local src=${1:-README.md}
|
239 |
|
240 | local name
|
241 | case $src in
|
242 | *.md)
|
243 | name=$(basename $src .md)
|
244 | ;;
|
245 | *.txt)
|
246 | name=$(basename $src .txt)
|
247 | ;;
|
248 | *)
|
249 | name=$(basename $src)
|
250 | ;;
|
251 | esac
|
252 |
|
253 | local out=${2:-$HTML_BASE_DIR/doc/$name.html}
|
254 | local css_files=${3:-'../web/manual.css ../web/toc.css'}
|
255 | local title=${4:-'Oils Source Code'}
|
256 |
|
257 | local prefix=_tmp/doc/$name
|
258 |
|
259 | local meta=${prefix}_meta.json
|
260 | cat >$meta <<EOF
|
261 | { "title": "$title",
|
262 | "repo_url": "$src",
|
263 | "css_files": "$css_files",
|
264 | "all_docs_url": ".",
|
265 |
|
266 | "build_timestamp": "$TIMESTAMP",
|
267 | "oil_version": "$OIL_VERSION"
|
268 | }
|
269 | EOF
|
270 |
|
271 | cmark $meta $src > $out
|
272 | log "Wrote $out"
|
273 | }
|
274 |
|
275 | special() {
|
276 | # TODO: do all READMEs
|
277 | split-and-render mycpp/README.md \
|
278 | $HTML_BASE_DIR/doc/oils-repo/mycpp/README.html \
|
279 | ../../../web
|
280 |
|
281 | local web_dir='../../web'
|
282 | render-only 'README.md' $HTML_BASE_DIR/doc/oils-repo/README.html \
|
283 | "$web_dir/base.css $web_dir/manual.css $web_dir/toc.css" 'Oils Source Code'
|
284 |
|
285 | local web_dir='../web'
|
286 | render-only INSTALL.txt '' \
|
287 | "$web_dir/base.css $web_dir/install.css" 'Installing Oils'
|
288 |
|
289 | render-only INSTALL-old.txt '' \
|
290 | "$web_dir/base.css $web_dir/install.css" 'Installing Oils - old CPython build'
|
291 |
|
292 | # These pages aren't in doc/
|
293 | split-and-render doc/release-index.md _tmp/release-index.html
|
294 | split-and-render doc/release-quality.md _tmp/release-quality.html
|
295 | }
|
296 |
|
297 | all-markdown() {
|
298 | make-dirs
|
299 |
|
300 | # TODO: We can set repo_url here! Then we don't need it for most docs.
|
301 | # split_doc.py can return {} if the doc doesn't start with ---
|
302 |
|
303 | #for d in doc/index.md doc/known-differences.md doc/*-manual.md \
|
304 | # doc/eggex.md doc/oil-options.md doc/oil-func-proc-block.md; do
|
305 | for d in "${MARKDOWN_DOCS[@]}"; do
|
306 | split-and-render doc/$d.md
|
307 | done
|
308 |
|
309 | special
|
310 | }
|
311 |
|
312 | redir-body() {
|
313 | local to_url=$1 # WARNING: no escaping
|
314 | cat <<EOF
|
315 | <head>
|
316 | <meta http-equiv="Refresh" content="0; URL=$to_url" />
|
317 | </head>
|
318 | EOF
|
319 | }
|
320 |
|
321 | redirect-pairs() {
|
322 | # we want want /release/latest/ URLs to still work
|
323 | cat <<EOF
|
324 | oil-language-tour ysh-tour
|
325 | oil-language-faq ysh-faq
|
326 | oil-help ysh-help
|
327 | oil-help-topics ysh-help-topics
|
328 | ysh-help ref/toc-ysh
|
329 | ysh-help-topics ref/toc-ysh
|
330 | EOF
|
331 | }
|
332 |
|
333 | all-redirects() {
|
334 | redirect-pairs | while read -r from_page to_page; do
|
335 | redir-body "$to_page.html" | tee "_release/VERSION/doc/$from_page.html"
|
336 | done
|
337 | }
|
338 |
|
339 | # TODO: This could use some CSS.
|
340 | man-page() {
|
341 | local root_dir=${1:-_release/VERSION}
|
342 | mandoc -T html doc/osh.1 > $root_dir/osh.1.html
|
343 | ls -l $root_dir
|
344 | }
|
345 |
|
346 | # I want to ship the INSTALL file literally, so just mutate things
|
347 | _sed-ext() {
|
348 | sed --regexp-extended -i "$@"
|
349 | }
|
350 |
|
351 | update-src-versions() {
|
352 | # Update tarball names, etc.
|
353 | _sed-ext \
|
354 | "s/[0-9]+\.[0-9]+\.[a-z0-9]+/$OIL_VERSION/g" \
|
355 | doc/release-*.md INSTALL.txt INSTALL-old.txt
|
356 |
|
357 | # Update /release/0.8.4/ URL, etc.
|
358 | _sed-ext \
|
359 | "s;/release/[0-9]+\.[0-9]+\.[a-z0-9]+/;/release/$OIL_VERSION/;g" \
|
360 | doc/osh.1
|
361 | }
|
362 |
|
363 | #
|
364 | # Test Tools
|
365 | #
|
366 |
|
367 | split-doc-demo() {
|
368 | cat > _tmp/testdoc.md <<EOF
|
369 | ---
|
370 | title: foo
|
371 | ---
|
372 |
|
373 | Title
|
374 | =====
|
375 |
|
376 | hello
|
377 |
|
378 | EOF
|
379 |
|
380 | doctools/split_doc.py _tmp/testdoc.md _tmp/testdoc
|
381 |
|
382 | head _tmp/testdoc*
|
383 | }
|
384 |
|
385 | #
|
386 | # Help is both markdown and text
|
387 | #
|
388 |
|
389 | readonly TMP_DIR=_tmp/doc
|
390 | readonly CODE_BLOCK_DIR=_tmp/code-blocks
|
391 | readonly TEXT_DIR=_devbuild/help
|
392 | readonly HTML_DIR=_release/VERSION
|
393 | readonly CODE_DIR=_devbuild/gen
|
394 |
|
395 | cards-from-indices() {
|
396 | ### Make help cards
|
397 |
|
398 | for lang in osh ysh data; do
|
399 | help-gen cards-from-index $lang $TEXT_DIR \
|
400 | < $HTML_DIR/doc/ref/toc-$lang.html
|
401 | done
|
402 | }
|
403 |
|
404 | cards-from-chapters() {
|
405 | ### Turn h3 topics into cards
|
406 |
|
407 | local py_out=$CODE_DIR/help_meta.py
|
408 |
|
409 | mkdir -p _gen/frontend
|
410 | local cc_prefix=_gen/frontend/help_meta
|
411 |
|
412 | help-gen cards-from-chapters $TEXT_DIR $py_out $cc_prefix \
|
413 | $HTML_DIR/doc/ref/chap-*.html
|
414 | }
|
415 |
|
416 | ref-check() {
|
417 | help-gen ref-check \
|
418 | doc/ref/toc-*.md \
|
419 | _release/VERSION/doc/ref/chap-*.html
|
420 | }
|
421 |
|
422 | fmt-check() {
|
423 | PYTHONPATH=. doctools/fmt_check.py _release/VERSION/doc/ref/*.html
|
424 | }
|
425 |
|
426 |
|
427 | write-metrics() {
|
428 | ### Check indexes and chapters against each other
|
429 |
|
430 | local out=_release/VERSION/doc/metrics.txt
|
431 |
|
432 | # send stderr to the log file too
|
433 | ref-check > $out 2>&1
|
434 |
|
435 | echo "Wrote $out"
|
436 | }
|
437 |
|
438 | tour() {
|
439 | ### Build the Tour of YSH, and execute code as validation
|
440 | local name=${1:-ysh-tour}
|
441 |
|
442 | split-and-render doc/$name.md
|
443 |
|
444 | local work_dir=$REPO_ROOT/_tmp/code-blocks/doc
|
445 |
|
446 | mkdir -p $work_dir/lib
|
447 |
|
448 | # Files used by module example
|
449 | touch $work_dir/{build,test}.sh
|
450 |
|
451 | cat >$work_dir/lib/util.ysh <<EOF
|
452 | log() { echo "$@" 1>&2; }
|
453 | EOF
|
454 |
|
455 | pushd $work_dir
|
456 | # Fix: don't supply stdin!
|
457 | $REPO_ROOT/bin/ysh $name.txt < /dev/null
|
458 | popd
|
459 |
|
460 | # My own dev tools
|
461 | # if test -d ~/vm-shared; then
|
462 | if false; then
|
463 | local path=_release/VERSION/doc/$name.html
|
464 | cp -v $path ~/vm-shared/$path
|
465 | fi
|
466 | }
|
467 |
|
468 | one() {
|
469 | ### Iterate on one doc quickly
|
470 |
|
471 | local name=${1:-options}
|
472 |
|
473 | split-and-render doc/$name.md
|
474 |
|
475 | # Make sure the doc has valid YSH code?
|
476 | # TODO: Maybe need an attribute for OSH or YSH
|
477 | pushd _tmp/code-blocks/doc
|
478 | $REPO_ROOT/bin/ysh $name.txt
|
479 | popd
|
480 |
|
481 | if test -d ~/vm-shared; then
|
482 | local out="${name%.md}.html"
|
483 | local path=_release/VERSION/$out
|
484 | cp -v $path ~/vm-shared/$path
|
485 | fi
|
486 | }
|
487 |
|
488 | make-dirs() {
|
489 | mkdir -p $TMP_DIR $CODE_BLOCK_DIR $TEXT_DIR $HTML_DIR/doc
|
490 | }
|
491 |
|
492 | one-ref() {
|
493 | local md=${1:-doc/ref/index.md}
|
494 | split-and-render $md '' '../../web'
|
495 | }
|
496 |
|
497 | all-ref() {
|
498 | ### Build doc/ref in text and HTML. Depends on libcmark.so
|
499 |
|
500 | log "Removing $TEXT_DIR/*"
|
501 | rm -f $TEXT_DIR/*
|
502 | make-dirs
|
503 |
|
504 | # Make the indexes and chapters
|
505 | for d in doc/ref/*.md; do
|
506 | split-and-render $d '' '../../web'
|
507 | done
|
508 |
|
509 | # Note: if we want a $ref-topic shortcut, we might want to use Ninja to
|
510 | # extract topics from all chapters first, and then make help_meta.json, like
|
511 | # we have _devbuild/gen/help_meta.py.
|
512 |
|
513 | # Text cards
|
514 | cards-from-indices
|
515 | # A few text cards, and HELP_TOPICS dict for URLs, for flat namespace
|
516 | cards-from-chapters
|
517 |
|
518 | if command -v pysum; then
|
519 | # 19 KB of embedded help, seems OK. Biggest card is 'ysh-option'. Could
|
520 | # compress it.
|
521 | echo 'Size of embedded help:'
|
522 | ls -l $TEXT_DIR | tee /dev/stderr | awk '{print $5}' | pysum
|
523 | fi
|
524 |
|
525 | # Better sorting
|
526 | #LANG=C ls -l $TEXT_DIR
|
527 | }
|
528 |
|
529 | _copy-path() {
|
530 | local src=$1 dest=$2
|
531 | mkdir -p $(dirname $dest)
|
532 | cp -v $src $dest
|
533 | }
|
534 |
|
535 | copy-web() {
|
536 | find web \
|
537 | \( -name _tmp -a -prune \) -o \
|
538 | \( -name '*.css' -o -name '*.js' \) -a -printf '%p _release/VERSION/%p\n' |
|
539 | xargs -n 2 -- $0 _copy-path
|
540 | }
|
541 |
|
542 | pretty-size() {
|
543 | local path=$1
|
544 | stat --format '%s' "$path" | python -c '
|
545 | import sys
|
546 | num_bytes = int(sys.stdin.read())
|
547 | print "{:,}".format(num_bytes)
|
548 | '
|
549 | }
|
550 |
|
551 | # NOTE: It might be better to link to files like this in the /release/ tree.
|
552 | # Although I am not signing them.
|
553 |
|
554 | # https://nodejs.org/dist/v8.11.4/SHASUMS256.txt.asc
|
555 |
|
556 | tarball-links-row-html() {
|
557 | local version=$1
|
558 |
|
559 | cat <<EOF
|
560 | <tr class="file-table-heading">
|
561 | <td></td>
|
562 | <td>File / SHA256 checksum</td>
|
563 | <td class="size">Size</td>
|
564 | <td></td>
|
565 | </tr>
|
566 | EOF
|
567 |
|
568 | # we switched to .gz for oils-for-unix
|
569 | # note: legacy names for old releases
|
570 | for name in \
|
571 | oils-for-unix-$version.tar.{gz,xz} \
|
572 | oil-$version.tar.{gz,xz} \
|
573 | oil-native-$version.tar.xz; do
|
574 |
|
575 | local url="/download/$name" # The server URL
|
576 | local path="../oilshell.org__deploy/download/$name"
|
577 |
|
578 | # Don't show tarballs that don't exist
|
579 | if [[ $name == oils-for-unix-* && ! -f $path ]]; then
|
580 | continue
|
581 | fi
|
582 | if [[ $name == oil-native-* && ! -f $path ]]; then
|
583 | continue
|
584 | fi
|
585 |
|
586 | local checksum
|
587 | checksum=$(sha256sum $path | awk '{print $1}')
|
588 | local size
|
589 | size=$(pretty-size $path)
|
590 |
|
591 | # TODO: Port this to oil with "commas" extension.
|
592 |
|
593 | # Three columns: date, version, and links
|
594 | cat <<EOF
|
595 | <tr>
|
596 | <td></td>
|
597 | <td class="filename"><a href="$url">$name</a></td>
|
598 | <td class="size">$size</td>
|
599 | </tr>
|
600 | <tr>
|
601 | <td></td>
|
602 | <td colspan=2 class="checksum">$checksum</td>
|
603 | </tr>
|
604 | EOF
|
605 | done
|
606 | }
|
607 |
|
608 | this-release-links() {
|
609 | echo '<div class="file-table">'
|
610 | echo '<table>'
|
611 | tarball-links-row-html "$OIL_VERSION"
|
612 | echo '</table>'
|
613 | echo '</div>'
|
614 | }
|
615 |
|
616 | # Turn HTML comment into a download link
|
617 | add-date-and-links() {
|
618 | local snippet
|
619 | snippet=$(this-release-links)
|
620 |
|
621 | awk -v date=$1 -v snippet="$snippet" '
|
622 | /<!-- REPLACE_WITH_DOWNLOAD_LINKS -->/ {
|
623 | print(snippet)
|
624 | next
|
625 | }
|
626 |
|
627 | /<!-- REPLACE_WITH_DATE -->/ {
|
628 | print(date)
|
629 | next
|
630 | }
|
631 |
|
632 | # Everything else
|
633 | { print }
|
634 | '
|
635 | }
|
636 |
|
637 | patch-release-pages() {
|
638 | local release_date
|
639 | release_date=$(cat _build/release-date.txt)
|
640 |
|
641 | local root=_release/VERSION
|
642 |
|
643 | add-date-and-links $release_date < _tmp/release-index.html > $root/index.html
|
644 | add-date-and-links $release_date < _tmp/release-quality.html > $root/quality.html
|
645 | }
|
646 |
|
647 | copy-release-pages() {
|
648 | ### For testing without releasing
|
649 |
|
650 | cat < _tmp/release-index.html > $root/index.html
|
651 | cat < _tmp/release-quality.html > $root/quality.html
|
652 | }
|
653 |
|
654 | run-for-release() {
|
655 | ### Build a tree. Requires _build/release-date.txt to exist
|
656 |
|
657 | local root=_release/VERSION
|
658 | mkdir -p $root/{doc,test,pub}
|
659 |
|
660 | tour
|
661 |
|
662 | # Metadata
|
663 | cp -v _build/release-date.txt oil-version.txt $root
|
664 |
|
665 | # Docs
|
666 | # Writes _release/VERSION and _tmp/release-index.html
|
667 | all-markdown
|
668 | all-ref
|
669 | all-redirects # backward compat
|
670 |
|
671 | fmt-check # Needs to run *after* we build the HTML
|
672 |
|
673 | patch-release-pages
|
674 |
|
675 | write-metrics
|
676 |
|
677 | # Problem: You can't preview it without .wwz!
|
678 | # Maybe have local redirects VERSION/test/wild/ to
|
679 | #
|
680 | # Instead of linking, I should compress them all here.
|
681 |
|
682 | copy-web
|
683 |
|
684 | if command -v tree >/dev/null; then
|
685 | tree $root
|
686 | else
|
687 | find $root
|
688 | fi
|
689 | }
|
690 |
|
691 | soil-run() {
|
692 | build/stamp.sh write-release-date
|
693 |
|
694 | run-for-release
|
695 | }
|
696 |
|
697 | "$@"
|
698 |
|