gsl-statique-litterateur/var/lib/gsl/scripts/gsl__post_makers

712 lines
20 KiB
Bash

#!/bin/bash
# file: gsl__post_makers
# Folder: /var/lib/gsl/scripts
# By echolib
# License: GNU AFFERO GENERAL PUBLIC LICENSE Version 3, 19 November 2007
#=======================================================================
# ---------
# MAIN CALL
# ---------
#=======================================================================
#=======================================================================
# From: gsl__loop_posts | Convert Post from modules to HTML
#=======================================================================
gsl__all_makers() {
# Code readability a11y
gsl_a11y_post=`mktemp`
gsl__post_content_only
# Create Listng Page
gsl__page_listing_create Add
gsl__do_italics "make" "$gsl_tmp_post" # First
gsl__do_header "make" "$gsl_marker_link" "$gsl_post"
gsl__do_header "make" "$gsl_marker_abbr" "$gsl_post"
gsl__make_files
gsl__do_strongs_bolds "make" "$gsl_tmp_post"
gsl__make_list
gsl__do_icode "make" "$gsl_tmp_post"
gsl__make_titles
gsl_ct=0 && gsl__make_titles_div_open
gsl_ct=0 && gsl__make_titles_div_close
gsl__do_header "make" "$gsl_marker_image" "$gsl_post"
gsl__make_paragraphs
# From File_Code
gsl_fcode_nbr=`grep "$gsl_mark_fcode" "$gsl_tmp_post" | wc -l`
for fcode in `seq 1 $gsl_fcode_nbr`
do
gsl__make_fcode
done
gsl__check_cites
gsl__make_a11y
echo -ne "\r\033[2K"
rm -f "$gsl_tmp_post" # No more Needed tmp file post
# Create SRV folder according to post type
[[ "$gsl_post_type" == "post" ]] \
&& mkdir -p "$gsl_srv_wip/$gsl_header_slug/" \
&& gsl__logs_print \
"$gsl_log_w" \
"SRV" \
"wip" \
"Create folder: $gsl_header_slug" \
"$gsl_srv_wip/$gsl_header_slug/" \
&& gsl_checker_war=true
# Create HTML Page using gsl_a11y_post
gsl__page_creator
gsl__db_set_post_status wip
rm -f "$gsl_a11y_post" # No more Needed a11y post
}
#=======================================================================
# ---------------------------------------------
# Tools and prepapre tmp file from CONTENT Post
# ---------------------------------------------
#=======================================================================
#=======================================================================
# Get line nbr from TMP CONTENT Post... $1: TERM | $2: File
#=======================================================================
gsl__get_mark_content_line() {
awk -v s="$1" \
'$0 ~ s {print NR}' \
"$2" || return
}
#=======================================================================
# ---------------------------------
# Converters
# ---------------------------------
#=======================================================================
#----------------------------------------------------------------------
#======================================================================
# Convert List.Get first/last line item / repeat till no more mark
#======================================================================
gsl__make_list() {
! [[ `awk -v m="$gsl_mark_list" \
'$1 == m {print $1;exit}' \
"$gsl_tmp_post"` ]] \
&& return
gsl_last_line=`cat $gsl_tmp_post | wc -l`
# Get block lists by line numbers From Content
while read -r "gsl_line" "gsl_content"
do
case "$gsl_content" in
"$gsl_mark_list"*)
! [[ $gsl_list ]] \
&& gsl_list=true \
&& gsl_list_o=$gsl_line \
&& continue
[[ $gsl_list ]] && (( $gsl_line == $gsl_last_line )) \
&& gsl_list_c=$gsl_line \
&& gsl__make_list_get_block \
&& break
;;
""|*)
[[ "$gsl_list" ]] \
&& gsl_list_c=$(( gsl_line - 1 )) \
&& unset gsl_list \
&& gsl__make_list_get_block \
&& break
;;
esac
done < <(cat -n "$gsl_tmp_post")
}
#======================================================================
# Convert List. tmp file block list. replace mark with number
#======================================================================
gsl__make_list_get_block() {
echo -ne "\r\033[2K: Converting Block-List... $gsl_list_o > $gsl_list_c"
# Create tmp file with this block-list and marks converted
gsl_file_block_list=`mktemp`
while read -r "mark" "content"
do
echo "${#mark} $content" >> "$gsl_file_block_list"
done < <(awk -v o="$gsl_list_o" -v c="$gsl_list_c" \
'NR >= o && NR <= c' \
"$gsl_tmp_post")
gsl__make_list_convert
# No more needed
unset gsl_list_o gsl_list_c
rm -f "$gsl_file_block_list"
}
#======================================================================
# Convert List. tmp HTML file.
#======================================================================
gsl__make_list_convert() {
# Get previeous line mark number
gsl__make_list_get_prev_mark() {
pm=`
awk -v pm="$(( $ln - 1))" \
'NR == pm {print $1}' \
"$gsl_file_block_list"`
! [[ $pm ]] && pm=0
}
# Create tmp file with HTML block-list
gsl_HTML_list=`mktemp`
#ln: line number ; mn: mark number
while read -r "ln" "mn" "content"
do
gsl__make_list_get_prev_mark
gsl_diff_pm_mn=$(( mn - pm ))
if (( $gsl_diff_pm_mn == 0 ));then
printf "%s%s\n" \
"" \
"<li>$content</li>" \
>> "$gsl_HTML_list"
elif (( $gsl_diff_pm_mn > 0 ));then
printf "%s%s\n" \
"" \
"<ul>" \
>> "$gsl_HTML_list"
printf "%s%s\n" \
"" \
"<li>$content</li>" \
>> "$gsl_HTML_list"
elif (( $gsl_diff_pm_mn < 0 ));then
printf '</ul>%.s\n' {1..eval $n} \
>> "$gsl_HTML_list"
printf "%s%s\n" \
"" \
"<li>$content</li>" \
>> "$gsl_HTML_list"
fi
done < <(cat -n "$gsl_file_block_list")
echo "</ul>" >> "$gsl_HTML_list"
# Remove useless list in tmp_post
gsl_list_rm_tl=$(( $gsl_list_c - $gsl_list_o ))
for rml in `seq 0 $gsl_list_rm_tl`
do
sed -i "${gsl_list_o}d" "$gsl_tmp_post"
done
# Insert new HTML file list in tmp_post
gsl_list_line_i=$(( gsl_list_o - 1))
sed -i "${gsl_list_line_i}r $gsl_HTML_list" "$gsl_tmp_post"
rm -f "$gsl_HTML_list" # No more needed HTML tmp file list
gsl__make_list
}
#----------------------------------------------------------------------
#======================================================================
# Replace gsl_mark_fcode
#======================================================================
gsl__make_fcode() {
while IFS=: read -r "line" "mark" "nbr"
do
# Get File
gsl_file_fcode=`
grep "${gsl_marker_code}$nbr" "$gsl_post" \
| awk -F" : " '{print $2}'`
echo -ne "\r\033[2K: Converting Block-Codes... $gsl_file_fcode"
gsl_css_class="${gsl_site_css}_block-code"
# Create tmp file from block-code, adding span for line numbers in css
gsl_file_bcode=`mktemp`
while read -r "gsl_bcode_line"
do
echo "<span>$gsl_bcode_line</span>" >> "$gsl_file_bcode"
done < <(cat "$gsl_dir_domain_files/$gsl_file_fcode")
gsl_file_tmp_code=`mktemp`
printf '%s %s\n%b\n%s\n' \
"<pre class=\"$gsl_css_class" \
"${gsl_css_class}-$gsl_post_header_field_1\">" \
"$(cat "$gsl_file_bcode")" \
"</pre>" \
> "$gsl_file_tmp_code"
# Replace in tmp post and delete mark
sed -i "${line}r $gsl_file_tmp_code" "$gsl_tmp_post"
sed -i "${line}d" "$gsl_tmp_post"
rm -f "$gsl_file_tmp_code"
rm -f "$gsl_file_bcode"
done < <(grep -n "$gsl_mark_fcode" "$gsl_tmp_post" | head -n 1)
}
#======================================================================
# From gsl__do_header: Make Link
#======================================================================
gsl__make_link() {
echo -ne "\r\033[2K: Converting Links... $gsl_post_hf_1"
gsl_post_hf_1="${gsl_post_hf_1}"
gsl_post_hf_1_t="${gsl_post_hf_1}+"
[[ "$sl_post_hf_3" ]] \
&& gsl_html_link_title=" title=\"$sl_post_hf_3\""
# target blank
gsl_html_link_t=`
printf '%s%s%s%s' \
"<a href=\"$gsl_post_hf_2\"$gsl_html_link_title " \
"targt=\"blank\">" \
"$gsl_post_hf_1" \
"</a>"`
sed -i "s|_${gsl_post_hf_1_t}|$gsl_html_link_t|g" "$gsl_tmp_post"
# No Target
gsl_html_link=`
printf '%s%s%s' \
"<a href=\"$gsl_post_hf_2\"$gsl_html_link_title>" \
"$gsl_post_hf_1" \
"</a>"`
sed -i "s|_${gsl_post_hf_1}|$gsl_html_link|g" "$gsl_tmp_post"
}
#======================================================================
# From gsl__do_header: Make ABBRS
#======================================================================
gsl__make_abbr() {
echo -ne "\r\033[2K: Converting ABBRs... $gsl_post_hf_1"
gsl_html_long="<abbr title=\"$gsl_post_hf_2\">$gsl_post_hf_1</abbr>"
sed -i "s|$gsl_post_hf_1|$gsl_html_long|" "$gsl_tmp_post"
}
#=======================================================================
# Find and convert Titles #1-6
#=======================================================================
gsl__make_titles() {
echo -ne ": Searching and converting Titles..."
for t in `seq 1 6`
do
while read -r "h" "content"
do
gsl_html_title="<h$t>$content</h$t>"
sed -i "s^$h $content^$gsl_html_title^" \
"$gsl_tmp_post"
done < <(grep "#$t" "$gsl_tmp_post")
done
}
#-----------------------------------------------------------------------
#
#=======================================================================
# Open DIVs after titles for css
#=======================================================================
gsl__make_titles_div_open() {
echo -ne "\r\033[2K: Searching Titles and Open DIVs..."
gsl_ct=$(( gsl_ct + 1 ))
gsl_content_title=`
grep -n "<h" "$gsl_tmp_post" \
| head -n $gsl_ct \
| tail -n 1`
[[ "$gsl_content_title" == "$gsl_content_same_title" ]] && return
! [[ "$gsl_content_title" ]] && return
gsl_content_line_nbr=`
awk -F: '{print $1}' \
<<< $gsl_content_title`
gsl_title_n=`
awk -F"<h" '{print $2}' \
<<< "$gsl_content_title" \
| awk -F">" '{print $1}'`
gsl_html_div="<div class=\"${gsl_site_css}_content-p$gsl_title_n\">"
sed -i "$(( $gsl_content_line_nbr + 1 ))i$gsl_html_div" \
"$gsl_tmp_post"
gsl_content_same_title="$gsl_content_title"
gsl__make_titles_div_open
}
#=======================================================================
# Close DIVs before titles for css
#=======================================================================
gsl__make_titles_div_close() {
echo -ne "\r\033[2K: Searching Titles and Open DIVs..."
gsl_test_line="<div class=\"${gsl_site_css}_content"
gsl_ct=$(( gsl_ct + 1 ))
gsl_content_title=`
grep -n "$gsl_test_line" "$gsl_tmp_post" \
| head -n $gsl_ct \
| tail -n 1`
[[ "$gsl_content_title" == "$gsl_content_same_title" ]] \
&& echo "</div>" >> "$gsl_tmp_post" \
&& return
! [[ "$gsl_content_title" ]] && return
gsl_content_line_nbr=`
awk -F: '{print $1}' \
<<< $gsl_content_title`
while read -r "gsl_search_line_nbr" "gsl_search_line"
do
case "$gsl_search_line" in
"<h"*)
sed -i "${gsl_search_line_nbr}i</div>" "$gsl_tmp_post"
break
;;
*) continue ;;
esac
done < <(awk -v l="$gsl_content_line_nbr" \
'NR > l {print NR,$0}' \
"$gsl_tmp_post")
gsl_content_same_title="$gsl_content_title"
gsl__make_titles_div_close
}
#
#-----------------------------------------------------------------------
#=======================================================================
# Find and convert paragraphs (3 different css styles)
#=======================================================================
gsl__make_paragraphs() {
gsl_html_par_o1="<p class=\"${gsl_site_css}_paragraph\">"
gsl_html_par_o2="<p class=\"${gsl_site_css}_paragraph-2\">"
gsl_html_par_o3="<p class=\"${gsl_site_css}_paragraph-3\">"
gsl_html_par_c="</p>"
echo -ne "\r\033[2K: Converting Paragraphs..."
while read -r "gsl_content_line_nbr" "gsl_content_line"
do
case "$gsl_content_line" in
")")
sed -i "${gsl_content_line_nbr}s,.*,$gsl_html_par_c," \
"$gsl_tmp_post"
;;
"("|"( 1")
sed -i "${gsl_content_line_nbr}s,.*,$gsl_html_par_o1," \
"$gsl_tmp_post"
;;
"( 2")
sed -i "${gsl_content_line_nbr}s,.*,$gsl_html_par_o2," \
"$gsl_tmp_post"
;;
"( 3")
sed -i "${gsl_content_line_nbr}s,.*,$gsl_html_par_o3," \
"$gsl_tmp_post"
;;
*)
continue
;;
esac
done < <(cat -n "$gsl_tmp_post")
}
#-----------------------------------------------------------------------
#=======================================================================
# Find BLOCKQUOTES
#=======================================================================
gsl__check_cites() {
echo -ne "\r\033[2K: Searching and converting Citations..."
unset gsl_lines_nbr
gsl_lines_nbr=(`
gsl__get_mark_content_line \
"$gsl_mark_blockquote" \
"$gsl_tmp_post"`)
gsl_lines_nbr=(${gsl_lines_nbr[0]} ${gsl_lines_nbr[1]})
! [[ "$gsl_lines_nbr" ]] \
&& return
for i in `seq 1 ${#gsl_lines_nbr[@]}`
do
if [[ "$gsl_cite_start" ]];then
unset gsl_cite_start
gsl_cite_line_stop=${gsl_lines_nbr[(($i-1))]}
gsl__make_cite
else
gsl_cite_start=true
gsl_cite_line_start=${gsl_lines_nbr[(($i-1))]}
fi
done
gsl__check_cites
}
#=======================================================================
# Replace and Convert this blockquote
#=======================================================================
gsl__make_cite() {
gsl_file_tmp_cite=`mktemp` # File to Put whole Blockquote
gsl_file_tmp_cite_content=`mktemp` # File to Put Blockquote CONTENT only
gsl_file_tmp_cite_html=`mktemp` # File to Put final HTML blockquote
# Put whole blockquote in file
awk -v o="$gsl_cite_line_start" -v c="$gsl_cite_line_stop" \
'NR >= o && NR <= c' \
"$gsl_tmp_post" \
> "$gsl_file_tmp_cite"
# No more DATAS needed for next if any
unset ${!gsl_bq_@} ${!gsl_html_cite_@}
# Get DATAS from blockquote
while read -r "gsl_content_line"
do
case "$gsl_content_line" in
---*)
continue
;;
_cite*)
gsl_bq_author=`awk -F" : " '{print $2}' <<< "$gsl_content_line"`
;;
_book*)
gsl_bq_book=`awk -F" : " '{print $2}' <<< "$gsl_content_line"`
;;
_lang*)
gsl_bq_lang=`awk -F" : " '{print $2}' <<< "$gsl_content_line"`
gsl_html_cite_lang=" lang=\"$gsl_bq_lang\""
;;
_year*)
gsl_bq_year=`awk -F" : " '{print $2}' <<< "$gsl_content_line"`
;;
_link*)
gsl_bq_link=`awk -F" : " '{print $2}' <<< "$gsl_content_line"`
gsl_html_cite_link=" cite=\"$gsl_bq_link\""
;;
*)
[[ "$gsl_bq_author" ]] \
&& echo " $gsl_content_line" >> "$gsl_file_tmp_cite_content" \
|| echo " $gsl_content_line" >> "$gsl_file_tmp_cite_content"
;;
esac
done < <(cat "$gsl_file_tmp_cite")
[[ "$gsl_bq_book" ]] \
&& gsl_bq_ref=" - $gsl_bq_book"
[[ "$gsl_bq_year" ]] \
&& gsl_bq_ref+=" ($gsl_bq_year)"
[[ "$gsl_bq_book" ]] \
&& gsl_html_cite_book=" title=\"Book:$gsl_bq_ref\""
! [[ "$gsl_bq_link" ]] && [[ "$gsl_bq_book" ]] \
&& gsl_html_cite_link=" cite=\"$gsl_bq_book$gsl_bq_year\""
rm -f "$gsl_file_tmp_cite" # No more whole blockquote file needed
gsl_file_tmp_cite=`mktemp` # File to put Converted HTML DATAS
# Advanced or simple blockquote
[[ "$gsl_bq_author" ]] \
&& gsl__make_cite_advanced \
|| gsl__make_cite_simple
# Insert html blockquote file in Post
sed -i "${gsl_cite_line_stop}r $gsl_file_tmp_cite_html" "$gsl_tmp_post"
# Remove useless lines
gsl_cite_total_lines=$(( $gsl_cite_line_stop - gsl_cite_line_start ))
for i in `seq 0 $gsl_cite_total_lines`
do
sed -i "${gsl_cite_line_start}d" "$gsl_tmp_post"
done
rm -f "$gsl_file_tmp_cite_content" # No more blockquote CONTENT needed
rm -f "$gsl_file_tmp_cite_html" # No more html blockquote file needed
}
#=======================================================================
# from gsl__post_maker_cite : ADVANCED blockquote
#=======================================================================
gsl__make_cite_advanced() {
gsl_css_class="class=\"${gsl_site_css}_quote\""
cat << EOCITE > "$gsl_file_tmp_cite_html"
<figure>
<blockquote $gsl_css_class$gsl_html_cite_lang$gsl_html_cite_link>
$(cat "$gsl_file_tmp_cite_content")
</blockquote>
<figcaption class="a-lec_fgc-quote">
<cite>
<a href="$gsl_bq_link" target="_blank"$gsl_html_cite_book>$gsl_bq_author</a>$gsl_bq_ref
</cite>
</figcaption>
</figure>
EOCITE
}
#=======================================================================
# from gsl__post_maker_cite : SIMPLE blockquote
#=======================================================================
gsl__make_cite_simple() {
gsl_css_class="${gsl_site_css}_simple-quote"
cat << EOCITE > "$gsl_file_tmp_cite_html"
<blockquote $gsl_css_class$gsl_html_cite_lang$gsl_html_cite_link>
$(cat "$gsl_file_tmp_cite_content")
</blockquote>
EOCITE
}
#-----------------------------------------------------------------------
#-----------------------------------------------------------------------
# Convert HTML: Images
#-----------------------------------------------------------------------
gsl__make_image() {
echo
gsl_this_mark="$gsl_mark_image:$gsl_post_hf_1"
while IFS=: read -r "line_nbr" "this_image"
do
align_image=`awk -F: '{print $3}' <<< "$this_image"`
echo -ne "\r\033[2K: Converting Image... Line:$line_nbr $this_image"
gsl_html_src="src=\"$gsl_uri/images/$gsl_post_hf_2\""
gsl_html_alt="alt=\"$gsl_post_hf_3\""
case "$align_image" in
R|r|D|d)
gsl_html_class="class=\"${gsl_site_css}_image_right\""
;;
L|l|G|g)
gsl_html_class="class=\"${gsl_site_css}_image_left\""
;;
C|c)
gsl_html_class="class=\"${gsl_site_css}_image_center\""
;;
esac
gsl_html_image="<img $gsl_html_src $gsl_html_class $gsl_html_alt />"
gsl_post_image="$gsl_mark_image:$gsl_post_hf_1:$align_image"
sed -i "${line_nbr}s|$gsl_post_image|$gsl_html_image|" "$gsl_tmp_post"
done < <(grep -n "$gsl_this_mark" "$gsl_tmp_post")
}
#-----------------------------------------------------------------------
# Convert HTML: Files
#-----------------------------------------------------------------------
gsl__make_files() {
echo
while read -r "gsl_this_file"
do
gsl_this_file=${gsl_this_file/$gsl_marker_file/}
gsl_filetxt=`
awk -F" : " \
'{print $1}' \
<<< "$gsl_this_file"`
gsl_filename=`
awk -F" : " \
'{print $2}' \
<<< "$gsl_this_file"`
gsl_filealt=`
awk -F" : " \
'{print $3}' \
<<< "$gsl_this_file"`
[[ "$gsl_filealt" ]] \
&& gsl_HTML_file_title=" title=\"$gsl_filealt\""
# Set HTML Link
case "$gsl_post_type" in
post) gsl_HTML_file_url="../files/$gsl_filename" ;;
page) gsl_HTML_file_url="files/$gsl_filename" ;;
esac
# Convert file to link
gsl_mark_file="$gsl_mark_file$gsl_filetxt"
gsl_HTML_file_link=`
printf '%s%s%s%s%s%s' \
"<a href=\"$gsl_HTML_file_url\"" \
"$gsl_HTML_file_title" \
" target=\"_blank\"" \
" class=\"${gsl_site_css}_file-link\">" \
"$gsl_filetxt" \
"</a>"`
sed -i "s|$gsl_mark_file|$gsl_HTML_file_link|g" \
"$gsl_tmp_post"
done < <(grep "$gsl_marker_file" "$gsl_post")
}
#-----------------------------------------------------------------------
# Convert Inline-code
#-----------------------------------------------------------------------
gsl__make_inline_codes() {
gsl_HTML_inline_code="<code class=\"${gsl_site_css}_inline-code\">"
sed -i "s|$gsl_mo_code|$gsl_HTML_inline_code|g" \
"$gsl_tmp_post"
sed -i "s|$gsl_mc_code|</code>|g" \
"$gsl_tmp_post"
}
#-----------------------------------------------------------------------
# Reading Code ; making readability
#-----------------------------------------------------------------------
gsl__make_a11y() {
echo -ne "\r\033[2K: Reading HTML Code for readability..."
gsl__make_a11y_add_space() {
gsl_set_space=$(( gsl_set_space * 2 ))
gsl_space=`printf "%${gsl_set_space}s" ""`
awk -v b="$gsl_start_line" \
-v c="$gsl_stop_line" \
-v space="$gsl_space" \
'NR > b && NR < c {print space $0}' \
"$gsl_tmp_post" \
>> "$gsl_a11y_post"
}
gsl_last_title_line=`
grep -n "<h" "$gsl_tmp_post" | tail -1 | awk -F: '{print $1}'`
while read -r "gsl_line_nbr" "gsl_line"
do
if (( $gsl_line_nbr > $gsl_last_title_line ));then
gsl_stop_line=`cat "$gsl_tmp_post" | wc -l`
gsl__make_a11y_add_space
break
fi
case "$gsl_line" in
"<h"*)
if [[ $gsl_start_line ]];then
gsl_stop_line=$gsl_line_nbr
gsl__make_a11y_add_space
unset gsl_start_line
fi
gsl_set_space=`expr substr "$gsl_line" 3 1`
gsl_start_line=$gsl_line_nbr
unset gsl_stop_line
printf '%s\n' "$gsl_line" >> "$gsl_a11y_post"
;;
esac
done < <(cat -n "$gsl_tmp_post")
}