Guide and scripts to locally build the FSFE website or single files of it
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fsfe-preview.sh 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #!/usr/bin/env bash
  2. ########################################################################
  3. # Copyright (C) 2016 Max Mehl <max.mehl@fsfe.org>
  4. ########################################################################
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. ########################################################################
  20. #
  21. # A script to preview XHTML files locally, best to use with FSFE's
  22. # website setup.
  23. #
  24. # Instructions: https://blog.mehl.mx/2016/build-fsfe-websites-locally/
  25. #
  26. #######################################################################
  27. # Coloured and tagged output
  28. function echo_ERR {
  29. echo -e '\033[0;31m'"[ERROR] $1"'\033[0m'
  30. }
  31. function echo_SUC {
  32. echo -e '\033[0;32m'"[SUCCESS] $1"'\033[0m'
  33. }
  34. function echo_WARN {
  35. echo -e '\033[0;33m'"[WARNING] $1"'\033[0m'
  36. }
  37. function echo_INFO {
  38. echo -e '\033[0;37m'"[INFO] $1"'\033[0m'
  39. }
  40. # Check dependencies (stolen from build_main.sh)
  41. deperrors=''
  42. for depend in realpath rsync xsltproc xmllint sed find egrep grep wc make tee date iconv; do
  43. if ! which "$depend" >/dev/null 2>&1; then
  44. deperrors="$depend $deperrors"
  45. fi
  46. done
  47. if [ -n "$deperrors" ]; then
  48. printf '\033[1;31m'
  49. cat <<-EOF
  50. The build script depends on some other programs to function.
  51. Not all of those programs could be located on your system.
  52. Please use your package manager to install the following programs:
  53. EOF
  54. printf '\n\033[0;31m%s\n' "$deperrors"
  55. exit 1
  56. fi >>/dev/stderr
  57. ROOT=$(dirname "$(readlink -f "$0")")
  58. if [ ! -e "$ROOT"/config.cfg ]; then echo "Missing config.cfg file. Edit and rename config.cfg.sample"; exit 1; fi
  59. source "$ROOT"/config.cfg
  60. # Help listing
  61. if [ "$1" = "" ]; then
  62. self=$(basename $0)
  63. echo_WARN "No parameters or variables given!"
  64. echo
  65. echo "Usage: "
  66. echo
  67. echo "$self file.en.xhtml"
  68. echo " -- build single XHTML file from your source to the local web "
  69. echo " directory"
  70. echo " The destination will be on the same relative level of the "
  71. echo " selected XHTML file"
  72. echo " If not a XHTML file, it will be copied to the web directory"
  73. echo
  74. echo "$self --copy picture.png look/style.css"
  75. echo " -- Just copies several files to the local web directory without "
  76. echo " even trying to build them"
  77. exit 0
  78. fi
  79. # Remove trailing slashes
  80. LOC_trunk=$(echo $LOC_trunk | sed 's|/$||')
  81. LOC_out=$(echo $LOC_out | sed 's|/$||')
  82. LOC_trunk_dev=$(echo $LOC_trunk_dev | sed 's|/$||')
  83. # Pure copy
  84. if [ "$1" = "--copy" ]; then
  85. numargs=$(($# - 1))
  86. echo_INFO "Starting a plain mass copy with $numargs file(s)..."
  87. for ((i=1; i <= $numargs; i++)); do
  88. SRC_full=$(realpath $2)
  89. DST_full=$(echo $SRC_full | sed -E "s|$LOC_trunk|$LOC_out|")
  90. echo_INFO "Copying $SRC_full to $DST_full..."
  91. if [ -d "$SRC_full" ] && [ -e "$DST_full" ]; then
  92. rm -rf "$DST_full"
  93. cp -R $SRC_full $DST_full
  94. else
  95. cp -R $SRC_full $DST_full
  96. fi
  97. shift
  98. done
  99. echo_SUC "Copying finished. Files should be visible via the local webserver now."
  100. exit 0
  101. fi
  102. # Run through number of files given as arguments
  103. numargs=$#
  104. echo_INFO "Building $numargs file(s)"
  105. if [ $numargs -gt 1 ]; then
  106. echo
  107. fi
  108. for ((i=1; i <= $numargs; i++)); do
  109. SRC_rel=$1 # e.g. index.en.xhtml
  110. SRC_full=$(realpath $SRC_rel) # Full path to source file
  111. # CHECK whether the source file is located in LOC_trunk
  112. if [ ! $(echo $SRC_full | grep "$LOC_trunk") ]; then
  113. echo_ERR "Given source file is not part of \$LOC_trunk (currently set to \"$LOC_trunk\"). Please only build files which reside in or below this directory. Aborting now."
  114. exit 1
  115. fi
  116. # CHECK whether the source file is a directory (only --copy allowed)
  117. if [ -d "$SRC_full" ]; then
  118. echo_ERR "The given file \"$SRC_full\" is a directory. Those can only be copied by using --copy as a parameter."
  119. echo_ERR "NOTE: Using --copy won't build potential .xhtml files inside a directory. If you have this case, please first copy an empty directory and then build the .xhtml files inside them."
  120. exit 1
  121. fi
  122. # TEST if a DEV SVN directory is used. If yes, copy clean file to this Dir
  123. if [ "$LOC_trunk_dev" != "" ]; then
  124. SRC_full_dev=$(echo $SRC_full | sed -E "s|$LOC_trunk|$LOC_trunk_dev|")
  125. echo_INFO "A \"dirty\" Development SVN directory ($LOC_trunk_dev) is being used. Copy file from clean SVN directory ($LOC_trunk) to Development directory..."
  126. cp $SRC_full $SRC_full_dev # copy file from clean SVN to dev/dirty SVN
  127. SRC_full=$SRC_full_dev
  128. LOC_trunk=$LOC_trunk_dev
  129. fi
  130. echo_INFO "Using file $SRC_full as source..."
  131. ### TRANSFORM XHTML file path
  132. DST_full=$(echo $SRC_full | sed -E "s|$LOC_trunk|$LOC_out|") # replace SVN directory by build/HTTP destination
  133. ### BUILD/COPY FILE depending on file extension
  134. ftype=${SRC_full##*.} # get extension of file
  135. ftype=$(echo "$ftype" | tr '[:upper:]' '[:lower:]')
  136. ftype_build="xhtml" # file types which have to be built
  137. if [[ "$ftype" == @($ftype_build) ]]; then # XHTML file
  138. DST_full=$(echo $DST_full | sed -E "s/$ftype_build/html/") # Replace xhtml by html
  139. echo_INFO "XHTML file detected. Going to build into $DST_full ..."
  140. xmllint --noout "$SRC_full" || (echo; echo "Syntax error in $SRC_full. Error message above. Please fix!"; exit 1)
  141. if [[ "$?" != "0" ]]; then exit 1; fi
  142. $LOC_trunk/build/build_main.sh process_file $SRC_full > $DST_full
  143. else # just copy file
  144. echo_INFO "File type ($ftype) is detected as not to be built. Just linking it to $DST_full ..."
  145. ln -sf $SRC_full $DST_full
  146. fi
  147. ### START WEBSERVER if necessary
  148. if [ "$HTTPD" == "python" ]; then # python
  149. if [ ! $(pgrep -f "pywebserver-fsfe.py $LOC_out") ]; then
  150. echo_INFO "Starting $HTTPD webserver"
  151. nohup "$ROOT/pywebserver-fsfe.py" "$LOC_out" "$HTTPD_port" > "$ROOT"/pywebserver-fsfe.log 2>&1 &
  152. else
  153. echo_INFO "$HTTPD webserver already seems to be running."
  154. fi
  155. elif [ "$HTTPD" == "lighttpd" ]; then
  156. if [ ! $(pgrep lighttpd) ]; then # lighttpd
  157. echo_INFO "Starting $HTTPD webserver"
  158. /usr/sbin/lighttpd -f "$HTTPD_conf"
  159. else
  160. echo_INFO "$HTTPD webserver already seems to be running."
  161. fi
  162. elif [ "$HTTPD" == "" ]; then # empty variable
  163. echo_ERR "No webserver specified. Please define variable HTTPD in config.cfg. Aborting now."
  164. exit 1
  165. else # something custom
  166. echo_WARN "Unknown webserver defined. Probably you won't be able to see the file in your browser. Building it anyway. Please check variable \"HTTPD\" in config.cfg."
  167. fi
  168. ### SHOW RESULTS
  169. DST_rel=$(echo $DST_full | sed "s|$LOC_out||") # relative path
  170. echo_SUC "Finished. File can be viewed at http://localhost:$HTTPD_port$DST_rel"
  171. if [ $numargs -gt 1 ]; then
  172. echo "++++++++++++++++++++++++++++++++++"
  173. fi
  174. shift
  175. done
  176. if [ $numargs -gt 1 ]; then
  177. echo
  178. echo_SUC "$numargs files have been processed. Preview links to each file can be found individually above."
  179. fi