#!/usr/bin/env sh

### Script: htmlescape
##
## HTML の特殊文字をエスケープする。
##
## Metadata:
##
##   id - 870b0360-c77b-4ff7-8de8-86a568293b67
##   author - <qq542vev at https://purl.org/meta/me/>
##   version - 1.0.4
##   date - 2022-10-12
##   since - 2019-07-26
##   copyright - Copyright (C) 2019-2022 qq542vev. Some rights reserved.
##   license - <CC-BY at https://creativecommons.org/licenses/by/4.0/>
##   package - w3mplus
##
## See Also:
##
##   * <Project homepage at https://github.com/qq542vev/w3mplus>
##   * <Bug report at https://github.com/qq542vev/w3mplus/issues>
##
## Help Output:
##
## ------ Text ------
## Usage:
##   htmlescape [OPTION]... [FILE]...
##
## Options:
##   -e,     --escape special | single | double 
##                               エスケープする種類を指定する
##   -h,     --help              このヘルプを表示して終了する
##   -v,     --version           バージョン情報を表示して終了する
##
## Exit Status:
##     0 - successful termination
##    64 - command line usage error
##    65 - data format error
##    66 - cannot open input
##    67 - addressee unknown
##    68 - host name unknown
##    69 - service unavailable
##    70 - internal software error
##    71 - system error (e.g., can't fork)
##    72 - critical OS file missing
##    73 - can't create (user) output file
##    74 - input/output error
##    75 - temp failure; user is invited to retry
##    76 - remote error in protocol
##    77 - permission denied
##    78 - configuration error
##   129 - received SIGHUP
##   130 - received SIGINT
##   131 - received SIGQUIT
##   143 - received SIGTERM
## ------------------

readonly 'VERSION=htmlescape 1.0.4'

. 'initialize.sh'
. 'option_error.sh'

# @getoptions
parser_definition() {
	setup REST plus:true abbr:true error:option_error no:0 help:usage \
		-- 'Usage:' "  ${2##*/} [OPTION]... [FILE]..." \
		'' 'Options:'

	param escape  -e --escape init:='special' pattern:'special | single | double' var:'special | single | double' -- 'エスケープする種類を指定する'
	disp  :usage  -h --help    -- 'このヘルプを表示して終了する'
	disp  VERSION -v --version -- 'バージョン情報を表示して終了する'

	msg -- '' 'Exit Status:' \
		'    0 - successful termination' \
		'   64 - command line usage error' \
		'   65 - data format error' \
		'   66 - cannot open input' \
		'   67 - addressee unknown' \
		'   68 - host name unknown' \
		'   69 - service unavailable' \
		'   70 - internal software error' \
		"   71 - system error (e.g., can't fork)" \
		'   72 - critical OS file missing' \
		"   73 - can't create (user) output file" \
		'   74 - input/output error' \
		'   75 - temp failure; user is invited to retry' \
		'   76 - remote error in protocol' \
		'   77 - permission denied' \
		'   78 - configuration error' \
		'  129 - received SIGHUP' \
		'  130 - received SIGINT' \
		'  131 - received SIGQUIT' \
		'  143 - received SIGTERM'
}
# @end

# @gengetoptions parser -i parser_definition parse "${1}"
# @end

eval "$(getoptions parser_definition parse "${0}")"
parse ${@+"${@}"}
eval "set -- ${REST}"

sedScript='s/&/\&amp;/g; s/</\&lt;/g; s/>/\&gt;/g;'

case "${escape}" in
	'special') sedScript="${sedScript} s/\"/\\&quot;/g; s/'/\\&#39;/g";;
	'single') sedScript="${sedScript} s/'/\\&#39;/g";;
	'double') sedScript="${sedScript} s/\"/\\&quot;/g";;
esac

# https://qiita.com/richmikan@github/items/5158eac47467d8a29056

{
	cat -- ${@+"${@}"}
	echo
} |	sed -e "${sedScript}" -- ${@+"${@}"} | awk -- 'BEGIN {
	getline line

	printf("%s", line)

	while(0 < (getline line)) {
		printf("\n%s", line)
	}
}'
