#!/bin/bash
# dev/tool/pygmentize - Python pygmentize tool support

set -e


######
# Native Dependencies

dev_tool_pygmentize_client_packages=( python-pygments )
dev_tool_pygmentize_server_packages=( )


######
# Library Initialization

dev_tool_pygmentize_lib_init() {
	: debug "dev_tool_pygmentize_lib_init called"
}


######
# Library Configuration

dev_tool_pygmentize_config_init() {
	lib_setting_vars --null pygmentize_lexer
	lib_setting_arrays pygmentize_filters
	lib_setting_vars --null pygmentize_formatter pygmentize_style
	lib_setting_arrays pygmentize_options
	lib_setting_vars --null pygmentize_arg pygmentize_params
	lib_setting_vars --null pygmentize_outfile
}

dev_tool_pygmentize_config_check() {
	: debug "dev_tool_pygmentize_config_check called"
}


######
# pygmentize CLI

pygmentize_dispatch() { lib_cmd_dispatch pygmentize "$@"; }
pygmentize_usage() {
	cat <<USAGE
...
Commands:
	list ...			Lists available pygmentize features
	gen ...				Generates output using pygmentize
USAGE
}

pygmentize_list() { cmd_dispatch "$@"; }
pygmentize_list_usage() {
	cat <<USAGE
...
Commands:
	lexers				Prints list of all input formats
	formats				Prints list of all output formats
	styles				Prints list of all output styles
	filters				Prints list of all filters
USAGE
}

pygmentize_format() { cmd_dispatch "$@"; }
pygmentize_format_usage() {
	cat <<USAGE
...
Commands:
	file <lexer> <format> <file>	Generates output given format/style
	style <format> <style>		Generates output formatter style rules
USAGE
}


######
# Public Interface

pygmentize_filters_add() { for_each pygmentize_filter_add "$@"; }
pygmentize_filter_add() { pygmentize_filters+=( -F "$1" ); }

pygmentize_options_add() { for_each pygmentize_options_add "$@"; }
pygmentize_option_add() { pygmentize_options+=( -O "$1" ); }

pygmentize_run() {
	local -a opts=()
	if [ "$pygmentize_lexer" ]; then
		opts+=( -l "$pygmentize_lexer" )
	elif [ "$pygmentize_style" ]; then
		opts+=( -S "$pygmentize_style" )
	else
		opts+=( -g )
	fi
	[ -z "$pygmentize_formatter" ] || opts+=( -f "$pygmentize_formatter" )
	[ -z "$pygmentize_arg" ] || opts+=( -a "$pygmentize_arg" )
	[ -z "$pygmentize_params" ] || opts+=( -P "$pygmentize_params" )
	[ ${#pygmentize_options[*]} -eq 0 ] \
		|| opts+=( "${pygmentize_options[@]}" )
	[ -z "$pygmentize_outfile" ] || opts+=( -o "$pygmentize_outfile" )
	run pygmentize "${opts[@]}" "$@"
}


######
# Introspection
# These functions in this section query pygmentize to determine its features.

pygmentize_list_lexers() { pygmentize_run -L lexers; }
pygmentize_list_filters() { pygmentize_run -L filters; }
pygmentize_list_formats() { pygmentize_run -L formats; }
pygmentize_list_styles() { pygmentize_run -L styles; }


######
# Formatting
# The functions in this section wrap pygmentize to generate output.

pygmentize_format_style() {
	min_args 2 "$@"
	local pygmentize_formatter=$1
	local pygmentize_style=$2
	pygmentize_run >"$3"
}

pygmentize_format_file() {
	min_args 3 "$@"
	local pygmentize_lexer=$1
	local pygmentize_formatter=$2
	local src=$3
	local pygmentize_outfile="$4"
	pygmentize_run "$src"
}

View the Developer Guide Index

View the Reference Manual Index


Generated on Tue Jul 4 17:00:41 PDT 2017 by mcsh d14 v0.21.0.