#!/bin/bash
# doc/html - HTML generation support

set -e

######
# Configuration

doc_html_config_init() {
	lib_setting_arrays html_tags
	html_tags+=( title main section nav aside article footer )
	html_tags+=( h1 h2 h3 h4 h5 h6 h7 )
	html_tags+=( p a ul ol li em br hr )
	html_tags+=( img code span div pre label script )
	html_tags+=( form input )
	html_tag_init

	lib_setting_arrays html_attrs
}

doc_html_config_check() {
	:
}


######
# HTML Tags

html_tag_init() {
	local tag
	for tag in "${html_tags[@]}"; do
		eval "html_${tag}() { html_tag $tag \"\$@\"; }"
		eval "html_${tag}_file() { html_tag_file $tag \"\$@\"; }"
		eval "html_${tag}_func() { html_tag_func $tag \"\$@\"; }"
	done
}

html_tag() {
	min_args 1 "$@"
	local tag=$1
	shift
	local str="$*"

	local attrs="${html_attrs:+ }${html_attrs[*]}"
	local open=''
	[ "$str" ] || open=' /'
	local close="</$tag>"
	echo -n "<$tag$attrs$open>$str${str:+$close}"
}

# html_tag_file - Prints tag containing contents of names files
#  $1 - tag
# ... - files to use for tag
html_tag_file() {
	min_args 2 "$@"
	local tag=$1
	shift

	local -a html_tag_lines
	for_each html_tag_file_read "$@"
	html_tag "$tag" "${html_tag_lines[@]}"
}

html_tag_file_read() {
	readarray -t html_tag_lines <"$1"
}

# html_tag_func - Prints tag containing output from command
#  $1 - tag
#  $2 - command name
# ... - command arguments
html_tag_func() {
	min_args 2 "$@"
	local tag=$1
	shift
	local tmp
	tmp=$(cmd_tempfile)
	"$@" >"$tmp"
	html_tag_file "$tag" "$tmp"
}


######
# Documents

html_doc() {
	html_doctype
	html_tag_func html html_doc_contents "$@"
}

html_doctype() {
	echo "<!DOCTYPE html>"
}

html_doc_contents() {
	local headfunc=$1
	local bodyfunc=$2
	shift 2
	html_tag_func head $headfunc "$@"
	html_tag_func body $bodyfunc "$@"
}


######
# Head Elements

html_link() {
	has_args 1 "$1"
	local rel=$1
	local -a html_attrs
	list_insert html_attrs "rel=\"$rel\""
	html_tag link
}

html_stylesheet() {
	local href=$1
	local kind=${2:-text/css}
	local -a html_attrs=( "href=\"$href\"" "type=\"$kind\"" )
	html_link stylesheet
}


######
# URLs

html_url() {
	min_args 2 "$@"
	local -a html_attrs
	html_attrs+=( "href=\"$1\"" )
	shift
	html_a "$@"
}

html_url_func() {
	min_args 2 "$@"
	local -a html_attrs
	html_attrs+=( "href=\"$1\"" )
	shift
	html_a_func "$@"
}

html_url_email() { min_args 1 "$@"; html_url "mailto:$1" "${2:$1}"; }


######
# Anchors

html_anchor() {
	min_args 2 "$@"
	local name=$1
	shift
	local -a html_attrs=( "name=\"$name\"" )
	html_a "$@"
}

html_anchor_func() {
	min_args 2 "$@"
	local -a html_attrs
	html_attrs+=( "name=\"$1\"" )
	shift
	html_a_func "$@"
}

View the Developer Guide Index

View the Reference Manual Index


Generated on Thu May 4 18:59:55 PDT 2017 by mcsh i7 v0.19.0.