OILS / build / doc.sh View on Github | oils.pub

848 lines, 411 significant
1#!/usr/bin/env bash
2#
3# Usage:
4# build/doc.sh <function name>
5
6set -o nounset
7set -o pipefail
8set -o errexit
9
10# https://oilshell.org/release/$VERSION/
11# doc/
12# index.html
13# INSTALL.html
14# INSTALL-old.html
15
16readonly OIL_VERSION=$(head -n 1 oils-version.txt)
17export OIL_VERSION # for quick_ref.py
18
19THIS_DIR=$(readlink -f $(dirname $0))
20readonly THIS_DIR
21REPO_ROOT=$(cd $THIS_DIR/.. && pwd)
22readonly REPO_ROOT
23
24
25readonly HTML_BASE_DIR=_release/VERSION
26
27
28log() {
29 echo "$@" 1>&2
30}
31
32#
33# Deps (similar to doctools/cmark.sh and build/codegen.sh)
34#
35
36readonly MANDOC_DIR='_deps/mdocml-1.14.1'
37
38download-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
44build-mandoc() {
45 cd $MANDOC_DIR
46 ./configure
47 make
48}
49
50mandoc() {
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
62help-gen() {
63 PYTHONPATH=.:vendor doctools/help_gen.py "$@"
64}
65
66cmark() {
67 # h2 and h3 are shown in TOC. The blog uses "legacy" h3 and h4.
68 PYTHONPATH=.:vendor doctools/cmark.py --toc-tag h2 --toc-tag h3 --toc-pretty-href "$@"
69}
70
71readonly 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 htm8
105 # Protocol
106 pretty-printing
107 stream-table-process
108 byo
109 ysh-doc-processing
110
111 table-object-doc
112
113 lib-osh
114
115 doc-toolchain
116 doc-plugins
117 ul-table
118 ul-table-compare
119 idioms
120 shell-idioms
121 ysh-faq
122
123 language-influences
124 ysh-vs-python
125 ysh-vs-shell
126
127 syntactic-concepts
128 syntax-feelings
129 command-vs-expression-mode
130
131 # needs polish
132 # Note: docs about the YSH are prefixed 'ysh-'.
133 # data-model and command-vs-expression-mode span both OSH and YSH
134
135 index
136 faq-doc
137
138 options
139
140 old/index
141 old/project-tour
142 old/legacy-array
143 old/ysh-keywords
144 old/modules
145 old/expression-language
146 old/word-language
147 old/errors
148 old/ysh-builtins
149
150 io-builtins
151 unicode
152 framing
153 xtrace
154 headless
155 completion
156 strings
157 variables
158
159 # Internal stuff
160 interpreter-state
161 process-model
162 architecture-notes
163 parser-architecture
164)
165
166# Bug fix: Plain $(date) can output unicode characters (e.g. in Japanese
167# locale), which is loaded by Python into say u'\u5e74'. But the default
168# encoding in Python 2 is still 'ascii', which means that '%s' % u_str may
169# fail.
170#
171# I believe --rfc-e-mail should never output a Unicode character.
172#
173# A better fix would be to implement json_utf8.load(f), which doesn't decode
174# into unicode instances. This would remove useless conversions.
175
176DOC_TIMESTAMP=${DOC_TIMESTAMP:-$(date --rfc-email)}
177
178split-and-render() {
179 local src=${1:-doc/known-differences.md}
180
181 local rel_path=${src%'.md'} # doc/known-differences
182 local tmp_prefix=_tmp/$rel_path # temp dir for splitting
183
184 local out=${2:-$HTML_BASE_DIR/$rel_path.html}
185 local web_url=${3:-'../web'}
186
187 mkdir -v -p $(dirname $out) $tmp_prefix
188
189 # Also add could add css_files. The one in the file takes precedence always?
190
191 # css_files: a space-separated list
192 # all_docs_url: so we link from doc/foo.html -> doc/
193
194 local css_files="$web_url/base.css $web_url/manual.css $web_url/toc.css $web_url/language.css $web_url/code.css"
195
196 PYTHONPATH='.:vendor' doctools/split_doc.py \
197 -v build_timestamp="$DOC_TIMESTAMP" \
198 -v oil_version="$OIL_VERSION" \
199 -v css_files="$css_files" \
200 -v all_docs_url='.' \
201 -v repo_url="$src" \
202 $src $tmp_prefix
203
204 #ls -l _tmp/doc
205 #head _tmp/doc/*
206 #return
207
208 # for ysh-tour code blocks
209 local code_out=_tmp/code-blocks/$rel_path.txt
210 mkdir -v -p $(dirname $code_out)
211
212 cmark \
213 --code-block-output $code_out \
214 ${tmp_prefix}_meta.json ${tmp_prefix}_content.md > $out
215
216 log "$tmp_prefix -> (doctools/cmark) -> $out"
217}
218
219render-from-kate() {
220 ### Make it easier to configure Kate editor
221
222 # It want to pass an absolute path
223 # TODO: I can't figure out how to run this from Kate?
224
225 local full_path=$1
226
227 case $full_path in
228 $REPO_ROOT/*)
229 rel_path=${full_path#"$REPO_ROOT/"}
230 echo "relative path = $rel_path"
231 ;;
232 *)
233 die "$full_path should start with repo root $REPO_ROOT"
234 ;;
235 esac
236
237 split-and-render $rel_path
238}
239
240# Special case for README
241# Do NOT split because we don't want front matter in the markdown source.
242render-only() {
243 local src=${1:-README.md}
244
245 local name
246 case $src in
247 *.md)
248 name=$(basename $src .md)
249 ;;
250 *.txt)
251 name=$(basename $src .txt)
252 ;;
253 *)
254 name=$(basename $src)
255 ;;
256 esac
257
258 local out=${2:-$HTML_BASE_DIR/doc/$name.html}
259 local css_files=${3:-'../web/manual.css ../web/toc.css'}
260 local title=${4:-'Oils Source Code'}
261
262 local prefix=_tmp/doc/$name
263
264 local meta=${prefix}_meta.json
265 cat >$meta <<EOF
266{ "title": "$title",
267 "repo_url": "$src",
268 "css_files": "$css_files",
269 "all_docs_url": ".",
270
271 "build_timestamp": "$DOC_TIMESTAMP",
272 "oil_version": "$OIL_VERSION"
273}
274EOF
275
276 cmark $meta $src > $out
277 log "Wrote $out"
278}
279
280help-mirror-md() {
281 echo '
282Oils Build `--help` Mirror
283=====
284
285<style>
286/* Similar to web/install.css */
287h1 { font-size: 1.5em; }
288h2 { font-size: 1.2em; }
289
290/* Exclude Markdown <pre><code> */
291code:not(pre code) {
292 color: green;
293}
294</style>
295
296This doc mirrors the `--help` for the 3 shell tools in the build sytsem:
297
2981. `configure` - Detect system settings
2991. `_build/oils.sh` - Compile `oils-for-unix` source into an executable
3001. `install` - Install the executable, and symlinks to it
301
302<div id="toc">
303</div>
304
305## configure
306
307```'
308 ./configure --help
309
310 echo '```
311
312## _build/oils.sh
313
314```'
315
316 devtools/release-native.sh gen-oils-sh
317 _build/oils.sh --help
318
319 echo '```
320
321## install
322
323```'
324 ./install --help
325 echo '```
326
327## Links
328
329- [INSTALL.html](INSTALL.html)
330- [Oils Packaging Guidelines]($wiki) (wiki)
331- [Oils Packaging Tips]($wiki) (wiki)
332
333 '
334}
335
336help-mirror() {
337 ### Mirror --help to HTML
338
339 local md=_tmp/doc/help-mirror.md
340
341 help-mirror-md > $md
342
343 local web_dir='../web'
344 #local css="$web_dir/base.css $web_dir/install.css $web_dir/toc.css"
345 local css="$web_dir/base.css $web_dir/toc.css"
346 render-only $md '' "$css" 'Oils Build Help Mirror'
347}
348
349special() {
350 # TODO: do all READMEs
351 split-and-render mycpp/README.md \
352 $HTML_BASE_DIR/doc/oils-repo/mycpp/README.html \
353 ../../../web
354
355 # TODO: README can just be a pointer to other docs, like "Repo Overview"
356 local web_dir='../../web'
357 render-only 'README.md' $HTML_BASE_DIR/doc/oils-repo/README.html \
358 "$web_dir/base.css $web_dir/manual.css $web_dir/toc.css" 'Oils Source Code'
359
360 help-mirror
361
362 local web_dir='../web'
363 render-only INSTALL.txt '' \
364 "$web_dir/base.css $web_dir/install.css" 'Installing Oils'
365
366 render-only INSTALL-old.txt '' \
367 "$web_dir/base.css $web_dir/install.css" 'Installing Oils - old CPython build'
368
369 # These pages aren't in doc/
370 split-and-render doc/release-index.md _tmp/release-index.html
371 split-and-render doc/release-quality.md _tmp/release-quality.html
372}
373
374all-markdown() {
375 make-dirs
376
377 # TODO: We can set repo_url here! Then we don't need it for most docs.
378 # split_doc.py can return {} if the doc doesn't start with ---
379
380 #for d in doc/index.md doc/known-differences.md doc/*-manual.md \
381 # doc/eggex.md doc/oil-options.md doc/oil-func-proc-block.md; do
382 for d in "${MARKDOWN_DOCS[@]}"; do
383 split-and-render doc/$d.md
384 done
385
386 special
387}
388
389redir-body() {
390 local to_url=$1 # WARNING: no escaping
391 cat <<EOF
392<head>
393 <meta http-equiv="Refresh" content="0; URL=$to_url" />
394</head>
395EOF
396}
397
398redirect-pairs() {
399 # we want want /release/latest/ URLs to still work
400 cat <<EOF
401oil-language-tour ysh-tour
402oil-language-faq ysh-faq
403oil-help ysh-help
404oil-help-topics ysh-help-topics
405ysh-help ref/toc-ysh
406ysh-help-topics ref/toc-ysh
407EOF
408}
409
410all-redirects() {
411 redirect-pairs | while read -r from_page to_page; do
412 redir-body "$to_page.html" | tee "_release/VERSION/doc/$from_page.html"
413 done
414}
415
416# TODO: This could use some CSS.
417man-page() {
418 local root_dir=${1:-_release/VERSION}
419 mandoc -T html doc/osh.1 > $root_dir/osh.1.html
420 ls -l $root_dir
421}
422
423# I want to ship the INSTALL file literally, so just mutate things
424_sed-ext() {
425 sed --regexp-extended -i "$@"
426}
427
428update-src-versions() {
429 # Update tarball names, etc.
430 _sed-ext \
431 "s/[0-9]+\.[0-9]+\.[a-z0-9]+/$OIL_VERSION/g" \
432 doc/release-*.md INSTALL.txt INSTALL-old.txt
433
434 # Update /release/0.8.4/ URL, etc.
435 _sed-ext \
436 "s;/release/[0-9]+\.[0-9]+\.[a-z0-9]+/;/release/$OIL_VERSION/;g" \
437 doc/osh.1
438}
439
440#
441# Test Tools
442#
443
444split-doc-demo() {
445 cat > _tmp/testdoc.md <<EOF
446---
447title: foo
448---
449
450Title
451=====
452
453hello
454
455EOF
456
457 doctools/split_doc.py _tmp/testdoc.md _tmp/testdoc
458
459 head _tmp/testdoc*
460}
461
462#
463# Help is both markdown and text
464#
465
466readonly TMP_DIR=_tmp/doc
467readonly CODE_BLOCK_DIR=_tmp/code-blocks
468readonly TEXT_DIR=_devbuild/help
469readonly HTML_DIR=_release/VERSION
470readonly CODE_DIR=_devbuild/gen
471
472cards-from-indices() {
473 ### Make help cards
474
475 for lang in osh ysh data; do
476 help-gen cards-from-index $lang $TEXT_DIR \
477 < $HTML_DIR/doc/ref/toc-$lang.html
478 done
479}
480
481cards-from-chapters() {
482 ### Turn h3 topics into cards
483
484 local py_out=$CODE_DIR/help_meta.py
485
486 mkdir -p _gen/frontend
487 local cc_prefix=_gen/frontend/help_meta
488
489 help-gen cards-from-chapters $TEXT_DIR $py_out $cc_prefix \
490 $HTML_DIR/doc/ref/chap-*.html
491}
492
493ref-check() {
494 help-gen ref-check \
495 doc/ref/toc-*.md \
496 _release/VERSION/doc/ref/chap-*.html
497}
498
499fmt-check() {
500 PYTHONPATH=.:vendor doctools/fmt_check.py _release/VERSION/doc/ref/*.html
501}
502
503
504write-metrics() {
505 ### Check indexes and chapters against each other
506
507 local out=_release/VERSION/doc/metrics.txt
508
509 # send stderr to the log file too
510 ref-check > $out 2>&1
511
512 echo "Wrote $out"
513}
514
515tour() {
516 ### Build the Tour of YSH, and execute code as validation
517 local name=${1:-ysh-tour}
518
519 split-and-render doc/$name.md
520
521 local work_dir=$REPO_ROOT/_tmp/code-blocks/doc
522
523 mkdir -p $work_dir/lib
524
525 # Files used by module example
526 touch $work_dir/{build,test}.sh
527
528 cat >$work_dir/lines.txt <<'EOF'
529 doc/hello.md
530 "doc/with spaces.md"
531b'doc/with byte \yff.md'
532EOF
533
534 cat >$work_dir/myargs.ysh <<EOF
535const __provide__ = :| proc1 p2 p3 |
536
537proc proc1 {
538 echo proc1
539}
540
541proc p2 {
542 echo p2
543}
544
545proc p3 {
546 echo p3
547}
548EOF
549
550 cat >$work_dir/demo.py <<EOF
551#!/usr/bin/env python
552
553print("hi")
554EOF
555 chmod +x $work_dir/demo.py
556
557 cat >$work_dir/lib/util.ysh <<EOF
558const __provide__ = :| log |
559
560proc log {
561 echo @ARGV >&2
562}
563EOF
564
565 pushd $work_dir
566
567 # Prepend extra code
568 cat >tour.ysh - $name.txt <<EOF
569func myMethod(self) {
570 echo 'myMethod'
571}
572
573func mutatingMethod(self) {
574 echo 'mutatingMethod'
575}
576
577func makeMyObject(x) {
578 var methods = Object(null, {myMethod, 'M/mutatingMethod': mutatingMethod})
579 return (Object(methods, {x}))
580}
581EOF
582
583 # Fix: don't supply stdin!
584 $REPO_ROOT/bin/ysh tour.ysh < /dev/null
585 popd
586
587 # My own dev tools
588 # if test -d ~/vm-shared; then
589 if false; then
590 local path=_release/VERSION/doc/$name.html
591 cp -v $path ~/vm-shared/$path
592 fi
593}
594
595one() {
596 ### Iterate on one doc quickly
597
598 local name=${1:-options}
599
600 split-and-render doc/$name.md
601
602 # Make sure the doc has valid YSH code?
603 # TODO: Maybe need an attribute for OSH or YSH
604 pushd _tmp/code-blocks/doc
605 $REPO_ROOT/bin/ysh $name.txt
606 popd
607
608 if test -d ~/vm-shared; then
609 local out="${name%.md}.html"
610 local path=_release/VERSION/$out
611 cp -v $path ~/vm-shared/$path
612 fi
613}
614
615make-dirs() {
616 mkdir -p $TMP_DIR $CODE_BLOCK_DIR $TEXT_DIR $HTML_DIR/doc
617}
618
619one-ref() {
620 local md=${1:-doc/ref/index.md}
621 split-and-render $md '' '../../web'
622}
623
624all-ref() {
625 ### Build doc/ref in text and HTML. Depends on libcmark.so
626
627 log "Removing $TEXT_DIR/*"
628 rm -f $TEXT_DIR/*
629 make-dirs
630
631 # Make the indexes and chapters
632 for d in doc/ref/*.md; do
633 split-and-render $d '' '../../web'
634 done
635
636 # Note: if we want a $ref-topic shortcut, we might want to use Ninja to
637 # extract topics from all chapters first, and then make help_meta.json, like
638 # we have _devbuild/gen/help_meta.py.
639
640 # Text cards
641 cards-from-indices
642 # A few text cards, and HELP_TOPICS dict for URLs, for flat namespace
643 cards-from-chapters
644
645 return
646 if command -v pysum; then
647 # 19 KB of embedded help, seems OK. Biggest card is 'ysh-option'. Could
648 # compress it.
649 echo 'Size of embedded help:'
650 ls -l $TEXT_DIR | tee /dev/stderr | awk '{print $5}' | pysum
651 fi
652
653 # Better sorting
654 #LANG=C ls -l $TEXT_DIR
655}
656
657_copy-path() {
658 local src=$1 dest=$2
659 mkdir -p $(dirname $dest)
660 cp -v $src $dest
661}
662
663copy-web() {
664 find web \
665 \( -name _tmp -a -prune \) -o \
666 \( -name '*.css' -o -name '*.js' \) -a -printf '%p _release/VERSION/%p\n' |
667 xargs -n 2 -- $0 _copy-path
668}
669
670pretty-size() {
671 local path=$1
672 stat --format '%s' "$path" | python -c '
673import sys
674num_bytes = int(sys.stdin.read())
675print "{:,}".format(num_bytes)
676'
677}
678
679# NOTE: It might be better to link to files like this in the /release/ tree.
680# Although I am not signing them.
681
682# https://nodejs.org/dist/v8.11.4/SHASUMS256.txt.asc
683
684tarball-links-row-html() {
685 local version=$1
686
687 cat <<EOF
688<tr class="file-table-heading">
689 <td></td>
690 <td>File / SHA256 checksum</td>
691 <td class="size">Size</td>
692 <td></td>
693</tr>
694EOF
695
696 # we switched to .gz for oils-for-unix
697 # note: legacy names for old releases
698 for name in \
699 oils-for-unix-$version.tar.{gz,xz} \
700 oil-$version.tar.{gz,xz} \
701 oil-native-$version.tar.xz; do
702
703 local url="/download/$name" # The server URL
704 local path="../oils.pub__deploy/download/$name"
705
706 # Don't show tarballs that don't exist
707 if [[ $name == oils-for-unix-* && ! -f $path ]]; then
708 continue
709 fi
710 if [[ $name == oil-native-* && ! -f $path ]]; then
711 continue
712 fi
713
714 local checksum
715 checksum=$(sha256sum $path | awk '{print $1}')
716 local size
717 size=$(pretty-size $path)
718
719 # TODO: Port this to oil with "commas" extension.
720
721 # Three columns: date, version, and links
722 cat <<EOF
723 <tr>
724 <td></td>
725 <td class="filename"><a href="$url">$name</a></td>
726 <td class="size">$size</td>
727 </tr>
728 <tr>
729 <td></td>
730 <td colspan=2 class="checksum">$checksum</td>
731 </tr>
732EOF
733 done
734}
735
736this-release-links() {
737 echo '<div class="file-table">'
738 echo '<table>'
739 tarball-links-row-html "$OIL_VERSION"
740 echo '</table>'
741 echo '</div>'
742}
743
744# Turn HTML comment into a download link
745add-date-and-links() {
746 local snippet
747 snippet=$(this-release-links)
748
749 awk -v date=$1 -v snippet="$snippet" '
750 /<!-- REPLACE_WITH_DOWNLOAD_LINKS -->/ {
751 print(snippet)
752 next
753 }
754
755 /<!-- REPLACE_WITH_DATE -->/ {
756 print(date)
757 next
758 }
759
760 # Everything else
761 { print }
762 '
763}
764
765patch-release-pages() {
766 local release_date
767 release_date=$(cat _build/release-date.txt)
768
769 local root=_release/VERSION
770
771 add-date-and-links $release_date < _tmp/release-index.html > $root/index.html
772 add-date-and-links $release_date < _tmp/release-quality.html > $root/quality.html
773}
774
775copy-release-pages() {
776 ### For testing without releasing
777
778 cat < _tmp/release-index.html > $root/index.html
779 cat < _tmp/release-quality.html > $root/quality.html
780}
781
782run-for-release() {
783 ### Build a tree. Requires _build/release-date.txt to exist
784
785 local root=_release/VERSION
786 mkdir -p $root/{doc,test,pub}
787
788 tour
789
790 # Metadata
791 cp -v _build/release-date.txt oils-version.txt $root
792
793 # Docs
794 # Writes _release/VERSION and _tmp/release-index.html
795 all-markdown
796 all-ref
797 all-redirects # backward compat
798
799 fmt-check # Needs to run *after* we build the HTML
800
801 patch-release-pages
802
803 write-metrics
804
805 # Problem: You can't preview it without .wwz!
806 # Maybe have local redirects VERSION/test/wild/ to
807 #
808 # Instead of linking, I should compress them all here.
809
810 copy-web
811
812 if command -v tree >/dev/null; then
813 tree $root
814 else
815 find $root
816 fi
817}
818
819soil-run() {
820 build/stamp.sh write-release-date
821
822 run-for-release
823}
824
825#
826# Golden tests
827#
828# $0 golden-tree
829# $0 determnistic-build # with new code
830# $0 compare-golden
831
832deterministic() {
833 # build without varying timestamp
834 DOC_TIMESTAMP='GOLD' $0 soil-run
835}
836
837golden-tree() {
838 rm -r -f _release/VERSION/ _release/VERSION_gold/
839 deterministic
840 cp -r _release/VERSION/ _release/VERSION_gold
841}
842
843compare-golden() {
844 diff -r -u _release/VERSION_gold _release/VERSION/
845}
846
847"$@"
848