#!/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
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 -srv $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
#----------------------------------------------------------------------
# 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
)
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"
#----------------------------------------------------------------------
# Method bsis (Bold, String, Italic, Strike)
# Same for inline_code, done after bsis
# Search all lines for bsis mark
## Read each letter from line, excluding ones inside inline_code
## Convert mark with specific special charcter
## Convert specific special character to HTML
#----------------------------------------------------------------------
# Convert bsis
make__bsis_search "$gsl_tmp_post"
make__bsis "$gsl_tmp_post"
# Convert inlinde_code
make__icode_convert "$gsl_tmp_post"
make__icode "$gsl_tmp_post"
#----------------------------------------------------------------------
# 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"
}
#======================================================================
# 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" ;;
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' \
"
" sed -i "${ln}s^$pm $class^$HTML_p^" "$3" else HTML_p="
"
sed -i "${ln}s^$pm^$HTML_p^" "$3"
fi
done < <(grep -n '^($\|^'"$1" "$3")
}
#======================================================================
# Convert new line "|" to
($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
[[ "$header_f3" ]] \
&& abbr_word=${header_f1,,} \
|| abbr_word="$header_f1"
abbr_title="title=\"$header_f2\""
abbr_class="class=\"${site_css}_abbr\""
abbr_words="$abbr_word"
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' \
"" \
"$header_f1"`
link_words2=`
printf '%s%s%s\n' \
"" \
"$header_f1"`
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
, 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"$(while read -r "code_ln" "code_content" do printf '%2s%s%s\n' "" \ "$code_ln" \ "$code_content" done < <(cat -n "$gsl_dir_domain_files/$header_f2"))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 # 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 "` if [[ "$image_link" ]];then link_css="class=\"$link_class\"" HTML_image=` printf '%s%s%s\n' \ "" \ "$HTML_image" \ ""` fi 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" "" \ "
$(while read -r "line" do printf '%4s%s\n' "" "$line" done < <(cat "$gsl_tmp_quote_brut"))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 "^
# $1: file (gsl_tmp_post)
#======================================================================
make__icode() {
if [[ $article_has_icode ]];then
HTML_icode_start=""
sed -i "s^$icode_start^$HTML_icode_start^g" "$1"
sed -i "s^$icode_close^
^g" "$1"
fi
}