OILS / build / doc.sh View on Github | oilshell.org

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