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

838 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 Help Mirror
283=====
284
285<style>
286/* Exclude <pre><code>. Is there a better way to do this? */
287p code, div code, li code, h2 code, h3 code {
288 color: green;
289}
290</style>
291
292This doc mirrors the `--help` for the 3 shell tools in the build sytsem:
293
2941. `configure` - Detect system settings
2951. `_build/oils.sh` - Compile `oils-for-unix` source into an executable
2961. `install` - Install the binary, and symlinks to it
297
298Related: [INSTALL.html](INSTALL.html)
299
300<div id="toc">
301</div>
302
303## configure
304
305```'
306 ./configure --help
307
308 echo '```
309
310## _build/oils.sh
311
312```'
313
314 devtools/release-native.sh gen-oils-sh
315 _build/oils.sh --help
316
317 echo '```
318
319## install
320
321```'
322 ./install --help
323 echo '```'
324}
325
326help-mirror() {
327 ### Mirror --help to HTML
328
329 local md=_tmp/doc/help-mirror.md
330
331 help-mirror-md > $md
332
333 local web_dir='../web'
334 #local css="$web_dir/base.css $web_dir/install.css $web_dir/toc.css"
335 local css="$web_dir/base.css $web_dir/toc.css"
336 render-only $md '' "$css" 'Oils Build Help Mirror'
337}
338
339special() {
340 # TODO: do all READMEs
341 split-and-render mycpp/README.md \
342 $HTML_BASE_DIR/doc/oils-repo/mycpp/README.html \
343 ../../../web
344
345 # TODO: README can just be a pointer to other docs, like "Repo Overview"
346 local web_dir='../../web'
347 render-only 'README.md' $HTML_BASE_DIR/doc/oils-repo/README.html \
348 "$web_dir/base.css $web_dir/manual.css $web_dir/toc.css" 'Oils Source Code'
349
350 help-mirror
351
352 local web_dir='../web'
353 render-only INSTALL.txt '' \
354 "$web_dir/base.css $web_dir/install.css" 'Installing Oils'
355
356 render-only INSTALL-old.txt '' \
357 "$web_dir/base.css $web_dir/install.css" 'Installing Oils - old CPython build'
358
359 # These pages aren't in doc/
360 split-and-render doc/release-index.md _tmp/release-index.html
361 split-and-render doc/release-quality.md _tmp/release-quality.html
362}
363
364all-markdown() {
365 make-dirs
366
367 # TODO: We can set repo_url here! Then we don't need it for most docs.
368 # split_doc.py can return {} if the doc doesn't start with ---
369
370 #for d in doc/index.md doc/known-differences.md doc/*-manual.md \
371 # doc/eggex.md doc/oil-options.md doc/oil-func-proc-block.md; do
372 for d in "${MARKDOWN_DOCS[@]}"; do
373 split-and-render doc/$d.md
374 done
375
376 special
377}
378
379redir-body() {
380 local to_url=$1 # WARNING: no escaping
381 cat <<EOF
382<head>
383 <meta http-equiv="Refresh" content="0; URL=$to_url" />
384</head>
385EOF
386}
387
388redirect-pairs() {
389 # we want want /release/latest/ URLs to still work
390 cat <<EOF
391oil-language-tour ysh-tour
392oil-language-faq ysh-faq
393oil-help ysh-help
394oil-help-topics ysh-help-topics
395ysh-help ref/toc-ysh
396ysh-help-topics ref/toc-ysh
397EOF
398}
399
400all-redirects() {
401 redirect-pairs | while read -r from_page to_page; do
402 redir-body "$to_page.html" | tee "_release/VERSION/doc/$from_page.html"
403 done
404}
405
406# TODO: This could use some CSS.
407man-page() {
408 local root_dir=${1:-_release/VERSION}
409 mandoc -T html doc/osh.1 > $root_dir/osh.1.html
410 ls -l $root_dir
411}
412
413# I want to ship the INSTALL file literally, so just mutate things
414_sed-ext() {
415 sed --regexp-extended -i "$@"
416}
417
418update-src-versions() {
419 # Update tarball names, etc.
420 _sed-ext \
421 "s/[0-9]+\.[0-9]+\.[a-z0-9]+/$OIL_VERSION/g" \
422 doc/release-*.md INSTALL.txt INSTALL-old.txt
423
424 # Update /release/0.8.4/ URL, etc.
425 _sed-ext \
426 "s;/release/[0-9]+\.[0-9]+\.[a-z0-9]+/;/release/$OIL_VERSION/;g" \
427 doc/osh.1
428}
429
430#
431# Test Tools
432#
433
434split-doc-demo() {
435 cat > _tmp/testdoc.md <<EOF
436---
437title: foo
438---
439
440Title
441=====
442
443hello
444
445EOF
446
447 doctools/split_doc.py _tmp/testdoc.md _tmp/testdoc
448
449 head _tmp/testdoc*
450}
451
452#
453# Help is both markdown and text
454#
455
456readonly TMP_DIR=_tmp/doc
457readonly CODE_BLOCK_DIR=_tmp/code-blocks
458readonly TEXT_DIR=_devbuild/help
459readonly HTML_DIR=_release/VERSION
460readonly CODE_DIR=_devbuild/gen
461
462cards-from-indices() {
463 ### Make help cards
464
465 for lang in osh ysh data; do
466 help-gen cards-from-index $lang $TEXT_DIR \
467 < $HTML_DIR/doc/ref/toc-$lang.html
468 done
469}
470
471cards-from-chapters() {
472 ### Turn h3 topics into cards
473
474 local py_out=$CODE_DIR/help_meta.py
475
476 mkdir -p _gen/frontend
477 local cc_prefix=_gen/frontend/help_meta
478
479 help-gen cards-from-chapters $TEXT_DIR $py_out $cc_prefix \
480 $HTML_DIR/doc/ref/chap-*.html
481}
482
483ref-check() {
484 help-gen ref-check \
485 doc/ref/toc-*.md \
486 _release/VERSION/doc/ref/chap-*.html
487}
488
489fmt-check() {
490 PYTHONPATH=.:vendor doctools/fmt_check.py _release/VERSION/doc/ref/*.html
491}
492
493
494write-metrics() {
495 ### Check indexes and chapters against each other
496
497 local out=_release/VERSION/doc/metrics.txt
498
499 # send stderr to the log file too
500 ref-check > $out 2>&1
501
502 echo "Wrote $out"
503}
504
505tour() {
506 ### Build the Tour of YSH, and execute code as validation
507 local name=${1:-ysh-tour}
508
509 split-and-render doc/$name.md
510
511 local work_dir=$REPO_ROOT/_tmp/code-blocks/doc
512
513 mkdir -p $work_dir/lib
514
515 # Files used by module example
516 touch $work_dir/{build,test}.sh
517
518 cat >$work_dir/lines.txt <<'EOF'
519 doc/hello.md
520 "doc/with spaces.md"
521b'doc/with byte \yff.md'
522EOF
523
524 cat >$work_dir/myargs.ysh <<EOF
525const __provide__ = :| proc1 p2 p3 |
526
527proc proc1 {
528 echo proc1
529}
530
531proc p2 {
532 echo p2
533}
534
535proc p3 {
536 echo p3
537}
538EOF
539
540 cat >$work_dir/demo.py <<EOF
541#!/usr/bin/env python
542
543print("hi")
544EOF
545 chmod +x $work_dir/demo.py
546
547 cat >$work_dir/lib/util.ysh <<EOF
548const __provide__ = :| log |
549
550proc log {
551 echo @ARGV >&2
552}
553EOF
554
555 pushd $work_dir
556
557 # Prepend extra code
558 cat >tour.ysh - $name.txt <<EOF
559func myMethod(self) {
560 echo 'myMethod'
561}
562
563func mutatingMethod(self) {
564 echo 'mutatingMethod'
565}
566
567func makeMyObject(x) {
568 var methods = Object(null, {myMethod, 'M/mutatingMethod': mutatingMethod})
569 return (Object(methods, {x}))
570}
571EOF
572
573 # Fix: don't supply stdin!
574 $REPO_ROOT/bin/ysh tour.ysh < /dev/null
575 popd
576
577 # My own dev tools
578 # if test -d ~/vm-shared; then
579 if false; then
580 local path=_release/VERSION/doc/$name.html
581 cp -v $path ~/vm-shared/$path
582 fi
583}
584
585one() {
586 ### Iterate on one doc quickly
587
588 local name=${1:-options}
589
590 split-and-render doc/$name.md
591
592 # Make sure the doc has valid YSH code?
593 # TODO: Maybe need an attribute for OSH or YSH
594 pushd _tmp/code-blocks/doc
595 $REPO_ROOT/bin/ysh $name.txt
596 popd
597
598 if test -d ~/vm-shared; then
599 local out="${name%.md}.html"
600 local path=_release/VERSION/$out
601 cp -v $path ~/vm-shared/$path
602 fi
603}
604
605make-dirs() {
606 mkdir -p $TMP_DIR $CODE_BLOCK_DIR $TEXT_DIR $HTML_DIR/doc
607}
608
609one-ref() {
610 local md=${1:-doc/ref/index.md}
611 split-and-render $md '' '../../web'
612}
613
614all-ref() {
615 ### Build doc/ref in text and HTML. Depends on libcmark.so
616
617 log "Removing $TEXT_DIR/*"
618 rm -f $TEXT_DIR/*
619 make-dirs
620
621 # Make the indexes and chapters
622 for d in doc/ref/*.md; do
623 split-and-render $d '' '../../web'
624 done
625
626 # Note: if we want a $ref-topic shortcut, we might want to use Ninja to
627 # extract topics from all chapters first, and then make help_meta.json, like
628 # we have _devbuild/gen/help_meta.py.
629
630 # Text cards
631 cards-from-indices
632 # A few text cards, and HELP_TOPICS dict for URLs, for flat namespace
633 cards-from-chapters
634
635 return
636 if command -v pysum; then
637 # 19 KB of embedded help, seems OK. Biggest card is 'ysh-option'. Could
638 # compress it.
639 echo 'Size of embedded help:'
640 ls -l $TEXT_DIR | tee /dev/stderr | awk '{print $5}' | pysum
641 fi
642
643 # Better sorting
644 #LANG=C ls -l $TEXT_DIR
645}
646
647_copy-path() {
648 local src=$1 dest=$2
649 mkdir -p $(dirname $dest)
650 cp -v $src $dest
651}
652
653copy-web() {
654 find web \
655 \( -name _tmp -a -prune \) -o \
656 \( -name '*.css' -o -name '*.js' \) -a -printf '%p _release/VERSION/%p\n' |
657 xargs -n 2 -- $0 _copy-path
658}
659
660pretty-size() {
661 local path=$1
662 stat --format '%s' "$path" | python -c '
663import sys
664num_bytes = int(sys.stdin.read())
665print "{:,}".format(num_bytes)
666'
667}
668
669# NOTE: It might be better to link to files like this in the /release/ tree.
670# Although I am not signing them.
671
672# https://nodejs.org/dist/v8.11.4/SHASUMS256.txt.asc
673
674tarball-links-row-html() {
675 local version=$1
676
677 cat <<EOF
678<tr class="file-table-heading">
679 <td></td>
680 <td>File / SHA256 checksum</td>
681 <td class="size">Size</td>
682 <td></td>
683</tr>
684EOF
685
686 # we switched to .gz for oils-for-unix
687 # note: legacy names for old releases
688 for name in \
689 oils-for-unix-$version.tar.{gz,xz} \
690 oil-$version.tar.{gz,xz} \
691 oil-native-$version.tar.xz; do
692
693 local url="/download/$name" # The server URL
694 local path="../oils.pub__deploy/download/$name"
695
696 # Don't show tarballs that don't exist
697 if [[ $name == oils-for-unix-* && ! -f $path ]]; then
698 continue
699 fi
700 if [[ $name == oil-native-* && ! -f $path ]]; then
701 continue
702 fi
703
704 local checksum
705 checksum=$(sha256sum $path | awk '{print $1}')
706 local size
707 size=$(pretty-size $path)
708
709 # TODO: Port this to oil with "commas" extension.
710
711 # Three columns: date, version, and links
712 cat <<EOF
713 <tr>
714 <td></td>
715 <td class="filename"><a href="$url">$name</a></td>
716 <td class="size">$size</td>
717 </tr>
718 <tr>
719 <td></td>
720 <td colspan=2 class="checksum">$checksum</td>
721 </tr>
722EOF
723 done
724}
725
726this-release-links() {
727 echo '<div class="file-table">'
728 echo '<table>'
729 tarball-links-row-html "$OIL_VERSION"
730 echo '</table>'
731 echo '</div>'
732}
733
734# Turn HTML comment into a download link
735add-date-and-links() {
736 local snippet
737 snippet=$(this-release-links)
738
739 awk -v date=$1 -v snippet="$snippet" '
740 /<!-- REPLACE_WITH_DOWNLOAD_LINKS -->/ {
741 print(snippet)
742 next
743 }
744
745 /<!-- REPLACE_WITH_DATE -->/ {
746 print(date)
747 next
748 }
749
750 # Everything else
751 { print }
752 '
753}
754
755patch-release-pages() {
756 local release_date
757 release_date=$(cat _build/release-date.txt)
758
759 local root=_release/VERSION
760
761 add-date-and-links $release_date < _tmp/release-index.html > $root/index.html
762 add-date-and-links $release_date < _tmp/release-quality.html > $root/quality.html
763}
764
765copy-release-pages() {
766 ### For testing without releasing
767
768 cat < _tmp/release-index.html > $root/index.html
769 cat < _tmp/release-quality.html > $root/quality.html
770}
771
772run-for-release() {
773 ### Build a tree. Requires _build/release-date.txt to exist
774
775 local root=_release/VERSION
776 mkdir -p $root/{doc,test,pub}
777
778 tour
779
780 # Metadata
781 cp -v _build/release-date.txt oils-version.txt $root
782
783 # Docs
784 # Writes _release/VERSION and _tmp/release-index.html
785 all-markdown
786 all-ref
787 all-redirects # backward compat
788
789 fmt-check # Needs to run *after* we build the HTML
790
791 patch-release-pages
792
793 write-metrics
794
795 # Problem: You can't preview it without .wwz!
796 # Maybe have local redirects VERSION/test/wild/ to
797 #
798 # Instead of linking, I should compress them all here.
799
800 copy-web
801
802 if command -v tree >/dev/null; then
803 tree $root
804 else
805 find $root
806 fi
807}
808
809soil-run() {
810 build/stamp.sh write-release-date
811
812 run-for-release
813}
814
815#
816# Golden tests
817#
818# $0 golden-tree
819# $0 determnistic-build # with new code
820# $0 compare-golden
821
822deterministic() {
823 # build without varying timestamp
824 DOC_TIMESTAMP='GOLD' $0 soil-run
825}
826
827golden-tree() {
828 rm -r -f _release/VERSION/ _release/VERSION_gold/
829 deterministic
830 cp -r _release/VERSION/ _release/VERSION_gold
831}
832
833compare-golden() {
834 diff -r -u _release/VERSION_gold _release/VERSION/
835}
836
837"$@"
838