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

428 lines
12 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() {
clear
unset gsl_check_done
gsl__prepare_makers
echo -ne ": Searching and converting Titles..."
gsl__make_titles
echo -ne "\r\033[2K: Searching Titles and adding DIVs..."
gsl_ct=0 && gsl__make_titles_div_open
gsl_ct=0 && gsl__make_titles_div_close
echo -ne "\r\033[2K: Searching and converting Paragraphs..."
gsl__make_paragraphs
echo -ne "\r\033[2K: Searching and converting Block-Codes..."
gsl__check_bcodes
echo -ne "\r\033[2K: Searching and converting Citations"
gsl__check_cites
#clear
cat -n "$gsl_uri_tmp_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
}
#=======================================================================
# Get NEEDED infos, and create TMP file from CONTENT
#=======================================================================
gsl__prepare_makers() {
# Getting NEEDED infos
gsl_post_begin=`
grep -n "#1" $gsl_post \
| head -1 \
| awk -F: '{print $1}'`
source "$gsl_this_conf_domain/$gsl_find_domain.conf"
# Create TMP file from CONTENT
gsl_dir_tmp="/tmp/gsl"
gsl_file_tmp_post_content="post_content"
gsl_uri_tmp_post="$gsl_dir_tmp/$gsl_file_tmp_post_content"
mkdir -p "$gsl_dir_tmp"
echo "" > "$gsl_uri_tmp_post"
awk -v l="$gsl_post_begin" \
'NR >= l' \
"$gsl_post" \
> "$gsl_uri_tmp_post"
}
#=======================================================================
# ---------------------------------
# Check if Convert is needed and do
# ---------------------------------
#=======================================================================
#=======================================================================
# Find BLOCK-CODES
#=======================================================================
gsl__check_bcodes() {
gsl_lines_nbr=(`
gsl__get_mark_content_line \
"$gsl_mark_code" \
"$gsl_uri_tmp_post"`)
! [[ "$gsl_lines_nbr" ]] \
&& return
for i in `seq 1 ${#gsl_lines_nbr[@]}`
do
gsl__make_bcode
done
}
#=======================================================================
# Find BLOCKQUOTES
#=======================================================================
gsl__check_cites() {
unset gsl_lines_nbr
gsl_lines_nbr=(`
gsl__get_mark_content_line \
"$gsl_mark_blockquote" \
"$gsl_uri_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
}
#=======================================================================
# -----------------------------------------------
# Convert To HTML in duplicated CONTENT Post File
# -----------------------------------------------
#=======================================================================
#=======================================================================
# Find and convert Titles #1-6
#=======================================================================
gsl__make_titles() {
while read -r "gsl_content_line_nbr" "gsl_content_line"
do
for t in `seq 1 6`
do
case "$gsl_content_line" in
"#$t"*)
gsl_content_title=`
awk -F"#$t " '{print $2}' \
<<< "$gsl_content_line"`
gsl_html_title="<h$t>$gsl_content_title</h$t>"
sed -i "${gsl_content_line_nbr}s^.*^$gsl_html_title^" \
"$gsl_uri_tmp_post"
;;
*) continue ;;
esac
done
done < <(cat -n "$gsl_uri_tmp_post")
}
#-----------------------------------------------------------------------
#
#=======================================================================
# Open DIVs after titles for css
#=======================================================================
gsl__make_titles_div_open() {
gsl_ct=$(( gsl_ct + 1 ))
gsl_content_title=`
grep -n "<h" "$gsl_uri_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_uri_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() {
gsl_test_line="<div class=\"${gsl_site_css}_content"
gsl_ct=$(( gsl_ct + 1 ))
gsl_content_title=`
grep -n "$gsl_test_line" "$gsl_uri_tmp_post" \
| head -n $gsl_ct \
| tail -n 1`
[[ "$gsl_content_title" == "$gsl_content_same_title" ]] \
&& echo "</div>" >> "$gsl_uri_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_uri_tmp_post"
break
;;
*) continue ;;
esac
done < <(awk -v l="$gsl_content_line_nbr" \
'NR > l {print NR,$0}' \
"$gsl_uri_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>"
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_uri_tmp_post"
;;
"("|"( 1")
sed -i "${gsl_content_line_nbr}s,.*,$gsl_html_par_o1," \
"$gsl_uri_tmp_post"
;;
"( 2")
sed -i "${gsl_content_line_nbr}s,.*,$gsl_html_par_o2," \
"$gsl_uri_tmp_post"
;;
"( 3")
sed -i "${gsl_content_line_nbr}s,.*,$gsl_html_par_o3," \
"$gsl_uri_tmp_post"
;;
*)
continue
;;
esac
done < <(cat -n "$gsl_uri_tmp_post")
}
#=======================================================================
# Replace gsl_mark_code
#=======================================================================
gsl__make_bcode() {
# Get mark line nbr
gsl_line_nbr=`
gsl__get_mark_content_line \
"$gsl_mark_code" \
"$gsl_uri_tmp_post" | head -1`
#---------
# Convert
#---------
gsl_css_class="${gsl_site_css}_block-code"
gsl_header_content_line=`gsl__get_header "$gsl_marker_code" "$gsl_post"`
gsl_file_tmp_code=`mktemp`
# Set gsl_post_header_field_1 gsl_post_header_field_2
gsl__get_header_fields "$gsl_marker_code"
# Create tmp file from block-code, adding span to get number lines 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_post_header_field_2")
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"
sed -i "${gsl_line_nbr}r $gsl_file_tmp_code" "$gsl_uri_tmp_post"
sed -i "${gsl_line_nbr}d" "$gsl_uri_tmp_post"
rm -f "$gsl_file_tmp_code"
rm -f "$gsl_file_bcode"
}
#-----------------------------------------------------------------------
#
#=======================================================================
# 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_uri_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_uri_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_uri_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
}
#
#-----------------------------------------------------------------------