1222 lines
31 KiB
Bash
1222 lines
31 KiB
Bash
#!/bin/bash
|
||
# file: gsl__post_makers
|
||
# Folder: /var/lib/gsl/scripts
|
||
# By echolib (XMPP: im@echolib.re)
|
||
# License: GNU AFFERO GENERAL PUBLIC LICENSE Version 3, 19 November 2007
|
||
|
||
|
||
#----------------------------------------------------------------------
|
||
# ---------------
|
||
# SIDEBAR Section
|
||
# ---------------
|
||
#----------------------------------------------------------------------
|
||
#======================================================================
|
||
# Get arguments from COMMAND (sidebar)
|
||
# $1: process
|
||
# $2: position
|
||
# $3: file
|
||
#======================================================================
|
||
sidebar__OPTIONS() {
|
||
|
||
# Check OPTIONS except for "latest"
|
||
# ---------------------------------
|
||
sidebar__check_args() {
|
||
# Position
|
||
[[ "$1" = *[!0-9]* ]] \
|
||
&& gsl__invalid_option "$1" "position (1 to $site_max_list)"
|
||
|
||
(( "$1" == "0" || "$1" > "$site_max_list" )) \
|
||
&& gsl__invalid_option "$1" "position (1 to $site_max_list)"
|
||
|
||
position="$1"
|
||
|
||
# file post
|
||
if__file "$2" post
|
||
gsl_post="$2"
|
||
! [[ -f "$gsl_dir_db_domain/$gsl_this_post.db" ]] \
|
||
&& echo "! Article not check yet" \
|
||
&& exit
|
||
}
|
||
|
||
case "$1" in
|
||
add|-a)
|
||
sidebar__check_args "$2" "$3"
|
||
;;
|
||
|
||
replace|-r)
|
||
sidebar__check_args "$2" "$3"
|
||
;;
|
||
|
||
latest|-l|oldest|-o)
|
||
position="1 to $site_max_list"
|
||
;;
|
||
|
||
*)
|
||
gsl__invalid_option "$1" "add|replace|latest|oldest"
|
||
;;
|
||
esac
|
||
|
||
# Start process
|
||
create__HTML_translation
|
||
sidebar__create "$1" "$position"
|
||
}
|
||
|
||
|
||
#======================================================================
|
||
# Sidebar: create /templates/sidebar.html
|
||
# $1: process
|
||
# $2: Position
|
||
# $3: DB file
|
||
#======================================================================
|
||
sidebar__create() {
|
||
log__process_begin "Sidebar"
|
||
|
||
gsl__logs_print -i -s $1 \
|
||
"Position: $2 in final sidebar" \
|
||
"$gsl_dir_domain_tpl/sidebar.html"
|
||
|
||
mkdir -p "$gsl_dir_domain_sidebar"
|
||
case "$1" in
|
||
latest|-l) sort_arg='-k2' ;;
|
||
oldest|-o) sort_arg='-k2r' ;;
|
||
esac
|
||
|
||
case "$1" in
|
||
latest|-l|oldest|-o)
|
||
sidebar_pos=0
|
||
while read -r "sorted_file"
|
||
do
|
||
((sidebar_pos++))
|
||
sidebar_HTML_item="$gsl_dir_domain_sidebar/$sidebar_pos.html"
|
||
db_name=`awk -F: '{print $1}' <<<"$sorted_file"`
|
||
source "$db_name"
|
||
gsl_post=`basename "$article_URI"`
|
||
|
||
gsl__logs_print -i -sdb $1 \
|
||
"Position: $sidebar_pos ; $article_epoch" \
|
||
"$sidebar_HTML_item"
|
||
|
||
srv__files datas
|
||
create__HTML_sidebar_item "$sidebar_HTML_item"
|
||
done < <(grep "Date" "$gsl_dir_db_domain/"*.gsl.db \
|
||
| sort -t= $sort_arg \
|
||
| head -n $site_max_list)
|
||
;;
|
||
|
||
replace|-r)
|
||
gsl__logs_print -i -sdb $1 \
|
||
"Position: $2" \
|
||
"$gsl_dir_domain_sidebar/$2.html"
|
||
|
||
source "$gsl_dir_db_domain/$gsl_this_post.db"
|
||
srv__files datas
|
||
create__HTML_sidebar_item "$gsl_dir_domain_sidebar/$2.html"
|
||
;;
|
||
|
||
add|-a)
|
||
for i in `seq $site_max_list -1 $2`
|
||
do
|
||
if (( $i == $site_max_list)) &&
|
||
[[ -f "$gsl_dir_domain_sidebar/$i.html" ]];then
|
||
rm -f "$gsl_dir_domain_sidebar/$i.html"
|
||
|
||
else
|
||
[[ -f "$gsl_dir_domain_sidebar/$i.html" ]] \
|
||
&& mv "$gsl_dir_domain_sidebar/$i.html" \
|
||
"$gsl_dir_domain_sidebar/$(( i + 1 )).html"
|
||
fi
|
||
done
|
||
|
||
gsl__logs_print -i -sdb $1 \
|
||
"Position: $2" \
|
||
"$gsl_dir_domain_sidebar/$2.html"
|
||
|
||
source "$gsl_dir_db_domain/$gsl_this_post.db"
|
||
srv__files datas
|
||
create__HTML_sidebar_item "$gsl_dir_domain_sidebar/$2.html"
|
||
;;
|
||
esac
|
||
|
||
create__HTML_sidebar "$1" "$gsl_dir_domain_tpl/sidebar.html"
|
||
|
||
gsl__logs_print -w -sdb "Tips" \
|
||
"Do not forget to sync ; use gsl sync" \
|
||
"$gsl_dir_domain_tpl/sidebar.html"
|
||
|
||
unset gsl_post
|
||
log__process_end "Sidebar"
|
||
}
|
||
|
||
|
||
#----------------------------------------------------------------------
|
||
# ------------
|
||
# MAKE Section
|
||
# ------------
|
||
#----------------------------------------------------------------------
|
||
#======================================================================
|
||
# Get arguments from COMMAND (make)
|
||
#======================================================================
|
||
make__OPTIONS() {
|
||
gsl_proc="wip"
|
||
|
||
while test "$2"
|
||
do
|
||
case "$2" in
|
||
-F)
|
||
gsl_force_make=true
|
||
log_info_force="Force "
|
||
;;
|
||
|
||
*".gsl")
|
||
if__file "$2" post
|
||
gsl_post="$2"
|
||
post_only="$2"
|
||
;;
|
||
|
||
-N)
|
||
gsl_make_newer=true
|
||
unset gsl_force_make
|
||
;;
|
||
|
||
*)
|
||
[[ "$gsl_post" ]] || [[ $gsl_force_make ]] \
|
||
&& echo "! Unused argument: $2" \
|
||
|| gsl__invalid_option "$2" "-F|(FILE)"
|
||
;;
|
||
esac
|
||
shift
|
||
done
|
||
|
||
[[ "$gsl_post" ]] || log_info_all=" ALL"
|
||
|
||
log__process_begin "${log_info_force}Make${log_info_all}"
|
||
posts__loop make
|
||
|
||
# For logs ; set post or unset if all
|
||
[[ $log_info_all ]] && unset gsl_post
|
||
[[ $post_only ]] && gsl_post="$post_only"
|
||
|
||
log__process_end "${log_info_force}Make${log_info_all}"
|
||
}
|
||
|
||
|
||
#=======================================================================
|
||
# From: posts__loop() ; Convert Post to HTML
|
||
# But, before: Compare hash ans existing files
|
||
#=======================================================================
|
||
Makers() {
|
||
# Check if hashes not match
|
||
! [[ "$gsl_do_make" ]] && return
|
||
|
||
# OPT: -F (force)
|
||
gsl_do_make=true
|
||
if [[ "$gsl_force_make" ]];then
|
||
if [[ "$gsl_srv_wip_file" ]];then
|
||
gsl__logs_print -w -g -M \
|
||
"WIP: Force again HTML $article_Type" \
|
||
"$PWD/$gsl_post"
|
||
fi
|
||
|
||
# Not force
|
||
else
|
||
# if file exists (value is true)
|
||
if [[ "$gsl_srv_wip_file" ]];then
|
||
if (( $article_Status_chk == $article_Status_wip ));then
|
||
gsl__logs_print -w -g -M \
|
||
"WIP: Already done ; Use -F to force" \
|
||
"$gsl_srv_post_wip"
|
||
unset gsl_do_make
|
||
fi
|
||
fi
|
||
fi
|
||
|
||
! [[ "$gsl_do_make" ]] && return
|
||
|
||
#----------------------------------------------------------------------
|
||
# Set HTML to replace first marker
|
||
# Closed HTML are static > in gsl.conf
|
||
#----------------------------------------------------------------------
|
||
HTML_icode_start="<code class=\"${site_css}_icode\">"
|
||
HTML_strong_start="<strong class=\"${site_css}_strong\">"
|
||
HTML_em_start="<i class=\"${site_css}_em\">"
|
||
HTML_strike_start="<span class=\"${site_css}_strike\">"
|
||
HTML_bold_start="<b class=\"${site_css}_bold\">"
|
||
|
||
#----------------------------------------------------------------------
|
||
# makers modules
|
||
#----------------------------------------------------------------------
|
||
# Titles
|
||
read__line_with "^#[1-6]" "make" "title" "content" "$gsl_tmp_post"
|
||
|
||
# Paragraphs
|
||
make__paragraphs "$gsl_mark_p_start " "$gsl_mark_p_close" "$gsl_tmp_post"
|
||
|
||
# New Line (convert | in <br />)
|
||
make__br "$gsl_mark_newline" "$gsl_tmp_post"
|
||
|
||
# abbr
|
||
read__line_with "^$gsl_marker_abbr" "make" \
|
||
"abbr" "metas" "$gsl_tmp_head"
|
||
|
||
# links
|
||
read__line_with "^$gsl_marker_link" "make" \
|
||
"link" "metas" "$gsl_tmp_head"
|
||
|
||
# flink
|
||
read__line_with "^$gsl_marker_flink" "make" \
|
||
"flink" "metas" "$gsl_tmp_head"
|
||
|
||
#----------------------------------------------------------------------
|
||
# Convert words between marks (icode, strong, emphasis, bolds)
|
||
# If these markers are in inline code, replace by temp marker + sed back
|
||
# Strong
|
||
make__loop_line "$gsl_mark_strong" "between" "$gsl_tmp_post" \
|
||
"$HTML_strong_start" "$HTML_strong_close" "ß"
|
||
# Bold
|
||
make__loop_line "$gsl_mark_bold" "between" "$gsl_tmp_post" \
|
||
"$HTML_bold_start" "$HTML_bold_close" "ð"
|
||
# Emphasis
|
||
make__loop_line "$gsl_mark_em" "between" "$gsl_tmp_post" \
|
||
"$HTML_em_start" "$HTML_em_close" "đ"
|
||
# Strike
|
||
make__loop_line "$gsl_mark_strike" "between" "$gsl_tmp_post" \
|
||
"$HTML_strike_start" "$HTML_strike_close" "ŋ"
|
||
# ! At last do : iCodes
|
||
make__loop_line "$gsl_mark_icode" "between" "$gsl_tmp_post" \
|
||
"$HTML_icode_start" "$HTML_icode_close"
|
||
|
||
# For lines having markers in inline-code, replace temp markers
|
||
if [[ ${incode_line[@]} ]];then
|
||
for ln in ${incode_line[@]}
|
||
do
|
||
sed -i "${ln}s^ß^•^g" "$gsl_tmp_post" # Strong
|
||
sed -i "${ln}s^ð^·^g" "$gsl_tmp_post" # Bold
|
||
sed -i "${ln}s^đ^”^g" "$gsl_tmp_post" # Emphasis
|
||
sed -i "${ln}s^ŋ^×^g" "$gsl_tmp_post" # Strike
|
||
done
|
||
fi
|
||
#----------------------------------------------------------------------
|
||
|
||
# Image
|
||
read__line_with "^$gsl_marker_image" "make" \
|
||
"image" "metas" "$gsl_tmp_head"
|
||
|
||
# lists
|
||
make__lists "$gsl_tmp_post"
|
||
|
||
# Quotes
|
||
make__quotes "$gsl_tmp_post"
|
||
|
||
# File code
|
||
read__line_with "^$gsl_marker_fcode" "make" \
|
||
"fcode" "metas" "$gsl_tmp_head"
|
||
|
||
# At the very end, make brut codes
|
||
read__line_with "^$gsl_marker_fbrut" "make" \
|
||
"fbrut" "metas" "$gsl_tmp_head"
|
||
|
||
# a11y + div between titles
|
||
make__div_title "$gsl_tmp_post"
|
||
|
||
|
||
# a11y (tabulation)
|
||
spc=' ' # 8 spaces. Main content (2*4)
|
||
sed -i -e "s|^|$spc|" "$gsl_tmp_post"
|
||
|
||
# Create wip HTML page
|
||
create__HTML_page
|
||
|
||
rm -f "$gsl_tmp_post"
|
||
DB__set_status "wip"
|
||
}
|
||
|
||
|
||
#======================================================================
|
||
# Loop called by each content module, till nothing found
|
||
# $1: marker
|
||
# $2: mark (between)
|
||
# $3: file
|
||
# $4: HTML Start
|
||
# $5: HTML Close
|
||
#======================================================================
|
||
make__loop_line() {
|
||
read__line_with \
|
||
"$1" "make" \
|
||
"$2" "content" \
|
||
"$3" "$4" "$5" "$6"
|
||
|
||
! [[ "$gsl_line" ]] && return
|
||
}
|
||
|
||
|
||
#======================================================================
|
||
# Filter content
|
||
# Called from read__line_with() in gsl__do_commons
|
||
# $1: marker
|
||
# $2: mark
|
||
# $3: file
|
||
# $4: HTML start
|
||
# $5: HTML close
|
||
#======================================================================
|
||
make__headers_from_filter() {
|
||
case "$2" in
|
||
title) make__titles "$3" ;;
|
||
between) make__words "$1" "$2" "$3" "$4" "$5" "$6" ;;
|
||
abbr) make__abbrs "$5" "$gsl_tmp_post" ;;
|
||
link|flink) make__links "$gsl_tmp_post" "$2" ;;
|
||
fcode) make__file_codes "$gsl_tmp_post" ;;
|
||
fbrut) make__file_bruts "$gsl_tmp_post" ;;
|
||
image) make__file_images "$gsl_tmp_post" ;;
|
||
esac
|
||
}
|
||
|
||
|
||
#======================================================================
|
||
# From make loop_line()
|
||
# Convert words between marks (strong, icode, emphasis)
|
||
# $1: marker
|
||
# #2: mark (betwee)
|
||
# $3: file
|
||
# $4: HTML start
|
||
# $5: HTML close
|
||
# $6: temp marker (if legacy in inline code)
|
||
#======================================================================
|
||
make__words() {
|
||
icm=0
|
||
for (( i=0; i<${#gsl_line}; i++ ))
|
||
do
|
||
|
||
# For markers not "inline-code", set characters to bypass
|
||
case "${gsl_line:$i:1}" in
|
||
"$gsl_mark_icode")
|
||
if [[ "$1" != "$gsl_mark_icode" ]];then
|
||
icm=$(( icm + 1 ))
|
||
incode=true;incode_s=$i
|
||
icm_p=$(( $icm % 2 ))
|
||
[[ $icm_p -eq 0 ]] && unset incode
|
||
fi
|
||
;;
|
||
esac
|
||
|
||
case "${gsl_line:$i:1}" in
|
||
"$1")
|
||
if [[ $incode ]] && (( "$i" > $incode_s ));then
|
||
# Get line containing temp markers and replace
|
||
[[ `grep "$ln" <<<"${incode_line[@]}"` ]] || incode_line+=($ln)
|
||
sed -i -r "${ln}s|^(.{"$i"})"$1"|\1$6|" "$3"
|
||
continue
|
||
fi
|
||
|
||
if [[ $open ]];then
|
||
unset open
|
||
sed -i -r "${ln}s|^(.{"$i"})"$1"|\1$5|" "$3"
|
||
break
|
||
|
||
else
|
||
open=true
|
||
sed -i -r "${ln}s|^(.{"$i"})"$1"|\1$4|" "$3"
|
||
break
|
||
fi
|
||
;;
|
||
esac
|
||
done
|
||
|
||
make__loop_line "$1" "$2" "$3" "$4" "$5" "$6"
|
||
}
|
||
|
||
|
||
#======================================================================
|
||
# Make HTML Titles
|
||
# $1: file (from $5 of read__line_with)
|
||
#======================================================================
|
||
make__titles() {
|
||
while read -r "t" "content"
|
||
do
|
||
tn=${t: -1}
|
||
HTML_title=`
|
||
printf '%s%s%s\n' \
|
||
"<h$tn class=\"${site_css}_title ${site_css}_title-$tn\">" \
|
||
"$content" \
|
||
"</h$tn>"`
|
||
|
||
sed -i "${ln}s^.*^$HTML_title^" "$1"
|
||
done <<<"$gsl_line"
|
||
}
|
||
|
||
|
||
#======================================================================
|
||
# Make HTML paragraphs
|
||
# $1: marker open
|
||
# $2: marker close
|
||
# $3: file
|
||
#======================================================================
|
||
make__paragraphs() {
|
||
[[ `grep "^$2$" "$3"` ]] \
|
||
&& sed -i "s,^$2$,$HTML_p_close,g" "$3" \
|
||
|| return
|
||
|
||
while read -r "lnpm" "class"
|
||
do
|
||
ln=`awk -F: '{print $1}' <<<$lnpm`
|
||
pm=`awk -F: '{print $2}' <<<$lnpm`
|
||
|
||
if [[ $class ]];then
|
||
HTML_p="<p class=\"${site_css}_p $class\">"
|
||
sed -i "${ln}s^$pm $class^$HTML_p^" "$3"
|
||
|
||
else
|
||
HTML_p="<p class=\"${site_css}_p\">"
|
||
sed -i "${ln}s^$pm^$HTML_p^" "$3"
|
||
fi
|
||
done < <(grep -n '^($\|^'"$1" "$3")
|
||
}
|
||
|
||
|
||
#======================================================================
|
||
# Convert new line "|" to <br /> ($HTML_br)
|
||
# $1: marker
|
||
# $2: file
|
||
#======================================================================
|
||
make__br() {
|
||
[[ `grep "^$1$" "$2"` ]] \
|
||
&& sed -i "s,^$1$,$HTML_br,g" "$2"
|
||
}
|
||
|
||
|
||
#======================================================================
|
||
# HTML abbr: SHORT must have 1 space before/after, or both
|
||
# $1: file header
|
||
# $2: file post
|
||
#======================================================================
|
||
make__abbrs() {
|
||
! [[ "$header_f1" ]] && return
|
||
|
||
abbr_title="title=\"$header_f2\""
|
||
abbr_class="class=\"${site_css}_abbr\""
|
||
abbr_words="<abbr $abbr_class $abbr_title>$header_f1</abbr>"
|
||
|
||
sed -i "s^ $header_f1 ^ $abbr_words ^g" "$2"
|
||
sed -i "s^$header_f1 ^$abbr_words ^g" "$2"
|
||
sed -i "s^ $header_f1^ $abbr_words^g" "$2"
|
||
}
|
||
|
||
|
||
#======================================================================
|
||
# HTML link: + to open in new window
|
||
# $1: file post
|
||
# $2: mark type
|
||
#======================================================================
|
||
make__links() {
|
||
! [[ "$header_f1" ]] && return
|
||
|
||
[[ "$header_f3" ]] \
|
||
&& link_title=" title=\"$header_f3\""
|
||
|
||
# link or flink
|
||
case "$2" in
|
||
"link")
|
||
mt="$gsl_mark_link"
|
||
cssn="link"
|
||
unset luri
|
||
;;
|
||
|
||
"flink")
|
||
mt="$gsl_mark_flink"
|
||
cssn="link-file"
|
||
luri="${srv_uri}files/"
|
||
;;
|
||
esac
|
||
|
||
link_class="class=\"${site_css}_$cssn\""
|
||
link_class2="class=\"${site_css}_$cssn ${site_css}_$cssn-window\""
|
||
link_words=`
|
||
printf '%s%s%s\n' \
|
||
"<a $link_class href=\"$luri$header_f2\"" \
|
||
"$link_title>" \
|
||
"$header_f1</a>"`
|
||
|
||
link_words2=`
|
||
printf '%s%s%s\n' \
|
||
"<a target=\"_blank\" $link_class2 href=\"$luri$header_f2\"" \
|
||
"$link_title>" \
|
||
"$header_f1</a>"`
|
||
|
||
sed -i "s^$mt${header_f1}+^$link_words2^g" "$1"
|
||
sed -i "s^$mt$header_f1^$link_words^g" "$1"
|
||
}
|
||
|
||
|
||
#======================================================================
|
||
# HTML code : create tmp file with source code
|
||
# converted in HTML version with <pre>, and replace mark with tmp file
|
||
# $1: file post
|
||
#======================================================================
|
||
make__file_codes() {
|
||
pre_class="class=\"${site_css}_code-block ${site_css}_code-block-$header_f1\""
|
||
pre_css_line="class=\"${site_css}_code-line\""
|
||
pre_css_content="class=\"${site_css}_code-content\""
|
||
pre_css_source="class=\"${site_css}_code-link\""
|
||
pre_css_p="class=\"${site_css}_code-p\""
|
||
|
||
# No Alt-Text ; generate
|
||
if [[ "$header_f3" ]];then
|
||
pre_title=" title=\"Code: source\""
|
||
|
||
else
|
||
header_f3="Code: source"
|
||
fi
|
||
|
||
gsl_tmp_code=`mktemp`
|
||
cat << EOCODE > "$gsl_tmp_code"
|
||
<pre $pre_class>
|
||
$(while read -r "code_ln" "code_content"
|
||
do
|
||
printf '%2s%s%s\n' "" \
|
||
"<span $pre_css_line>$code_ln</span>" \
|
||
"<span $pre_css_content>$code_content</span>"
|
||
done < <(cat -n "$gsl_dir_domain_files/$header_f2"))
|
||
</pre>
|
||
<div $pre_css_p>
|
||
<a $pre_css_source
|
||
target="_blank"
|
||
href="${srv_uri}files/$header_f2"$pre_title>
|
||
$header_f3
|
||
</a>
|
||
</div>
|
||
EOCODE
|
||
|
||
# for/from make__file_codes (Replace mark till no more)
|
||
# $1: file post
|
||
#-------------------------------------------------
|
||
make__file_codes_replace() {
|
||
lncode=`
|
||
grep -n "$gsl_mark_fcode:$header_f1" "$1" \
|
||
| head -n1 \
|
||
| awk -F: '{print $1}'`
|
||
|
||
[[ "$lncode" ]] || return
|
||
|
||
sed -i "${lncode}r $gsl_tmp_code" "$1"
|
||
sed -i "${lncode}d" "$1"
|
||
make__file_codes_replace "$1"
|
||
}
|
||
|
||
make__file_codes_replace "$1"
|
||
rm -f "$gsl_tmp_code"
|
||
}
|
||
|
||
|
||
#======================================================================
|
||
# Replace marks with brut file. Must be done at the end !
|
||
# $1: file post
|
||
#======================================================================
|
||
make__file_bruts() {
|
||
lncode=`
|
||
grep -n "$gsl_mark_fbrut:$header_f1" "$1" \
|
||
| head -n1 \
|
||
| awk -F: '{print $1}'`
|
||
|
||
[[ "$lncode" ]] || return
|
||
|
||
sed -i "${lncode}r $gsl_dir_domain_files/$header_f2" "$1"
|
||
sed -i "${lncode}d" "$1"
|
||
make__file_bruts "$1"
|
||
}
|
||
|
||
|
||
#======================================================================
|
||
# Make images
|
||
# $1: file post
|
||
#======================================================================
|
||
make__file_images() {
|
||
unset ${!image_@}
|
||
link_class="${site_css}_image-link"
|
||
this_image=`grep -n "$gsl_mark_image:$header_f1" "$1" | head -n 1`
|
||
[[ "$this_image" ]] || return
|
||
echo "$this_image"
|
||
|
||
# local link with "+" or external link if http*
|
||
# $1: field
|
||
make__file_image_link() {
|
||
case "$1" in
|
||
"+") image_link="${srv_uri}images/$header_f2" ;;
|
||
"http"*|"ftp"*) image_link="$image_field_link" ;;
|
||
esac
|
||
}
|
||
|
||
mark_image=${this_image#*:} # Get content mark to replace
|
||
while IFS=: read -r "ln" \
|
||
"marker" \
|
||
"f1" \
|
||
"class" \
|
||
"width" \
|
||
"height" \
|
||
"target"
|
||
do
|
||
# Class
|
||
if [[ "$class" ]];then
|
||
image_field_link="$class$width"
|
||
case "$class" in
|
||
R|r|D|d) image_class="${site_css}_image-right" ;;
|
||
G|g|L|l) image_class="${site_css}_image-left" ;;
|
||
C|c|D|d) image_class="${site_css}_image-center" ;;
|
||
|
||
# Use this class if not known target
|
||
*)
|
||
make__file_image_link "$class"
|
||
if [[ $image_link ]];then
|
||
image_class="${site_css}_image-center"
|
||
unset width height
|
||
|
||
else
|
||
image_class="$class"
|
||
link_class+=" $class"
|
||
fi
|
||
;;
|
||
esac
|
||
|
||
else
|
||
image_class="${site_css}_image-center"
|
||
fi
|
||
|
||
# Width
|
||
if [[ "$width" ]];then
|
||
image_field_link="$width$height"
|
||
make__file_image_link "$width"
|
||
if [[ $image_link ]];then
|
||
unset width height target
|
||
|
||
else
|
||
image_width=`
|
||
grep -Eo '[[:alpha:]]+|[0-9]+' \
|
||
<<<"$width" \
|
||
| head -n 1`
|
||
if (( $image_width == 0 ));then
|
||
unset width
|
||
|
||
else
|
||
image_unitw=${width/$image_width/}
|
||
! [[ "$image_unitw" ]] && image_unitw="px"
|
||
fi
|
||
fi
|
||
fi
|
||
|
||
# Height
|
||
if [[ "$height" ]];then
|
||
image_field_link="$height$target"
|
||
make__file_image_link "$height"
|
||
if [[ $image_link ]];then
|
||
unset height target
|
||
|
||
else
|
||
image_height=`
|
||
grep -Eo '[[:alpha:]]+|[0-9]+' \
|
||
<<<"$height" \
|
||
| head -n 1`
|
||
if (( $image_height == 0 ));then
|
||
unset height
|
||
|
||
else
|
||
image_unith=${height/$image_height/}
|
||
! [[ "$image_unith" ]] && image_unith="px"
|
||
fi
|
||
fi
|
||
fi
|
||
|
||
# Target
|
||
if [[ "$target" ]];then
|
||
image_field_link="$target"
|
||
make__file_image_link "$target"
|
||
fi
|
||
|
||
|
||
# Create HTML width & height for <img ...
|
||
if [[ "$height" && "$width" ]];then
|
||
image_style=`
|
||
printf ' %s%s%s\n' \
|
||
"style=\"" \
|
||
"width:$image_width$image_unitw;" \
|
||
"height:$image_height$image_unith;\""`
|
||
|
||
elif [[ "$width" ]];then
|
||
image_style=`
|
||
printf ' %s%s\n' \
|
||
"style=\"" \
|
||
"width:$image_width$image_unitw;\""`
|
||
|
||
elif [[ "$height" ]];then
|
||
image_style=`
|
||
printf ' %s%s\n' \
|
||
"style=\"" \
|
||
"height:$image_height$image_unith;\""`
|
||
fi
|
||
|
||
# Create HTML
|
||
image_css="class=\"${site_css}_image $image_class\""
|
||
image_src="src=\"${srv_uri}images/$header_f2\""
|
||
image_alt="alt=\"$header_f3\" title=\"$header_f3\""
|
||
|
||
HTML_image=`
|
||
printf '%s%s\n' \
|
||
"<img $image_css $image_src $image_alt" \
|
||
"$image_style />"`
|
||
|
||
if [[ "$image_link" ]];then
|
||
link_css="class=\"$link_class\""
|
||
HTML_image=`
|
||
printf '%s%s%s\n' \
|
||
"<a $link_css href=\"$image_link\">" \
|
||
"$HTML_image" \
|
||
"</a>"`
|
||
fi
|
||
|
||
echo "$HTML_image"
|
||
echo
|
||
sed -i "${ln}s^$mark_image^$HTML_image^" "$1"
|
||
|
||
done <<<"$this_image"
|
||
|
||
make__file_images "$1"
|
||
}
|
||
|
||
|
||
#======================================================================
|
||
# Lists (independant module)
|
||
# repeat for each ^>> found
|
||
# Put bloc list in a file: gsl_tmp_list_brut
|
||
# Convert list to HTML in a new file: gsl_tmp_list_HTML
|
||
# Replace gsl bloc with new file
|
||
# $1: file
|
||
#======================================================================
|
||
make__lists() {
|
||
# Check for last bloc Item list
|
||
[[ `grep '^>>' "$1"` ]] || return
|
||
|
||
unset mark list mn sign muo_class my_class
|
||
|
||
# Define start and close line number
|
||
list_ln_start=`grep -n "^<<" "$1" | head -n 1 | awk -F: '{print $1}'`
|
||
list_ln_close=`grep -n "^>>" "$1" | head -n 1 | awk -F: '{print $1}'`
|
||
|
||
#----------------------------------------------------------------------
|
||
# Put gsl block list in tmp file
|
||
#----------------------------------------------------------------------
|
||
gsl_tmp_list_brut=`mktemp`
|
||
awk -v lns="$list_ln_start" -v lnc="$list_ln_close" \
|
||
'NR >= lns && NR <= lnc {print}' \
|
||
"$1" \
|
||
> "$gsl_tmp_list_brut"
|
||
|
||
# File to print HTML List
|
||
gsl_tmp_list_HTML=`mktemp`
|
||
|
||
# Define CSS class
|
||
li_class="${site_css}_li ${site_css}_li-"
|
||
uo_class="${site_css}_list ${site_css}_list-"
|
||
|
||
# Custom class for bloc only
|
||
my_class=`awk 'NR == 1 {print $2}' "$gsl_tmp_list_brut"`
|
||
[[ $my_class ]] \
|
||
&& muo_class=" ${site_css}_list-$my_class ${site_css}_list-$my_class-"
|
||
|
||
#----------------------------------------------------------------------
|
||
# Define list in arrays
|
||
#----------------------------------------------------------------------
|
||
i=0;l=0
|
||
while read -r "ln" "mark" "content"
|
||
do
|
||
((i++))
|
||
mark[$i]="${mark}"
|
||
sign[$i]="${mark:0:1}"
|
||
line[$i]="$ln"
|
||
mn[$i]="${#mark}"
|
||
case "$mark" in
|
||
"="*) list[$i]="ul" ;;
|
||
"+"*) list[$i]="ol" ;;
|
||
esac
|
||
done < <(awk '($1 ~ "=" || $1 ~ "+" || $1 ~ ">>" ) \
|
||
{print NR,$0}' \
|
||
"$gsl_tmp_list_brut")
|
||
|
||
|
||
# Generic prints li
|
||
# -----------------
|
||
make__list_li() {
|
||
printf "%$((sp + 2))s%s\n" "" \
|
||
"<li class=\"$li_class${mn[$n]}\">${content[$n]}</li>" \
|
||
| awk -v sp="$((sp+2))" \
|
||
'NR == 1 {print $0};NR > 1 {printf "%" sp "s%s\n", "", $0}' \
|
||
>> "$gsl_tmp_list_HTML"
|
||
}
|
||
|
||
# Generic print ul | ol (open current)
|
||
# ------------------------------------
|
||
make__list_open_n() {
|
||
l=$(( l + 1 ))
|
||
printf "%${sp}s%s\n" "" \
|
||
"<${list[n]}>" \
|
||
>> "$gsl_tmp_list_HTML"
|
||
}
|
||
|
||
|
||
#----------------------------------------------------------------------
|
||
# Define content items and make HTML list
|
||
#----------------------------------------------------------------------
|
||
for n in `seq 1 $i`
|
||
do
|
||
# Define content items for each list
|
||
content[$n]=`
|
||
awk -v cts="${line[$n]}" -v ctc="${line[$((n+1))]}" \
|
||
'NR >= cts && NR < ctc {print $0}' \
|
||
"$gsl_tmp_list_brut" \
|
||
| awk '{ sub(/^[ \t]+/, ""); print }'`
|
||
|
||
content[$n]=${content[$n]/${mark[$n]}/}
|
||
content[$n]=`awk '{sub(/^[ \t]+/, ""); print}' <<<"${content[$n]}"`
|
||
|
||
# Define HTML ul | ol from previous item list
|
||
# ----------------------------------------
|
||
# Define a11y tabulation for each item
|
||
sp=$(( ${mn[$n]} * 2 - 2 ))
|
||
|
||
# ----------
|
||
# First Item
|
||
# ----------
|
||
if (( $n == 1 ));then
|
||
uo_class+="${list[n]}"
|
||
[[ $my_class ]] \
|
||
&& muo_class+="${list[n]}"
|
||
|
||
l=$(( l + 1 ))
|
||
printf "%${sp}s%s\n" "" \
|
||
"<${list[n]} class=\"$uo_class$muo_class\">" \
|
||
>> "$gsl_tmp_list_HTML"
|
||
printf "%$((sp + 2))s%s\n" "" \
|
||
"<li class=\"${li_class}1\">${content[$n]}</li>" \
|
||
>> "$gsl_tmp_list_HTML"
|
||
|
||
# ---------
|
||
# Last Item
|
||
# ---------
|
||
elif (( $n == $i ));then
|
||
sp=$(( ${mn[$(( n - 1 ))]} * 2 - 2 ))
|
||
|
||
# ---------------------
|
||
# Same sign | Last Item
|
||
# ---------------------
|
||
if [[ ${sign[1]} == ${sign[$(( n - 1 ))]} ]];then
|
||
for c in `seq 1 ${mn[$(( n - 1 ))]}`
|
||
do
|
||
printf "%${sp}s%s\n" "" \
|
||
"</${list[l]}>" \
|
||
>> "$gsl_tmp_list_HTML"
|
||
sp=$(( sp - 2 ))
|
||
l=$(( l - 1 ))
|
||
done
|
||
|
||
# ---------------------
|
||
# Diff sign | Last Item
|
||
# ---------------------
|
||
else
|
||
printf "%${sp}s%s\n" "" \
|
||
"</${list[$(( n - 1 ))]}>" \
|
||
>> "$gsl_tmp_list_HTML"
|
||
l=$(( l - 1 ))
|
||
|
||
for c in `seq 1 ${mn[$(( n - 1 ))]}`
|
||
do
|
||
l=$(( l - 1 ))
|
||
sp=$(( sp - 2 ))
|
||
(( $sp < 0 )) && sp=0
|
||
printf "%${sp}s%s\n" "" \
|
||
"</${list[l]}>" \
|
||
>> "$gsl_tmp_list_HTML"
|
||
done
|
||
fi
|
||
|
||
# -----------
|
||
# Middle Item
|
||
# -----------
|
||
else
|
||
# Diff NBR between current and previous mark: mark difference
|
||
md=$(( ${mn[$n]} - ${mn[$((n - 1))]} ))
|
||
|
||
#echo "${mark[n]} | ${sign[n]} <==> ${mark[$(( n - 1 ))]} | ${sign[$(( n - 1 ))]}"
|
||
|
||
# ----------------------------
|
||
# Same Diff from previous Item
|
||
# ----------------------------
|
||
if (( $md == 0 ));then
|
||
|
||
# ---------------------
|
||
# Same sign | Same Item
|
||
# ---------------------
|
||
if [[ ${sign[n]} == ${sign[$(( n - 1 ))]} ]];then
|
||
make__list_li
|
||
|
||
# ---------------------
|
||
# Diff sign | Same Item
|
||
# ---------------------
|
||
else
|
||
make__list_open_n
|
||
make__list_li
|
||
fi
|
||
|
||
# -------------------------
|
||
# NEW Item UP from previous
|
||
# -------------------------
|
||
elif (( $md > 0 ));then
|
||
|
||
# -----------------------
|
||
# Same sign | New Item UP
|
||
# -----------------------
|
||
if [[ ${sign[n]} == ${sign[$(( n - 1 ))]} ]];then
|
||
make__list_open_n
|
||
make__list_li
|
||
|
||
# -----------------------
|
||
# Diff sign | New item UP
|
||
# -----------------------
|
||
else
|
||
make__list_open_n
|
||
make__list_li
|
||
fi
|
||
|
||
|
||
# ---------------------------
|
||
# New Item DOWN from previous
|
||
# ---------------------------
|
||
else
|
||
bsp=$(( ${mn[$(( n - 1 ))]} * 2 - 2 ))
|
||
|
||
# -------------------------
|
||
# Same sign | New Item Down
|
||
# -------------------------
|
||
if [[ ${sign[n]} == ${sign[$(( n - 1 ))]} ]];then
|
||
for c in `seq 1 ${md/-/}`
|
||
do
|
||
printf "%${bsp}s%s\n" "" \
|
||
"</${list[l]}>" \
|
||
>> "$gsl_tmp_list_HTML"
|
||
bsp=$(( bsp - 2 ))
|
||
l=$(( l - 1 ))
|
||
done
|
||
make__list_li
|
||
|
||
# -------------------------
|
||
# Diff sing | New Item Down
|
||
# -------------------------
|
||
else
|
||
l=$(( l + 1 ))
|
||
for c in `seq 1 $(( ${md/-/} + 1 ))`
|
||
do
|
||
printf "%${bsp}s%s\n" "" \
|
||
"</${list[$(( n - 1 ))]}>" \
|
||
>> "$gsl_tmp_list_HTML"
|
||
bsp=$(( bsp - 2 ))
|
||
l=$(( l - 1 ))
|
||
done
|
||
make__list_li
|
||
fi
|
||
fi
|
||
fi
|
||
done
|
||
|
||
# Delete source lines and insert HTML file
|
||
del_ln=`seq $list_ln_start $list_ln_close | wc -l`
|
||
for i in `seq 1 $del_ln`
|
||
do
|
||
sed -i "${list_ln_start}d" "$1"
|
||
done
|
||
sed -i "$((list_ln_start - 1))r$gsl_tmp_list_HTML" "$1"
|
||
|
||
rm -f "$gsl_tmp_list_brut"
|
||
rm -f "$gsl_tmp_list_HTML"
|
||
|
||
make__lists "$gsl_tmp_post"
|
||
}
|
||
|
||
|
||
#======================================================================
|
||
# Quotes (independant module)
|
||
# $1: file
|
||
#======================================================================
|
||
make__quotes() {
|
||
# Get block quote
|
||
unset ${!quote@} my_class ${!q_@}
|
||
while IFS=: read -r "ln" "mk"
|
||
do
|
||
[[ $quote_ln_start ]] \
|
||
&& quote_ln_close="$ln" \
|
||
|| quote_ln_start="$ln"
|
||
done < <(grep -n "^---" "$1" | head -n 2)
|
||
|
||
[[ $quote_ln_close ]] || return
|
||
|
||
# Put in a file brut block quote
|
||
gsl_tmp_quote_brut=`mktemp`
|
||
awk -v lns="$quote_ln_start" -v lnc="$quote_ln_close" \
|
||
'NR >= lns && NR <= lnc {print}' \
|
||
"$1" \
|
||
> "$gsl_tmp_quote_brut"
|
||
|
||
# Define custom CSS class
|
||
my_class=`awk 'NR == 1 {print $2}' "$gsl_tmp_quote_brut"`
|
||
[[ $my_class ]] \
|
||
&& my_class=" $my_class"
|
||
sed -i "1d" "$gsl_tmp_quote_brut"
|
||
sed -i "`cat "$gsl_tmp_quote_brut" | wc -l`d" "$gsl_tmp_quote_brut"
|
||
|
||
# Get metadatas from quote
|
||
#-------------------------
|
||
make__quotes_set() {
|
||
grep "$1" "$gsl_tmp_quote_brut" | awk -F" : " '{print $2}'
|
||
}
|
||
|
||
# Delete useless line from source
|
||
#--------------------------------
|
||
make__quotes_rmln() {
|
||
[[ "$2" ]] || return
|
||
q_ln=`grep -n "$1" "$gsl_tmp_quote_brut" | awk -F: '{print $1}'`
|
||
sed -i "${q_ln}d" "$gsl_tmp_quote_brut"
|
||
}
|
||
|
||
q_link=`make__quotes_set '^_link : '` && \
|
||
make__quotes_rmln '^_link : ' "$q_link"
|
||
q_lang=`make__quotes_set '^_lang : '` && \
|
||
make__quotes_rmln '^_lang : ' "$q_lang"
|
||
q_auth=`make__quotes_set '^_cite : '` && \
|
||
make__quotes_rmln '^_cite : ' "$q_auth"
|
||
q_book=`make__quotes_set '^_book : '` && \
|
||
make__quotes_rmln '^_book : ' "$q_book"
|
||
q_year=`make__quotes_set '^_year : '` && \
|
||
make__quotes_rmln '^_year : ' "$q_year"
|
||
|
||
# Put in a file HTML quote
|
||
gsl_tmp_quote_HTML=`mktemp`
|
||
|
||
[[ "$q_auth" ]] && q_ref="$q_auth "
|
||
[[ "$q_book" ]] && q_book="- $q_book"
|
||
[[ "$q_year" ]] && q_year="($q_year)"
|
||
|
||
# Define HTML
|
||
[[ "$q_link" ]] && q_HTML_link="href=\"$q_link\" target=\"_blank\""
|
||
[[ "$q_lang" ]] && q_HTML_lang=" lang=\"$q_lang\""
|
||
[[ "$q_link" ]] && q_HTML_cite=" cite=\"$q_link\""
|
||
|
||
q_CSS="class=\"${site_css}_quote$my_class\""
|
||
q_CSS_fig="class=\"${site_css}_quote-fig$my_class\""
|
||
q_CSS_link="class=\"${site_css}_quote-link\""
|
||
|
||
if [[ "$q_book" && "$q_year" ]];then
|
||
q_ref+="$q_book$q_year"
|
||
q_data=" $q_book$q_year"
|
||
q_TITLE=" title=\"$q_ref\""
|
||
|
||
elif [[ "$q_book" ]] && ! [[ "$q_year" ]];then
|
||
q_ref+="$q_book"
|
||
q_data=" $q_book"
|
||
q_TITLE=" title=\"$q_ref\""
|
||
|
||
elif [[ "$q_year" ]] && ! [[ "$q_book" ]];then
|
||
q_ref+="$q_year"
|
||
q_data=" $q_year"
|
||
q_TITLE=" title=\"$q_ref\""
|
||
fi
|
||
|
||
# Make HTML file
|
||
if [[ "$q_auth" ]];then
|
||
# case figure
|
||
cat <<EOQUOTE > "$gsl_tmp_quote_HTML"
|
||
<figure $q_CSS_fig>
|
||
<blockquote$q_HTML_lang$q_HTML_cite>
|
||
$(while read -r "line"
|
||
do
|
||
printf '%4s%s\n' "" "$line"
|
||
done < <(cat "$gsl_tmp_quote_brut"))
|
||
</blockquote>
|
||
<figcaption>
|
||
<cite>
|
||
<a $q_HTML_link$q_TITLE>$q_auth</a>$q_data
|
||
</cite>
|
||
</figcaption>
|
||
</figure>
|
||
EOQUOTE
|
||
|
||
else
|
||
# Simple quote
|
||
cat <<EOQUOTE > "$gsl_tmp_quote_HTML"
|
||
<blockquote $q_CSS$q_HTML_lang$q_HTML_cite$q_TITLE>
|
||
$(while read -r "line"
|
||
do
|
||
printf '%4s%s\n' "" "$line"
|
||
done < <(cat "$gsl_tmp_quote_brut"))
|
||
</blockquote>
|
||
EOQUOTE
|
||
fi
|
||
|
||
# Remove source lines and insert HTML file
|
||
del_ln=`seq $quote_ln_start $quote_ln_close | wc -l`
|
||
for i in `seq 1 $del_ln`
|
||
do
|
||
sed -i "${quote_ln_start}d" "$1"
|
||
done
|
||
sed -i "$((quote_ln_start - 1))r$gsl_tmp_quote_HTML" "$1"
|
||
|
||
#cat "$gsl_tmp_quote_brut"
|
||
rm -f "$gsl_tmp_quote_brut"
|
||
rm -f "$gsl_tmp_quote_HTML"
|
||
|
||
make__quotes "$gsl_tmp_post"
|
||
}
|
||
|
||
|
||
#======================================================================
|
||
# div container between titles
|
||
# $1: file
|
||
#======================================================================
|
||
make__div_title() {
|
||
max_lines=`cat "$1" | wc -l`
|
||
|
||
# Mark if betwee
|
||
while IFS=: read -r "ln" "content"
|
||
do
|
||
[[ $title_start ]] \
|
||
&& title_close="$ln" \
|
||
|| title_start="$ln"
|
||
|
||
! [[ $title_close ]] && continue
|
||
|
||
if [[ -z `awk -v lns="$title_start" -v lnc="$title_close" \
|
||
'NR > lns && NR < lnc' "$1"` ]];then
|
||
sed -i -e "${title_start}s|^|NODIV|" "$1"
|
||
|
||
else
|
||
sed -i -e "${title_close}s|$|ENDIV|" "$1"
|
||
fi
|
||
|
||
title_start=$title_close;unset title_close
|
||
done < <(grep "^<h" -n "$1")
|
||
|
||
# Add div after title
|
||
n=0
|
||
while IFS=: read -r "ln" "content"
|
||
do
|
||
(( n++ ))
|
||
tn=${content:2:1}
|
||
div_title="<div class=\"${site_css}_content ${site_css}_content-h$tn\">"
|
||
sed -i "$(( ln + n ))i$div_title" "$1"
|
||
done < <(grep "^<h" -n "$1")
|
||
|
||
n=1
|
||
# add /div before title
|
||
while IFS=: read -r "ln" "content"
|
||
do
|
||
sed -i "$(( ln + n -1 ))i</div>" "$1"
|
||
(( n++ ))
|
||
done < <(grep -n 'ENDIV$' "$1")
|
||
echo "</div>" >> "$1"
|
||
sed -i "s^NODIV^^g" "$1"
|
||
sed -i "s^ENDIV^^g" "$1"
|
||
}
|