Home | History | Annotate | Download | only in tools
      1 #!/bin/sh
      2 #
      3 # Copyright (C) 2013 The Android Open Source Project
      4 #
      5 # Licensed under the Apache License, Version 2.0 (the "License");
      6 # you may not use this file except in compliance with the License.
      7 # You may obtain a copy of the License at
      8 #
      9 #      http://www.apache.org/licenses/LICENSE-2.0
     10 #
     11 # Unless required by applicable law or agreed to in writing, software
     12 # distributed under the License is distributed on an "AS IS" BASIS,
     13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14 # See the License for the specific language governing permissions and
     15 # limitations under the License.
     16 #
     17 
     18 # include common function and variable definitions
     19 . `dirname $0`/prebuilt-common.sh
     20 
     21 DEFAULT_OUT_DIR=$TMPDIR/docs
     22 
     23 IN_DIR=
     24 OUT_DIR=$DEFAULT_OUT_DIR
     25 FORCE=
     26 RUN_CHECKS=
     27 
     28 PROGRAM_PARAMETERS=""
     29 PROGRAM_DESCRIPTION="Rebuild the HTML documentation from the Markdown text.
     30 
     31 Rebuild the NDK html documentation from the Markdown input source files
     32 in \$NDK/docs/text. See \$NDK/docs/tools/README for the input file format.
     33 
     34 Builds are incremental, but you can use --force to rebuild everything.
     35 
     36 Output files are placed in $DEFAULT_OUT_DIR, unless --out-dir=<path>
     37 is used."
     38 
     39 register_var_option "--force" FORCE "Rebuild all documentation"
     40 
     41 register_var_option "--run-checks" RUN_CHECKS "Run internal consistency checks"
     42 
     43 register_option "--in-dir=<path>" do_in_dir "Specify input directory" "\$NDK/docs/text"
     44 do_in_dir () {
     45   IN_DIR=$1
     46 }
     47 
     48 register_var_option "--out-dir=<path>" OUT_DIR "Specify output directory"
     49 
     50 MARKDOWN=markdown_py
     51 register_var_option "--markdown=<executable>" MARKDOWN "Specify markdown executable to use"
     52 
     53 extract_parameters "$@"
     54 
     55 # Path to a Markdown filter program that is used to perform a few NDK-specific
     56 # substitution in the input .text files. Note that this tool generates Markdown
     57 # text, not HTML. TODO(digit): Use Markdown Extensions API to perform something
     58 # similar, which is a lot more work though.
     59 SUBST_PROGRAM=$ANDROID_NDK_ROOT/docs/tools/ndk-markdown-substitutions.py
     60 
     61 if [ "$RUN_CHECKS" ]; then
     62   # Run unit tests for our $SUBST_PROGRAM
     63   $SUBST_PROGRAM --run-checks
     64   exit $?
     65 fi
     66 
     67 if [ -z "$IN_DIR" ]; then
     68   IN_DIR=$ANDROID_NDK_ROOT/docs/text
     69 fi
     70 IN_DIR=${IN_DIR%%/}
     71 log "Input directory: $IN_DIR"
     72 
     73 if [ -z "$OUT_DIR" ]; then
     74   OUT_DIR=$ANDROID_NDK_ROOT/docs
     75 fi
     76 OUT_DIR=${OUT_DIR%%/}
     77 log "Output directory: $OUT_DIR"
     78 
     79 TEMP_DIR=$NDK_TMPDIR/build-docs
     80 mkdir -p "$TEMP_DIR"
     81 fail_panic "Could not create temporary directory"
     82 
     83 SRC_FILES=$(find "$IN_DIR" -name "*.text")
     84 for SRC_FILE in $SRC_FILES; do
     85   # Compute destination file.
     86   DST_FILE=${SRC_FILE%%.text}.html
     87   DST_FILE=$OUT_DIR${DST_FILE##$IN_DIR}
     88   DST_DIR=$(dirname "$DST_FILE")
     89   # Only rebuild file is source is older than destination, unless --force
     90   # is being used.
     91   if [ -z "$FORCE" -a -f "$DST_FILE" -a "$SRC_FILE" -ot "$DST_FILE" ]; then
     92     log "Skipping: $SRC_FILE --> $DST_FILE"
     93   else
     94     log "Converting: $SRC_FILE --> $DST_FILE"
     95     mkdir -p "$DST_DIR"
     96     fail_panic "Could not create output directory: $DST_DIR"
     97     # Apply custom substitutions.
     98     TMP_FILE=$TEMP_DIR/$(basename "$DST_FILE").temp
     99     run $SUBST_PROGRAM --output "$TMP_FILE" "$SRC_FILE"
    100     fail_panic "Could not pre-process $SRC_FILE!"
    101     # Process with Markdown.
    102     run run $MARKDOWN --file="$DST_FILE" "$TMP_FILE"
    103     fail_panic "Could not convert $SRC_FILE!"
    104   fi
    105 done
    106 
    107 run rm -rf "$TEMP_DIR"
    108 
    109 if [ "$OUT_DIR" != "$DEFAULT_OUT_DIR" ]; then
    110   dump "Done!"
    111 else
    112   dump "Done, see $OUT_DIR"
    113 fi
    114 
    115