#!/bin/bash
# Name: Statique Littérateur
# Type: Article makers
# file: wip__article
# Folder: /var/lib/stl/scripts/
# By echolib (XMPP: im@echolib.re)
# License: GNU AFFERO GENERAL PUBLIC LICENSE Version 3, 19 November 2007
#------------
# funny stats
#------------
# lines: 1023
# functions: 19
#-----------------------------------------------------------------------
#**********************************************************************
#======================================================================
# Argument make
# $1: mak
# $2: arguments
#======================================================================
wip__OPTIONS() {
! [[ "$2" ]] \
&& noarg "Cannot be empty"
case "$2" in
sidebar)
if ! [[ "$3" ]];then
domain__get
sidebar__create_HTML
return
fi
;;
esac
while test "$2"
do
case "$2" in
-F) wip_force=true ;;
*".stl") article__hash "$2" ;;
"sidebar="*) sidebar="$2";position=${sidebar/sidebar=} ;;
*) noarg "$2" "Use: -F, (ARTICLE), sidebar" ;;
esac
shift
done
# Before convert, check if article has db, not changed, errors
if__article_db "$article_db"
if [[ $need_check ]];then
echo "! Article has changed. you must check it first"
exit
# Article has error
elif [[ $check_force ]];then
echo "! Article is not valid. Check for errors first"
exit
fi
# check wip hash and wip file exists
do_wip=true
this_article="$domain_dir_wip$article_uri_srv"
if ! [[ $article_wip_hash ]];then
log__add -i -M -A \
"Create new HTML convertion"
else
if ! [[ -f "$domain_dir_wip$article_uri_srv" ]];then
log__add -w -M -A \
"Was manually deleted ! Convert again"
else
if [[ $article_wip_hash == $article_chk_hash ]];then
if [[ $wip_force ]];then
log__add -w -M -A \
"Already Exists and up. Force connvert again"
else
log__add -w -M -A \
"Already Exists and up. Use -F to connvert again"
unset do_wip
fi
else
log__add -i -M -A \
"Already Exists but old. New connversion"
fi
fi
fi
# Convert to HTML if asked or needed
if [[ $do_wip ]];then
split_article "$uri_article" # Split metas & content
makers "$article_tmp_post"
fi
# Option sidebar from command line with wip
if [[ $sidebar ]];then
sidebar__OPTIONS sidebar add $position "$this_article"
fi
}
#======================================================================
# Modules to convert article to HTML
# $1: $article_tmp_post
#======================================================================
makers() {
# First, if inlince code: convert it to base64
make__icode_b64 code "$1"
# Quotes
make__quotes "$1"
# Titles
make__titles "$1"
make__lists "$1"
get__content_metas "^abbr: " "$article_tmp_head" make "$1"
get__content_metas "^file: " "$article_tmp_head" make "$1"
get__content_metas "^link: " "$article_tmp_head" make "$1"
get__content_metas "^image: " "$article_tmp_head" make "$1"
get__content_metas "^code: " "$article_tmp_head" make "$1"
# Bold, strong, emphasis...
make__strongs "$1"
make__bolds "$1"
make__emphasis "$1"
make__cross "$1"
make__del "$1"
make__br "$1"
# Paragraphs
make__paragraphs "$1"
# Last, decode icode from base64
make__icode_b64 decode "$1"
# At the very last converter
get__content_metas "^brut: " "$article_tmp_head" make "$1"
# a11y (tabulation)
spc=' ' # 8 spaces. Main content (2*4)
sed -i -e "s|^|$spc|" "$article_tmp_post"
#cat "$1"
create__HTML_translation
create__HTML_page
# Write wip hash to DB
db__srv_status_hash WIP set
# Remove tmp files
rm -f "$article_tmp_post"
rm -f "$article_tmp_head"
}
#======================================================================
# code, decode to,from base64 icode content. Ensure keep brut text
# for decode: convert also to HTML
# $2: $article_tmp_post
#======================================================================
make__icode_b64() {
case "$1" in
code)
while IFS=: read -r 'ln' 'content'
do
while read -r 'bcode'
do
b64_bcode=`echo "$bcode" | base64`
sed -i "${ln}s^$bcode^$b64_bcode^" "$2"
done < <(grep -o -P '(?<=_`).*?(?=`_)' <<<"$content")
done < <(grep -n '`' "$2")
;;
decode)
while read -r 'bcode'
do
b64_bcode=`echo "$bcode" | base64 --decode`
sed -i "s^$bcode^$b64_bcode^" "$2"
done < <(grep -o -P '(?<=_`).*?(?=`_)' "$2")
HTML_icode=""
sed -i "s^_\`^$HTML_icode^g" "$2"
sed -i 's^`_^
^g' "$2"
;;
esac
}
#======================================================================
# Covnert words in content from abbr in meta
# $1: $article_tmp_post
#======================================================================
make__abbrs() {
[[ $meta ]] || return
[[ "$header_f3" ]] \
&& abbr_use="$header_f3" \
|| abbr_use="$header_f1"
HTML_abbr=`
printf '%s %s%s%s' \
"" \
"$abbr_use" \
""`
sed -i "s^$header_f1^$HTML_abbr^g" "$1"
}
#======================================================================
# Convert IMAGES
# $1: $article_tmp_post
#======================================================================
make__images() {
[[ $meta ]] || return
unset ${!image_@}
case "$header_f2" in
"@"*)
header_f2=${header_f2/@/}
uri_file="${sub_genuri_srv}images/$header_f2"
;;
*)
uri_file="$header_f2"
;;
esac
while read -r 'image_article'
do
image_css="center" # Default class
mark_image=${image_article#*:} # image mark to replace in article
read -r -a image_field <<<"$image_article" # fields in array
ln=`echo "${image_field[0]}" | awk -F: '{print $1}'` # Line number
for i in `seq 1 ${#image_field[@]}`
do
case "${image_field[$i]}" in
"c="*)
image_class=${image_field[$i]/c=/}
case "$image_class" in
l|g|L|G) image_css="left" ;;
r|d|R|D) image_css="right" ;;
c|C) image_css="center" ;;
*) image_css="$image_class" ;;
esac
;;
"t="*)
image_target=${image_field[$i]/t=/}
case "$image_target" in
http*|ftp*)
image_html_link_start=""
image_html_link_close=""
;;
"+")
image_html_link_start=""
image_html_link_close=""
;;
esac
;;
"w="*)
image_width=${image_field[$i]/w=/}
width=`
grep -Eo '[[:alpha:]]+|[0-9]+' \
<<<"$image_width" \
| head -n 1`
if (( $width != 0 ));then
width_u=${image_width/$width}
! [[ $width_u ]] && width_u="px"
image_html_w="width:$width$width_u"
fi
;;
"h="*)
image_height=${image_field[$i]/h=/}
height=`
grep -Eo '[[:alpha:]]+|[0-9]+' \
<<<"$image_height" \
| head -n 1`
if (( $height != 0 ));then
height_u=${image_height/$height}
! [[ $height_u ]] && height_u="px"
image_html_h="height:$height$height_u"
fi
;;
esac
done
# Define Style
if [[ $width && $height ]];then
image_style=" style=\"$image_html_w;$image_html_h\""
elif [[ $width ]];then
image_style=" style=\"$image_html_w\""
elif [[ $height ]];then
image_style=" style=\"$image_html_h\""
fi
image_HTML=`
printf '%s%s%s%s%s%s\n' \
"$image_html_link_start" \
"" \
"$image_html_link_close"`
sed -i "${ln}s^$mark_image^$image_HTML^" "$1"
done < <(grep -n "_image:$header_f1" "$1")
}
#======================================================================
# Convert links (with file)
# $1: $article_tmp_post
#======================================================================
make__links_file() {
[[ $meta ]] || return
case "$header_f2" in
"@"*)
header_f2=${header_f2/@/}
uri_file="${sub_genuri_srv}files/$header_f2";;
*)
uri_file="$header_f2"
;;
esac
flink_name="__$header_f1"
[[ "$header_f3" ]] \
&& flink_title=" title=\"$header_f3\""
HTML_flink=`
printf '%s %s%s%s' \
"" \
"$header_f1" \
""`
HTML_flink_target=`
printf '%s %s%s%s' \
"" \
"$header_f1" \
""`
sed -i "s^${flink_name}+^$HTML_flink_target^g" "$1"
sed -i "s^$flink_name^$HTML_flink^g" "$1"
}
#======================================================================
# Covnert links (not links with file)
# $1: $article_tmp_post
#======================================================================
make__links() {
[[ $meta ]] || return
link_name="_$header_f1"
[[ "$header_f3" ]] \
&& link_title=" title=\"$header_f3\""
HTML_link=`
printf '%s %s%s%s' \
"" \
"$header_f1" \
""`
HTML_link_target=`
printf '%s %s%s%s' \
"" \
"$header_f1" \
""`
sed -i "s^${link_name}+^$HTML_link_target^g" "$1"
sed -i "s^${link_name}^$HTML_link^g" "$1"
}
#======================================================================
# Convert strong word(s)
# $1: $article_tmp_post
#======================================================================
make__strongs() {
HTML_strong=""
sed -i "s^\*_^$HTML_strong^g" "$1"
sed -i 's^_\*^^g' "$1"
}
#======================================================================
# Convert bold word(s)
# $1: $article_tmp_post
#======================================================================
make__bolds() {
HTML_bold=""
sed -i "s^+_^$HTML_bold^g" "$1"
sed -i 's^_+^^g' "$1"
}
#======================================================================
# Convert emphasis word(s)
# $1: $article_tmp_post
#======================================================================
make__emphasis() {
HTML_em=""
sed -i "s^\\\_^$HTML_em^g" "$1"
sed -i 's^_\\^^g' "$1"
}
#======================================================================
# Convert cross word(s)
# $1: $article_tmp_post
#======================================================================
make__cross() {
HTML_cross=""
sed -i "s^×_^$HTML_cross^g" "$1"
sed -i 's^_×^^g' "$1"
}
#======================================================================
# Convert del word(s)
# $1: $article_tmp_post
#======================================================================
make__del() {
HTML_del=""
sed -i "s^~_^$HTML_del^g" "$1"
sed -i 's^_~^^g' "$1"
}
#======================================================================
# Create paragraphs (classic and custom)
# $1: $article_tmp_post
#======================================================================
make__paragraphs() {
[[ `grep "^)$" "$1"` ]] \
&& sed -i "s,^)$,
" sed -i "${ln}s^$pm $class^$HTML_p^" "$1" else HTML_p="
"
sed -i "${ln}s^$pm^$HTML_p^" "$1"
fi
done < <(grep -n '^($\|^( ' "$1")
}
#======================================================================
# Create
(with | at begining line
# $1: $article_tmp_post
#======================================================================
make__br() {
sed -i 's,^|$,
,g' "$1"
}
#======================================================================
# Create tmp file to copy article content
# Create HTML TITLES and add divs if content after
# Get rid of comments
# Copy tmp file to new article_tmp_post
# $1: $article_tmp_post
#======================================================================
make__titles() {
article_tmp_content=`mktemp` # Create tmp file to copy content
while read -r 'line'
do
if [[ $new_title ]];then
case "$line" in
''|"#"[1-6]*) ;; # If no content or title after title, do nothing
*)
printf '%s%s\n' \
"
$(printf '%s\n' "${quote_line[@]}")EOQ fi } while read -r 'line' do case "$line" in '---'*) if [[ $quote_start ]];then quote_close=true make__quote_print unset ${!quote_@} else quote_start=true unset quote_close quote_css=`awk '{print $2}' <<<"$line"` ! [[ $quote_css ]] \ && quote_css="${domain_css}_quote" \ || quote_css="${domain_css}_quote $quote_css" fi ;; *) if [[ $quote_start ]];then case "$line" in "_cite"*) quote_fig=true quote_author=`awk -F" : " '{print $2}' <<<"$line"` ;; "_link"*) quote_link=`awk -F" : " '{print $2}' <<<"$line"` quote_html_cite=" cite=\"$quote_link\"" ;; "_lang"*) quote_lang=`awk -F" : " '{print $2}' <<<"$line"` quote_lang=" lang=\"$quote_lang\"" ;; "_book"*) quote_book=`awk -F" : " '{print $2}' <<<"$line"` ;; "_year"*) quote_year=`awk -F" : " '{print $2}' <<<"$line"` quote_year="($quote_year)" ;; *) ((ln++)) quote_line[ln]="$line" ;; esac else printf '%s\n' "$line" >> "$article_tmp_content" fi ;; esac done < <(cat "$1") # Copy tmp file new content to legacy article file cat "$article_tmp_content" > "$1" # Remove tmp content created for quotes rm -f "$article_tmp_content" } #====================================================================== # 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: $article_tmp_post #====================================================================== 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="${domain_css}_li ${domain_css}_li-" uo_class="${domain_css}_list ${domain_css}_list-" # Custom class for bloc only my_class=`awk 'NR == 1 {print $2}' "$gsl_tmp_list_brut"` [[ $my_class ]] \ && muo_class=" $my_class $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" "" \ "
$(while read -r 'ln' 'content' do printf '%2s%s%s\n' "" \ "$ln" \ "$content" done < <(cat -n "$uri_file_hard"))EOCODE while IFS=: read -r 'ln' 'content' do sed -i "${ln}r $article_tmp_content" "$1" sed -i "${ln}d" "$1" done < <(grep -n "_code:$header_f1" "$1") # Remove TMP file rm -f "$article_tmp_content" } #====================================================================== # make conteent brut (copy file as is) # $1: $article_tmp_post #====================================================================== make__brut() { [[ $meta ]] || return case "$header_f2" in "@"*) header_f2=${header_f2/@/} uri_file="$domain_dir_files/$header_f2" ;; *) uri_file="$uri_folder/$header_f2" ;; esac while IFS=: read -r 'ln' 'content' do sed -i "${ln}r $uri_file" "$1" sed -i "${ln}d" "$1" done < <(grep -n "_brut:$header_f1" "$1") }