#!/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,^)$,

,g" "$1" \ || return while read -r "lnpm" "class" do ln=`awk -F: '{print $1}' <<<$lnpm` pm=`awk -F: '{print $2}' <<<$lnpm` if [[ $class ]];then HTML_p="

" 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' \ "

" \ >> "$article_tmp_content" unset new_title div_open=true ;; esac fi case "$line" in "#"[1-6]*) if [[ $div_open ]];then printf '%s\n\n' \ "
" \ >> "$article_tmp_content" unset div_open fi tn=${line:1:1} printf '%s%s%s%s\n' \ "" \ "${line:3}" \ "" \ >> "$article_tmp_content" new_title=true ;; "# "*) continue ;; # Get rid of comments '') continue ;; *) printf '%s\n' "$line" >> "$article_tmp_content" ;; esac done < <(cat "$1") # Close last div [[ $div_open ]] \ && printf '%s\n' \ "" \ >> "$article_tmp_content" # Copy tmp file to legacy article file cat "$article_tmp_content" > "$1" # Remove tmp content created for titles and divs rm -f "$article_tmp_content" } #====================================================================== # make conteent Quotes # $1: $article_tmp_post #====================================================================== make__quotes() { [[ `grep '^---' "$1"` ]] \ && article_tmp_content=`mktemp` \ || return # After getting datas from loop below, print in tmp file content # -------------------------------------------------------------- make__quote_print() { if [[ $quote_fig ]];then if [[ $quote_book && $quote_year ]];then quote_title=" title=\"$quote_book $quote_year\"" quote_datas=" $quote_book $quote_year" elif [[ $quote_book ]];then quote_title=" titlee=\"$quote_book\"" quote_datas=" $quote_book" elif [[ $quote_year ]];then quote_datas=" $quote_year" fi cat <> "$article_tmp_content"
$(printf '%s\n' "${quote_line[@]}")
$quote_author$quote_datas
EOQ else cat <> "$article_tmp_content"
$(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" "" \ "
  • ${content[$n]}
  • " \ | 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" "" \ "
  • ${content[$n]}
  • " \ >> "$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" "" \ "" \ >> "$gsl_tmp_list_HTML" sp=$(( sp - 2 )) l=$(( l - 1 )) done # --------------------- # Diff sign | Last Item # --------------------- else printf "%${sp}s%s\n" "" \ "" \ >> "$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" "" \ "" \ >> "$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" "" \ "" \ >> "$gsl_tmp_list_HTML" bsp=$(( bsp - 2 )) l=$(( l - 1 )) done make__list_li # ------------------------- # Diff sign | New Item Down # ------------------------- else l=$(( l + 1 )) for c in `seq 1 $(( ${md/-/} + 1 ))` do printf "%${bsp}s%s\n" "" \ "" \ >> "$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 "$1" } #====================================================================== # make conteent precode # $1: $article_tmp_post #====================================================================== make__precode() { [[ $meta ]] || return unset ${!precode_@} # Define CSS precode_css="class=\"${domain_css}_code-block code-block-$header_f1\"" precode_css_line="class=\"${domain_css}_code-line\"" precode_css_content="class=\"${domain_css}_code-content\"" precode_css_source="class=\"${domain_css}_code-link\"" precode_css_div="class=\"${domain_css}_code-div\"" # No Alt-Text ; generate [[ "$header_f3" ]] \ && precode_title=" title=\"Code: source\"" \ || header_f3="Code: source" case "$header_f2" in "@"*) header_f2=${header_f2/@/} uri_file="${sub_genuri_srv}files/$header_f2" uri_file_hard="$domain_dir_files/$header_f2" ;; *) uri_file="$header_f2" uri_file_hard="$uri_folder/$header_f2" ;; esac # Create TMP file with HTML precode article_tmp_content=`mktemp` cat << EOCODE > "$article_tmp_content"
    $(while read -r 'ln' 'content'
      do
        printf '%2s%s%s\n' "" \
               "$ln" \
               "$content"
      done < <(cat -n "$uri_file_hard"))
    
    $header_f3
    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") }